BOOL LASreaderASC::reopen(const char* file_name) { if (file_name == 0) { fprintf(stderr,"ERROR: fine name pointer is zero\n"); return FALSE; } file = fopen_compressed(file_name, "r", &piped); if (file == 0) { fprintf(stderr, "ERROR: cannot reopen file '%s'\n", file_name); return FALSE; } // read the header lines I32 i; for (i = 0; i < header_lines; i++) { fgets(line, line_size, file); } col = 0; row = 0; line_curr = 0; p_count = 0; return TRUE; }
/* * Searches for file in the directory 'dirname'. If it finds it, it opens * it. This also searches for compressed versions of the file in dirname * too. * * Returns FILE pointer if found * NULL if not */ static FILE *find_file_dir(char *file, char *dirname) { char path[PATH_MAX+1], path2[PATH_MAX+1]; size_t len = strlen(dirname); int num_magics = sizeof(magics) / sizeof(*magics); int i; if (dirname[len-1] == '/') len--; /* Special case for "./" */ if (len==1 && *dirname == '.') sprintf(path, "%s", file); else sprintf(path, "%.*s/%s", (int)len, dirname, file); /* Is it lurking under another, compressed, name? */ for (i = 0; i < num_magics; i++) { sprintf(path2, "%s%s", path, magics[i]); if (file_exists(path2)) { return fopen_compressed(path2, NULL); } } return NULL; }
/* * Read the SCF format sequence with name `fn' into a 'scf' structure. * A NULL result indicates failure. */ Scf *read_scf(char *fn) { Scf *scf; FILE *fp; /* Open fn for reading in binary mode */ if (NULL == (fp = fopen_compressed(fn, NULL))) return NULL; scf = fread_scf(fp); fclose(fp); return scf; }
bool LASreaderSHP::reopen(const char* file_name) { if (file_name == 0) { fprintf(stderr,"ERROR: fine name pointer is zero\n"); return FALSE; } file = fopen_compressed(file_name, "rb", &piped); if (file == 0) { fprintf(stderr, "ERROR: cannot reopen file '%s'\n", file_name); return FALSE; } // read SHP header again to skip to the right point in the file int int_input; if (fread(&int_input, sizeof(int), 1, file) != 1) return FALSE; // file code (BIG) if (fread(&int_input, sizeof(int), 1, file) != 1) return FALSE; // unused (BIG) if (fread(&int_input, sizeof(int), 1, file) != 1) return FALSE; // unused (BIG) if (fread(&int_input, sizeof(int), 1, file) != 1) return FALSE; // unused (BIG) if (fread(&int_input, sizeof(int), 1, file) != 1) return FALSE; // unused (BIG) if (fread(&int_input, sizeof(int), 1, file) != 1) return FALSE; // unused (BIG) if (fread(&int_input, sizeof(int), 1, file) != 1) return FALSE; // file length (BIG) if (fread(&int_input, sizeof(int), 1, file) != 1) return FALSE; // version (LITTLE) if (fread(&int_input, sizeof(int), 1, file) != 1) return FALSE; // shape type (LITTLE) double double_input; if (fread(&double_input, sizeof(double), 1, file) != 1) return FALSE; // xmin (LITTLE) if (fread(&double_input, sizeof(double), 1, file) != 1) return FALSE; // ymin (LITTLE) if (fread(&double_input, sizeof(double), 1, file) != 1) return FALSE; // xmax (LITTLE) if (fread(&double_input, sizeof(double), 1, file) != 1) return FALSE; // ymax (LITTLE) if (fread(&double_input, sizeof(double), 1, file) != 1) return FALSE; // zmin (LITTLE) if (fread(&double_input, sizeof(double), 1, file) != 1) return FALSE; // zmax (LITTLE) if (fread(&double_input, sizeof(double), 1, file) != 1) return FALSE; // mmin (LITTLE) if (fread(&double_input, sizeof(double), 1, file) != 1) return FALSE; // mmax (LITTLE) p_count = 0; return TRUE; }
BOOL LASreaderASC::open(const char* file_name) { if (file_name == 0) { fprintf(stderr,"ERROR: fine name pointer is zero\n"); return FALSE; } clean(); file = fopen_compressed(file_name, "r", &piped); if (file == 0) { fprintf(stderr, "ERROR: cannot open file '%s'\n", file_name); return FALSE; } // clean the header header.clean(); // populate the header as much as it makes sense sprintf(header.system_identifier, "LAStools (c) by Martin Isenburg"); sprintf(header.generating_software, "via LASreaderASC (%d)", LAS_TOOLS_VERSION); // maybe set creation date #ifdef _WIN32 WIN32_FILE_ATTRIBUTE_DATA attr; SYSTEMTIME creation; GetFileAttributesEx(file_name, GetFileExInfoStandard, &attr); FileTimeToSystemTime(&attr.ftCreationTime, &creation); int startday[13] = {-1, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; header.file_creation_day = startday[creation.wMonth] + creation.wDay; header.file_creation_year = creation.wYear; // leap year handling if ((((creation.wYear)%4) == 0) && (creation.wMonth > 2)) header.file_creation_day++; #else header.file_creation_day = 333; header.file_creation_year = 2012; #endif header.point_data_format = 0; header.point_data_record_length = 20; // initialize point point.init(&header, header.point_data_format, header.point_data_record_length, &header); // read header of ASC file if (line == 0) { line_size = 1024; line = (CHAR*)malloc(sizeof(CHAR)*line_size); } CHAR dummy[32]; BOOL complete = FALSE; ncols = 0; nrows = 0; F64 xllcorner = F64_MAX; F64 yllcorner = F64_MAX; xllcenter = F64_MAX; xllcenter = F64_MAX; cellsize = 0; nodata = -9999; header_lines = 0; while (!complete) { if (!fgets(line, line_size, file)) break; if (strstr(line, "ncols") || strstr(line, "NCOLS")) { sscanf(line, "ncols %d", &ncols); free(line); line_size = 1024+16*ncols; line = (CHAR*)malloc(sizeof(CHAR)*line_size); } else if (strstr(line, "nrows") || strstr(line, "NROWS")) { sscanf(line, "%s %d", dummy, &nrows); } else if (strstr(line, "xllcorner") || strstr(line, "XLLCORNER")) { sscanf(line, "%s %lf", dummy, &xllcorner); } else if (strstr(line, "yllcorner") || strstr(line, "YLLCORNER")) { sscanf(line, "%s %lf", dummy, &yllcorner); } else if (strstr(line, "xllcenter") || strstr(line, "XLLCENTER")) { sscanf(line, "%s %lf", dummy, &xllcenter); } else if (strstr(line, "yllcenter") || strstr(line, "YLLCENTER")) { sscanf(line, "%s %lf", dummy, &yllcenter); } else if (strstr(line, "cellsize") || strstr(line, "CELLSIZE")) { sscanf(line, "%s %f", dummy, &cellsize); } else if (strstr(line, "nodata_value") || strstr(line, "NODATA_VALUE") || strstr(line, "nodata_VALUE") || strstr(line, "NODATA_value")) { sscanf(line, "%s %f", dummy, &nodata); } else if ((ncols != 0) && (nrows != 0) && (((xllcorner != F64_MAX) && (yllcorner != F64_MAX)) || ((xllcenter != F64_MAX) && (xllcenter != F64_MAX))) && (cellsize > 0)) { if (ncols == 1) { F32 e0, e1; if ( sscanf(line, "%f %f", &e0, &e1) == 1) { complete = TRUE; } } else if (ncols == 2) { F32 e0, e1, e2; if ( sscanf(line, "%f %f %f", &e0, &e1, &e2) == 2) { complete = TRUE; } } else if (ncols == 3) { F32 e0, e1, e2, e3; if ( sscanf(line, "%f %f %f %f", &e0, &e1, &e2, &e3) == 3) { complete = TRUE; } } else if (ncols == 4) { F32 e0, e1, e2, e3, e4; if ( sscanf(line, "%f %f %f %f %f", &e0, &e1, &e2, &e3, &e4) == 4) { complete = TRUE; } } else { F32 e0, e1, e2, e3, e4; if ( sscanf(line, "%f %f %f %f %f", &e0, &e1, &e2, &e3, &e4) == 5) { complete = TRUE; } } } header_lines++; } if (!complete) { fprintf(stderr,"ERROR: was not able to find header\n"); return FALSE; } // shift the llcorner to the pixel center if ((xllcorner != F64_MAX) && (yllcorner != F64_MAX)) { xllcenter = xllcorner + 0.5*cellsize; yllcenter = yllcorner + 0.5*cellsize; } // init the bounding box x y header.min_x = xllcenter; header.min_y = yllcenter; header.max_x = xllcenter + (ncols-1)*cellsize; header.max_y = yllcenter + (nrows-1)*cellsize; // init the bounding box z and count the rasters F32 elevation = 0; npoints = 0; header.min_z = F64_MAX; header.max_z = F64_MIN; for (row = 0; row < nrows; row++) { line_curr = 0; for (col = 0; col < ncols; col++) { // skip leading spaces while ((line[line_curr] != '\0') && (line[line_curr] == ' ')) line_curr++; // get elevation value sscanf(&(line[line_curr]), "%f", &elevation); // skip parsed number while ((line[line_curr] != '\0') && (line[line_curr] != ' ')) line_curr++; // should we use the raster if (elevation != nodata) { npoints++; if (header.max_z < elevation) header.max_z = elevation; if (header.min_z > elevation) header.min_z = elevation; } } if (!fgets(line, line_size, file)) break; } // close the file close(); // check the header values if ((header.min_z == F64_MAX) || (header.max_z == F64_MIN)) { fprintf(stderr,"WARNING: raster contains only no data\n"); header.min_z = 0; header.max_z = 0; } header.number_of_point_records = (U32)npoints; // populate scale and offset populate_scale_and_offset(); // check bounding box for this scale and offset populate_bounding_box(); // reopen return reopen(file_name); }
bool LASreaderSHP::open(const char* file_name) { if (file_name == 0) { fprintf(stderr,"ERROR: fine name pointer is zero\n"); return FALSE; } clean(); file = fopen_compressed(file_name, "rb", &piped); if (file == 0) { fprintf(stderr, "ERROR: cannot open file '%s'\n", file_name); return FALSE; } // clean the header header.clean(); // populate the header as much as it makes sense sprintf(header.system_identifier, "LAStools (c) by rapidlasso GmbH"); sprintf(header.generating_software, "via LASreaderSHP (%d)", LAS_TOOLS_VERSION); // maybe set creation date #ifdef _WIN32 WIN32_FILE_ATTRIBUTE_DATA attr; SYSTEMTIME creation; GetFileAttributesEx(file_name, GetFileExInfoStandard, &attr); FileTimeToSystemTime(&attr.ftCreationTime, &creation); int startday[13] = {-1, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; header.file_creation_day = startday[creation.wMonth] + creation.wDay; header.file_creation_year = creation.wYear; // leap year handling if ((((creation.wYear)%4) == 0) && (creation.wMonth > 2)) header.file_creation_day++; #else header.file_creation_day = 111; header.file_creation_year = 2011; #endif header.point_data_format = 0; header.point_data_record_length = 20; // initialize point point.init(&header, header.point_data_format, header.point_data_record_length); // read SHP header and populate the LAS header with the bounding box int int_input; if (fread(&int_input, sizeof(int), 1, file) != 1) return false; // file code (BIG) from_big_endian(&int_input); if (int_input != 9994) { fprintf(stderr, "ERROR: wrong shapefile code %d != 9994\n", int_input); return FALSE; } if (fread(&int_input, sizeof(int), 1, file) != 1) return false; // unused (BIG) if (fread(&int_input, sizeof(int), 1, file) != 1) return false; // unused (BIG) if (fread(&int_input, sizeof(int), 1, file) != 1) return false; // unused (BIG) if (fread(&int_input, sizeof(int), 1, file) != 1) return false; // unused (BIG) if (fread(&int_input, sizeof(int), 1, file) != 1) return false; // unused (BIG) if (fread(&int_input, sizeof(int), 1, file) != 1) return false; // file length (BIG) from_big_endian(&int_input); int file_length = int_input; if (fread(&int_input, sizeof(int), 1, file) != 1) return false; // version (LITTLE) from_little_endian(&int_input); if (int_input != 1000) { fprintf(stderr, "ERROR: wrong shapefile version %d != 1000\n", int_input); return FALSE; } if (fread(&int_input, sizeof(int), 1, file) != 1) return false; // shape type (LITTLE) from_little_endian(&int_input); shape_type = int_input; if (shape_type != 1 && shape_type != 11 && shape_type != 21 && shape_type != 8 && shape_type != 18 && shape_type != 28) { fprintf(stderr, "ERROR: wrong shape type %d != 1,11,21,8,18,28\n", shape_type); return FALSE; } double double_input; if (fread(&double_input, sizeof(double), 1, file) != 1) return false; // xmin (LITTLE) from_little_endian(&double_input); header.min_x = double_input; if (fread(&double_input, sizeof(double), 1, file) != 1) return false; // ymin (LITTLE) from_little_endian(&double_input); header.min_y = double_input; if (fread(&double_input, sizeof(double), 1, file) != 1) return false; // xmax (LITTLE) from_little_endian(&double_input); header.max_x = double_input; if (fread(&double_input, sizeof(double), 1, file) != 1) return false; // ymax (LITTLE) from_little_endian(&double_input); header.max_y = double_input; if (fread(&double_input, sizeof(double), 1, file) != 1) return false; // zmin (LITTLE) from_little_endian(&double_input); header.min_z = double_input; if (fread(&double_input, sizeof(double), 1, file) != 1) return false; // zmax (LITTLE) from_little_endian(&double_input); header.max_z = double_input; if (fread(&double_input, sizeof(double), 1, file) != 1) return false; // mmin (LITTLE) from_little_endian(&double_input); if (fread(&double_input, sizeof(double), 1, file) != 1) return false; // mmax (LITTLE) from_little_endian(&double_input); // maybe calculate npoints if (shape_type == 1) { npoints = (file_length-50)/(14); } else if (shape_type == 11) { npoints = (file_length-50)/(22); } else if (shape_type == 21) { npoints = (file_length-50)/(18); } else if (shape_type == 8) { npoints = (file_length-50-20)/(8); // over-estimate (assumes all in one record) } else if (shape_type == 18) { npoints = (file_length-50-36)/(16); // over-estimate (assumes all in one record) } else if (shape_type == 28) { npoints = (file_length-50-28)/(12); // over-estimate (assumes all in one record) } header.number_of_point_records = (U32)npoints; header.number_of_points_by_return[0] = (U32)npoints; // figure out the right scale factor and offset populate_scale_and_offset(); // populate the quantized bounding box populate_bounding_box(); p_count = 0; return TRUE; }
static int do_it(char *file, int argc, char **argv) { mFILE *fp; Header h; char *c, *p, *p2; int all = 0; int found = 0; if (NULL == (fp = fopen_compressed(file, NULL))) { if (!silent) perror(file); return 2; } /* Read the SCF header */ if (-1 == read_scf_header(fp, &h)) { if (!silent) fprintf(stderr, "Failed to read header\n"); return 2; } /* Read our comments */ if (NULL == (c = (char *)xmalloc(h.comments_size + 1))) return 2; if (-1 == mfseek(fp, h.comments_offset, SEEK_SET)) { if (!silent) perror("seek()"); return 2; } if (-1 == read_scf_comment(fp, c, h.comments_size)) { if (!silent) perror("read() of comments"); return 2; } if (argc == 0) all = 1; p = c; for (; p2 = strstr(p, "\n"); p = p2+1) { char *q; *p2 = '\0'; if (NULL == (q = strchr(p, '='))) continue; *q = '\0'; if (!all) { int i; for (i=0; i<argc; i++) if (strcmp(argv[i], p) == 0) break; if (i == argc) continue; } if (suppress) printf("%s\n", q+1); else printf("%s=%s\n", p, q+1); found = 1; } return found ? 0 : 3; }