int main(int argc, char *argv[]) { void *raster, *ptr; /* char *null_row; */ RASTER_MAP_TYPE out_type, map_type; char *outfile; char null_str[80]; char cell_buf[300]; int fd; int row, col; int nrows, ncols, dp; int do_stdout; FILE *fp; double cellsize; struct GModule *module; struct { struct Option *map; struct Option *output; struct Option *dp; struct Option *null; } parm; struct { struct Flag *noheader; struct Flag *singleline; struct Flag *ccenter; } flag; G_gisinit(argv[0]); module = G_define_module(); G_add_keyword(_("raster")); G_add_keyword(_("export")); module->description = _("Converts a raster map layer into an ESRI ARCGRID file."); /* Define the different options */ parm.map = G_define_standard_option(G_OPT_R_INPUT); parm.output = G_define_standard_option(G_OPT_R_OUTPUT); parm.output->gisprompt = "new_file,file,output"; parm.output->description = _("Name of an output ARC-GRID map (use out=- for stdout)"); parm.dp = G_define_option(); parm.dp->key = "dp"; parm.dp->type = TYPE_INTEGER; parm.dp->required = NO; parm.dp->answer = "8"; parm.dp->description = _("Number of decimal places"); flag.noheader = G_define_flag(); flag.noheader->key = 'h'; flag.noheader->description = _("Suppress printing of header information"); /* Added to optionally produce a single line output. -- emes -- 12.10.92 */ flag.singleline = G_define_flag(); flag.singleline->key = '1'; flag.singleline->description = _("List one entry per line instead of full row"); /* use cell center in header instead of cell corner */ flag.ccenter = G_define_flag(); flag.ccenter->key = 'c'; flag.ccenter->description = _("Use cell center reference in header instead of cell corner"); if (G_parser(argc, argv)) exit(EXIT_FAILURE); sscanf(parm.dp->answer, "%d", &dp); if (dp > 20 || dp < 0) G_fatal_error("dp has to be from 0 to 20"); outfile = parm.output->answer; if ((strcmp("-", outfile)) == 0) do_stdout = 1; else do_stdout = 0; sprintf(null_str, "-9999"); fd = Rast_open_old(parm.map->answer, ""); map_type = Rast_get_map_type(fd); out_type = map_type; /* null_row = Rast_allocate_null_buf(); */ raster = Rast_allocate_buf(out_type); nrows = Rast_window_rows(); ncols = Rast_window_cols(); /* open arc file for writing */ if (do_stdout) fp = stdout; else if (NULL == (fp = fopen(outfile, "w"))) G_fatal_error(_("Unable to open file <%s>"), outfile); if (!flag.noheader->answer) { struct Cell_head region; char buf[128]; G_get_window(®ion); fprintf(fp, "ncols %d\n", region.cols); fprintf(fp, "nrows %d\n", region.rows); cellsize = fabs(region.east - region.west) / region.cols; if (G_projection() != PROJECTION_LL) { /* Is Projection != LL (3) */ if (!flag.ccenter->answer) { G_format_easting(region.west, buf, region.proj); fprintf(fp, "xllcorner %s\n", buf); G_format_northing(region.south, buf, region.proj); fprintf(fp, "yllcorner %s\n", buf); } else { G_format_easting(region.west + cellsize / 2., buf, region.proj); fprintf(fp, "xllcenter %s\n", buf); G_format_northing(region.south + cellsize / 2., buf, region.proj); fprintf(fp, "yllcenter %s\n", buf); } } else { /* yes, lat/long */ fprintf(fp, "xllcorner %f\n", region.west); fprintf(fp, "yllcorner %f\n", region.south); } fprintf(fp, "cellsize %f\n", cellsize); fprintf(fp, "NODATA_value %s\n", null_str); } for (row = 0; row < nrows; row++) { G_percent(row, nrows, 2); Rast_get_row(fd, raster, row, out_type); /* Rast_get_null_value_row(fd, null_row, row); */ for (col = 0, ptr = raster; col < ncols; col++, ptr = G_incr_void_ptr(ptr, Rast_cell_size(out_type))) { if (!Rast_is_null_value(ptr, out_type)) { if (out_type == CELL_TYPE) fprintf(fp, "%d", *((CELL *) ptr)); else if (out_type == FCELL_TYPE) { sprintf(cell_buf, "%.*f", dp, *((FCELL *) ptr)); G_trim_decimal(cell_buf); fprintf(fp, "%s", cell_buf); } else if (out_type == DCELL_TYPE) { sprintf(cell_buf, "%.*f", dp, *((DCELL *) ptr)); G_trim_decimal(cell_buf); fprintf(fp, "%s", cell_buf); } } else fprintf(fp, "%s", null_str); if (!flag.singleline->answer) fprintf(fp, " "); else fprintf(fp, "\n"); } if (!flag.singleline->answer) fprintf(fp, "\n"); /* for (col = 0; col < ncols; col++) fprintf (fp,"%d ", null_row[col]); fprintf (fp,"\n"); */ } /* make sure it got to 100% */ G_percent(1, 1, 2); Rast_close(fd); fclose(fp); exit(EXIT_SUCCESS); }
void print_info(const struct Map_info *Map) { int i, map_type; char line[1024]; char timebuff[256]; struct TimeStamp ts; int time_ok, first_time_ok, second_time_ok; struct bound_box box; char tmp1[1024], tmp2[1024]; time_ok = first_time_ok = second_time_ok = FALSE; map_type = Vect_maptype(Map); /* Check the Timestamp */ time_ok = G_read_vector_timestamp(Vect_get_name(Map), NULL, "", &ts); /* Check for valid entries, show none if no timestamp available */ if (time_ok == TRUE) { if (ts.count > 0) first_time_ok = TRUE; if (ts.count > 1) second_time_ok = TRUE; } divider('+'); sprintf(line, "%-17s%s", _("Name:"), Vect_get_name(Map)); printline(line); sprintf(line, "%-17s%s", _("Mapset:"), Vect_get_mapset(Map)); printline(line); sprintf(line, "%-17s%s", _("Location:"), G_location()); printline(line); sprintf(line, "%-17s%s", _("Database:"), G_gisdbase()); printline(line); sprintf(line, "%-17s%s", _("Title:"), Vect_get_map_name(Map)); printline(line); sprintf(line, "%-17s1:%d", _("Map scale:"), Vect_get_scale(Map)); printline(line); sprintf(line, "%-17s%s", _("Name of creator:"), Vect_get_person(Map)); printline(line); sprintf(line, "%-17s%s", _("Organization:"), Vect_get_organization(Map)); printline(line); sprintf(line, "%-17s%s", _("Source date:"), Vect_get_map_date(Map)); printline(line); /* This shows the TimeStamp (if present) */ if (time_ok == TRUE && (first_time_ok || second_time_ok)) { G_format_timestamp(&ts, timebuff); sprintf(line, "%-17s%s", _("Timestamp (first layer): "), timebuff); printline(line); } else { strcpy(line, _("Timestamp (first layer): none")); printline(line); } divider('|'); if (map_type == GV_FORMAT_OGR || map_type == GV_FORMAT_OGR_DIRECT) { sprintf(line, "%-17s%s (%s)", _("Map format:"), Vect_maptype_info(Map), Vect_get_finfo_format_info(Map)); printline(line); /* for OGR format print also datasource and layer */ sprintf(line, "%-17s%s", _("OGR layer:"), Vect_get_finfo_layer_name(Map)); printline(line); sprintf(line, "%-17s%s", _("OGR datasource:"), Vect_get_finfo_dsn_name(Map)); printline(line); sprintf(line, "%-17s%s", _("Feature type:"), Vect_get_finfo_geometry_type(Map)); printline(line); } else if (map_type == GV_FORMAT_POSTGIS) { int topo_format; char *toposchema_name, *topogeom_column; int topo_geo_only; const struct Format_info *finfo; finfo = Vect_get_finfo(Map); sprintf(line, "%-17s%s (%s)", _("Map format:"), Vect_maptype_info(Map), Vect_get_finfo_format_info(Map)); printline(line); /* for PostGIS format print also datasource and layer */ sprintf(line, "%-17s%s", _("DB table:"), Vect_get_finfo_layer_name(Map)); printline(line); sprintf(line, "%-17s%s", _("DB name:"), Vect_get_finfo_dsn_name(Map)); printline(line); sprintf(line, "%-17s%s", _("Geometry column:"), finfo->pg.geom_column); printline(line); sprintf(line, "%-17s%s", _("Feature type:"), Vect_get_finfo_geometry_type(Map)); printline(line); topo_format = Vect_get_finfo_topology_info(Map, &toposchema_name, &topogeom_column, &topo_geo_only); if (topo_format == GV_TOPO_POSTGIS) { sprintf(line, "%-17s%s (%s %s%s)", _("Topology:"), "PostGIS", _("schema:"), toposchema_name, topo_geo_only ? ", topo-geo-only: yes" : ""); printline(line); sprintf(line, "%-17s%s", _("Topology column:"), topogeom_column); } else sprintf(line, "%-17s%s", _("Topology:"), "pseudo (simple features)"); printline(line); } else { sprintf(line, "%-17s%s", _("Map format:"), Vect_maptype_info(Map)); printline(line); } divider('|'); sprintf(line, " %s: %s (%s: %i)", _("Type of map"), _("vector"), _("level"), Vect_level(Map)); printline(line); if (Vect_level(Map) > 0) { printline(""); sprintf(line, " %-24s%-9d %-22s%-9d", _("Number of points:"), Vect_get_num_primitives(Map, GV_POINT), _("Number of centroids:"), Vect_get_num_primitives(Map, GV_CENTROID)); printline(line); sprintf(line, " %-24s%-9d %-22s%-9d", _("Number of lines:"), Vect_get_num_primitives(Map, GV_LINE), _("Number of boundaries:"), Vect_get_num_primitives(Map, GV_BOUNDARY)); printline(line); sprintf(line, " %-24s%-9d %-22s%-9d", _("Number of areas:"), Vect_get_num_areas(Map), _("Number of islands:"), Vect_get_num_islands(Map)); printline(line); if (Vect_is_3d(Map)) { sprintf(line, " %-24s%-9d %-22s%-9d", _("Number of faces:"), Vect_get_num_primitives(Map, GV_FACE), _("Number of kernels:"), Vect_get_num_primitives(Map, GV_KERNEL)); printline(line); sprintf(line, " %-24s%-9d %-22s%-9d", _("Number of volumes:"), Vect_get_num_volumes(Map), _("Number of holes:"), Vect_get_num_holes(Map)); printline(line); } printline(""); sprintf(line, " %-24s%s", _("Map is 3D:"), Vect_is_3d(Map) ? _("Yes") : _("No")); printline(line); sprintf(line, " %-24s%-9d", _("Number of dblinks:"), Vect_get_num_dblinks(Map)); printline(line); } printline(""); /* this differs from r.info in that proj info IS taken from the map here, not the location settings */ /* Vect_get_proj_name() and _zone() are typically unset?! */ if (G_projection() == PROJECTION_UTM) { int utm_zone; utm_zone = Vect_get_zone(Map); if (utm_zone < 0 || utm_zone > 60) strcpy(tmp1, _("invalid")); else if (utm_zone == 0) strcpy(tmp1, _("unspecified")); else sprintf(tmp1, "%d", utm_zone); sprintf(line, " %s: %s (%s %s)", _("Projection"), Vect_get_proj_name(Map), _("zone"), tmp1); } else sprintf(line, " %s: %s", _("Projection"), Vect_get_proj_name(Map)); printline(line); printline(""); Vect_get_map_box(Map, &box); G_format_northing(box.N, tmp1, G_projection()); G_format_northing(box.S, tmp2, G_projection()); sprintf(line, " %c: %17s %c: %17s", 'N', tmp1, 'S', tmp2); printline(line); G_format_easting(box.E, tmp1, G_projection()); G_format_easting(box.W, tmp2, G_projection()); sprintf(line, " %c: %17s %c: %17s", 'E', tmp1, 'W', tmp2); printline(line); if (Vect_is_3d(Map)) { format_double(box.B, tmp1); format_double(box.T, tmp2); sprintf(line, " %c: %17s %c: %17s", 'B', tmp1, 'T', tmp2); printline(line); } printline(""); format_double(Vect_get_thresh(Map), tmp1); sprintf(line, " %s: %s", _("Digitization threshold"), tmp1); printline(line); sprintf(line, " %s:", _("Comment")); printline(line); sprintf(line, " %s", Vect_get_comment(Map)); printline(line); divider('+'); fprintf(stdout, "\n"); }
int E_edit_cellhd(struct Cell_head *cellhd, int type) { char ll_north[20]; char ll_south[20]; char ll_east[20]; char ll_west[20]; char ll_nsres[20]; char ll_ewres[20]; char ll_def_north[20]; char ll_def_south[20]; char ll_def_east[20]; char ll_def_west[20]; char ll_def_ewres[20]; char ll_def_nsres[20]; char projection[80]; char **screen; struct Cell_head def_wind; double north, south, east, west; double nsres, ewres; char buf[64], buf2[30], *p; short ok; int line; char *prj; char *err; if (type == AS_CELLHD && (cellhd->rows <= 0 || cellhd->cols <= 0)) { G_message("E_edit_cellhd() - programmer error"); G_message(" ** rows and cols must be positive **"); return -1; } if (type != AS_DEF_WINDOW) { if (G_get_default_window(&def_wind) != 1) return -1; if (cellhd->proj < 0) { cellhd->proj = def_wind.proj; cellhd->zone = def_wind.zone; } else if (cellhd->zone < 0) cellhd->zone = def_wind.zone; } prj = G__projection_name(cellhd->proj); if (!prj) prj = "** unknown **"; sprintf(projection, "%d (%s)", cellhd->proj, prj); if (type != AS_DEF_WINDOW) { if (cellhd->west >= cellhd->east || cellhd->south >= cellhd->north) { cellhd->north = def_wind.north; cellhd->south = def_wind.south; cellhd->west = def_wind.west; cellhd->east = def_wind.east; if (type != AS_CELLHD) { cellhd->ew_res = def_wind.ew_res; cellhd->ns_res = def_wind.ns_res; cellhd->rows = def_wind.rows; cellhd->cols = def_wind.cols; } } if (cellhd->proj != def_wind.proj) { if (type == AS_CELLHD) G_message ("header projection %d differs from default projection %d", cellhd->proj, def_wind.proj); else G_message ("region projection %d differs from default projection %d", cellhd->proj, def_wind.proj); if (!G_yes("do you want to make them match? ", 1)) return -1; cellhd->proj = def_wind.proj; cellhd->zone = def_wind.zone; } if (cellhd->zone != def_wind.zone) { if (type == AS_CELLHD) G_message("header zone %d differs from default zone %d", cellhd->zone, def_wind.zone); else G_message("region zone %d differs from default zone %d", cellhd->zone, def_wind.zone); if (!G_yes("do you want to make them match? ", 1)) return -1; cellhd->zone = def_wind.zone; } *ll_def_north = 0; *ll_def_south = 0; *ll_def_east = 0; *ll_def_west = 0; *ll_def_ewres = 0; *ll_def_nsres = 0; format_northing(def_wind.north, ll_def_north, def_wind.proj); format_northing(def_wind.south, ll_def_south, def_wind.proj); format_easting(def_wind.east, ll_def_east, def_wind.proj); format_easting(def_wind.west, ll_def_west, def_wind.proj); format_resolution(def_wind.ew_res, ll_def_ewres, def_wind.proj); format_resolution(def_wind.ns_res, ll_def_nsres, def_wind.proj); } *ll_north = 0; *ll_south = 0; *ll_east = 0; *ll_west = 0; *ll_ewres = 0; *ll_nsres = 0; format_northing(cellhd->north, ll_north, cellhd->proj); format_northing(cellhd->south, ll_south, cellhd->proj); format_easting(cellhd->east, ll_east, cellhd->proj); format_easting(cellhd->west, ll_west, cellhd->proj); format_resolution(cellhd->ew_res, ll_ewres, cellhd->proj); format_resolution(cellhd->ns_res, ll_nsres, cellhd->proj); while (1) { ok = 1; /* List window options on the screen for the user to answer */ switch (type) { case AS_CELLHD: screen = cellhd_screen; break; case AS_DEF_WINDOW: screen = def_window_screen; break; default: screen = window_screen; break; } V_clear(); line = 0; while (*screen) V_line(line++, *screen++); /* V_ques ( variable, type, row, col, length) ; */ V_ques(ll_north, 's', 6, 36, 10); V_ques(ll_south, 's', 10, 36, 10); V_ques(ll_west, 's', 9, 12, 10); V_ques(ll_east, 's', 9, 52, 10); if (type != AS_CELLHD) { V_ques(ll_ewres, 's', 18, 48, 10); V_ques(ll_nsres, 's', 19, 48, 10); } if (type != AS_DEF_WINDOW) { V_const(ll_def_north, 's', 3, 36, 10); V_const(ll_def_south, 's', 13, 36, 10); V_const(ll_def_west, 's', 9, 1, 10); V_const(ll_def_east, 's', 9, 65, 10); if (type != AS_CELLHD) { V_const(ll_def_ewres, 's', 18, 21, 10); V_const(ll_def_nsres, 's', 19, 21, 10); } } V_const(projection, 's', 15, 23, (int)strlen(projection)); V_const(&cellhd->zone, 'i', 15, 60, 3); V_intrpt_ok(); if (!V_call()) return -1; G_squeeze(ll_north); G_squeeze(ll_south); G_squeeze(ll_east); G_squeeze(ll_west); if (type != AS_CELLHD) { G_squeeze(ll_ewres); G_squeeze(ll_nsres); } if (!G_scan_northing(ll_north, &cellhd->north, cellhd->proj)) { G_warning("Illegal value for north: %s", ll_north); ok = 0; } if (!G_scan_northing(ll_south, &cellhd->south, cellhd->proj)) { G_warning("Illegal value for south: %s", ll_south); ok = 0; } if (!G_scan_easting(ll_east, &cellhd->east, cellhd->proj)) { G_warning("Illegal value for east: %s", ll_east); ok = 0; } if (!G_scan_easting(ll_west, &cellhd->west, cellhd->proj)) { G_warning("Illegal value for west: %s", ll_west); ok = 0; } if (type != AS_CELLHD) { if (!G_scan_resolution(ll_ewres, &cellhd->ew_res, cellhd->proj)) { G_warning("Illegal east-west resolution: %s", ll_ewres); ok = 0; } if (!G_scan_resolution(ll_nsres, &cellhd->ns_res, cellhd->proj)) { G_warning("Illegal north-south resolution: %s", ll_nsres); ok = 0; } } if (!ok) { hitreturn(); continue; } /* Adjust and complete the cell header */ north = cellhd->north; south = cellhd->south; east = cellhd->east; west = cellhd->west; nsres = cellhd->ns_res; ewres = cellhd->ew_res; if ((err = G_adjust_Cell_head(cellhd, type == AS_CELLHD, type == AS_CELLHD))) { G_message("%s", err); hitreturn(); continue; } if (type == AS_CELLHD) { nsres = cellhd->ns_res; ewres = cellhd->ew_res; } SHOW: fprintf(stderr, "\n\n"); G_message(" projection: %s", projection); G_message(" zone: %d", cellhd->zone); G_format_northing(cellhd->north, buf, cellhd->proj); G_format_northing(north, buf2, cellhd->proj); fprintf(stderr, " north: %s", buf); if (strcmp(buf, buf2) != 0) { ok = 0; fprintf(stderr, " (Changed to match resolution)"); } fprintf(stderr, "\n"); G_format_northing(cellhd->south, buf, cellhd->proj); G_format_northing(south, buf2, cellhd->proj); fprintf(stderr, " south: %s", buf); if (strcmp(buf, buf2) != 0) { ok = 0; fprintf(stderr, " (Changed to match resolution)"); } fprintf(stderr, "\n"); G_format_easting(cellhd->east, buf, cellhd->proj); G_format_easting(east, buf2, cellhd->proj); fprintf(stderr, " east: %s", buf); if (strcmp(buf, buf2) != 0) { ok = 0; fprintf(stderr, " (Changed to match resolution)"); } fprintf(stderr, "\n"); G_format_easting(cellhd->west, buf, cellhd->proj); G_format_easting(west, buf2, cellhd->proj); fprintf(stderr, " west: %s", buf); if (strcmp(buf, buf2) != 0) { ok = 0; fprintf(stderr, " (Changed to match resolution)"); } fprintf(stderr, "\n\n"); G_format_resolution(cellhd->ew_res, buf, cellhd->proj); G_format_resolution(ewres, buf2, cellhd->proj); fprintf(stderr, " e-w res: %s", buf); if (strcmp(buf, buf2) != 0) { ok = 0; fprintf(stderr, " (Changed to conform to grid)"); } fprintf(stderr, "\n"); G_format_resolution(cellhd->ns_res, buf, cellhd->proj); G_format_resolution(nsres, buf2, cellhd->proj); fprintf(stderr, " n-s res: %s", buf); if (strcmp(buf, buf2) != 0) { ok = 0; fprintf(stderr, " (Changed to conform to grid)"); } fprintf(stderr, "\n\n"); G_message(" total rows: %15d", cellhd->rows); G_message(" total cols: %15d", cellhd->cols); sprintf(buf, "%lf", (double)cellhd->rows * cellhd->cols); *(p = strchr(buf, '.')) = 0; G_insert_commas(buf); G_message(" total cells: %15s", buf); fprintf(stderr, "\n"); if (type != AS_DEF_WINDOW) { if (cellhd->north > def_wind.north) { G_warning("north falls outside the default region"); ok = 0; } if (cellhd->south < def_wind.south) { G_warning("south falls outside the default region"); ok = 0; } if (cellhd->proj != PROJECTION_LL) { if (cellhd->east > def_wind.east) { G_warning("east falls outside the default region"); ok = 0; } if (cellhd->west < def_wind.west) { G_warning("west falls outside the default region"); ok = 0; } } } ASK: fflush(stdin); if (type == AS_CELLHD) fprintf(stderr, "\nDo you accept this header? (y/n) [%s] > ", ok ? "y" : "n"); else fprintf(stderr, "\nDo you accept this region? (y/n) [%s] > ", ok ? "y" : "n"); if (!G_gets(buf)) goto SHOW; G_strip(buf); switch (*buf) { case 0: break; case 'y': case 'Y': ok = 1; break; case 'n': case 'N': ok = 0; break; default: goto ASK; } if (ok) return 0; } }
int main(int argc, char *argv[]) { double e1, n1, e2, n2; char buf[256]; char command[2048]; int n, err; int projection; char *mapset; char name[GNAME_MAX]; struct GModule *module; struct { struct Option *map; struct Option *line; struct Option *null_str; /* struct Option *width; struct Option *result; */ } parms; struct Flag *coord; char coord_str[3]; G_gisinit(argv[0]); module = G_define_module(); module->keywords = _("raster, transect"); module->description = _("Outputs raster map layer values lying along " "user defined transect line(s)."); parms.map = G_define_standard_option(G_OPT_R_MAP); parms.map->description = _("Raster map to be queried"); /* parms.result = G_define_option(); parms.result->key = "result"; parms.result->key_desc = "type"; parms.result->type = TYPE_STRING; parms.result->description = _("Type of result to be output"); parms.result->required = NO; parms.result->multiple = NO; parms.result->options = "raw,median,average"; parms.result->answer = "raw"; */ parms.line = G_define_option(); parms.line->key = "line"; parms.line->key_desc = "east,north,azimuth,distance"; parms.line->type = TYPE_STRING; parms.line->description = _("Transect definition"); parms.line->required = YES; parms.line->multiple = YES; parms.null_str = G_define_option(); parms.null_str->key = "null"; parms.null_str->type = TYPE_STRING; parms.null_str->required = NO; parms.null_str->answer = "*"; parms.null_str->description = _("Char string to represent no data cell"); /* parms.width = G_define_option(); parms.width->key = "width"; parms.width->type = TYPE_INTEGER; parms.width->description = _("Transect width, in cells (odd number)"); parms.width->answer = "1"; */ coord = G_define_flag(); coord->key = 'g'; coord->description = _("Output easting and northing in first two columns of four column output"); if (G_parser(argc, argv)) exit(EXIT_FAILURE); projection = G_projection(); /* sscanf (parms.width->answer, "%d", &n); if (n <= 0 || n%2 == 0) { fprintf(stderr,"<%s=%s> ** illegal value **\n", parms.width->key, parms.width->answer); G_usage(); exit(EXIT_FAILURE); } */ strncpy(name, parms.map->answer, 255); mapset = G_find_cell(name, ""); if (mapset == NULL) G_fatal_error(_("Raster map <%s> not found"), name); if (coord->answer) strcpy(coord_str, "-g"); else strcpy(coord_str, ""); sprintf(command, "r.profile %s input=\"%s\" output=\"-\" null=\"%s\" profile=", coord_str, parms.map->answer, parms.null_str->answer); err = 0; for (n = 0; parms.line->answers[n]; n += 4) { err += parse_line(parms.line->key, parms.line->answers + n, &e1, &n1, &e2, &n2, projection); if (!err) { if (n) strcat(command, ","); G_format_easting(e1, buf, projection); strcat(command, buf); G_format_northing(n1, buf, projection); strcat(command, ","); strcat(command, buf); G_format_easting(e2, buf, projection); strcat(command, ","); strcat(command, buf); G_format_northing(n2, buf, projection); strcat(command, ","); strcat(command, buf); } } if (err) { G_usage(); exit(EXIT_FAILURE); } G_verbose_message(_("End coordinate: %.15g, %.15g"), e2, n2); exit(system(command)); }
char *G_site_format(const Site * s, const char *fs, int id) /* sprintf analog to G_site_put with the addition of a field separator fs and option of printing site attribute identifiers */ { char ebuf[MAX_SITE_STRING], nbuf[MAX_SITE_STRING]; char xbuf[MAX_SITE_STRING]; const char *nfs; char *buf; int fmt, i, j, k; buf = (char *)G_malloc(MAX_SITE_LEN * sizeof(char)); fmt = G_projection(); G_format_northing(s->north, nbuf, fmt); G_format_easting(s->east, ebuf, fmt); nfs = (char *)((fs == (char *)NULL) ? "|" : fs); sprintf(buf, "%s%s%s", ebuf, nfs, nbuf); for (i = 0; i < s->dim_alloc; ++i) { format_double(s->dim[i], nbuf); sprintf(xbuf, "%s%s", nfs, nbuf); G_strcat(buf, xbuf); } nfs = (fs == NULL) ? " " : fs; switch (s->cattype) { case CELL_TYPE: sprintf(xbuf, "%s%s%d ", nfs, ((id == 0) ? "" : "#"), (int)s->ccat); G_strcat(buf, xbuf); break; case FCELL_TYPE: case DCELL_TYPE: sprintf(xbuf, "%s%s%g ", nfs, ((id == 0) ? "" : "#"), (float)s->fcat); G_strcat(buf, xbuf); break; } for (i = 0; i < s->dbl_alloc; ++i) { format_double(s->dbl_att[i], nbuf); sprintf(xbuf, "%s%s%s", nfs, ((id == 0) ? "" : "%"), nbuf); G_strcat(buf, xbuf); } for (i = 0; i < s->str_alloc; ++i) { if (strlen(s->str_att[i]) != 0) { /* escape double quotes */ j = k = 0; /* do not uncomment this code because sites file was created * as we want. So it's enough to print them out as it is. * if (G_index (s->str_att[i], DQUOTE) != (char *) NULL) { while (!isnull(s->str_att[i][j])) { if (isquote(s->str_att[i][j])) { xbuf[k++] = BSLASH; xbuf[k++] = DQUOTE; } else xbuf[k++] = s->str_att[i][j]; j++; } xbuf[k] = (char) NULL; } else */ G_strcpy(xbuf, s->str_att[i]); G_strcpy(s->str_att[i], xbuf); if (G_index(s->str_att[i], SPACE) != (char *)NULL) sprintf(xbuf, "%s%s\"%s\"", nfs, ((id == 0) ? "" : "@"), s->str_att[i]); else sprintf(xbuf, "%s%s%s", nfs, ((id == 0) ? "" : "@"), s->str_att[i]); G_strcat(buf, xbuf); } } return buf; }
int main(int argc, char **argv) { char *mapname, /* ptr to name of output layer */ *setname, /* ptr to name of input mapset */ *ipolname; /* name of interpolation method */ int fdi, /* input map file descriptor */ fdo, /* output map file descriptor */ method, /* position of method in table */ permissions, /* mapset permissions */ cell_type, /* output celltype */ cell_size, /* size of a cell in bytes */ row, col, /* counters */ irows, icols, /* original rows, cols */ orows, ocols, have_colors, /* Input map has a colour table */ overwrite, /* Overwrite */ curr_proj; /* output projection (see gis.h) */ void *obuffer; /* buffer that holds one output row */ struct cache *ibuffer; /* buffer that holds the input map */ func interpolate; /* interpolation routine */ double xcoord2, /* temporary x coordinates */ ycoord2, /* temporary y coordinates */ onorth, osouth, /* save original border coords */ oeast, owest, inorth, isouth, ieast, iwest; char north_str[30], south_str[30], east_str[30], west_str[30]; struct Colors colr; /* Input map colour table */ struct History history; struct pj_info iproj, /* input map proj parameters */ oproj; /* output map proj parameters */ struct Key_Value *in_proj_info, /* projection information of */ *in_unit_info, /* input and output mapsets */ *out_proj_info, *out_unit_info; struct GModule *module; struct Flag *list, /* list files in source location */ *nocrop, /* don't crop output map */ *print_bounds, /* print output bounds and exit */ *gprint_bounds; /* same but print shell style */ struct Option *imapset, /* name of input mapset */ *inmap, /* name of input layer */ *inlocation, /* name of input location */ *outmap, /* name of output layer */ *indbase, /* name of input database */ *interpol, /* interpolation method: nearest neighbor, bilinear, cubic */ *memory, /* amount of memory for cache */ *res; /* resolution of target map */ struct Cell_head incellhd, /* cell header of input map */ outcellhd; /* and output map */ G_gisinit(argv[0]); module = G_define_module(); G_add_keyword(_("raster")); G_add_keyword(_("projection")); G_add_keyword(_("transformation")); module->description = _("Re-projects a raster map from given location to the current location."); inmap = G_define_standard_option(G_OPT_R_INPUT); inmap->description = _("Name of input raster map to re-project"); inmap->required = NO; inmap->guisection = _("Source"); inlocation = G_define_option(); inlocation->key = "location"; inlocation->type = TYPE_STRING; inlocation->required = YES; inlocation->description = _("Location containing input raster map"); inlocation->gisprompt = "old,location,location"; inlocation->key_desc = "name"; imapset = G_define_standard_option(G_OPT_M_MAPSET); imapset->label = _("Mapset containing input raster map"); imapset->description = _("Default: name of current mapset"); imapset->guisection = _("Source"); indbase = G_define_option(); indbase->key = "dbase"; indbase->type = TYPE_STRING; indbase->required = NO; indbase->description = _("Path to GRASS database of input location"); indbase->gisprompt = "old,dbase,dbase"; indbase->key_desc = "path"; indbase->guisection = _("Source"); outmap = G_define_standard_option(G_OPT_R_OUTPUT); outmap->required = NO; outmap->description = _("Name for output raster map (default: same as 'input')"); outmap->guisection = _("Target"); ipolname = make_ipol_list(); interpol = G_define_option(); interpol->key = "method"; interpol->type = TYPE_STRING; interpol->required = NO; interpol->answer = "nearest"; interpol->options = ipolname; interpol->description = _("Interpolation method to use"); interpol->guisection = _("Target"); interpol->descriptions = make_ipol_desc(); memory = G_define_option(); memory->key = "memory"; memory->type = TYPE_INTEGER; memory->required = NO; memory->answer = "300"; memory->label = _("Maximum memory to be used (in MB)"); memory->description = _("Cache size for raster rows"); res = G_define_option(); res->key = "resolution"; res->type = TYPE_DOUBLE; res->required = NO; res->description = _("Resolution of output raster map"); res->guisection = _("Target"); list = G_define_flag(); list->key = 'l'; list->description = _("List raster maps in input mapset and exit"); list->guisection = _("Print"); nocrop = G_define_flag(); nocrop->key = 'n'; nocrop->description = _("Do not perform region cropping optimization"); print_bounds = G_define_flag(); print_bounds->key = 'p'; print_bounds->description = _("Print input map's bounds in the current projection and exit"); print_bounds->guisection = _("Print"); gprint_bounds = G_define_flag(); gprint_bounds->key = 'g'; gprint_bounds->description = _("Print input map's bounds in the current projection and exit (shell style)"); gprint_bounds->guisection = _("Print"); /* The parser checks if the map already exists in current mapset, we switch out the check and do it in the module after the parser */ overwrite = G_check_overwrite(argc, argv); if (G_parser(argc, argv)) exit(EXIT_FAILURE); /* get the method */ for (method = 0; (ipolname = menu[method].name); method++) if (strcmp(ipolname, interpol->answer) == 0) break; if (!ipolname) G_fatal_error(_("<%s=%s> unknown %s"), interpol->key, interpol->answer, interpol->key); interpolate = menu[method].method; mapname = outmap->answer ? outmap->answer : inmap->answer; if (mapname && !list->answer && !overwrite && !print_bounds->answer && !gprint_bounds->answer && G_find_raster(mapname, G_mapset())) G_fatal_error(_("option <%s>: <%s> exists."), "output", mapname); setname = imapset->answer ? imapset->answer : G_store(G_mapset()); if (strcmp(inlocation->answer, G_location()) == 0 && (!indbase->answer || strcmp(indbase->answer, G_gisdbase()) == 0)) #if 0 G_fatal_error(_("Input and output locations can not be the same")); #else G_warning(_("Input and output locations are the same")); #endif G_get_window(&outcellhd); if(gprint_bounds->answer && !print_bounds->answer) print_bounds->answer = gprint_bounds->answer; curr_proj = G_projection(); /* Get projection info for output mapset */ if ((out_proj_info = G_get_projinfo()) == NULL) G_fatal_error(_("Unable to get projection info of output raster map")); if ((out_unit_info = G_get_projunits()) == NULL) G_fatal_error(_("Unable to get projection units of output raster map")); if (pj_get_kv(&oproj, out_proj_info, out_unit_info) < 0) G_fatal_error(_("Unable to get projection key values of output raster map")); /* Change the location */ G_create_alt_env(); G__setenv("GISDBASE", indbase->answer ? indbase->answer : G_gisdbase()); G__setenv("LOCATION_NAME", inlocation->answer); permissions = G__mapset_permissions(setname); if (permissions < 0) /* can't access mapset */ G_fatal_error(_("Mapset <%s> in input location <%s> - %s"), setname, inlocation->answer, permissions == 0 ? _("permission denied") : _("not found")); /* if requested, list the raster maps in source location - MN 5/2001 */ if (list->answer) { int i; char **list; G_verbose_message(_("Checking location <%s> mapset <%s>"), inlocation->answer, setname); list = G_list(G_ELEMENT_RASTER, G__getenv("GISDBASE"), G__getenv("LOCATION_NAME"), setname); for (i = 0; list[i]; i++) { fprintf(stdout, "%s\n", list[i]); } fflush(stdout); exit(EXIT_SUCCESS); /* leave r.proj after listing */ } if (!inmap->answer) G_fatal_error(_("Required parameter <%s> not set"), inmap->key); if (!G_find_raster(inmap->answer, setname)) G_fatal_error(_("Raster map <%s> in location <%s> in mapset <%s> not found"), inmap->answer, inlocation->answer, setname); /* Read input map colour table */ have_colors = Rast_read_colors(inmap->answer, setname, &colr); /* Get projection info for input mapset */ if ((in_proj_info = G_get_projinfo()) == NULL) G_fatal_error(_("Unable to get projection info of input map")); if ((in_unit_info = G_get_projunits()) == NULL) G_fatal_error(_("Unable to get projection units of input map")); if (pj_get_kv(&iproj, in_proj_info, in_unit_info) < 0) G_fatal_error(_("Unable to get projection key values of input map")); G_free_key_value(in_proj_info); G_free_key_value(in_unit_info); G_free_key_value(out_proj_info); G_free_key_value(out_unit_info); if (G_verbose() > G_verbose_std()) pj_print_proj_params(&iproj, &oproj); /* this call causes r.proj to read the entire map into memeory */ Rast_get_cellhd(inmap->answer, setname, &incellhd); Rast_set_input_window(&incellhd); if (G_projection() == PROJECTION_XY) G_fatal_error(_("Unable to work with unprojected data (xy location)")); /* Save default borders so we can show them later */ inorth = incellhd.north; isouth = incellhd.south; ieast = incellhd.east; iwest = incellhd.west; irows = incellhd.rows; icols = incellhd.cols; onorth = outcellhd.north; osouth = outcellhd.south; oeast = outcellhd.east; owest = outcellhd.west; orows = outcellhd.rows; ocols = outcellhd.cols; if (print_bounds->answer) { G_message(_("Input map <%s@%s> in location <%s>:"), inmap->answer, setname, inlocation->answer); outcellhd.north = -1e9; outcellhd.south = 1e9; outcellhd.east = -1e9; outcellhd.west = 1e9; bordwalk2(&incellhd, &outcellhd, &iproj, &oproj); inorth = outcellhd.north; isouth = outcellhd.south; ieast = outcellhd.east; iwest = outcellhd.west; G_format_northing(inorth, north_str, curr_proj); G_format_northing(isouth, south_str, curr_proj); G_format_easting(ieast, east_str, curr_proj); G_format_easting(iwest, west_str, curr_proj); if(gprint_bounds->answer) { fprintf(stdout, "n=%s s=%s w=%s e=%s rows=%d cols=%d\n", north_str, south_str, west_str, east_str, irows, icols); } else { fprintf(stdout, "Source cols: %d\n", icols); fprintf(stdout, "Source rows: %d\n", irows); fprintf(stdout, "Local north: %s\n", north_str); fprintf(stdout, "Local south: %s\n", south_str); fprintf(stdout, "Local west: %s\n", west_str); fprintf(stdout, "Local east: %s\n", east_str); } /* somehow approximate local ewres, nsres ?? (use 'g.region -m' on lat/lon side) */ exit(EXIT_SUCCESS); } /* Cut non-overlapping parts of input map */ if (!nocrop->answer) bordwalk(&outcellhd, &incellhd, &oproj, &iproj); /* Add 2 cells on each side for bilinear/cubic & future interpolation methods */ /* (should probably be a factor based on input and output resolution) */ incellhd.north += 2 * incellhd.ns_res; incellhd.east += 2 * incellhd.ew_res; incellhd.south -= 2 * incellhd.ns_res; incellhd.west -= 2 * incellhd.ew_res; if (incellhd.north > inorth) incellhd.north = inorth; if (incellhd.east > ieast) incellhd.east = ieast; if (incellhd.south < isouth) incellhd.south = isouth; if (incellhd.west < iwest) incellhd.west = iwest; Rast_set_input_window(&incellhd); /* And switch back to original location */ G_switch_env(); /* Adjust borders of output map */ if (!nocrop->answer) bordwalk(&incellhd, &outcellhd, &iproj, &oproj); #if 0 outcellhd.west = outcellhd.south = HUGE_VAL; outcellhd.east = outcellhd.north = -HUGE_VAL; for (row = 0; row < incellhd.rows; row++) { ycoord1 = Rast_row_to_northing((double)(row + 0.5), &incellhd); for (col = 0; col < incellhd.cols; col++) { xcoord1 = Rast_col_to_easting((double)(col + 0.5), &incellhd); pj_do_proj(&xcoord1, &ycoord1, &iproj, &oproj); if (xcoord1 > outcellhd.east) outcellhd.east = xcoord1; if (ycoord1 > outcellhd.north) outcellhd.north = ycoord1; if (xcoord1 < outcellhd.west) outcellhd.west = xcoord1; if (ycoord1 < outcellhd.south) outcellhd.south = ycoord1; } } #endif if (res->answer != NULL) /* set user defined resolution */ outcellhd.ns_res = outcellhd.ew_res = atof(res->answer); G_adjust_Cell_head(&outcellhd, 0, 0); Rast_set_output_window(&outcellhd); G_message(" "); G_message(_("Input:")); G_message(_("Cols: %d (%d)"), incellhd.cols, icols); G_message(_("Rows: %d (%d)"), incellhd.rows, irows); G_message(_("North: %f (%f)"), incellhd.north, inorth); G_message(_("South: %f (%f)"), incellhd.south, isouth); G_message(_("West: %f (%f)"), incellhd.west, iwest); G_message(_("East: %f (%f)"), incellhd.east, ieast); G_message(_("EW-res: %f"), incellhd.ew_res); G_message(_("NS-res: %f"), incellhd.ns_res); G_message(" "); G_message(_("Output:")); G_message(_("Cols: %d (%d)"), outcellhd.cols, ocols); G_message(_("Rows: %d (%d)"), outcellhd.rows, orows); G_message(_("North: %f (%f)"), outcellhd.north, onorth); G_message(_("South: %f (%f)"), outcellhd.south, osouth); G_message(_("West: %f (%f)"), outcellhd.west, owest); G_message(_("East: %f (%f)"), outcellhd.east, oeast); G_message(_("EW-res: %f"), outcellhd.ew_res); G_message(_("NS-res: %f"), outcellhd.ns_res); G_message(" "); /* open and read the relevant parts of the input map and close it */ G_switch_env(); Rast_set_input_window(&incellhd); fdi = Rast_open_old(inmap->answer, setname); cell_type = Rast_get_map_type(fdi); ibuffer = readcell(fdi, memory->answer); Rast_close(fdi); G_switch_env(); Rast_set_output_window(&outcellhd); if (strcmp(interpol->answer, "nearest") == 0) { fdo = Rast_open_new(mapname, cell_type); obuffer = (CELL *) Rast_allocate_output_buf(cell_type); } else { fdo = Rast_open_fp_new(mapname); cell_type = FCELL_TYPE; obuffer = (FCELL *) Rast_allocate_output_buf(cell_type); } cell_size = Rast_cell_size(cell_type); xcoord2 = outcellhd.west + (outcellhd.ew_res / 2); ycoord2 = outcellhd.north - (outcellhd.ns_res / 2); G_important_message(_("Projecting...")); for (row = 0; row < outcellhd.rows; row++) { /* obufptr = obuffer */; G_percent(row, outcellhd.rows - 1, 2); #if 0 /* parallelization does not always work, * segfaults in the interpolation functions * can happen */ #pragma omp parallel for schedule (static) #endif for (col = 0; col < outcellhd.cols; col++) { void *obufptr = (void *)((const unsigned char *)obuffer + col * cell_size); double xcoord1 = xcoord2 + (col) * outcellhd.ew_res; double ycoord1 = ycoord2; /* project coordinates in output matrix to */ /* coordinates in input matrix */ if (pj_do_proj(&xcoord1, &ycoord1, &oproj, &iproj) < 0) Rast_set_null_value(obufptr, 1, cell_type); else { /* convert to row/column indices of input matrix */ /* column index in input matrix */ double col_idx = (xcoord1 - incellhd.west) / incellhd.ew_res; /* row index in input matrix */ double row_idx = (incellhd.north - ycoord1) / incellhd.ns_res; /* and resample data point */ interpolate(ibuffer, obufptr, cell_type, col_idx, row_idx, &incellhd); } /* obufptr = G_incr_void_ptr(obufptr, cell_size); */ } Rast_put_row(fdo, obuffer, cell_type); xcoord2 = outcellhd.west + (outcellhd.ew_res / 2); ycoord2 -= outcellhd.ns_res; } Rast_close(fdo); release_cache(ibuffer); if (have_colors > 0) { Rast_write_colors(mapname, G_mapset(), &colr); Rast_free_colors(&colr); } Rast_short_history(mapname, "raster", &history); Rast_command_history(&history); Rast_write_history(mapname, &history); G_done_msg(" "); exit(EXIT_SUCCESS); }
int WriteProfile(char *raster, char *mapset, char *fname, char letter, struct Profile *profile) { int proj; char *buf = G_calloc(UCAT_STR_SIZE, sizeof(char)); char *buf2 = G_calloc(UCAT_STR_SIZE, sizeof(char)); char *outfile = G_calloc(strlen(fname) + 3, sizeof(char)); char coords[4][80]; char outfmt[24]; FILE *outFile; struct ProfileNode *ptr; memset(coords[0], 0, 80); memset(coords[1], 0, 80); memset(coords[2], 0, 80); memset(coords[3], 0, 80); /* Something's wrong if all of these are not NULL */ if (raster == NULL || mapset == NULL || fname == NULL || profile == NULL) return -1; /* If no profiles were done, do nothing */ if (profile->ptr == NULL || profile->count == 0) return 0; /* Each profile has the letter appended to the name */ sprintf(outfile, "%s.%c", fname, letter); outFile = fopen(outfile, "w"); if (outFile == NULL) return -1; proj = G_projection(); /* Do the Header */ fprintf(outFile, "# Profile %c of %s@%s\n", letter, raster, mapset); G_format_easting(profile->e1, coords[0], proj); G_format_northing(profile->n1, coords[1], proj); G_format_easting(profile->e2, coords[2], proj); G_format_northing(profile->n2, coords[3], proj); fprintf(outFile, "# From (%s, %s) to (%s, %s)\n", coords[0], coords[1], coords[2], coords[3]); fprintf(outFile, "# Stats: Count = %ld, Min = %s, Max = %s\n", profile->count, _fmt_ucat(&profile->MinCat, &profile->MinCat, buf), _fmt_ucat(&profile->MaxCat, &profile->MinCat, buf2)); fprintf(outFile, "# dist value east north\n"); if (G_projection() == PROJECTION_LL) strcpy(outfmt, "%f %s %.8f %.8f\n"); else strcpy(outfmt, "%f %s %.3f %.3f\n"); /* Now loop through the nodes, one value per line */ for (ptr = profile->ptr; ptr != NULL; ptr = ptr->next) { fprintf(outFile, outfmt, ptr->dist, _fmt_ucat(&ptr->cat, &profile->MinCat, buf), ptr->east, ptr->north); } G_free(buf); G_free(buf2); G_free(outfile); fclose(outFile); return 0; } /* WriteProfile() */