Exemplo n.º 1
0
int main()
{

    LASHeaderH header = NULL;
    LASWriterH writer = NULL;
    LASReaderH reader = NULL;
    LASError err;
    // Limitation about seeking past 4GB output size.  At 20 bytes / record, we
    // can successfully write 204 million records, but not 205.
    const unsigned long nMillionPoints = 205;
    const unsigned long NPOINTS = 1024*1024*nMillionPoints ;
    const char * OutputName = "Issue147.las";

    // Write a LAS file and after the points are in, update the header.
    header = LASHeader_Create();
    writer = LASWriter_Create(OutputName, header, LAS_MODE_WRITE);
    for (unsigned long i = 0; i < NPOINTS; i++)
    {
      double percentDone = ((double)i)/NPOINTS * 100.0;
      if (i % 1000 == 0)
         printf("\b\b\b\b\b\b\b%6.2f%%", percentDone);
      
      LASPointH pt = LASPoint_Create();
      err = LASPoint_SetX(pt, 0);
      if (err) printf ("For point %lu, failed to set point value X\n", i);
      err = LASPoint_SetY(pt, 0);
      if (err) printf ("For point %lu, failed to set point value Y\n", i);
      err = LASPoint_SetZ(pt, 0);
      if (err) printf ("For point %lu, failed to set point value Z\n", i);
      err = LASWriter_WritePoint(writer, pt);  
      if (err) printf ("For point %lu, failed to WritePoint\n", i);
      LASPoint_Destroy(pt);
    }
   err = LASHeader_SetPointRecordsCount(header, NPOINTS);
   if (err) dumperror ("Failed to LASHeader_SetPointRecordsCount\n");
   err = LASWriter_WriteHeader(writer, header);
   if (err) dumperror ("Failed to LASWriter_WriteHeader");
   LASWriter_Destroy(writer);
   LASHeader_Destroy(header);
   
   // Read the file we just wrote and check the header data.
    reader = LASReader_Create(OutputName);
    header = LASReader_GetHeader(reader);
    unsigned long npoints = LASHeader_GetPointRecordsCount(header);
    printf ("\n\nWrote %lu, Read %lu (testing %lu Million (1024 x 1024) Points)\n", NPOINTS, npoints, nMillionPoints);
}
Exemplo n.º 2
0
int main(int argc, char *argv[])
{
    int i;
    int dry = FALSE;
    int verbose = FALSE;
    char* file_name_in = 0;
    char* file_name_out = 0;
    double xyz_min[3] = {0.0, 0.0, 0.0};
    double xyz_max[3] = {0.0, 0.0, 0.0};
    double xyz_scale[3] = {0.01,0.01,0.01};
    double xyz_offset[3] = {0.0,0.0,0.0};
    unsigned int number_of_point_records = 0;
    unsigned int number_of_points_by_return[8] = {0,0,0,0,0,0,0,0};
    char* parse_string = "xyz";
    int file_creation_day = 0;
    int file_creation_year = 0;
    char* system_identifier = 0;
    char* generating_software = 0;
#define MAX_CHARACTERS_PER_LINE 512
    char line[MAX_CHARACTERS_PER_LINE];
    double xyz[3];
    LASPointH point = NULL;
    double gps_time;
    FILE* file_in = NULL;
    char* parse_less = NULL;
    LASHeaderH header = NULL;
    LASWriterH writer = NULL;
    LASError err;
    int format = LAS_FORMAT_12;

    int xyz_min_quant[3] = {0, 0, 0};
    int xyz_max_quant[3] = {0, 0, 0};

    double xyz_min_dequant[3] = {0.0, 0.0, 0.0};
    double xyz_max_dequant[3] = {0.0, 0.0, 0.0};

    for (i = 1; i < argc; i++)
    {
        if (    strcmp(argv[i],"-h") == 0       ||
                strcmp(argv[i],"--help") == 0
            )
        {
            usage();
            exit(0);
        }
        else if (   strcmp(argv[i],"-v") == 0   ||
                    strcmp(argv[i],"--verbose") == 0
            )
        {
            verbose = TRUE;
        }    
        else if (   strcmp(argv[i],"-d") == 0       ||
                    strcmp(argv[i],"-dry") == 0     ||
                    strcmp(argv[i],"--dry") == 0
            )
        {
            dry = TRUE;
        }          
        else if (   strcmp(argv[i],"--parse") == 0  ||
                    strcmp(argv[i],"-parse") == 0   ||
                    strcmp(argv[i],"-p") == 0       
                )
        {
            i++;
            parse_string = argv[i];
        }
        else if (   strcmp(argv[i],"--scale") == 0  ||
                    strcmp(argv[i],"-scale") == 0   ||
                    strcmp(argv[i],"-s") == 0       
                )
        {

            i++;
            sscanf(argv[i], "%lf", &(xyz_scale[2]));
            xyz_scale[0] = xyz_scale[1] = xyz_scale[2];
        }
        else if (   strcmp(argv[i],"--xyz_scale") == 0  ||
                    strcmp(argv[i],"-xyz_scale") == 0    
                )

        {
            i++;
            sscanf(argv[i], "%lf", &(xyz_scale[0]));
            i++;
            sscanf(argv[i], "%lf", &(xyz_scale[1]));
            i++;
            sscanf(argv[i], "%lf", &(xyz_scale[2]));
        }
        else if (   strcmp(argv[i],"--xyz_offset") == 0  ||
                    strcmp(argv[i],"-xyz_offset") == 0    
                )
        {
            i++;
            sscanf(argv[i], "%lf", &(xyz_offset[0]));
            i++;
            sscanf(argv[i], "%lf", &(xyz_offset[1]));
            i++;
            sscanf(argv[i], "%lf", &(xyz_offset[2]));
        }
        else if (   strcmp(argv[i],"--input") == 0  ||
                    strcmp(argv[i],"-input") == 0   ||
                    strcmp(argv[i],"-i") == 0       ||
                    strcmp(argv[i],"-in") == 0
                )
        {
            i++;
            file_name_in = argv[i];
        }
        
        else if (   strcmp(argv[i],"--output") == 0  ||
                    strcmp(argv[i],"--out") == 0     ||
                    strcmp(argv[i],"-out") == 0     ||
                    strcmp(argv[i],"-o") == 0       
                )
        {
            i++;
            file_name_out = argv[i];
        }

        else if (   strcmp(argv[i],"--format") == 0   ||
                    strcmp(argv[i],"-f") == 0    ||
                    strcmp(argv[i],"-format") == 0 
                )
        {
            i++;
            if (strcmp(argv[i], "1.0") == 0) {
                format = LAS_FORMAT_10;
            }
            else if (strcmp(argv[i], "1.1") == 0) {
                format = LAS_FORMAT_11;
            } 
            else if (strcmp(argv[i], "1.2") == 0) {
                format = LAS_FORMAT_12;
            }
            else {
                LASError_Print("Format must be specified as 1.0, 1.1, or 1.2");
            }

        }
        else if (   strcmp(argv[i],"--system_identifier") == 0   ||
                    strcmp(argv[i],"-system_identifier") == 0   ||
                    strcmp(argv[i],"-s") == 0   ||
                    strcmp(argv[i],"-sys_id") == 0)
        {
            i++;
            system_identifier = (char*) malloc(31 * sizeof(char));
            strcpy(system_identifier, argv[i]);
        }

        else if (   strcmp(argv[i],"--generating_software") == 0   ||
                    strcmp(argv[i],"-generating_software") == 0   ||
                    strcmp(argv[i],"-g") == 0   ||
                    strcmp(argv[i],"-gen_soft") == 0)
        {
            i++;
            generating_software = (char*) malloc(31*sizeof(char));
            strcpy(generating_software, argv[i]);
        }

        else if (   strcmp(argv[i],"--file_creation") == 0   ||
                    strcmp(argv[i],"-file_creation") == 0)
        {
            i++;
            file_creation_day = (unsigned short)atoi(argv[i]);
            i++;
            file_creation_year = (unsigned short)atoi(argv[i]);
        }
        else if (   i == argc - 2 
                    && file_name_in == NULL && 
                    file_name_out == NULL
                )
        {
            file_name_in = argv[i];
        }
        else if (   i == argc - 1 && 
                    file_name_in == NULL && 
                    file_name_out == NULL
                )
        {
            file_name_in = argv[i];
        }
        else if (   i == argc - 1 && 
                    file_name_in && 
                    file_name_out == NULL
                )
        {
            file_name_out = argv[i];
        }
        else 
        {
            fprintf(stderr, "ERROR: unknown argument '%s'\n",argv[i]);
            usage();
            exit(1);
        }
    }

    /* create output file name if none specified and no piped output requested */
    if (file_name_out == NULL && file_name_in != NULL)
    {
        int len = (int)strlen(file_name_in);
        file_name_out = strdup(file_name_in);

        while (len > 0 && file_name_out[len] != '.')
        {
            len--;
        }
        file_name_out[len] = '.';
        file_name_out[len+1] = 'l';
        file_name_out[len+2] = 'a';
        file_name_out[len+3] = 's';
        file_name_out[len+4] = '\0';
    }


    /* make sure that input and output are not *both* piped */
    if (file_name_in == NULL && file_name_out == NULL)
    {
        LASError_Print("both input and output filenames are null!");
        usage();
        exit(1);
    }

    file_in = fopen(file_name_in, "r");

    if (file_in == NULL)
    {
        LASError_Print("could not open file to read for first pass");
        exit(1);
    }

    /* create a cheaper parse string that only looks for 'x' 'y' 'z' and 'r' */
    parse_less = strdup(parse_string);
    for (i = 0; i < (int)strlen(parse_string); i++)
    {
        if (parse_less[i] != 'x' && 
            parse_less[i] != 'y' && 
            parse_less[i] != 'z' && 
            parse_less[i] != 'r') 
        {
            parse_less[i] = 's';
        }
    }

    do
    {
        parse_less[i] = '\0';
        printf("nuking %d for %c\n", i, parse_less[i]);
        i--;
    } while (parse_less[i] == 's');
    
    
    /* first pass to figure out the bounding box and number of returns */
    if (verbose) {
        fprintf(stderr, 
                "first pass over file '%s' with parse '%s'\n", 
                file_name_in, 
                parse_less);
    }

    /* read the first line */
    while (fgets(line, sizeof(char) * MAX_CHARACTERS_PER_LINE, file_in))
    {
        point = LASPoint_Create();
        if (parse(parse_less, line, xyz, point, &gps_time))
        {
            /* init the bounding box */
            VecCopy3dv(xyz_min, xyz);
            VecCopy3dv(xyz_max, xyz);

            /* mark that we found the first point */
            number_of_point_records = 1;

            /* create return histogram */
            number_of_points_by_return[LASPoint_GetReturnNumber(point)]++;
            
            /* we can stop this loop */
            break;
        }
        else
        {
            fprintf(stderr, "WARNING: cannot parse '%s' with '%s'. skipping ...\n", 
                    line, 
                    parse_less);

        }
        LASPoint_Destroy(point);
        point = NULL;
    }

    /* did we manage to parse a line? */
    if (number_of_point_records != 1)
    {
        fprintf(stderr, "ERROR: could not parse any lines with '%s'\n", 
                parse_less);
        exit(1);
    }

    /* loop over the remaining lines */
    while (fgets(line, sizeof(char) * MAX_CHARACTERS_PER_LINE, file_in))
    {
        point = LASPoint_Create();
        if (parse(parse_less, line, xyz, point, &gps_time))
        {
            /* update bounding box */
            VecUpdateMinMax3dv(xyz_min, xyz_max, xyz);

            /* count points */
            number_of_point_records++;

            /* create return histogram */
            number_of_points_by_return[LASPoint_GetReturnNumber(point)]++;
        }
        else
        {
            fprintf(stderr, "WARNING: cannot parse '%s' with '%s'. skipping ...\n", 
            line, 
            parse_less);

        }
        LASPoint_Destroy(point);
        point = NULL;
    }

    /* output some stats */
    if (verbose)
    {
        fprintf(stderr, 
                "npoints %d min %g %g %g max %g %g %g\n", 
                number_of_point_records, 
                xyz_min[0], 
                xyz_min[1], 
                xyz_min[2], 
                xyz_max[0], 
                xyz_max[1], 
                xyz_max[2]
                );
        fprintf(stderr, 
                "return histogram %d %d %d %d %d %d %d %d\n", 
                number_of_points_by_return[0], 
                number_of_points_by_return[1], 
                number_of_points_by_return[2], 
                number_of_points_by_return[3], 
                number_of_points_by_return[4], 
                number_of_points_by_return[5], 
                number_of_points_by_return[6], 
                number_of_points_by_return[7]
                );
    }

    /* close the input file */    
      fclose(file_in);

    /*  compute bounding box after quantization */
    for (i = 0; i < 3; i++)
    {
        xyz_min_quant[i] = (int)(0.5 + (xyz_min[i] - xyz_offset[i]) / xyz_scale[i]);
        xyz_max_quant[i] = (int)(0.5 + (xyz_max[i] - xyz_offset[i]) / xyz_scale[i]);
    }

    for (i = 0; i < 3; i++)
    {
        xyz_min_dequant[i] = xyz_offset[i] + (xyz_min_quant[i] * xyz_scale[i]);
        xyz_max_dequant[i] = xyz_offset[i] + (xyz_max_quant[i] * xyz_scale[i]);
    }


#define log_xor !=0==!

    /* make sure there is not sign flip */
    for (i = 0; i < 3; i++)
    {
        if ((xyz_min[i] > 0) log_xor (xyz_min_dequant[i] > 0))
        {
            fprintf(stderr, 
                    "WARNING: quantization sign flip for %s min coord %g -> %g. use offset or scale up\n", 
                    (i ? (i == 1 ? "y" : "z") : "x"), 
                    xyz_min[i], 
                    xyz_min_dequant[i]
                   );
        }
        if ((xyz_max[i] > 0) log_xor (xyz_max_dequant[i] > 0))
        {
            fprintf(stderr, 
                    "WARNING: quantization sign flip for %s max coord %g -> %g. use offset or scale up\n", 
                    (i ? (i == 1 ? "y" : "z") : "x"), 
                    xyz_max[i], 
                    xyz_max_dequant[i]
                   );
        }
    }

#undef log_xor

    /* populate the header */
      
    header = LASHeader_Create();

    if (system_identifier) LASHeader_SetSystemId(header, system_identifier);
    if (generating_software) LASHeader_SetSoftwareId(header, generating_software);
    LASHeader_SetCreationDOY(header, file_creation_day);
    LASHeader_SetCreationYear(header, file_creation_year);

    
    if (format == LAS_FORMAT_10) {
        LASHeader_SetVersionMinor(header, 0);
    } else if (format == LAS_FORMAT_11){
        LASHeader_SetVersionMinor(header, 1);
    } else if (format == LAS_FORMAT_12) {
        LASHeader_SetVersionMinor(header, 2);
    }      

    if (strstr(parse_string,"t") && (strstr(parse_string, "R") || strstr(parse_string, "G") ||strstr(parse_string, "B") ) )
    {
        fprintf(stderr, "Setting point format to 3, overriding version to 1.2 -- RGB + time\n");
        LASHeader_SetDataFormatId(header, 3);
        LASHeader_SetVersionMinor(header, 2);
    }
    else if ((strstr(parse_string, "R") || strstr(parse_string, "G") ||strstr(parse_string, "B") ) )
    {
        fprintf(stderr, "Setting point format to 2, overriding version to 1.2 -- RGB\n");
        LASHeader_SetDataFormatId(header, 2);
        LASHeader_SetVersionMinor(header, 2);
    }
    else if (strstr(parse_string,"t")) 
    {
        fprintf(stderr, "Setting point format to 1\n");
        LASHeader_SetDataFormatId(header, 1);
    }
    
    else
    {
        LASHeader_SetDataFormatId(header, 0);
    }
    LASHeader_SetPointRecordsCount(header, number_of_point_records);
    LASHeader_SetScale(header, xyz_scale[0], xyz_scale[1], xyz_scale[2]);
    LASHeader_SetOffset(header, xyz_offset[0], xyz_offset[1], xyz_offset[2]);
    LASHeader_SetMin(header, xyz_min_dequant[0], xyz_min_dequant[1], xyz_min_dequant[2]);
    LASHeader_SetMax(header, xyz_max_dequant[0], xyz_max_dequant[1], xyz_max_dequant[2]);
    LASHeader_SetPointRecordsByReturnCount(header, 0, number_of_points_by_return[1]);
    LASHeader_SetPointRecordsByReturnCount(header, 1, number_of_points_by_return[2]);
    LASHeader_SetPointRecordsByReturnCount(header, 2, number_of_points_by_return[3]);
    LASHeader_SetPointRecordsByReturnCount(header, 3, number_of_points_by_return[4]);
    LASHeader_SetPointRecordsByReturnCount(header, 4, number_of_points_by_return[5]);




    /* reopen input file for the second pass */
    file_in = fopen(file_name_in, "r");

    if (file_in == 0)
    {
        fprintf(stderr, "ERROR: could not open '%s' for second pass\n",file_name_in);
        exit(1);
    }

    /*
        because the output goes to a file we can do everything in a 
        single pass and compute the header information along the way 
    */
    
    /* open output file */
    printf("Creating file...\n");
    writer = LASWriter_Create(file_name_out, header, LAS_MODE_WRITE);
    if (!writer) { 
        LASError_Print("Could not open file for write mode ");
        exit(1);
    }     

    if (verbose) {
        fprintf(stderr, 
                "scanning %s with parse '%s' writing to %s\n", 
                file_name_in , 
                parse_string, 
                file_name_out
               );
    }
    

    /* read the first line */
    while (fgets(line, sizeof(char) * MAX_CHARACTERS_PER_LINE, file_in))
    {
        point = LASPoint_Create();
        if (parse(parse_string, line, xyz, point, &gps_time))
        {
            /* init the bounding box */
            VecCopy3dv(xyz_min, xyz);
            VecCopy3dv(xyz_max, xyz);

            /* mark that we found the first point */
            number_of_point_records = 1;

            /* create return histogram */
            number_of_points_by_return[LASPoint_GetReturnNumber(point)]++;
            
            /* compute the quantized x, y, and z values */
            LASPoint_SetX(point, xyz[0]);
            LASPoint_SetY(point, xyz[1]);
            LASPoint_SetZ(point, xyz[2]);

            /* write the first point */
            err = LASWriter_WritePoint(writer, point);
            if (err) {
                LASError_Print("could not write point");
                exit(1);
            }
              
            /* we can stop this loop */
            break;
        }
        else
        {
            fprintf(stderr, "WARNING: cannot parse '%s' with '%s'. skipping ...\n", 
                    line, 
                    parse_string);
        }
        LASPoint_Destroy(point);
        point = NULL;
    }

    /* did we manage to parse a line? */
    if (number_of_point_records != 1)
    {
        fprintf(stderr, "ERROR: could not parse any lines with '%s'\n", 
                parse_less);
        exit(1);
    }

    /* loop over the remaining lines */
    while (fgets(line, sizeof(char) * MAX_CHARACTERS_PER_LINE, file_in))
    {
        point = LASPoint_Create();
        if (parse(parse_string, line, xyz, point, &gps_time))
        {
            /* update bounding box */
            VecUpdateMinMax3dv(xyz_min, xyz_max, xyz);

            /* count points */
            number_of_point_records++;

            /* create return histogram */
            number_of_points_by_return[LASPoint_GetReturnNumber(point)]++;

            /* compute the quantized x, y, and z values */
            LASPoint_SetX(point, xyz[0]);
            LASPoint_SetY(point, xyz[1]);
            LASPoint_SetZ(point, xyz[2]);

            /* write the first point */
            err = LASWriter_WritePoint(writer, point);
            if (err) {
                LASError_Print("could not write point");
                exit(1);
            }

        }
        else
        {
            fprintf(stderr, "WARNING: cannot parse '%s' with '%s'. skipping ...\n", 
            line, 
            parse_string);
        }
        LASPoint_Destroy(point);
        point = NULL;
    }


    /* close up stuff */
    fclose(file_in);
    LASWriter_Destroy(writer);
    if (verbose)
    {
        fprintf(stderr, "done.\n");
    }


    return 0;
}