void update_default_window(struct Cell_head *cellhd) { /* -------------------------------------------------------------------- */ /* Extend current window based on dataset. */ /* -------------------------------------------------------------------- */ struct Cell_head cur_wind; if (strcmp(G_mapset(), "PERMANENT") == 0) /* fixme: expand WIND and DEFAULT_WIND independently. (currently WIND gets forgotten and DEFAULT_WIND is expanded for both) */ G_get_default_window(&cur_wind); else G_get_window(&cur_wind); cur_wind.north = MAX(cur_wind.north, cellhd->north); cur_wind.south = MIN(cur_wind.south, cellhd->south); cur_wind.west = MIN(cur_wind.west, cellhd->west); cur_wind.east = MAX(cur_wind.east, cellhd->east); cur_wind.rows = (int)ceil((cur_wind.north - cur_wind.south) / cur_wind.ns_res); cur_wind.south = cur_wind.north - cur_wind.rows * cur_wind.ns_res; cur_wind.cols = (int)ceil((cur_wind.east - cur_wind.west) / cur_wind.ew_res); cur_wind.east = cur_wind.west + cur_wind.cols * cur_wind.ew_res; if (strcmp(G_mapset(), "PERMANENT") == 0) { G_put_element_window(&cur_wind, "", "DEFAULT_WIND"); G_message(_("Default region for this location updated")); } G_put_window(&cur_wind); G_message(_("Region for the current mapset updated")); }
void update_default_window(struct Cell_head *cellhd) { /* -------------------------------------------------------------------- */ /* Extend current window based on dataset. */ /* -------------------------------------------------------------------- */ struct Cell_head def_wind; G_get_default_window(&def_wind); def_wind.north = MAX(def_wind.north, cellhd->north); def_wind.south = MIN(def_wind.south, cellhd->south); def_wind.west = MIN(def_wind.west, cellhd->west); def_wind.east = MAX(def_wind.east, cellhd->east); def_wind.rows = (int)ceil((def_wind.north - def_wind.south) / def_wind.ns_res); def_wind.south = def_wind.north - def_wind.rows * def_wind.ns_res; def_wind.cols = (int)ceil((def_wind.east - def_wind.west) / def_wind.ew_res); def_wind.east = def_wind.west + def_wind.cols * def_wind.ew_res; G__put_window(&def_wind, "../PERMANENT", "DEFAULT_WIND"); }
/* Zoom - default region */ int zoom_default(void) { struct Cell_head defwin; G_debug(2, "zoom_default()"); driver_open(); G_get_default_window(&defwin); G_put_window(&defwin); G_set_window(&defwin); display_redraw(); driver_close(); G_debug(3, "zoom_default(): End"); return 1; }
QgsCoordinateReferenceSystem GRASS_EXPORT QgsGrass::crsDirect( QString gisdbase, QString location ) { QString Wkt; struct Cell_head cellhd; QgsGrass::resetError(); QgsGrass::setLocation( gisdbase, location ); const char *oldlocale = setlocale( LC_NUMERIC, NULL ); setlocale( LC_NUMERIC, "C" ); try { G_get_default_window( &cellhd ); } catch ( QgsGrass::Exception &e ) { Q_UNUSED( e ); setlocale( LC_NUMERIC, oldlocale ); QgsDebugMsg( QString( "Cannot get default window: %1" ).arg( e.what() ) ); return QgsCoordinateReferenceSystem(); } if ( cellhd.proj != PROJECTION_XY ) { struct Key_Value *projinfo = G_get_projinfo(); struct Key_Value *projunits = G_get_projunits(); char *wkt = GPJ_grass_to_wkt( projinfo, projunits, 0, 0 ); Wkt = QString( wkt ); G_free( wkt ); } setlocale( LC_NUMERIC, oldlocale ); QgsCoordinateReferenceSystem srs; srs.createFromWkt( Wkt ); return srs; }
int G__make_mapset(const char *gisdbase_name, const char *location_name, const char *mapset_name) { char path[GPATH_MAX]; struct Cell_head default_window; /* Get location */ if (location_name == NULL) location_name = G_location(); /* Get GISDBASE */ if (gisdbase_name == NULL) gisdbase_name = G_gisdbase(); /* TODO: Should probably check that user specified location and gisdbase are valid */ /* Make the mapset. */ sprintf(path, "%s/%s/%s", gisdbase_name, location_name, mapset_name); if (G_mkdir(path) != 0) return -1; G__create_alt_env(); /* Get PERMANENT default window */ G__setenv("GISDBASE", gisdbase_name); G__setenv("LOCATION", location_name); G__setenv("MAPSET", "PERMANENT"); G_get_default_window(&default_window); /* Change to the new mapset */ G__setenv("MAPSET", mapset_name); /* Copy default window/regions to new mapset */ G__put_window(&default_window, "", "WIND"); /* And switch back to original environment */ G__switch_env(); return 0; }
void modify_projinfo() { const char *mapset = G_mapset(); struct Cell_head old_cellhd; if (strcmp(mapset, "PERMANENT") != 0) G_fatal_error(_("You must select the PERMANENT mapset before updating the " "current location's projection (current mapset is <%s>)."), mapset); /* Read projection information from current location first */ G_get_default_window(&old_cellhd); char path[GPATH_MAX]; /* Write out the PROJ_INFO, and PROJ_UNITS if available. */ if (projinfo != NULL) { G_file_name(path, "", "PROJ_INFO", "PERMANENT"); G_write_key_value_file(path, projinfo); } if (projunits != NULL) { G_file_name(path, "", "PROJ_UNITS", "PERMANENT"); G_write_key_value_file(path, projunits); } if ((old_cellhd.zone != cellhd.zone) || (old_cellhd.proj != cellhd.proj)) { /* Recreate the default, and current window files if projection * number or zone have changed */ G__put_window(&cellhd, "", "DEFAULT_WIND"); G__put_window(&cellhd, "", "WIND"); G_message(_("Default region was updated to the new projection, but if you have " "multiple mapsets `g.region -d` should be run in each to update the " "region from the default")); } G_important_message(_("Projection information updated")); }
int main(int argc, char *argv[]) { struct GModule *module; struct _param { struct Option *dsn, *out, *layer, *spat, *where, *min_area; struct Option *snap, *type, *outloc, *cnames; } param; struct _flag { struct Flag *list, *tlist, *no_clean, *z, *notab, *region; struct Flag *over, *extend, *formats, *tolower, *no_import; } flag; int i, j, layer, arg_s_num, nogeom, ncnames; float xmin, ymin, xmax, ymax; int ncols = 0, type; double min_area, snap; char buf[2000], namebuf[2000], tempvect[GNAME_MAX]; char *separator; struct Key_Value *loc_proj_info, *loc_proj_units; struct Key_Value *proj_info, *proj_units; struct Cell_head cellhd, loc_wind, cur_wind; char error_msg[8192]; /* Vector */ struct Map_info Map, Tmp, *Out; int cat; /* Attributes */ struct field_info *Fi; dbDriver *driver; dbString sql, strval; int dim, with_z; /* OGR */ OGRDataSourceH Ogr_ds; OGRLayerH Ogr_layer; OGRFieldDefnH Ogr_field; char *Ogr_fieldname; OGRFieldType Ogr_ftype; OGRFeatureH Ogr_feature; OGRFeatureDefnH Ogr_featuredefn; OGRGeometryH Ogr_geometry, Ogr_oRing, poSpatialFilter; OGRSpatialReferenceH Ogr_projection; OGREnvelope oExt; OGRwkbGeometryType Ogr_geom_type; int OFTIntegerListlength; char *output; char **layer_names; /* names of layers to be imported */ int *layers; /* layer indexes */ int nlayers; /* number of layers to import */ char **available_layer_names; /* names of layers to be imported */ int navailable_layers; int layer_id; unsigned int n_features, feature_count; int overwrite; double area_size; int use_tmp_vect; xmin = ymin = xmax = ymax = 0.0; loc_proj_info = loc_proj_units = NULL; Ogr_ds = Ogr_oRing = poSpatialFilter = NULL; OFTIntegerListlength = 40; /* hack due to limitation in OGR */ area_size = 0.0; use_tmp_vect = FALSE; G_gisinit(argv[0]); module = G_define_module(); G_add_keyword(_("vector")); G_add_keyword(_("import")); module->description = _("Converts vector data into a GRASS vector map using OGR library."); param.dsn = G_define_option(); param.dsn->key = "dsn"; param.dsn->type = TYPE_STRING; param.dsn->required =YES; param.dsn->label = _("OGR datasource name"); param.dsn->description = _("Examples:\n" "\t\tESRI Shapefile: directory containing shapefiles\n" "\t\tMapInfo File: directory containing mapinfo files"); param.layer = G_define_option(); param.layer->key = "layer"; param.layer->type = TYPE_STRING; param.layer->required = NO; param.layer->multiple = YES; param.layer->label = _("OGR layer name. If not given, all available layers are imported"); param.layer->description = _("Examples:\n" "\t\tESRI Shapefile: shapefile name\n" "\t\tMapInfo File: mapinfo file name"); param.layer->guisection = _("Selection"); param.out = G_define_standard_option(G_OPT_V_OUTPUT); param.out->required = NO; param.out->guisection = _("Output"); param.spat = G_define_option(); param.spat->key = "spatial"; param.spat->type = TYPE_DOUBLE; param.spat->multiple = YES; param.spat->required = NO; param.spat->key_desc = "xmin,ymin,xmax,ymax"; param.spat->label = _("Import subregion only"); param.spat->guisection = _("Selection"); param.spat->description = _("Format: xmin,ymin,xmax,ymax - usually W,S,E,N"); param.where = G_define_standard_option(G_OPT_DB_WHERE); param.where->guisection = _("Selection"); param.min_area = G_define_option(); param.min_area->key = "min_area"; param.min_area->type = TYPE_DOUBLE; param.min_area->required = NO; param.min_area->answer = "0.0001"; param.min_area->label = _("Minimum size of area to be imported (square units)"); param.min_area->guisection = _("Selection"); param.min_area->description = _("Smaller areas and " "islands are ignored. Should be greater than snap^2"); param.type = G_define_standard_option(G_OPT_V_TYPE); param.type->options = "point,line,boundary,centroid"; param.type->answer = ""; param.type->description = _("Optionally change default input type"); param.type->descriptions = _("point;import area centroids as points;" "line;import area boundaries as lines;" "boundary;import lines as area boundaries;" "centroid;import points as centroids"); param.type->guisection = _("Selection"); param.snap = G_define_option(); param.snap->key = "snap"; param.snap->type = TYPE_DOUBLE; param.snap->required = NO; param.snap->answer = "-1"; param.snap->label = _("Snapping threshold for boundaries"); param.snap->description = _("'-1' for no snap"); param.outloc = G_define_option(); param.outloc->key = "location"; param.outloc->type = TYPE_STRING; param.outloc->required = NO; param.outloc->description = _("Name for new location to create"); param.outloc->key_desc = "name"; param.cnames = G_define_option(); param.cnames->key = "cnames"; param.cnames->type = TYPE_STRING; param.cnames->required = NO; param.cnames->multiple = YES; param.cnames->description = _("List of column names to be used instead of original names, " "first is used for category column"); param.cnames->guisection = _("Attributes"); flag.list = G_define_flag(); flag.list->key = 'l'; flag.list->description = _("List available OGR layers in data source and exit"); flag.list->suppress_required = YES; flag.list->guisection = _("Print"); flag.tlist = G_define_flag(); flag.tlist->key = 'a'; flag.tlist->description = _("List available OGR layers including feature types " "in data source and exit"); flag.tlist->suppress_required = YES; flag.tlist->guisection = _("Print"); flag.formats = G_define_flag(); flag.formats->key = 'f'; flag.formats->description = _("List supported formats and exit"); flag.formats->suppress_required = YES; flag.formats->guisection = _("Print"); /* if using -c, you lose topological information ! */ flag.no_clean = G_define_flag(); flag.no_clean->key = 'c'; flag.no_clean->description = _("Do not clean polygons (not recommended)"); flag.no_clean->guisection = _("Output"); flag.z = G_define_flag(); flag.z->key = 'z'; flag.z->description = _("Create 3D output"); flag.z->guisection = _("Output"); flag.notab = G_define_flag(); flag.notab->key = 't'; flag.notab->description = _("Do not create attribute table"); flag.notab->guisection = _("Attributes"); flag.over = G_define_flag(); flag.over->key = 'o'; flag.over->description = _("Override dataset projection (use location's projection)"); flag.region = G_define_flag(); flag.region->key = 'r'; flag.region->guisection = _("Selection"); flag.region->description = _("Limit import to the current region"); flag.extend = G_define_flag(); flag.extend->key = 'e'; flag.extend->description = _("Extend location extents based on new dataset"); flag.tolower = G_define_flag(); flag.tolower->key = 'w'; flag.tolower->description = _("Change column names to lowercase characters"); flag.tolower->guisection = _("Attributes"); flag.no_import = G_define_flag(); flag.no_import->key = 'i'; flag.no_import->description = _("Create the location specified by the \"location\" parameter and exit." " Do not import the vector data."); /* The parser checks if the map already exists in current mapset, this is * wrong if location options is used, so 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); G_begin_polygon_area_calculations(); /* Used in geom() */ OGRRegisterAll(); /* list supported formats */ if (flag.formats->answer) { int iDriver; G_message(_("Available OGR Drivers:")); for (iDriver = 0; iDriver < OGRGetDriverCount(); iDriver++) { OGRSFDriverH poDriver = OGRGetDriver(iDriver); const char *pszRWFlag; if (OGR_Dr_TestCapability(poDriver, ODrCCreateDataSource)) pszRWFlag = "rw"; else pszRWFlag = "ro"; fprintf(stdout, " %s (%s): %s\n", OGR_Dr_GetName(poDriver), pszRWFlag, OGR_Dr_GetName(poDriver)); } exit(EXIT_SUCCESS); } if (param.dsn->answer == NULL) { G_fatal_error(_("Required parameter <%s> not set"), param.dsn->key); } min_area = atof(param.min_area->answer); snap = atof(param.snap->answer); type = Vect_option_to_types(param.type); ncnames = 0; if (param.cnames->answers) { i = 0; while (param.cnames->answers[i++]) { ncnames++; } } /* Open OGR DSN */ Ogr_ds = NULL; if (strlen(param.dsn->answer) > 0) Ogr_ds = OGROpen(param.dsn->answer, FALSE, NULL); if (Ogr_ds == NULL) G_fatal_error(_("Unable to open data source <%s>"), param.dsn->answer); /* Make a list of available layers */ navailable_layers = OGR_DS_GetLayerCount(Ogr_ds); available_layer_names = (char **)G_malloc(navailable_layers * sizeof(char *)); if (flag.list->answer || flag.tlist->answer) G_message(_("Data source <%s> (format '%s') contains %d layers:"), param.dsn->answer, OGR_Dr_GetName(OGR_DS_GetDriver(Ogr_ds)), navailable_layers); for (i = 0; i < navailable_layers; i++) { Ogr_layer = OGR_DS_GetLayer(Ogr_ds, i); Ogr_featuredefn = OGR_L_GetLayerDefn(Ogr_layer); Ogr_geom_type = OGR_FD_GetGeomType(Ogr_featuredefn); available_layer_names[i] = G_store((char *)OGR_FD_GetName(Ogr_featuredefn)); if (flag.tlist->answer) fprintf(stdout, "%s (%s)\n", available_layer_names[i], OGRGeometryTypeToName(Ogr_geom_type)); else if (flag.list->answer) fprintf(stdout, "%s\n", available_layer_names[i]); } if (flag.list->answer || flag.tlist->answer) { fflush(stdout); exit(EXIT_SUCCESS); } /* Make a list of layers to be imported */ if (param.layer->answer) { /* From option */ nlayers = 0; while (param.layer->answers[nlayers]) nlayers++; layer_names = (char **)G_malloc(nlayers * sizeof(char *)); layers = (int *)G_malloc(nlayers * sizeof(int)); for (i = 0; i < nlayers; i++) { layer_names[i] = G_store(param.layer->answers[i]); /* Find it in the source */ layers[i] = -1; for (j = 0; j < navailable_layers; j++) { if (strcmp(available_layer_names[j], layer_names[i]) == 0) { layers[i] = j; break; } } if (layers[i] == -1) G_fatal_error(_("Layer <%s> not available"), layer_names[i]); } } else { /* use list of all layers */ nlayers = navailable_layers; layer_names = available_layer_names; layers = (int *)G_malloc(nlayers * sizeof(int)); for (i = 0; i < nlayers; i++) layers[i] = i; } if (param.out->answer) { output = G_store(param.out->answer); } else { if (nlayers < 1) G_fatal_error(_("No OGR layers available")); output = G_store(layer_names[0]); G_message(_("All available OGR layers will be imported into vector map <%s>"), output); } if (!param.outloc->answer) { /* Check if the map exists */ if (G_find_vector2(output, G_mapset()) && !overwrite) G_fatal_error(_("Vector map <%s> already exists"), output); } /* Get first imported layer to use for extents and projection check */ Ogr_layer = OGR_DS_GetLayer(Ogr_ds, layers[0]); if (flag.region->answer) { if (param.spat->answer) G_fatal_error(_("Select either the current region flag or the spatial option, not both")); G_get_window(&cur_wind); xmin = cur_wind.west; xmax = cur_wind.east; ymin = cur_wind.south; ymax = cur_wind.north; } if (param.spat->answer) { /* See as reference: gdal/ogr/ogr_capi_test.c */ /* cut out a piece of the map */ /* order: xmin,ymin,xmax,ymax */ arg_s_num = 0; i = 0; while (param.spat->answers[i]) { if (i == 0) xmin = atof(param.spat->answers[i]); if (i == 1) ymin = atof(param.spat->answers[i]); if (i == 2) xmax = atof(param.spat->answers[i]); if (i == 3) ymax = atof(param.spat->answers[i]); arg_s_num++; i++; } if (arg_s_num != 4) G_fatal_error(_("4 parameters required for 'spatial' parameter")); } if (param.spat->answer || flag.region->answer) { G_debug(2, "cut out with boundaries: xmin:%f ymin:%f xmax:%f ymax:%f", xmin, ymin, xmax, ymax); /* in theory this could be an irregular polygon */ poSpatialFilter = OGR_G_CreateGeometry(wkbPolygon); Ogr_oRing = OGR_G_CreateGeometry(wkbLinearRing); OGR_G_AddPoint(Ogr_oRing, xmin, ymin, 0.0); OGR_G_AddPoint(Ogr_oRing, xmin, ymax, 0.0); OGR_G_AddPoint(Ogr_oRing, xmax, ymax, 0.0); OGR_G_AddPoint(Ogr_oRing, xmax, ymin, 0.0); OGR_G_AddPoint(Ogr_oRing, xmin, ymin, 0.0); OGR_G_AddGeometryDirectly(poSpatialFilter, Ogr_oRing); OGR_L_SetSpatialFilter(Ogr_layer, poSpatialFilter); } if (param.where->answer) { /* select by attribute */ OGR_L_SetAttributeFilter(Ogr_layer, param.where->answer); } /* fetch boundaries */ if ((OGR_L_GetExtent(Ogr_layer, &oExt, 1)) == OGRERR_NONE) { G_get_window(&cellhd); cellhd.north = oExt.MaxY; cellhd.south = oExt.MinY; cellhd.west = oExt.MinX; cellhd.east = oExt.MaxX; cellhd.rows = 20; /* TODO - calculate useful values */ cellhd.cols = 20; cellhd.ns_res = (cellhd.north - cellhd.south) / cellhd.rows; cellhd.ew_res = (cellhd.east - cellhd.west) / cellhd.cols; } else { cellhd.north = 1.; cellhd.south = 0.; cellhd.west = 0.; cellhd.east = 1.; cellhd.top = 1.; cellhd.bottom = 1.; cellhd.rows = 1; cellhd.rows3 = 1; cellhd.cols = 1; cellhd.cols3 = 1; cellhd.depths = 1; cellhd.ns_res = 1.; cellhd.ns_res3 = 1.; cellhd.ew_res = 1.; cellhd.ew_res3 = 1.; cellhd.tb_res = 1.; } /* suppress boundary splitting ? */ if (flag.no_clean->answer) { split_distance = -1.; } else { split_distance = 0.; area_size = sqrt((cellhd.east - cellhd.west) * (cellhd.north - cellhd.south)); } /* Fetch input map projection in GRASS form. */ proj_info = NULL; proj_units = NULL; Ogr_projection = OGR_L_GetSpatialRef(Ogr_layer); /* should not be freed later */ /* Do we need to create a new location? */ if (param.outloc->answer != NULL) { /* Convert projection information non-interactively as we can't * assume the user has a terminal open */ if (GPJ_osr_to_grass(&cellhd, &proj_info, &proj_units, Ogr_projection, 0) < 0) { G_fatal_error(_("Unable to convert input map projection to GRASS " "format; cannot create new location.")); } else { G_make_location(param.outloc->answer, &cellhd, proj_info, proj_units, NULL); G_message(_("Location <%s> created"), param.outloc->answer); } /* If the i flag is set, clean up? and exit here */ if(flag.no_import->answer) { exit(EXIT_SUCCESS); } } else { int err = 0; /* Projection only required for checking so convert non-interactively */ if (GPJ_osr_to_grass(&cellhd, &proj_info, &proj_units, Ogr_projection, 0) < 0) G_warning(_("Unable to convert input map projection information to " "GRASS format for checking")); /* Does the projection of the current location match the dataset? */ /* G_get_window seems to be unreliable if the location has been changed */ G__get_window(&loc_wind, "", "DEFAULT_WIND", "PERMANENT"); /* fetch LOCATION PROJ info */ if (loc_wind.proj != PROJECTION_XY) { loc_proj_info = G_get_projinfo(); loc_proj_units = G_get_projunits(); } if (flag.over->answer) { cellhd.proj = loc_wind.proj; cellhd.zone = loc_wind.zone; G_message(_("Over-riding projection check")); } else if (loc_wind.proj != cellhd.proj || (err = G_compare_projections(loc_proj_info, loc_proj_units, proj_info, proj_units)) != TRUE) { int i_value; strcpy(error_msg, _("Projection of dataset does not" " appear to match current location.\n\n")); /* TODO: output this info sorted by key: */ if (loc_wind.proj != cellhd.proj || err != -2) { if (loc_proj_info != NULL) { strcat(error_msg, _("GRASS LOCATION PROJ_INFO is:\n")); for (i_value = 0; i_value < loc_proj_info->nitems; i_value++) sprintf(error_msg + strlen(error_msg), "%s: %s\n", loc_proj_info->key[i_value], loc_proj_info->value[i_value]); strcat(error_msg, "\n"); } if (proj_info != NULL) { strcat(error_msg, _("Import dataset PROJ_INFO is:\n")); for (i_value = 0; i_value < proj_info->nitems; i_value++) sprintf(error_msg + strlen(error_msg), "%s: %s\n", proj_info->key[i_value], proj_info->value[i_value]); } else { strcat(error_msg, _("Import dataset PROJ_INFO is:\n")); if (cellhd.proj == PROJECTION_XY) sprintf(error_msg + strlen(error_msg), "Dataset proj = %d (unreferenced/unknown)\n", cellhd.proj); else if (cellhd.proj == PROJECTION_LL) sprintf(error_msg + strlen(error_msg), "Dataset proj = %d (lat/long)\n", cellhd.proj); else if (cellhd.proj == PROJECTION_UTM) sprintf(error_msg + strlen(error_msg), "Dataset proj = %d (UTM), zone = %d\n", cellhd.proj, cellhd.zone); else if (cellhd.proj == PROJECTION_SP) sprintf(error_msg + strlen(error_msg), "Dataset proj = %d (State Plane), zone = %d\n", cellhd.proj, cellhd.zone); else sprintf(error_msg + strlen(error_msg), "Dataset proj = %d (unknown), zone = %d\n", cellhd.proj, cellhd.zone); } } else { if (loc_proj_units != NULL) { strcat(error_msg, "GRASS LOCATION PROJ_UNITS is:\n"); for (i_value = 0; i_value < loc_proj_units->nitems; i_value++) sprintf(error_msg + strlen(error_msg), "%s: %s\n", loc_proj_units->key[i_value], loc_proj_units->value[i_value]); strcat(error_msg, "\n"); } if (proj_units != NULL) { strcat(error_msg, "Import dataset PROJ_UNITS is:\n"); for (i_value = 0; i_value < proj_units->nitems; i_value++) sprintf(error_msg + strlen(error_msg), "%s: %s\n", proj_units->key[i_value], proj_units->value[i_value]); } } sprintf(error_msg + strlen(error_msg), _("\nYou can use the -o flag to %s to override this projection check.\n"), G_program_name()); strcat(error_msg, _("Consider generating a new location with 'location' parameter" " from input data set.\n")); G_fatal_error(error_msg); } else { G_message(_("Projection of input dataset and current location " "appear to match")); } } db_init_string(&sql); db_init_string(&strval); /* open output vector */ /* strip any @mapset from vector output name */ G_find_vector(output, G_mapset()); Vect_open_new(&Map, output, flag.z->answer != 0); Out = ⤅ n_polygon_boundaries = 0; if (!flag.no_clean->answer) { /* check if we need a tmp vector */ /* estimate distance for boundary splitting --> */ for (layer = 0; layer < nlayers; layer++) { layer_id = layers[layer]; Ogr_layer = OGR_DS_GetLayer(Ogr_ds, layer_id); Ogr_featuredefn = OGR_L_GetLayerDefn(Ogr_layer); n_features = feature_count = 0; n_features = OGR_L_GetFeatureCount(Ogr_layer, 1); OGR_L_ResetReading(Ogr_layer); /* count polygons and isles */ G_message(_("Counting polygons for %d features (OGR layer <%s>)..."), n_features, layer_names[layer]); while ((Ogr_feature = OGR_L_GetNextFeature(Ogr_layer)) != NULL) { G_percent(feature_count++, n_features, 1); /* show something happens */ /* Geometry */ Ogr_geometry = OGR_F_GetGeometryRef(Ogr_feature); if (Ogr_geometry != NULL) { poly_count(Ogr_geometry, (type & GV_BOUNDARY)); } OGR_F_Destroy(Ogr_feature); } } G_debug(1, "n polygon boundaries: %d", n_polygon_boundaries); if (n_polygon_boundaries > 50) { split_distance = area_size / log(n_polygon_boundaries); /* divisor is the handle: increase divisor to decrease split_distance */ split_distance = split_distance / 5.; G_debug(1, "root of area size: %f", area_size); G_verbose_message(_("Boundary splitting distance in map units: %G"), split_distance); } /* <-- estimate distance for boundary splitting */ use_tmp_vect = n_polygon_boundaries > 0; if (use_tmp_vect) { /* open temporary vector, do the work in the temporary vector * at the end copy alive lines to output vector * in case of polygons this reduces the coor file size by a factor of 2 to 5 * only needed when cleaning polygons */ sprintf(tempvect, "%s_tmp", output); G_verbose_message(_("Using temporary vector <%s>"), tempvect); Vect_open_new(&Tmp, tempvect, flag.z->answer != 0); Out = &Tmp; } } Vect_hist_command(&Map); /* Points and lines are written immediately with categories. Boundaries of polygons are * written to the vector then cleaned and centroids are calculated for all areas in cleaan vector. * Then second pass through finds all centroids in each polygon feature and adds its category * to the centroid. The result is that one centroids may have 0, 1 ore more categories * of one ore more (more input layers) fields. */ with_z = 0; for (layer = 0; layer < nlayers; layer++) { layer_id = layers[layer]; Ogr_layer = OGR_DS_GetLayer(Ogr_ds, layer_id); Ogr_featuredefn = OGR_L_GetLayerDefn(Ogr_layer); /* Add DB link */ if (!flag.notab->answer) { char *cat_col_name = GV_KEY_COLUMN; if (nlayers == 1) { /* one layer only */ Fi = Vect_default_field_info(&Map, layer + 1, NULL, GV_1TABLE); } else { Fi = Vect_default_field_info(&Map, layer + 1, NULL, GV_MTABLE); } if (ncnames > 0) { cat_col_name = param.cnames->answers[0]; } Vect_map_add_dblink(&Map, layer + 1, layer_names[layer], Fi->table, cat_col_name, Fi->database, Fi->driver); ncols = OGR_FD_GetFieldCount(Ogr_featuredefn); G_debug(2, "%d columns", ncols); /* Create table */ sprintf(buf, "create table %s (%s integer", Fi->table, cat_col_name); db_set_string(&sql, buf); for (i = 0; i < ncols; i++) { Ogr_field = OGR_FD_GetFieldDefn(Ogr_featuredefn, i); Ogr_ftype = OGR_Fld_GetType(Ogr_field); G_debug(3, "Ogr_ftype: %i", Ogr_ftype); /* look up below */ if (i < ncnames - 1) { Ogr_fieldname = G_store(param.cnames->answers[i + 1]); } else { /* Change column names to [A-Za-z][A-Za-z0-9_]* */ Ogr_fieldname = G_store(OGR_Fld_GetNameRef(Ogr_field)); G_debug(3, "Ogr_fieldname: '%s'", Ogr_fieldname); G_str_to_sql(Ogr_fieldname); G_debug(3, "Ogr_fieldname: '%s'", Ogr_fieldname); } /* avoid that we get the 'cat' column twice */ if (strcmp(Ogr_fieldname, GV_KEY_COLUMN) == 0) { sprintf(namebuf, "%s_", Ogr_fieldname); Ogr_fieldname = G_store(namebuf); } /* captial column names are a pain in SQL */ if (flag.tolower->answer) G_str_to_lower(Ogr_fieldname); if (strcmp(OGR_Fld_GetNameRef(Ogr_field), Ogr_fieldname) != 0) { G_warning(_("Column name changed: '%s' -> '%s'"), OGR_Fld_GetNameRef(Ogr_field), Ogr_fieldname); } /** Simple 32bit integer OFTInteger = 0 **/ /** List of 32bit integers OFTIntegerList = 1 **/ /** Double Precision floating point OFTReal = 2 **/ /** List of doubles OFTRealList = 3 **/ /** String of ASCII chars OFTString = 4 **/ /** Array of strings OFTStringList = 5 **/ /** Double byte string (unsupported) OFTWideString = 6 **/ /** List of wide strings (unsupported) OFTWideStringList = 7 **/ /** Raw Binary data (unsupported) OFTBinary = 8 **/ /** OFTDate = 9 **/ /** OFTTime = 10 **/ /** OFTDateTime = 11 **/ if (Ogr_ftype == OFTInteger) { sprintf(buf, ", %s integer", Ogr_fieldname); } else if (Ogr_ftype == OFTIntegerList) { /* hack: treat as string */ sprintf(buf, ", %s varchar ( %d )", Ogr_fieldname, OFTIntegerListlength); G_warning(_("Writing column <%s> with fixed length %d chars (may be truncated)"), Ogr_fieldname, OFTIntegerListlength); } else if (Ogr_ftype == OFTReal) { sprintf(buf, ", %s double precision", Ogr_fieldname); #if GDAL_VERSION_NUM >= 1320 } else if (Ogr_ftype == OFTDate) { sprintf(buf, ", %s date", Ogr_fieldname); } else if (Ogr_ftype == OFTTime) { sprintf(buf, ", %s time", Ogr_fieldname); } else if (Ogr_ftype == OFTDateTime) { sprintf(buf, ", %s datetime", Ogr_fieldname); #endif } else if (Ogr_ftype == OFTString) { int fwidth; fwidth = OGR_Fld_GetWidth(Ogr_field); /* TODO: read all records first and find the longest string length */ if (fwidth == 0) { G_warning(_("Width for column %s set to 255 (was not specified by OGR), " "some strings may be truncated!"), Ogr_fieldname); fwidth = 255; } sprintf(buf, ", %s varchar ( %d )", Ogr_fieldname, fwidth); } else if (Ogr_ftype == OFTStringList) { /* hack: treat as string */ sprintf(buf, ", %s varchar ( %d )", Ogr_fieldname, OFTIntegerListlength); G_warning(_("Writing column %s with fixed length %d chars (may be truncated)"), Ogr_fieldname, OFTIntegerListlength); } else { G_warning(_("Column type not supported (%s)"), Ogr_fieldname); buf[0] = 0; } db_append_string(&sql, buf); G_free(Ogr_fieldname); } db_append_string(&sql, ")"); G_debug(3, db_get_string(&sql)); driver = db_start_driver_open_database(Fi->driver, Vect_subst_var(Fi->database, &Map)); if (driver == NULL) { G_fatal_error(_("Unable open database <%s> by driver <%s>"), Vect_subst_var(Fi->database, &Map), Fi->driver); } if (db_execute_immediate(driver, &sql) != DB_OK) { db_close_database(driver); db_shutdown_driver(driver); G_fatal_error(_("Unable to create table: '%s'"), db_get_string(&sql)); } if (db_create_index2(driver, Fi->table, cat_col_name) != DB_OK) G_warning(_("Unable to create index for table <%s>, key <%s>"), Fi->table, cat_col_name); if (db_grant_on_table (driver, Fi->table, DB_PRIV_SELECT, DB_GROUP | DB_PUBLIC) != DB_OK) G_fatal_error(_("Unable to grant privileges on table <%s>"), Fi->table); db_begin_transaction(driver); } /* Import feature */ cat = 1; nogeom = 0; OGR_L_ResetReading(Ogr_layer); n_features = feature_count = 0; n_features = OGR_L_GetFeatureCount(Ogr_layer, 1); G_important_message(_("Importing %d features (OGR layer <%s>)..."), n_features, layer_names[layer]); while ((Ogr_feature = OGR_L_GetNextFeature(Ogr_layer)) != NULL) { G_percent(feature_count++, n_features, 1); /* show something happens */ /* Geometry */ Ogr_geometry = OGR_F_GetGeometryRef(Ogr_feature); if (Ogr_geometry == NULL) { nogeom++; } else { dim = OGR_G_GetCoordinateDimension(Ogr_geometry); if (dim > 2) with_z = 1; geom(Ogr_geometry, Out, layer + 1, cat, min_area, type, flag.no_clean->answer); } /* Attributes */ if (!flag.notab->answer) { sprintf(buf, "insert into %s values ( %d", Fi->table, cat); db_set_string(&sql, buf); for (i = 0; i < ncols; i++) { Ogr_field = OGR_FD_GetFieldDefn(Ogr_featuredefn, i); Ogr_ftype = OGR_Fld_GetType(Ogr_field); if (OGR_F_IsFieldSet(Ogr_feature, i)) { if (Ogr_ftype == OFTInteger || Ogr_ftype == OFTReal) { sprintf(buf, ", %s", OGR_F_GetFieldAsString(Ogr_feature, i)); #if GDAL_VERSION_NUM >= 1320 /* should we use OGR_F_GetFieldAsDateTime() here ? */ } else if (Ogr_ftype == OFTDate || Ogr_ftype == OFTTime || Ogr_ftype == OFTDateTime) { char *newbuf; db_set_string(&strval, (char *) OGR_F_GetFieldAsString(Ogr_feature, i)); db_double_quote_string(&strval); sprintf(buf, ", '%s'", db_get_string(&strval)); newbuf = G_str_replace(buf, "/", "-"); /* fix 2001/10/21 to 2001-10-21 */ sprintf(buf, "%s", newbuf); #endif } else if (Ogr_ftype == OFTString || Ogr_ftype == OFTIntegerList) { db_set_string(&strval, (char *) OGR_F_GetFieldAsString(Ogr_feature, i)); db_double_quote_string(&strval); sprintf(buf, ", '%s'", db_get_string(&strval)); } } else { /* G_warning (_("Column value not set" )); */ if (Ogr_ftype == OFTInteger || Ogr_ftype == OFTReal) { sprintf(buf, ", NULL"); #if GDAL_VERSION_NUM >= 1320 } else if (Ogr_ftype == OFTString || Ogr_ftype == OFTIntegerList || Ogr_ftype == OFTDate) { #else } else if (Ogr_ftype == OFTString || Ogr_ftype == OFTIntegerList) { #endif sprintf(buf, ", ''"); } } db_append_string(&sql, buf); } db_append_string(&sql, " )"); G_debug(3, db_get_string(&sql)); if (db_execute_immediate(driver, &sql) != DB_OK) { db_close_database(driver); db_shutdown_driver(driver); G_fatal_error(_("Cannot insert new row: %s"), db_get_string(&sql)); } } OGR_F_Destroy(Ogr_feature); cat++; } G_percent(1, 1, 1); /* finish it */ if (!flag.notab->answer) { db_commit_transaction(driver); db_close_database_shutdown_driver(driver); } if (nogeom > 0) G_warning(_("%d %s without geometry"), nogeom, nogeom == 1 ? "feature" : "features"); } separator = "-----------------------------------------------------"; G_message("%s", separator); if (use_tmp_vect) { /* TODO: is it necessary to build here? probably not, consumes time */ /* GV_BUILD_BASE is sufficient to toggle boundary cleaning */ Vect_build_partial(&Tmp, GV_BUILD_BASE); } if (use_tmp_vect && !flag.no_clean->answer && Vect_get_num_primitives(Out, GV_BOUNDARY) > 0) { int ret, centr, ncentr, otype, n_overlaps, n_nocat; CENTR *Centr; struct spatial_index si; double x, y, total_area, overlap_area, nocat_area; struct bound_box box; struct line_pnts *Points; int nmodif; Points = Vect_new_line_struct(); G_message("%s", separator); G_warning(_("Cleaning polygons, result is not guaranteed!")); if (snap >= 0) { G_message("%s", separator); G_message(_("Snapping boundaries (threshold = %.3e)..."), snap); Vect_snap_lines(&Tmp, GV_BOUNDARY, snap, NULL); } /* It is not to clean to snap centroids, but I have seen data with 2 duplicate polygons * (as far as decimal places were printed) and centroids were not identical */ /* Disabled, because overlapping polygons result in many duplicate centroids anyway */ /* fprintf ( stderr, separator ); fprintf ( stderr, "Snap centroids (threshold 0.000001):\n" ); Vect_snap_lines ( &Map, GV_CENTROID, 0.000001, NULL, stderr ); */ G_message("%s", separator); G_message(_("Breaking polygons...")); Vect_break_polygons(&Tmp, GV_BOUNDARY, NULL); /* It is important to remove also duplicate centroids in case of duplicate input polygons */ G_message("%s", separator); G_message(_("Removing duplicates...")); Vect_remove_duplicates(&Tmp, GV_BOUNDARY | GV_CENTROID, NULL); /* in non-pathological cases, the bulk of the cleaning is now done */ /* Vect_clean_small_angles_at_nodes() can change the geometry so that new intersections * are created. We must call Vect_break_lines(), Vect_remove_duplicates() * and Vect_clean_small_angles_at_nodes() until no more small angles are found */ do { G_message("%s", separator); G_message(_("Breaking boundaries...")); Vect_break_lines(&Tmp, GV_BOUNDARY, NULL); G_message("%s", separator); G_message(_("Removing duplicates...")); Vect_remove_duplicates(&Tmp, GV_BOUNDARY, NULL); G_message("%s", separator); G_message(_("Cleaning boundaries at nodes...")); nmodif = Vect_clean_small_angles_at_nodes(&Tmp, GV_BOUNDARY, NULL); } while (nmodif > 0); /* merge boundaries */ G_message("%s", separator); G_message(_("Merging boundaries...")); Vect_merge_lines(&Tmp, GV_BOUNDARY, NULL, NULL); G_message("%s", separator); if (type & GV_BOUNDARY) { /* that means lines were converted to boundaries */ G_message(_("Changing boundary dangles to lines...")); Vect_chtype_dangles(&Tmp, -1.0, NULL); } else { G_message(_("Removing dangles...")); Vect_remove_dangles(&Tmp, GV_BOUNDARY, -1.0, NULL); } G_message("%s", separator); if (type & GV_BOUNDARY) { G_message(_("Changing boundary bridges to lines...")); Vect_chtype_bridges(&Tmp, NULL); } else { G_message(_("Removing bridges...")); Vect_remove_bridges(&Tmp, NULL); } /* Boundaries are hopefully clean, build areas */ G_message("%s", separator); Vect_build_partial(&Tmp, GV_BUILD_ATTACH_ISLES); /* Calculate new centroids for all areas, centroids have the same id as area */ ncentr = Vect_get_num_areas(&Tmp); G_debug(3, "%d centroids/areas", ncentr); Centr = (CENTR *) G_calloc(ncentr + 1, sizeof(CENTR)); Vect_spatial_index_init(&si, 0); for (centr = 1; centr <= ncentr; centr++) { Centr[centr].valid = 0; Centr[centr].cats = Vect_new_cats_struct(); ret = Vect_get_point_in_area(&Tmp, centr, &x, &y); if (ret < 0) { G_warning(_("Unable to calculate area centroid")); continue; } Centr[centr].x = x; Centr[centr].y = y; Centr[centr].valid = 1; box.N = box.S = y; box.E = box.W = x; box.T = box.B = 0; Vect_spatial_index_add_item(&si, centr, &box); } /* Go through all layers and find centroids for each polygon */ for (layer = 0; layer < nlayers; layer++) { G_message("%s", separator); G_message(_("Finding centroids for OGR layer <%s>..."), layer_names[layer]); layer_id = layers[layer]; Ogr_layer = OGR_DS_GetLayer(Ogr_ds, layer_id); n_features = OGR_L_GetFeatureCount(Ogr_layer, 1); OGR_L_ResetReading(Ogr_layer); cat = 0; /* field = layer + 1 */ G_percent(cat, n_features, 2); while ((Ogr_feature = OGR_L_GetNextFeature(Ogr_layer)) != NULL) { cat++; G_percent(cat, n_features, 2); /* Geometry */ Ogr_geometry = OGR_F_GetGeometryRef(Ogr_feature); if (Ogr_geometry != NULL) { centroid(Ogr_geometry, Centr, &si, layer + 1, cat, min_area, type); } OGR_F_Destroy(Ogr_feature); } } /* Write centroids */ G_message("%s", separator); G_message(_("Writing centroids...")); n_overlaps = n_nocat = 0; total_area = overlap_area = nocat_area = 0.0; for (centr = 1; centr <= ncentr; centr++) { double area; G_percent(centr, ncentr, 2); area = Vect_get_area_area(&Tmp, centr); total_area += area; if (!(Centr[centr].valid)) { continue; } if (Centr[centr].cats->n_cats == 0) { nocat_area += area; n_nocat++; continue; } if (Centr[centr].cats->n_cats > 1) { Vect_cat_set(Centr[centr].cats, nlayers + 1, Centr[centr].cats->n_cats); overlap_area += area; n_overlaps++; } Vect_reset_line(Points); Vect_append_point(Points, Centr[centr].x, Centr[centr].y, 0.0); if (type & GV_POINT) otype = GV_POINT; else otype = GV_CENTROID; Vect_write_line(&Tmp, otype, Points, Centr[centr].cats); } if (Centr) G_free(Centr); Vect_spatial_index_destroy(&si); if (n_overlaps > 0) { G_warning(_("%d areas represent more (overlapping) features, because polygons overlap " "in input layer(s). Such areas are linked to more than 1 row in attribute table. " "The number of features for those areas is stored as category in layer %d"), n_overlaps, nlayers + 1); } G_message("%s", separator); Vect_hist_write(&Map, separator); Vect_hist_write(&Map, "\n"); sprintf(buf, _("%d input polygons\n"), n_polygons); G_message(_("%d input polygons"), n_polygons); Vect_hist_write(&Map, buf); sprintf(buf, _("Total area: %G (%d areas)\n"), total_area, ncentr); G_message(_("Total area: %G (%d areas)"), total_area, ncentr); Vect_hist_write(&Map, buf); sprintf(buf, _("Overlapping area: %G (%d areas)\n"), overlap_area, n_overlaps); G_message(_("Overlapping area: %G (%d areas)"), overlap_area, n_overlaps); Vect_hist_write(&Map, buf); sprintf(buf, _("Area without category: %G (%d areas)\n"), nocat_area, n_nocat); G_message(_("Area without category: %G (%d areas)"), nocat_area, n_nocat); Vect_hist_write(&Map, buf); G_message("%s", separator); } /* needed? * OGR_DS_Destroy( Ogr_ds ); */ if (use_tmp_vect) { /* Copy temporary vector to output vector */ Vect_copy_map_lines(&Tmp, &Map); /* release memory occupied by topo, we may need that memory for main output */ Vect_set_release_support(&Tmp); Vect_close(&Tmp); Vect_delete(tempvect); } Vect_build(&Map); Vect_close(&Map); /* -------------------------------------------------------------------- */ /* Extend current window based on dataset. */ /* -------------------------------------------------------------------- */ if (flag.extend->answer) { G_get_default_window(&loc_wind); loc_wind.north = MAX(loc_wind.north, cellhd.north); loc_wind.south = MIN(loc_wind.south, cellhd.south); loc_wind.west = MIN(loc_wind.west, cellhd.west); loc_wind.east = MAX(loc_wind.east, cellhd.east); loc_wind.rows = (int)ceil((loc_wind.north - loc_wind.south) / loc_wind.ns_res); loc_wind.south = loc_wind.north - loc_wind.rows * loc_wind.ns_res; loc_wind.cols = (int)ceil((loc_wind.east - loc_wind.west) / loc_wind.ew_res); loc_wind.east = loc_wind.west + loc_wind.cols * loc_wind.ew_res; G__put_window(&loc_wind, "../PERMANENT", "DEFAULT_WIND"); } if (with_z && !flag.z->answer) G_warning(_("Input data contains 3D features. Created vector is 2D only, " "use -z flag to import 3D vector.")); exit(EXIT_SUCCESS); }
int main(int argc, char *argv[]) { struct GModule *module; int Out_proj; int out_stat; int old_zone, old_proj; int i; int stat; char cmnd2[500]; char proj_out[20], proj_name[50], set_name[20]; char path[1024], buffa[1024], buffb[1024], answer[200], answer1[200]; char answer2[200], buff[1024]; char tmp_buff[20], *buf; struct Key_Value *old_proj_keys, *out_proj_keys, *in_unit_keys; double aa, e2; double f; FILE *FPROJ; int exist = 0; char spheroid[100]; int j, k, sph_check; struct Cell_head cellhd; char datum[100], dat_ellps[100], dat_params[100]; struct proj_parm *proj_parms; G_gisinit(argv[0]); module = G_define_module(); G_add_keyword(_("general")); G_add_keyword(_("projection")); module->description = _("Interactively reset the location's projection settings."); if (G_parser(argc, argv)) exit(EXIT_FAILURE); if (strcmp(G_mapset(), "PERMANENT") != 0) G_fatal_error(_("You must be in the PERMANENT mapset to run g.setproj")); /*** * no longer necessary, table is a static struct * init_unit_table(); ***/ sprintf(set_name, "PERMANENT"); G_file_name(path, "", PROJECTION_FILE, set_name); /* get the output projection parameters, if existing */ /* Check for ownership here */ stat = G__mapset_permissions(set_name); if (stat == 0) { G_fatal_error(_("PERMANENT: permission denied")); } G_get_default_window(&cellhd); if (-1 == G_set_window(&cellhd)) G_fatal_error(_("Current region cannot be set")); if (G_get_set_window(&cellhd) == -1) G_fatal_error(_("Retrieving and setting region failed")); Out_proj = cellhd.proj; old_zone = cellhd.zone; old_proj = cellhd.proj; if (access(path, 0) == 0) { exist = 1; FPROJ = fopen(path, "r"); old_proj_keys = G_fread_key_value(FPROJ); fclose(FPROJ); buf = G_find_key_value("name", old_proj_keys); fprintf(stderr, "\nWARNING: A projection file already exists for this location\n(Filename '%s')\n", path); fprintf(stderr, "\nThis file contains all the parameters for the location's projection:\n %s\n", buf); fprintf(stderr, "\n Overriding this information implies that the old projection parameters\n" " were incorrect. If you change the parameters, all existing data will\n" " be interpreted differently by the projection software.\n%c%c%c", 7, 7, 7); fprintf(stderr, " GRASS will not re-project your data automatically.\n\n"); if (!G_yes (_("Would you still like to change some of the parameters?"), 0)) { G_message(_("The projection information will not be updated")); leave(SP_NOCHANGE); } } out_proj_keys = G_create_key_value(); if (exist) { buf = G_find_key_value("zone", old_proj_keys); if (buf != NULL) sscanf(buf, "%d", &zone); if (zone != old_zone) { G_warning(_("Zone in default geographic region definition: %d\n" " is different from zone in PROJ_INFO file: %d"), old_zone, zone); old_zone = zone; } } switch (Out_proj) { case 0: /* No projection/units */ if (!exist) { /* leap frog over code, and just make sure we remove the file */ G_warning(_("XY-location cannot be projected")); goto write_file; break; } case PROJECTION_UTM: if (!exist) { sprintf(proj_name, "%s", G__projection_name(PROJECTION_UTM)); sprintf(proj_out, "utm"); break; } case PROJECTION_SP: if (!exist) { sprintf(proj_name, "%s", G__projection_name(PROJECTION_SP)); sprintf(proj_out, "stp"); break; } case PROJECTION_LL: if (!exist) { sprintf(proj_name, "%s", G__projection_name(PROJECTION_LL)); sprintf(proj_out, "ll"); break; } case PROJECTION_OTHER: if (G_ask_proj_name(proj_out, proj_name) < 0) leave(SP_NOCHANGE); if (G_strcasecmp(proj_out, "LL") == 0) Out_proj = PROJECTION_LL; else if (G_strcasecmp(proj_out, "UTM") == 0) Out_proj = PROJECTION_UTM; else if (G_strcasecmp(proj_out, "STP") == 0) Out_proj = PROJECTION_SP; break; default: G_fatal_error(_("Unknown projection")); } cellhd.proj = Out_proj; proj_parms = get_proj_parms(proj_out); if (!proj_parms) G_fatal_error(_("Projection %s is not specified in the file 'proj-parms.table'"), proj_out); G_set_key_value("name", proj_name, out_proj_keys); sph_check = 0; if (G_yes (_("Do you wish to specify a geodetic datum for this location?"), 1)) { char lbuf[100], lbufa[100]; if (exist && (G_get_datumparams_from_projinfo(old_proj_keys, lbuf, lbufa) == 2)) { G_strip(lbuf); if ((i = G_get_datum_by_name(lbuf)) > 0) { G_message(_("The current datum is %s (%s)"), G_datum_name(i), G_datum_description(i)); if (G_yes (_("Do you wish to change the datum (or datum transformation parameters)?"), 0)) sph_check = ask_datum(datum, dat_ellps, dat_params); else { sprintf(datum, lbuf); sprintf(dat_params, lbufa); sprintf(dat_ellps, G_datum_ellipsoid(i)); sph_check = 1; G_message(_("The datum information has not been changed")); } } else sph_check = ask_datum(datum, dat_ellps, dat_params); } else sph_check = ask_datum(datum, dat_ellps, dat_params); } if (sph_check > 0) { char *paramkey, *paramvalue; /* write out key/value pairs to out_proj_keys */ if (G_strcasecmp(datum, "custom") != 0) G_set_key_value("datum", datum, out_proj_keys); /* G_set_key_value("datumparams", dat_params, out_proj_keys); */ paramkey = strtok(dat_params, "="); paramvalue = dat_params + strlen(paramkey) + 1; G_set_key_value(paramkey, paramvalue, out_proj_keys); sprintf(spheroid, "%s", dat_ellps); } else { /***************** GET spheroid **************************/ if (Out_proj != PROJECTION_SP) { /* some projections have * fixed spheroids */ if (G_strcasecmp(proj_out, "ALSK") == 0 || G_strcasecmp(proj_out, "GS48") == 0 || G_strcasecmp(proj_out, "GS50") == 0) { sprintf(spheroid, "%s", "clark66"); G_set_key_value("ellps", spheroid, out_proj_keys); sph_check = 1; } else if (G_strcasecmp(proj_out, "LABRD") == 0 || G_strcasecmp(proj_out, "NZMG") == 0) { sprintf(spheroid, "%s", "international"); G_set_key_value("ellps", spheroid, out_proj_keys); sph_check = 1; } else if (G_strcasecmp(proj_out, "SOMERC") == 0) { sprintf(spheroid, "%s", "bessel"); G_set_key_value("ellps", spheroid, out_proj_keys); sph_check = 1; } else if (G_strcasecmp(proj_out, "OB_TRAN") == 0) { /* Hard coded to use "Equidistant Cylincrical" * until g.setproj has been changed to run * recurively, to allow input of options for * a second projection, MHu991010 */ G_set_key_value("o_proj", "eqc", out_proj_keys); sph_check = 2; } else { if (exist && (buf = G_find_key_value("ellps", old_proj_keys)) != NULL) { strcpy(spheroid, buf); G_strip(spheroid); if (G_get_spheroid_by_name(spheroid, &aa, &e2, &f)) { /* if legal ellips. exist, ask wether or not to change it */ G_message(_("The current ellipsoid is %s"), spheroid); if (G_yes (_("Do you want to change ellipsoid parameter?"), 0)) sph_check = G_ask_ellipse_name(spheroid); else { G_message(_("The ellipse information has not been changed")); sph_check = 1; } } /* the val is legal */ else sph_check = G_ask_ellipse_name(spheroid); } else sph_check = G_ask_ellipse_name(spheroid); } } if (sph_check > 0) { if (sph_check == 2) { /* ask radius */ if (exist) { buf = G_find_key_value("a", old_proj_keys); if ((buf != NULL) && (sscanf(buf, "%lf", &radius) == 1)) { G_message(_("The radius is currently %f"), radius); if (G_yes(_("Do you want to change the radius?"), 0)) radius = prompt_num_double(_("Enter radius for the sphere in meters"), RADIUS_DEF, 1); } } else radius = prompt_num_double(_("Enter radius for the sphere in meters"), RADIUS_DEF, 1); } /* end ask radius */ } } /*** END get spheroid ***/ /* create the PROJ_INFO & PROJ_UNITS files, if required */ if (G_strcasecmp(proj_out, "LL") == 0) ; else if (G_strcasecmp(proj_out, "STP") == 0) get_stp_proj(buffb); else if (sph_check != 2) { G_strip(spheroid); if (G_get_spheroid_by_name(spheroid, &aa, &e2, &f) == 0) G_fatal_error(_("Invalid input ellipsoid")); } write_file: /* ** NOTE the program will (hopefully) never exit abnormally ** after this point. Thus we know the file will be completely ** written out once it is opened for write */ if (exist) { sprintf(buff, "%s~", path); G_rename_file(path, buff); } if (Out_proj == 0) goto write_units; /* ** Include MISC parameters for PROJ_INFO */ if (G_strcasecmp(proj_out, "STP") == 0) { for (i = 0; i < strlen(buffb); i++) if (buffb[i] == ' ') buffb[i] = '\t'; sprintf(cmnd2, "%s\t\n", buffb); for (i = 0; i < strlen(cmnd2); i++) { j = k = 0; if (cmnd2[i] == '+') { while (cmnd2[++i] != '=') buffa[j++] = cmnd2[i]; buffa[j] = 0; while (cmnd2[++i] != '\t' && cmnd2[i] != '\n' && cmnd2[i] != 0) buffb[k++] = cmnd2[i]; buffb[k] = 0; G_set_key_value(buffa, buffb, out_proj_keys); } } } else if (G_strcasecmp(proj_out, "LL") == 0) { G_set_key_value("proj", "ll", out_proj_keys); G_set_key_value("ellps", spheroid, out_proj_keys); } else { if (sph_check != 2) { G_set_key_value("proj", proj_out, out_proj_keys); G_set_key_value("ellps", spheroid, out_proj_keys); sprintf(tmp_buff, "%.10f", aa); G_set_key_value("a", tmp_buff, out_proj_keys); sprintf(tmp_buff, "%.10f", e2); G_set_key_value("es", tmp_buff, out_proj_keys); sprintf(tmp_buff, "%.10f", f); G_set_key_value("f", tmp_buff, out_proj_keys); } else { G_set_key_value("proj", proj_out, out_proj_keys); /* G_set_key_value ("ellps", "sphere", out_proj_keys); */ sprintf(tmp_buff, "%.10f", radius); G_set_key_value("a", tmp_buff, out_proj_keys); G_set_key_value("es", "0.0", out_proj_keys); G_set_key_value("f", "0.0", out_proj_keys); } for (i = 0;; i++) { struct proj_parm *parm = &proj_parms[i]; struct proj_desc *desc; if (!parm->name) break; desc = get_proj_desc(parm->name); if (!desc) break; if (parm->ask) { if (G_strcasecmp(desc->type, "bool") == 0) { if (G_yes((char *)desc->desc, 0)) { G_set_key_value(desc->key, "defined", out_proj_keys); if (G_strcasecmp(parm->name, "SOUTH") == 0) cellhd.zone = -abs(cellhd.zone); } } else if (G_strcasecmp(desc->type, "lat") == 0) { double val; while (!get_LL_stuff(parm, desc, 1, &val)) ; sprintf(tmp_buff, "%.10f", val); G_set_key_value(desc->key, tmp_buff, out_proj_keys); } else if (G_strcasecmp(desc->type, "lon") == 0) { double val; while (!get_LL_stuff(parm, desc, 0, &val)) ; sprintf(tmp_buff, "%.10f", val); G_set_key_value(desc->key, tmp_buff, out_proj_keys); } else if (G_strcasecmp(desc->type, "float") == 0) { double val; while (!get_double(parm, desc, &val)) ; sprintf(tmp_buff, "%.10f", val); G_set_key_value(desc->key, tmp_buff, out_proj_keys); } else if (G_strcasecmp(desc->type, "int") == 0) { int val; while (!get_int(parm, desc, &val)) ; sprintf(tmp_buff, "%d", val); G_set_key_value(desc->key, tmp_buff, out_proj_keys); } else if (G_strcasecmp(desc->type, "zone") == 0) { if ((Out_proj == PROJECTION_UTM) && (old_zone != 0)) { G_message(_("The UTM zone is now set to %d"), old_zone); if (!G_yes (_("Do you want to change the UTM zone?"), 0)) { G_message(_("UTM zone information has not been updated")); zone = old_zone; break; } else { G_message(_("But if you change zone, all the existing " "data will be interpreted by projection software. " "GRASS will not automatically re-project or even " "change the headers for existing maps.")); if (!G_yes (_("Would you still like to change the UTM zone?"), 0)) { zone = old_zone; break; } } } /* UTM */ while (!get_zone()) ; sprintf(tmp_buff, "%d", zone); G_set_key_value("zone", tmp_buff, out_proj_keys); cellhd.zone = zone; } } else if (parm->def_exists) { /* don't ask, use the default */ if (G_strcasecmp(desc->type, "float") == 0 || G_strcasecmp(desc->type, "lat") == 0 || G_strcasecmp(desc->type, "lon") == 0) { sprintf(tmp_buff, "%.10f", parm->deflt); G_set_key_value(desc->key, tmp_buff, out_proj_keys); } else if (G_strcasecmp(desc->type, "int") == 0) { sprintf(tmp_buff, "%d", (int)parm->deflt); G_set_key_value(desc->key, tmp_buff, out_proj_keys); } } } /* for OPTIONS */ } /* create the PROJ_INFO & PROJ_UNITS files, if required */ G_write_key_value_file(path, out_proj_keys, &out_stat); if (out_stat != 0) { G_fatal_error(_("Error writing PROJ_INFO file <%s>"), path); } G_free_key_value(out_proj_keys); if (exist) G_free_key_value(old_proj_keys); write_units: G_file_name(path, "", UNIT_FILE, set_name); /* if we got this far, the user ** already affirmed to write over old info ** so if units file is here, remove it. */ if (access(path, 0) == 0) { sprintf(buff, "%s~", path); G_rename_file(path, buff); } if (Out_proj == 0) leave(0); { in_unit_keys = G_create_key_value(); switch (Out_proj) { case PROJECTION_UTM: G_set_key_value("unit", "meter", in_unit_keys); G_set_key_value("units", "meters", in_unit_keys); G_set_key_value("meters", "1.0", in_unit_keys); break; case PROJECTION_SP: for (;;) { do { fprintf(stderr, "\nSpecify the correct units to use:\n"); fprintf(stderr, "Enter the corresponding number\n"); fprintf(stderr, "1.\tUS Survey Foot (Default for State Plane 1927)\n"); fprintf(stderr, "2.\tInternational Foot\n"); fprintf(stderr, "3.\tMeter\n"); fprintf(stderr, ">"); } while (!G_gets(answer)); G_strip(answer); if (strcmp(answer, "1") == 0) { G_set_key_value("unit", "USfoot", in_unit_keys); G_set_key_value("units", "USfeet", in_unit_keys); G_set_key_value("meters", "0.30480060960121920243", in_unit_keys); break; } else if (strcmp(answer, "2") == 0) { G_set_key_value("unit", "foot", in_unit_keys); G_set_key_value("units", "feet", in_unit_keys); G_set_key_value("meters", "0.3048", in_unit_keys); break; } else if (strcmp(answer, "3") == 0) { G_set_key_value("unit", "meter", in_unit_keys); G_set_key_value("units", "meters", in_unit_keys); G_set_key_value("meters", "1.0", in_unit_keys); break; } else fprintf(stderr, "\nInvalid Entry (number 1 - 3)\n"); } break; case PROJECTION_LL: G_set_key_value("unit", "degree", in_unit_keys); G_set_key_value("units", "degrees", in_unit_keys); G_set_key_value("meters", "1.0", in_unit_keys); break; default: if (G_strcasecmp(proj_out, "LL") != 0) { fprintf(stderr, _("Enter plural form of units [meters]: ")); G_gets(answer); if (strlen(answer) == 0) { G_set_key_value("unit", "meter", in_unit_keys); G_set_key_value("units", "meters", in_unit_keys); G_set_key_value("meters", "1.0", in_unit_keys); } else { const struct proj_unit *unit; G_strip(answer); unit = get_proj_unit(answer); if (unit) { #ifdef FOO if (G_strcasecmp(proj_out, "STP") == 0 && !strcmp(answer, "feet")) { fprintf(stderr, "%cPROJECTION 99 State Plane cannot be in FEET.\n", 7); remove(path); /* remove file */ leave(SP_FATAL); } #endif G_set_key_value("unit", unit->unit, in_unit_keys); G_set_key_value("units", unit->units, in_unit_keys); sprintf(buffb, "%.10f", unit->fact); G_set_key_value("meters", buffb, in_unit_keys); } else { double unit_fact; while (1) { fprintf(stderr, _("Enter singular for unit: ")); G_gets(answer1); G_strip(answer1); if (strlen(answer1) > 0) break; } while (1) { fprintf(stderr, _("Enter conversion factor from %s to meters: "), answer); G_gets(answer2); G_strip(answer2); if (! (strlen(answer2) == 0 || (1 != sscanf(answer2, "%lf", &unit_fact)))) break; } G_set_key_value("unit", answer1, in_unit_keys); G_set_key_value("units", answer, in_unit_keys); sprintf(buffb, "%.10f", unit_fact); G_set_key_value("meters", buffb, in_unit_keys); } } } else { G_set_key_value("unit", "degree", in_unit_keys); G_set_key_value("units", "degrees", in_unit_keys); G_set_key_value("meters", "1.0", in_unit_keys); } } /* switch */ G_write_key_value_file(path, in_unit_keys, &out_stat); if (out_stat != 0) G_fatal_error(_("Error writing into UNITS output file <%s>"), path); G_free_key_value(in_unit_keys); } /* if */ if (G__put_window(&cellhd, "", "DEFAULT_WIND") < 0) G_fatal_error(_("Unable to write to DEFAULT_WIND region file")); fprintf(stderr, _("\nProjection information has been recorded for this location\n\n")); if ((old_zone != zone) | (old_proj != cellhd.proj)) { G_message(_("The geographic region information in WIND is now obsolete")); G_message(_("Run g.region -d to update it")); } leave(0); }
int make_window_box(struct Cell_head *window, double magnify, int full, int hand) { int screen_x, screen_y; double px, py, ux1, uy1, ux2, uy2; double ns, ew; int button; int cur_screen_x, cur_screen_y; int mode; /* 1, 2 */ int resetwin; struct Cell_head defwin; int printmenu = 1; G_get_default_window(&defwin); mode = 1; while (1) { resetwin = 0; if (!hand) { if (printmenu) { fprintf(stderr, "\n\nButtons:\n"); fprintf(stderr, "Left: 1. corner\n"); fprintf(stderr, "Middle: Unzoom\n"); if (full) fprintf(stderr, "Right: Main menu\n\n"); else fprintf(stderr, "Right: Quit\n\n"); printmenu = 0; } } else { if (mode == 1) fprintf(stderr, "\r1. corner"); else fprintf(stderr, "\r2. corner"); } if (mode == 1) { if (!hand) { R_get_location_with_pointer(&screen_x, &screen_y, &button); } else { R_get_location_with_box(0, 0, &screen_x, &screen_y, &button); } cur_screen_x = screen_x; cur_screen_y = screen_y; } else { R_get_location_with_box(cur_screen_x, cur_screen_y, &screen_x, &screen_y, &button); } /* For print only */ px = D_d_to_u_col((double)screen_x); py = D_d_to_u_row((double)screen_y); if (!hand) print_coor(window, py, px); if (button == 1) { if (!hand) { if (mode == 1) { fprintf(stderr, "\n\nButtons:\n"); fprintf(stderr, "Left: 1. corner (reset)\n"); fprintf(stderr, "Middle: 2. corner\n"); if (full) fprintf(stderr, "Right: Main menu\n\n"); else fprintf(stderr, "Right: Quit\n\n"); mode = 2; } if (mode == 2) { cur_screen_x = screen_x; cur_screen_y = screen_y; } } else { /* hand */ if (mode == 1) { mode = 2; } else { ux1 = D_d_to_u_col((double)cur_screen_x); uy1 = D_d_to_u_row((double)cur_screen_y); ux2 = D_d_to_u_col((double)screen_x); uy2 = D_d_to_u_row((double)screen_y); resetwin = 1; mode = 1; } } } else if (button == 2) { if (mode == 1) { /* unzoom */ ux2 = D_d_to_u_col((double)screen_x); uy2 = D_d_to_u_row((double)screen_y); ew = window->east - window->west; ns = window->north - window->south; if (ns <= window->ns_res) ns = 2 * window->ns_res; else ew /= magnify; if (ew <= window->ew_res) ew = 2 * window->ew_res; else ns /= magnify; ux1 = window->east + ew / 2; ux2 = window->west - ew / 2; uy1 = window->north + ns / 2; uy2 = window->south - ns / 2; } else { ux1 = D_d_to_u_col((double)cur_screen_x); uy1 = D_d_to_u_row((double)cur_screen_y); ux2 = D_d_to_u_col((double)screen_x); uy2 = D_d_to_u_row((double)screen_y); printmenu = 1; mode = 1; } fprintf(stderr, "\n"); resetwin = 1; } else { fprintf(stderr, "\n"); return 1; } if (resetwin) { set_win(window, ux1, uy1, ux2, uy2, hand); } } fprintf(stderr, "\n"); return 1; /* not reached */ }
int main(int argc, char *argv[]) { int i; int print_flag = 0; int flat_flag; int set_flag; double x; int ival; int row_flag = 0, col_flag = 0; struct Cell_head window, temp_window; const char *value; const char *name; const char *mapset; char **rast_ptr, **vect_ptr; struct GModule *module; struct { struct Flag *update, *print, *gprint, *flprint, *lprint, *eprint, *nangle, *center, *res_set, *dist_res, *dflt, *z, *savedefault, *bbox, *gmt_style, *wms_style; } flag; struct { struct Option *north, *south, *east, *west, *top, *bottom, *res, *nsres, *ewres, *res3, *tbres, *rows, *cols, *save, *region, *raster, *raster3d, *align, *zoom, *vect; } parm; G_gisinit(argv[0]); module = G_define_module(); G_add_keyword(_("general")); G_add_keyword(_("settings")); module->description = _("Manages the boundary definitions for the " "geographic region."); /* flags */ flag.dflt = G_define_flag(); flag.dflt->key = 'd'; flag.dflt->description = _("Set from default region"); flag.dflt->guisection = _("Existing"); flag.savedefault = G_define_flag(); flag.savedefault->key = 's'; flag.savedefault->label = _("Save as default region"); flag.savedefault->description = _("Only possible from the PERMANENT mapset"); flag.savedefault->guisection = _("Existing"); flag.print = G_define_flag(); flag.print->key = 'p'; flag.print->description = _("Print the current region"); flag.print->guisection = _("Print"); flag.lprint = G_define_flag(); flag.lprint->key = 'l'; flag.lprint->description = _("Print the current region in lat/long " "using the current ellipsoid/datum"); flag.lprint->guisection = _("Print"); flag.eprint = G_define_flag(); flag.eprint->key = 'e'; flag.eprint->description = _("Print the current region extent"); flag.eprint->guisection = _("Print"); flag.center = G_define_flag(); flag.center->key = 'c'; flag.center->description = _("Print the current region map center coordinates"); flag.center->guisection = _("Print"); flag.gmt_style = G_define_flag(); flag.gmt_style->key = 't'; flag.gmt_style->description = _("Print the current region in GMT style"); flag.gmt_style->guisection = _("Print"); flag.wms_style = G_define_flag(); flag.wms_style->key = 'w'; flag.wms_style->description = _("Print the current region in WMS style"); flag.wms_style->guisection = _("Print"); flag.dist_res = G_define_flag(); flag.dist_res->key = 'm'; flag.dist_res->description = _("Print region resolution in meters (geodesic)"); flag.dist_res->guisection = _("Print"); flag.nangle = G_define_flag(); flag.nangle->key = 'n'; flag.nangle->label = _("Print the convergence angle (degrees CCW)"); flag.nangle->description = _("The difference between the projection's grid north and true north, " "measured at the center coordinates of the current region."); flag.nangle->guisection = _("Print"); flag.z = G_define_flag(); flag.z->key = '3'; flag.z->description = _("Print also 3D settings"); flag.z->guisection = _("Print"); flag.bbox = G_define_flag(); flag.bbox->key = 'b'; flag.bbox->description = _("Print the maximum bounding box in lat/long on WGS84"); flag.bbox->guisection = _("Print"); flag.gprint = G_define_flag(); flag.gprint->key = 'g'; flag.gprint->description = _("Print in shell script style"); flag.gprint->guisection = _("Print"); flag.flprint = G_define_flag(); flag.flprint->key = 'f'; flag.flprint->description = _("Print in shell script style, but in one line (flat)"); flag.flprint->guisection = _("Print"); flag.res_set = G_define_flag(); flag.res_set->key = 'a'; flag.res_set->description = _("Align region to resolution (default = align to bounds, " "works only for 2D resolution)"); flag.res_set->guisection = _("Bounds"); flag.update = G_define_flag(); flag.update->key = 'u'; flag.update->description = _("Do not update the current region"); flag.update->guisection = _("Effects"); /* parameters */ parm.region = G_define_standard_option(G_OPT_M_REGION); parm.region->description = _("Set current region from named region"); parm.region->guisection = _("Existing"); parm.raster = G_define_standard_option(G_OPT_R_MAP); parm.raster->key = "raster"; parm.raster->required = NO; parm.raster->multiple = YES; parm.raster->description = _("Set region to match raster map(s)"); parm.raster->guisection = _("Existing"); parm.raster3d = G_define_standard_option(G_OPT_R3_MAP); parm.raster3d->key = "raster_3d"; parm.raster3d->required = NO; parm.raster3d->multiple = NO; parm.raster3d->description = _("Set region to match 3D raster map(s) (both 2D and 3D " "values)"); parm.raster3d->guisection = _("Existing"); parm.vect = G_define_standard_option(G_OPT_V_MAP); parm.vect->key = "vector"; parm.vect->required = NO; parm.vect->multiple = YES; parm.vect->label = _("Set region to match vector map(s)"); parm.vect->description = NULL; parm.vect->guisection = _("Existing"); parm.north = G_define_option(); parm.north->key = "n"; parm.north->key_desc = "value"; parm.north->required = NO; parm.north->multiple = NO; parm.north->type = TYPE_STRING; parm.north->description = _("Value for the northern edge"); parm.north->guisection = _("Bounds"); parm.south = G_define_option(); parm.south->key = "s"; parm.south->key_desc = "value"; parm.south->required = NO; parm.south->multiple = NO; parm.south->type = TYPE_STRING; parm.south->description = _("Value for the southern edge"); parm.south->guisection = _("Bounds"); parm.east = G_define_option(); parm.east->key = "e"; parm.east->key_desc = "value"; parm.east->required = NO; parm.east->multiple = NO; parm.east->type = TYPE_STRING; parm.east->description = _("Value for the eastern edge"); parm.east->guisection = _("Bounds"); parm.west = G_define_option(); parm.west->key = "w"; parm.west->key_desc = "value"; parm.west->required = NO; parm.west->multiple = NO; parm.west->type = TYPE_STRING; parm.west->description = _("Value for the western edge"); parm.west->guisection = _("Bounds"); parm.top = G_define_option(); parm.top->key = "t"; parm.top->key_desc = "value"; parm.top->required = NO; parm.top->multiple = NO; parm.top->type = TYPE_STRING; parm.top->description = _("Value for the top edge"); parm.top->guisection = _("Bounds"); parm.bottom = G_define_option(); parm.bottom->key = "b"; parm.bottom->key_desc = "value"; parm.bottom->required = NO; parm.bottom->multiple = NO; parm.bottom->type = TYPE_STRING; parm.bottom->description = _("Value for the bottom edge"); parm.bottom->guisection = _("Bounds"); parm.rows = G_define_option(); parm.rows->key = "rows"; parm.rows->key_desc = "value"; parm.rows->required = NO; parm.rows->multiple = NO; parm.rows->type = TYPE_INTEGER; parm.rows->description = _("Number of rows in the new region"); parm.rows->guisection = _("Resolution"); parm.cols = G_define_option(); parm.cols->key = "cols"; parm.cols->key_desc = "value"; parm.cols->required = NO; parm.cols->multiple = NO; parm.cols->type = TYPE_INTEGER; parm.cols->description = _("Number of columns in the new region"); parm.cols->guisection = _("Resolution"); parm.res = G_define_option(); parm.res->key = "res"; parm.res->key_desc = "value"; parm.res->required = NO; parm.res->multiple = NO; parm.res->type = TYPE_STRING; parm.res->description = _("2D grid resolution (north-south and east-west)"); parm.res->guisection = _("Resolution"); parm.res3 = G_define_option(); parm.res3->key = "res3"; parm.res3->key_desc = "value"; parm.res3->required = NO; parm.res3->multiple = NO; parm.res3->type = TYPE_STRING; parm.res3->description = _("3D grid resolution (north-south, east-west and top-bottom)"); parm.res3->guisection = _("Resolution"); parm.nsres = G_define_option(); parm.nsres->key = "nsres"; parm.nsres->key_desc = "value"; parm.nsres->required = NO; parm.nsres->multiple = NO; parm.nsres->type = TYPE_STRING; parm.nsres->description = _("North-south 2D grid resolution"); parm.nsres->guisection = _("Resolution"); parm.ewres = G_define_option(); parm.ewres->key = "ewres"; parm.ewres->key_desc = "value"; parm.ewres->required = NO; parm.ewres->multiple = NO; parm.ewres->type = TYPE_STRING; parm.ewres->description = _("East-west 2D grid resolution"); parm.ewres->guisection = _("Resolution"); parm.tbres = G_define_option(); parm.tbres->key = "tbres"; parm.tbres->key_desc = "value"; parm.tbres->required = NO; parm.tbres->multiple = NO; parm.tbres->type = TYPE_STRING; parm.tbres->description = _("Top-bottom 3D grid resolution"); parm.tbres->guisection = _("Resolution"); parm.zoom = G_define_option(); parm.zoom->key = "zoom"; parm.zoom->key_desc = "name"; parm.zoom->required = NO; parm.zoom->multiple = NO; parm.zoom->type = TYPE_STRING; parm.zoom->description = _("Shrink region until it meets non-NULL data from this raster map"); parm.zoom->gisprompt = "old,cell,raster"; parm.zoom->guisection = _("Bounds"); parm.align = G_define_option(); parm.align->key = "align"; parm.align->key_desc = "name"; parm.align->required = NO; parm.align->multiple = NO; parm.align->type = TYPE_STRING; parm.align->description = _("Adjust region cells to cleanly align with this raster map"); parm.align->gisprompt = "old,cell,raster"; parm.align->guisection = _("Bounds"); parm.save = G_define_option(); parm.save->key = "save"; parm.save->key_desc = "name"; parm.save->required = NO; parm.save->multiple = NO; parm.save->type = TYPE_STRING; parm.save->description = _("Save current region settings in named region file"); parm.save->gisprompt = "new,windows,region"; parm.save->guisection = _("Effects"); G_option_required(flag.dflt, flag.savedefault, flag.print, flag.lprint, flag.eprint, flag.center, flag.gmt_style, flag.wms_style, flag.dist_res, flag.nangle, flag. z, flag.bbox, flag.gprint, flag.res_set, flag.update, parm.region, parm.raster, parm.raster3d, parm.vect, parm.north, parm.south, parm.east, parm.west, parm.top, parm.bottom, parm.rows, parm.cols, parm.res, parm.res3, parm.nsres, parm.ewres, parm.tbres, parm.zoom, parm.align, parm.save, NULL); if (G_parser(argc, argv)) exit(EXIT_FAILURE); G_get_default_window(&window); set_flag = !flag.update->answer; flat_flag = flag.flprint->answer; if (flag.print->answer) print_flag |= PRINT_REG; if (flag.gprint->answer) print_flag |= PRINT_SH; if (flag.lprint->answer) print_flag |= PRINT_LL; if (flag.eprint->answer) print_flag |= PRINT_EXTENT; if (flag.center->answer) print_flag |= PRINT_CENTER; if (flag.gmt_style->answer) print_flag |= PRINT_GMT; if (flag.wms_style->answer) print_flag |= PRINT_WMS; if (flag.nangle->answer) print_flag |= PRINT_NANGLE; if (flag.dist_res->answer) print_flag |= PRINT_METERS; if (flag.z->answer) print_flag |= PRINT_3D; if (flag.bbox->answer) print_flag |= PRINT_MBBOX; if (print_flag == PRINT_METERS) print_flag |= PRINT_SH; if (print_flag == PRINT_SH || print_flag & PRINT_3D || print_flag == PRINT_METERS + PRINT_SH) { print_flag |= PRINT_REG; } if (!flag.dflt->answer) G_get_window(&window); /* region= */ if ((name = parm.region->answer)) { mapset = G_find_file2("windows", name, ""); if (!mapset) G_fatal_error(_("Region <%s> not found"), name); G_get_element_window(&window, "windows", name, mapset); } /* raster= */ if (parm.raster->answer) { int first = 0; rast_ptr = parm.raster->answers; for (; *rast_ptr != NULL; rast_ptr++) { char rast_name[GNAME_MAX]; strcpy(rast_name, *rast_ptr); mapset = G_find_raster2(rast_name, ""); if (!mapset) G_fatal_error(_("Raster map <%s> not found"), rast_name); Rast_get_cellhd(rast_name, mapset, &temp_window); if (!first) { window = temp_window; first = 1; } else { window.north = (window.north > temp_window.north) ? window.north : temp_window.north; window.south = (window.south < temp_window.south) ? window.south : temp_window.south; window.east = (window.east > temp_window.east) ? window.east : temp_window.east; window.west = (window.west < temp_window.west) ? window.west : temp_window.west; } } G_adjust_Cell_head3(&window, 0, 0, 0); } /* raster3d= */ if ((name = parm.raster3d->answer)) { RASTER3D_Region win; if ((mapset = G_find_raster3d(name, "")) == NULL) G_fatal_error(_("3D raster map <%s> not found"), name); if (Rast3d_read_region_map(name, mapset, &win) < 0) G_fatal_error(_("Unable to read header of 3D raster map <%s@%s>"), name, mapset); Rast3d_region_to_cell_head(&win, &window); } /* vector= */ if (parm.vect->answer) { int first = 0; vect_ptr = parm.vect->answers; for (; *vect_ptr != NULL; vect_ptr++) { struct Map_info Map; struct bound_box box; char vect_name[GNAME_MAX]; struct Cell_head map_window; strcpy(vect_name, *vect_ptr); mapset = G_find_vector2(vect_name, ""); if (!mapset) G_fatal_error(_("Vector map <%s> not found"), vect_name); temp_window = window; Vect_set_open_level(2); if (2 > Vect_open_old_head(&Map, vect_name, mapset)) G_fatal_error(_("Unable to open vector map <%s> on topological level"), vect_name); Vect_get_map_box(&Map, &box); map_window = window; map_window.north = box.N; map_window.south = box.S; map_window.west = box.W; map_window.east = box.E; map_window.top = box.T; map_window.bottom = box.B; if (!first) { window = map_window; first = 1; } else { window.north = (window.north > map_window.north) ? window.north : map_window.north; window.south = (window.south < map_window.south) ? window.south : map_window.south; window.east = (window.east > map_window.east) ? window.east : map_window.east; window.west = (window.west < map_window.west) ? window.west : map_window.west; if (map_window.top > window.top) window.top = map_window.top; if (map_window.bottom < window.bottom) window.bottom = map_window.bottom; } if (window.north == window.south) { window.north = window.north + 0.5 * temp_window.ns_res; window.south = window.south - 0.5 * temp_window.ns_res; } if (window.east == window.west) { window.west = window.west - 0.5 * temp_window.ew_res; window.east = window.east + 0.5 * temp_window.ew_res; } if (window.top == window.bottom) { window.bottom = (window.bottom - 0.5 * temp_window.tb_res); window.top = (window.top + 0.5 * temp_window.tb_res); } if (flag.res_set->answer) Rast_align_window(&window, &temp_window); Vect_close(&Map); } } /* n= */ if ((value = parm.north->answer)) { if ((i = nsew(value, "n+", "n-", "s+"))) { if (!G_scan_resolution(value + 2, &x, window.proj)) die(parm.north); switch (i) { case 1: window.north += x; break; case 2: window.north -= x; break; case 3: window.north = window.south + x; break; } } else if (G_scan_northing(value, &x, window.proj)) window.north = x; else die(parm.north); } /* s= */ if ((value = parm.south->answer)) { if ((i = nsew(value, "s+", "s-", "n-"))) { if (!G_scan_resolution(value + 2, &x, window.proj)) die(parm.south); switch (i) { case 1: window.south += x; break; case 2: window.south -= x; break; case 3: window.south = window.north - x; break; } } else if (G_scan_northing(value, &x, window.proj)) window.south = x; else die(parm.south); } /* e= */ if ((value = parm.east->answer)) { if ((i = nsew(value, "e+", "e-", "w+"))) { if (!G_scan_resolution(value + 2, &x, window.proj)) die(parm.east); switch (i) { case 1: window.east += x; break; case 2: window.east -= x; break; case 3: window.east = window.west + x; break; } } else if (G_scan_easting(value, &x, window.proj)) window.east = x; else die(parm.east); } /* w= */ if ((value = parm.west->answer)) { if ((i = nsew(value, "w+", "w-", "e-"))) { if (!G_scan_resolution(value + 2, &x, window.proj)) die(parm.west); switch (i) { case 1: window.west += x; break; case 2: window.west -= x; break; case 3: window.west = window.east - x; break; } } else if (G_scan_easting(value, &x, window.proj)) window.west = x; else die(parm.west); } /* t= */ if ((value = parm.top->answer)) { if ((i = nsew(value, "t+", "t-", "b+"))) { if (sscanf(value + 2, "%lf", &x) != 1) die(parm.top); switch (i) { case 1: window.top += x; break; case 2: window.top -= x; break; case 3: window.top = window.bottom + x; break; } } else if (sscanf(value, "%lf", &x) == 1) window.top = x; else die(parm.top); } /* b= */ if ((value = parm.bottom->answer)) { if ((i = nsew(value, "b+", "b-", "t-"))) { if (sscanf(value + 2, "%lf", &x) != 1) die(parm.bottom); switch (i) { case 1: window.bottom += x; break; case 2: window.bottom -= x; break; case 3: window.bottom = window.top - x; break; } } else if (sscanf(value, "%lf", &x) == 1) window.bottom = x; else die(parm.bottom); } /* res= */ if ((value = parm.res->answer)) { if (!G_scan_resolution(value, &x, window.proj)) die(parm.res); window.ns_res = x; window.ew_res = x; if (flag.res_set->answer) { window.north = ceil(window.north / x) * x; window.south = floor(window.south / x) * x; window.east = ceil(window.east / x) * x; window.west = floor(window.west / x) * x; } } /* res3= */ if ((value = parm.res3->answer)) { if (!G_scan_resolution(value, &x, window.proj)) die(parm.res); window.ns_res3 = x; window.ew_res3 = x; window.tb_res = x; } /* nsres= */ if ((value = parm.nsres->answer)) { if (!G_scan_resolution(value, &x, window.proj)) die(parm.nsres); window.ns_res = x; if (flag.res_set->answer) { window.north = ceil(window.north / x) * x; window.south = floor(window.south / x) * x; } } /* ewres= */ if ((value = parm.ewres->answer)) { if (!G_scan_resolution(value, &x, window.proj)) die(parm.ewres); window.ew_res = x; if (flag.res_set->answer) { window.east = ceil(window.east / x) * x; window.west = floor(window.west / x) * x; } } /* tbres= */ if ((value = parm.tbres->answer)) { if (sscanf(value, "%lf", &x) != 1) die(parm.tbres); window.tb_res = x; if (flag.res_set->answer) { window.top = ceil(window.top / x) * x; window.bottom = floor(window.bottom / x) * x; } } /* rows= */ if ((value = parm.rows->answer)) { if (sscanf(value, "%i", &ival) != 1) die(parm.rows); window.rows = ival; row_flag = 1; } /* cols= */ if ((value = parm.cols->answer)) { if (sscanf(value, "%i", &ival) != 1) die(parm.cols); window.cols = ival; col_flag = 1; } /* zoom= */ if ((name = parm.zoom->answer)) { mapset = G_find_raster2(name, ""); if (!mapset) G_fatal_error(_("Raster map <%s> not found"), name); zoom(&window, name, mapset); } /* align= */ if ((name = parm.align->answer)) { mapset = G_find_raster2(name, ""); if (!mapset) G_fatal_error(_("Raster map <%s> not found"), name); Rast_get_cellhd(name, mapset, &temp_window); Rast_align_window(&window, &temp_window); } /* save= */ if ((name = parm.save->answer)) { temp_window = window; G_adjust_Cell_head3(&temp_window, 0, 0, 0); if (G_put_element_window(&temp_window, "windows", name) < 0) G_fatal_error(_("Unable to set region <%s>"), name); } G_adjust_Cell_head3(&window, row_flag, col_flag, 0); if (set_flag) { if (G_put_window(&window) < 0) G_fatal_error(_("Unable to update current region")); } if (flag.savedefault->answer) { if (strcmp(G_mapset(), "PERMANENT") == 0) { G_put_element_window(&window, "", "DEFAULT_WIND"); } else { G_fatal_error(_("Unable to change default region. " "The current mapset is not <PERMANENT>.")); } } /* / flag.savedefault->answer */ if (print_flag) print_window(&window, print_flag, flat_flag); exit(EXIT_SUCCESS); }
/* ************************************************************************* */ int main(int argc, char *argv[]) { char *output = NULL; RASTER3D_Region region; struct Cell_head window2d; struct Cell_head default_region; FILE *fp = NULL; struct GModule *module; int dp, i, changemask = 0; int rows, cols; const char *mapset, *name; double scale = 1.0, llscale = 1.0; input_maps *in; /* Initialize GRASS */ G_gisinit(argv[0]); module = G_define_module(); G_add_keyword(_("raster3d")); G_add_keyword(_("export")); G_add_keyword(_("voxel")); G_add_keyword("VTK"); module->description = _("Converts 3D raster maps into the VTK-ASCII format."); /* Get parameters from user */ set_params(); /* Have GRASS get inputs */ if (G_parser(argc, argv)) exit(EXIT_FAILURE); /*The precision of the output */ if (param.decimals->answer) { if (sscanf(param.decimals->answer, "%d", &dp) != 1) G_fatal_error(_("failed to interpret dp as an integer")); if (dp > 20 || dp < 0) G_fatal_error(_("dp has to be from 0 to 20")); } else { dp = 8; /*This value is taken from the lib settings in G_format_easting */ } /*Check the input */ check_input_maps(); /*Correct the coordinates, so the precision of VTK is not hurt :( */ if (param.coorcorr->answer) { /*Get the default region for coordiante correction */ G_get_default_window(&default_region); /*Use the center of the current region as extent */ y_extent = (default_region.north + default_region.south) / 2; x_extent = (default_region.west + default_region.east) / 2; } else { x_extent = 0; y_extent = 0; } /*open the output */ if (param.output->answer) { fp = fopen(param.output->answer, "w"); if (fp == NULL) { perror(param.output->answer); G_usage(); exit(EXIT_FAILURE); } } else fp = stdout; /* Figure out the region from the map */ Rast3d_init_defaults(); Rast3d_get_window(®ion); /*initiate the input mpas structure */ in = create_input_maps_struct(); /* read and compute the scale factor */ sscanf(param.elevscale->answer, "%lf", &scale); /*if LL projection, convert the elevation values to degrees */ if (param.scalell->answer && region.proj == PROJECTION_LL) { llscale = M_PI / (180) * 6378137; scale /= llscale; } /*Open the top and bottom file */ if (param.structgrid->answer) { /*Check if the g3d-region is equal to the 2d rows and cols */ rows = Rast_window_rows(); cols = Rast_window_cols(); /*If not equal, set the 2D windows correct */ if (rows != region.rows || cols != region.cols) { G_message(_("The 2D and 3D region settings are different. " "Using the 2D window settings to adjust the 2D part of the 3D region.")); G_get_set_window(&window2d); window2d.ns_res = region.ns_res; window2d.ew_res = region.ew_res; window2d.rows = region.rows; window2d.cols = region.cols; Rast_set_window(&window2d); } /*open top */ mapset = NULL; name = NULL; name = param.top->answer; mapset = G_find_raster2(name, ""); in->top = open_input_map(name, mapset); in->topMapType = Rast_get_map_type(in->top); /*open bottom */ mapset = NULL; name = NULL; name = param.bottom->answer; mapset = G_find_raster2(name, ""); in->bottom = open_input_map(name, mapset); in->bottomMapType = Rast_get_map_type(in->bottom); /* Write the vtk-header and the points */ if (param.point->answer) { write_vtk_structured_grid_header(fp, output, region); write_vtk_points(in, fp, region, dp, 1, scale); } else { write_vtk_unstructured_grid_header(fp, output, region); write_vtk_points(in, fp, region, dp, 0, scale); write_vtk_unstructured_grid_cells(fp, region); } Rast_close(in->top); in->top = -1; Rast_close(in->bottom); in->bottom = -1; } else { /* Write the structured point vtk-header */ write_vtk_structured_point_header(fp, output, region, dp, scale); } /*Write the normal VTK data (cell or point data) */ /*Loop over all 3d input maps! */ if (param.input->answers != NULL) { for (i = 0; param.input->answers[i] != NULL; i++) { G_debug(3, "Open 3D raster map <%s>", param.input->answers[i]); /*Open the map */ in->map = Rast3d_open_cell_old(param.input->answers[i], G_find_raster3d(param.input->answers[i], ""), ®ion, RASTER3D_TILE_SAME_AS_FILE, RASTER3D_USE_CACHE_DEFAULT); if (in->map == NULL) { G_warning(_("Unable to open 3D raster map <%s>"), param.input->answers[i]); fatal_error(" ", in); } /*if requested set the Mask on */ if (param.mask->answer) { if (Rast3d_mask_file_exists()) { changemask = 0; if (Rast3d_mask_is_off(in->map)) { Rast3d_mask_on(in->map); changemask = 1; } } } /* Write the point or cell data */ write_vtk_data(fp, in->map, region, param.input->answers[i], dp); /*We set the Mask off, if it was off before */ if (param.mask->answer) { if (Rast3d_mask_file_exists()) if (Rast3d_mask_is_on(in->map) && changemask) Rast3d_mask_off(in->map); } /* Close the 3d raster map */ if (!Rast3d_close(in->map)) { in->map = NULL; fatal_error(_("Unable to close 3D raster map, the VTK file may be incomplete"), in); } in->map = NULL; } } /*Write the RGB voxel data */ open_write_rgb_maps(in, region, fp, dp); open_write_vector_maps(in, region, fp, dp); /*Close the output file */ if (param.output->answer && fp != NULL) if (fclose(fp)) fatal_error(_("Unable to close VTK-ASCII file"), in); /*close all open maps and free memory */ release_input_maps_struct(in); return 0; }
/* ************************************************************************* */ int main(int argc, char *argv[]) { struct Cell_head region; struct Cell_head default_region; FILE *fp = NULL; struct GModule *module; int i = 0, polytype = 0; char *null_value; int out_type; int fd; /*Normale maps ;) */ int rgbfd[3]; int vectfd[3]; int celltype[3] = { 0, 0, 0 }; int headertype; double scale = 1.0, llscale = 1.0, eleval = 0.0; int digits = 12; /* Initialize GRASS */ G_gisinit(argv[0]); module = G_define_module(); G_add_keyword(_("raster")); G_add_keyword(_("export")); module->description = _("Converts raster maps into the VTK-ASCII format."); /* Get parameters from user */ set_params(); /* Have GRASS get inputs */ if (G_parser(argc, argv)) exit(EXIT_FAILURE); if (param.input->answers == NULL && param.rgbmaps->answers == NULL && param.vectmaps->answers == NULL) { G_fatal_error(_("No input maps specified. You need to specify at least one input map or three vector maps or three rgb maps.")); } /*open the output */ if (param.output->answer) { fp = fopen(param.output->answer, "w"); if (fp == NULL) { perror(param.output->answer); G_usage(); exit(EXIT_FAILURE); } } else fp = stdout; /*Correct the coordinates, so the precision of VTK is not hurt :( */ if (param.coorcorr->answer) { /*Get the default region for coordiante correction */ G_get_default_window(&default_region); /*Use the center of the current region as extent */ y_extent = (default_region.north + default_region.south) / 2; x_extent = (default_region.west + default_region.east) / 2; } else { x_extent = 0; y_extent = 0; } /* Figure out the region from the map */ G_get_window(®ion); /*Set the null Value, maybe i have to check this? */ null_value = param.null_val->answer; /*number of significant digits */ sscanf(param.decimals->answer, "%i", &digits); /* read and compute the scale factor */ sscanf(param.elevscale->answer, "%lf", &scale); sscanf(param.elev->answer, "%lf", &eleval); /*if LL projection, convert the elevation values to degrees */ if (region.proj == PROJECTION_LL) { llscale = M_PI / (180) * 6378137; scale /= llscale; } /********************* WRITE ELEVATION *************************************/ if (param.elevationmap->answer) { /*If the elevation is set, write the correct Header */ if (param.usestruct->answer) { write_vtk_structured_elevation_header(fp, region); } else { write_vtk_polygonal_elevation_header(fp, region); } G_debug(3, _("Open Raster file %s"), param.elevationmap->answer); /* open raster map */ fd = Rast_open_old(param.elevationmap->answer, ""); out_type = Rast_get_map_type(fd); /*The write the Coordinates */ if (param.usestruct->answer) { write_vtk_structured_coordinates(fd, fp, param.elevationmap->answer, region, out_type, null_value, scale, digits); } else { polytype = QUADS; /*The default */ if (param.usetriangle->answer) polytype = TRIANGLE_STRIPS; if (param.usevertices->answer) polytype = VERTICES; write_vtk_polygonal_coordinates(fd, fp, param.elevationmap->answer, region, out_type, null_value, scale, polytype, digits); } Rast_close(fd); } else { /*Should pointdata or celldata be written */ if (param.point->answer) headertype = 1; else headertype = 0; /*If no elevation is given, write the normal Header */ if (param.origin->answer) write_vtk_normal_header(fp, region, scale * eleval, headertype); else write_vtk_normal_header(fp, region, eleval / llscale, headertype); } /******************** WRITE THE POINT OR CELL DATA HEADER ******************/ if (param.input->answers != NULL || param.rgbmaps->answers != NULL) { if (param.point->answer || param.elevationmap->answer) write_vtk_pointdata_header(fp, region); else write_vtk_celldata_header(fp, region); } /********************** WRITE NORMAL DATA; CELL OR POINT *******************/ /*Loop over all input maps! */ if (param.input->answers != NULL) { for (i = 0; param.input->answers[i] != NULL; i++) { G_debug(3, _("Open Raster file %s"), param.input->answers[i]); /* open raster map */ fd = Rast_open_old(param.input->answers[i], ""); out_type = Rast_get_map_type(fd); /*Now write the data */ write_vtk_data(fd, fp, param.input->answers[i], region, out_type, null_value, digits); Rast_close(fd); } } /********************** WRITE RGB IMAGE DATA; CELL OR POINT ****************/ if (param.rgbmaps->answers != NULL) { if (param.rgbmaps->answers[0] != NULL && param.rgbmaps->answers[1] != NULL && param.rgbmaps->answers[2] != NULL) { /*Loop over all three rgb input maps! */ for (i = 0; i < 3; i++) { G_debug(3, _("Open Raster file %s"), param.rgbmaps->answers[i]); /* open raster map */ rgbfd[i] = Rast_open_old(param.rgbmaps->answers[i], ""); celltype[i] = Rast_get_map_type(rgbfd[i]); } /*Maps have to be from the same type */ if (celltype[0] == celltype[1] && celltype[0] == celltype[2]) { G_debug(3, _("Writing VTK ImageData\n")); out_type = celltype[0]; /*Now write the data */ write_vtk_rgb_image_data(rgbfd[0], rgbfd[1], rgbfd[2], fp, "RGB_Image", region, out_type, digits); } else { G_warning(_("Wrong RGB maps. Maps should have the same type! RGB output not added!")); /*do nothing */ } /*Close the maps */ for (i = 0; i < 3; i++) Rast_close(rgbfd[i]); } } /********************** WRITE VECTOR DATA; CELL OR POINT ****************/ if (param.vectmaps->answers != NULL) { if (param.vectmaps->answers[0] != NULL && param.vectmaps->answers[1] != NULL && param.vectmaps->answers[2] != NULL) { /*Loop over all three vect input maps! */ for (i = 0; i < 3; i++) { G_debug(3, _("Open Raster file %s"), param.vectmaps->answers[i]); /* open raster map */ vectfd[i] = Rast_open_old(param.vectmaps->answers[i], ""); celltype[i] = Rast_get_map_type(vectfd[i]); } /*Maps have to be from the same type */ if (celltype[0] == celltype[1] && celltype[0] == celltype[2]) { G_debug(3, _("Writing VTK Vector Data\n")); out_type = celltype[0]; /*Now write the data */ write_vtk_vector_data(vectfd[0], vectfd[1], vectfd[2], fp, "Vector_Data", region, out_type, digits); } else { G_warning(_("Wrong vector maps. Maps should have the same type! Vector output not added!")); /*do nothing */ } /*Close the maps */ for (i = 0; i < 3; i++) Rast_close(vectfd[i]); } } if (param.output->answer && fp != NULL) if (fclose(fp)) { G_fatal_error(_("Error closing VTK-ASCII file")); } return 0; }
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[]) { int i; double x; struct Cell_head cellhd, window; const char *value; const char *name; struct GModule *module; struct { struct Flag *dflt, *cur; } flag; struct { struct Option *map, *north, *south, *east, *west, *raster, *vect, *region, *align; } parm; G_gisinit(argv[0]); module = G_define_module(); G_add_keyword(_("raster")); G_add_keyword(_("metadata")); module->description = _("Sets the boundary definitions for a raster map."); /* flags */ flag.cur = G_define_flag(); flag.cur->key = 'c'; flag.cur->description = _("Set from current region"); flag.cur->guisection = _("Existing"); flag.dflt = G_define_flag(); flag.dflt->key = 'd'; flag.dflt->description = _("Set from default region"); flag.dflt->guisection = _("Existing"); /* parameters */ parm.map = G_define_standard_option(G_OPT_R_MAP); parm.map->description = _("Name of raster map to change"); parm.region = G_define_option(); parm.region->key = "region"; parm.region->key_desc = "name"; parm.region->required = NO; parm.region->multiple = NO; parm.region->type = TYPE_STRING; parm.region->description = _("Set region from named region"); parm.region->gisprompt = "old,windows,region"; parm.region->guisection = _("Existing"); parm.raster = G_define_standard_option(G_OPT_R_MAP); parm.raster->key = "raster"; parm.raster->required = NO; parm.raster->multiple = NO; parm.raster->description = _("Set region to match this raster map"); parm.raster->guisection = _("Existing"); parm.vect = G_define_standard_option(G_OPT_V_MAP); parm.vect->key = "vector"; parm.vect->required = NO; parm.vect->multiple = NO; parm.vect->description = _("Set region to match this vector map"); parm.vect->guisection = _("Existing"); parm.north = G_define_option(); parm.north->key = "n"; parm.north->key_desc = "value"; parm.north->required = NO; parm.north->multiple = NO; parm.north->type = TYPE_STRING; parm.north->description = _("Value for the northern edge"); parm.north->guisection = _("Bounds"); parm.south = G_define_option(); parm.south->key = "s"; parm.south->key_desc = "value"; parm.south->required = NO; parm.south->multiple = NO; parm.south->type = TYPE_STRING; parm.south->description = _("Value for the southern edge"); parm.south->guisection = _("Bounds"); parm.east = G_define_option(); parm.east->key = "e"; parm.east->key_desc = "value"; parm.east->required = NO; parm.east->multiple = NO; parm.east->type = TYPE_STRING; parm.east->description = _("Value for the eastern edge"); parm.east->guisection = _("Bounds"); parm.west = G_define_option(); parm.west->key = "w"; parm.west->key_desc = "value"; parm.west->required = NO; parm.west->multiple = NO; parm.west->type = TYPE_STRING; parm.west->description = _("Value for the western edge"); parm.west->guisection = _("Bounds"); parm.align = G_define_standard_option(G_OPT_R_MAP); parm.align->key = "align"; parm.align->required = NO; parm.align->multiple = NO; parm.align->description = _("Raster map to align to"); parm.align->guisection = _("Existing"); if (G_parser(argc, argv)) exit(EXIT_FAILURE); G_get_window(&window); name = parm.map->answer; Rast_get_cellhd(name, G_mapset(), &cellhd); window = cellhd; if (flag.dflt->answer) G_get_default_window(&window); if (flag.cur->answer) G_get_window(&window); if ((name = parm.region->answer)) /* region= */ G__get_window(&window, "windows", name, ""); if ((name = parm.raster->answer)) { /* raster= */ Rast_get_cellhd(name, "", &window); } if ((name = parm.vect->answer)) { /* vect= */ struct Map_info Map; struct bound_box box; Vect_set_open_level(1); if (Vect_open_old(&Map, name, "") != 1) G_fatal_error(_("Unable to open vector map <%s>"), name); Vect_get_map_box(&Map, &box); window.north = box.N; window.south = box.S; window.west = box.W; window.east = box.E; Rast_align_window(&window, &cellhd); Vect_close(&Map); } if ((value = parm.north->answer)) { /* n= */ if ((i = nsew(value, "n+", "n-", "s+"))) { if (!G_scan_resolution(value + 2, &x, window.proj)) die(parm.north); switch (i) { case 1: window.north += x; break; case 2: window.north -= x; break; case 3: window.north = window.south + x; break; } } else if (G_scan_northing(value, &x, window.proj)) window.north = x; else die(parm.north); } if ((value = parm.south->answer)) { /* s= */ if ((i = nsew(value, "s+", "s-", "n-"))) { if (!G_scan_resolution(value + 2, &x, window.proj)) die(parm.south); switch (i) { case 1: window.south += x; break; case 2: window.south -= x; break; case 3: window.south = window.north - x; break; } } else if (G_scan_northing(value, &x, window.proj)) window.south = x; else die(parm.south); } if ((value = parm.east->answer)) { /* e= */ if ((i = nsew(value, "e+", "e-", "w+"))) { if (!G_scan_resolution(value + 2, &x, window.proj)) die(parm.east); switch (i) { case 1: window.east += x; break; case 2: window.east -= x; break; case 3: window.east = window.west + x; break; } } else if (G_scan_easting(value, &x, window.proj)) window.east = x; else die(parm.east); } if ((value = parm.west->answer)) { /* w= */ if ((i = nsew(value, "w+", "w-", "e-"))) { if (!G_scan_resolution(value + 2, &x, window.proj)) die(parm.west); switch (i) { case 1: window.west += x; break; case 2: window.west -= x; break; case 3: window.west = window.east - x; break; } } else if (G_scan_easting(value, &x, window.proj)) window.west = x; else die(parm.west); } if ((name = parm.align->answer)) { /* align= */ struct Cell_head temp_window; Rast_get_cellhd(name, "", &temp_window); Rast_align_window(&window, &temp_window); } window.rows = cellhd.rows; window.cols = cellhd.cols; G_adjust_Cell_head(&window, 1, 1); cellhd.north = window.north; cellhd.south = window.south; cellhd.east = window.east; cellhd.west = window.west; Rast_put_cellhd(parm.map->answer, &cellhd); G_done_msg(" "); return 0; }