/* * close_vect - builds vector support and frees up resources */ int close_vect(struct Map_info *map, const int build_support) { if (build_support) Vect_build(map); Vect_set_release_support(map); Vect_close(map); return 1; }
int lister(char *name, char *mapset, char *title) { struct Map_info Map; *title = 0; if (*name) { if (Vect_open_old_head(&Map, name, mapset) < 0) G_fatal_error(_("Unable to open vector map <%s>"), name); strcpy(title, Vect_get_map_name(&Map)); Vect_close(&Map); } return 0; }
OGRGRASSDataSource::~OGRGRASSDataSource() { for( int i = 0; i < nLayers; i++ ) delete papoLayers[i]; if ( pszName ) CPLFree( pszName ); if ( papoLayers ) CPLFree( papoLayers ); if ( pszGisdbase ) G_free( pszGisdbase ); if ( pszLocation ) G_free( pszLocation ); if ( pszMapset ) G_free( pszMapset ); if ( pszMap ) G_free( pszMap ); if (bOpened) Vect_close(&map); }
void QgsGrassPlugin::newVector() { // QgsDebugMsg("entered."); if ( QgsGrassEdit::isRunning() ) { QMessageBox::warning( 0, tr( "Warning" ), tr( "GRASS Edit is already running." ) ); return; } bool ok; QString name; QgsGrassElementDialog dialog( qGisInterface->mainWindow() ); name = dialog.getItem( "vector", tr( "New vector name" ), tr( "New vector name" ), "", "", &ok ); if ( !ok ) return; // Create new map QgsGrass::setMapset( QgsGrass::getDefaultGisdbase(), QgsGrass::getDefaultLocation(), QgsGrass::getDefaultMapset() ); struct Map_info *Map = 0; G_TRY { Map = QgsGrass::vectNewMapStruct(); Vect_open_new( Map, name.toUtf8().data(), 0 ); #if defined(GRASS_VERSION_MAJOR) && defined(GRASS_VERSION_MINOR) && \ ( ( GRASS_VERSION_MAJOR == 6 && GRASS_VERSION_MINOR >= 4 ) || GRASS_VERSION_MAJOR > 6 ) Vect_build( Map ); #else Vect_build( Map, stderr ); #endif Vect_set_release_support( Map ); Vect_close( Map ); QgsGrass::vectDestroyMapStruct( Map ); } G_CATCH( QgsGrass::Exception &e ) { QgsGrass::warning( tr( "Cannot create new vector: %1" ).arg( e.what() ) ); QgsGrass::vectDestroyMapStruct( Map ); return; }
void G_sites_close(struct Map_info *Map) { int i, j; if (Map->mode == GV_MODE_WRITE || Map->mode == GV_MODE_RW) Vect_build(Map); Vect_close(Map); for (i = 0; i < Map->n_site_att; i++) { free(Map->site_att[i].dbl); for (j = 0; j < Map->n_site_str; j++) free(Map->site_att[i].str[j]); free(Map->site_att[i].str); } free(Map->site_att); G_free(Map); }
bool QgsGrassVectorMap::openMap() { // TODO: refresh layers (reopen) QgsDebugMsg( toString() ); QgsGrass::lock(); QgsGrass::setLocation( mGrassObject.gisdbase(), mGrassObject.location() ); // Find the vector const char *ms = G_find_vector2( mGrassObject.name().toUtf8().data(), mGrassObject.mapset().toUtf8().data() ); if ( !ms ) { QgsDebugMsg( "Cannot find GRASS vector" ); QgsGrass::unlock(); return false; } // Read the time of vector dir before Vect_open_old, because it may take long time (when the vector // could be owerwritten) QFileInfo di( mGrassObject.mapsetPath() + "/vector/" + mGrassObject.name() ); mLastModified = di.lastModified(); di.setFile( mGrassObject.mapsetPath() + "/vector/" + mGrassObject.name() + "/dbln" ); mLastAttributesModified = di.lastModified(); mMap = QgsGrass::vectNewMapStruct(); // Do we have topology and cidx (level2) int level = -1; G_TRY { Vect_set_open_level( 2 ); level = Vect_open_old_head( mMap, mGrassObject.name().toUtf8().data(), mGrassObject.mapset().toUtf8().data() ); Vect_close( mMap ); } G_CATCH( QgsGrass::Exception &e ) { QgsGrass::warning( e ); level = -1; }
int do_vpoints(int after_masking) { int n; struct Map_info Map; n = vector.count; while (n-- > 0) { if (vector.layer[n].type != VPOINTS) continue; if (after_masking && vector.layer[n].masked) continue; if (!after_masking && !vector.layer[n].masked) continue; G_message(_("Reading vector points file <%s in %s> ..."), vector.layer[n].name, vector.layer[n].mapset); Vect_set_open_level(2); Vect_set_fatal_error(GV_FATAL_PRINT); if (2 > Vect_open_old(&Map, vector.layer[n].name, vector.layer[n].mapset)) { char name[100]; sprintf(name, "%s in %s", vector.layer[n].name, vector.layer[n].mapset); error("vector map", name, "can't open"); continue; } PS_vpoints_plot(&Map, n, LINE_DRAW_LINE); Vect_close(&Map); fprintf(PS.fp, "[] 0 setdash\n"); } return 0; }
int main(int argc, char *argv[]) { struct Map_info In, Out; static struct line_pnts *Points; struct line_cats *Cats; struct GModule *module; /* GRASS module for parsing arguments */ struct Option *map_in, *map_out; struct Option *cat_opt, *field_opt, *where_opt, *abcol, *afcol; struct Option *iter_opt, *error_opt; struct Flag *geo_f, *add_f; int chcat, with_z; int layer, mask_type; struct varray *varray; dglGraph_s *graph; int i, geo, nnodes, nlines, j, max_cat; char buf[2000], *covered; /* initialize GIS environment */ G_gisinit(argv[0]); /* reads grass env, stores program name to G_program_name() */ /* initialize module */ module = G_define_module(); module->keywords = _("vector, network, centrality measures"); module->description = _("Computes degree, centrality, betweeness, closeness and eigenvector " "centrality measures in the network."); /* Define the different options as defined in gis.h */ map_in = G_define_standard_option(G_OPT_V_INPUT); field_opt = G_define_standard_option(G_OPT_V_FIELD); map_out = G_define_standard_option(G_OPT_V_OUTPUT); cat_opt = G_define_standard_option(G_OPT_V_CATS); cat_opt->guisection = _("Selection"); where_opt = G_define_standard_option(G_OPT_WHERE); where_opt->guisection = _("Selection"); afcol = G_define_standard_option(G_OPT_COLUMN); afcol->key = "afcolumn"; afcol->required = NO; afcol->description = _("Name of arc forward/both direction(s) cost column"); afcol->guisection = _("Cost"); abcol = G_define_standard_option(G_OPT_COLUMN); abcol->key = "abcolumn"; abcol->required = NO; abcol->description = _("Name of arc backward direction cost column"); abcol->guisection = _("Cost"); deg_opt = G_define_standard_option(G_OPT_COLUMN); deg_opt->key = "degree"; deg_opt->required = NO; deg_opt->description = _("Name of degree centrality column"); deg_opt->guisection = _("Columns"); close_opt = G_define_standard_option(G_OPT_COLUMN); close_opt->key = "closeness"; close_opt->required = NO; close_opt->description = _("Name of closeness centrality column"); close_opt->guisection = _("Columns"); betw_opt = G_define_standard_option(G_OPT_COLUMN); betw_opt->key = "betweenness"; betw_opt->required = NO; betw_opt->description = _("Name of betweenness centrality column"); betw_opt->guisection = _("Columns"); eigen_opt = G_define_standard_option(G_OPT_COLUMN); eigen_opt->key = "eigenvector"; eigen_opt->required = NO; eigen_opt->description = _("Name of eigenvector centrality column"); eigen_opt->guisection = _("Columns"); iter_opt = G_define_option(); iter_opt->key = "iterations"; iter_opt->answer = "1000"; iter_opt->type = TYPE_INTEGER; iter_opt->required = NO; iter_opt->description = _("Maximum number of iterations to compute eigenvector centrality"); error_opt = G_define_option(); error_opt->key = "error"; error_opt->answer = "0.1"; error_opt->type = TYPE_DOUBLE; error_opt->required = NO; error_opt->description = _("Cummulative error tolerance for eigenvector centrality"); geo_f = G_define_flag(); geo_f->key = 'g'; geo_f->description = _("Use geodesic calculation for longitude-latitude locations"); add_f = G_define_flag(); add_f->key = 'a'; add_f->description = _("Add points on nodes"); /* options and flags parser */ if (G_parser(argc, argv)) exit(EXIT_FAILURE); /* TODO: make an option for this */ mask_type = GV_LINE | GV_BOUNDARY; Points = Vect_new_line_struct(); Cats = Vect_new_cats_struct(); Vect_check_input_output_name(map_in->answer, map_out->answer, GV_FATAL_EXIT); Vect_set_open_level(2); if (1 > Vect_open_old(&In, map_in->answer, "")) G_fatal_error(_("Unable to open vector map <%s>"), map_in->answer); with_z = Vect_is_3d(&In); if (0 > Vect_open_new(&Out, map_out->answer, with_z)) { Vect_close(&In); G_fatal_error(_("Unable to create vector map <%s>"), map_out->answer); } if (geo_f->answer) { geo = 1; if (G_projection() != PROJECTION_LL) G_warning(_("The current projection is not longitude-latitude")); } else geo = 0; /* parse filter option and select appropriate lines */ layer = atoi(field_opt->answer); chcat = (NetA_initialise_varray (&In, layer, mask_type, where_opt->answer, cat_opt->answer, &varray) == 1); /* Create table */ Fi = Vect_default_field_info(&Out, 1, NULL, GV_1TABLE); Vect_map_add_dblink(&Out, 1, NULL, Fi->table, "cat", Fi->database, Fi->driver); db_init_string(&sql); driver = db_start_driver_open_database(Fi->driver, Fi->database); if (driver == NULL) G_fatal_error(_("Unable to open database <%s> by driver <%s>"), Fi->database, Fi->driver); db_init_string(&tmp); if (deg_opt->answer) append_string(&tmp, deg_opt->answer); if (close_opt->answer) append_string(&tmp, close_opt->answer); if (betw_opt->answer) append_string(&tmp, betw_opt->answer); if (eigen_opt->answer) append_string(&tmp, eigen_opt->answer); sprintf(buf, "create table %s(cat integer%s)", Fi->table, db_get_string(&tmp)); db_set_string(&sql, buf); G_debug(2, db_get_string(&sql)); if (db_execute_immediate(driver, &sql) != DB_OK) { db_close_database_shutdown_driver(driver); G_fatal_error(_("Unable to create table: '%s'"), db_get_string(&sql)); } if (db_create_index2(driver, Fi->table, "cat") != DB_OK) G_warning(_("Cannot create index")); if (db_grant_on_table (driver, Fi->table, DB_PRIV_SELECT, DB_GROUP | DB_PUBLIC) != DB_OK) G_fatal_error(_("Cannot grant privileges on table <%s>"), Fi->table); db_begin_transaction(driver); Vect_copy_head_data(&In, &Out); Vect_hist_copy(&In, &Out); Vect_hist_command(&Out); Vect_net_build_graph(&In, mask_type, atoi(field_opt->answer), 0, afcol->answer, abcol->answer, NULL, geo, 0); graph = &(In.graph); nnodes = dglGet_NodeCount(graph); deg = closeness = betw = eigen = NULL; covered = (char *)G_calloc(nnodes + 1, sizeof(char)); if (!covered) G_fatal_error(_("Out of memory")); if (deg_opt->answer) { deg = (double *)G_calloc(nnodes + 1, sizeof(double)); if (!deg) G_fatal_error(_("Out of memory")); } if (close_opt->answer) { closeness = (double *)G_calloc(nnodes + 1, sizeof(double)); if (!closeness) G_fatal_error(_("Out of memory")); } if (betw_opt->answer) { betw = (double *)G_calloc(nnodes + 1, sizeof(double)); if (!betw) G_fatal_error(_("Out of memory")); } if (eigen_opt->answer) { eigen = (double *)G_calloc(nnodes + 1, sizeof(double)); if (!eigen) G_fatal_error(_("Out of memory")); } if (deg_opt->answer) { G_message(_("Computing degree centrality measure")); NetA_degree_centrality(graph, deg); } if (betw_opt->answer || close_opt->answer) { G_message(_("Computing betweenness and/or closeness centrality measure")); NetA_betweenness_closeness(graph, betw, closeness); if (closeness) for (i = 1; i <= nnodes; i++) closeness[i] /= (double)In.cost_multip; } if (eigen_opt->answer) { G_message(_("Computing eigenvector centrality measure")); NetA_eigenvector_centrality(graph, atoi(iter_opt->answer), atof(error_opt->answer), eigen); } nlines = Vect_get_num_lines(&In); G_message(_("Writing data into the table...")); G_percent_reset(); for (i = 1; i <= nlines; i++) { G_percent(i, nlines, 1); int type = Vect_read_line(&In, Points, Cats, i); if (type == GV_POINT && (!chcat || varray->c[i])) { int cat, node; if (!Vect_cat_get(Cats, layer, &cat)) continue; Vect_reset_cats(Cats); Vect_cat_set(Cats, 1, cat); Vect_write_line(&Out, type, Points, Cats); Vect_get_line_nodes(&In, i, &node, NULL); process_node(node, cat); covered[node] = 1; } } if (add_f->answer && !chcat) { max_cat = 0; for (i = 1; i <= nlines; i++) { Vect_read_line(&In, NULL, Cats, i); for (j = 0; j < Cats->n_cats; j++) if (Cats->cat[j] > max_cat) max_cat = Cats->cat[j]; } max_cat++; for (i = 1; i <= nnodes; i++) if (!covered[i]) { Vect_reset_cats(Cats); Vect_cat_set(Cats, 1, max_cat); NetA_add_point_on_node(&In, &Out, i, Cats); process_node(i, max_cat); max_cat++; } } db_commit_transaction(driver); db_close_database_shutdown_driver(driver); G_free(covered); if (deg) G_free(deg); if (closeness) G_free(closeness); if (betw) G_free(betw); if (eigen) G_free(eigen); Vect_build(&Out); Vect_close(&In); Vect_close(&Out); exit(EXIT_SUCCESS); }
int main(int argc, char *argv[]) { struct Map_info in, out, vis; struct GModule *module; /* GRASS module for parsing arguments */ struct Option *input, *output; /* The input map */ struct Option *coor, *ovis; char *mapset; struct Point *points; struct Line *lines; int num_points, num_lines; int n = 0; /* initialize GIS environment */ G_gisinit(argv[0]); /* reads grass env, stores program name to G_program_name() */ /* initialize module */ module = G_define_module(); module->keywords = _("vector, path, visibility"); module->description = _("Visibility graph construction."); /* define the arguments needed */ input = G_define_standard_option(G_OPT_V_INPUT); output = G_define_standard_option(G_OPT_V_OUTPUT); coor = G_define_option(); coor->key = "coordinate"; coor->key_desc = "x,y"; coor->type = TYPE_STRING; coor->required = NO; coor->multiple = YES; coor->description = _("One or more coordinates"); ovis = G_define_option(); ovis->key = "vis"; ovis->type = TYPE_STRING; ovis->required = NO; ovis->description = _("Add points after computing the vis graph"); /* options and flags parser */ if (G_parser(argc, argv)) exit(EXIT_FAILURE); Vect_check_input_output_name(input->answer, output->answer, GV_FATAL_EXIT); Vect_set_open_level(2); mapset = G_find_vector2(input->answer, NULL); /* finds the map */ if (mapset == NULL) G_fatal_error("Vector map <%s> not found", input->answer); if (Vect_open_old(&in, input->answer, mapset) < 1) /* opens the map */ G_fatal_error(_("Unable to open vector map <%s>"), G_fully_qualified_name(input->answer, mapset)); if (Vect_open_new(&out, output->answer, WITHOUT_Z) < 0) { Vect_close(&in); G_fatal_error(_("Unable to create vector map <%s>"), output->answer); } if (ovis->answer != NULL) { mapset = G_find_vector2(ovis->answer, NULL); if (Vect_open_old(&vis, ovis->answer, mapset) < 1) G_fatal_error(_("Unable to open vector map <%s>"), G_fully_qualified_name(ovis->answer, mapset)); if (Vect_copy_map_lines(&vis, &out) > 0) G_fatal_error(_("Unable to copy elements from vector map <%s>"), G_fully_qualified_name(ovis->answer, mapset)); } if (G_projection() == PROJECTION_LL) G_warning(_("Lat-long projection")); /* counting how many points and lines we have to allocate */ count(&in, &num_points, &num_lines); /* modify the number if we have new points to add */ if (coor->answers != NULL) num_points += count_new(coor->answers); /* and allocate */ points = G_malloc(num_points * sizeof(struct Point)); lines = G_malloc(num_lines * sizeof(struct Line)); /* and finally set the lines */ load_lines(&in, &points, &num_points, &lines, &num_lines); if (coor->answers != NULL) add_points(coor->answers, &points, &num_points); if (ovis->answer == NULL) construct_visibility(points, num_points, lines, num_lines, &out); else visibility_points(points, num_points, lines, num_lines, &out, n); G_free(points); G_free(lines); Vect_build(&out); Vect_close(&out); Vect_close(&in); exit(EXIT_SUCCESS); }
int main(int argc, char **argv) { struct Flag *printattributes, *topo_flag, *shell_flag; struct Option *opt1, *coords_opt, *maxdistance; struct Cell_head window; struct GModule *module; char *mapset; char *str; char buf[2000]; int i, j, level, width = 0, mwidth = 0, ret; double xval, yval, xres, yres, maxd, x; double EW_DIST1, EW_DIST2, NS_DIST1, NS_DIST2; char nsres[30], ewres[30]; char ch; /* Initialize the GIS calls */ G_gisinit(argv[0]); module = G_define_module(); module->keywords = _("vector, querying"); module->description = _("Queries a vector map layer at given locations."); opt1 = G_define_standard_option(G_OPT_V_MAP); opt1->multiple = YES; opt1->required = YES; coords_opt = G_define_option(); coords_opt->key = "east_north"; coords_opt->type = TYPE_DOUBLE; coords_opt->key_desc = "east,north"; coords_opt->required = NO; coords_opt->multiple = YES; coords_opt->label = _("Coordinates for query"); coords_opt->description = _("If not given reads from standard input"); maxdistance = G_define_option(); maxdistance->type = TYPE_DOUBLE; maxdistance->key = "distance"; maxdistance->answer = "0"; maxdistance->multiple = NO; maxdistance->description = _("Query threshold distance"); topo_flag = G_define_flag(); topo_flag->key = 'd'; topo_flag->description = _("Print topological information (debugging)"); printattributes = G_define_flag(); printattributes->key = 'a'; printattributes->description = _("Print attribute information"); shell_flag = G_define_flag(); shell_flag->key = 'g'; shell_flag->description = _("Print the stats in shell script style"); if ((argc > 1 || !vect) && G_parser(argc, argv)) exit(EXIT_FAILURE); if (opt1->answers && opt1->answers[0]) vect = opt1->answers; maxd = atof(maxdistance->answer); /* * fprintf(stdout, maxdistance->answer); * fprintf(stdout, "Maxd is %f", maxd); * fprintf(stdout, xcoord->answer); * fprintf(stdout, "xval is %f", xval); * fprintf(stdout, ycoord->answer); * fprintf(stdout, "yval is %f", yval); */ if (maxd == 0.0) { G_get_window(&window); x = window.proj; G_format_resolution(window.ew_res, ewres, x); G_format_resolution(window.ns_res, nsres, x); EW_DIST1 = G_distance(window.east, window.north, window.west, window.north); /* EW Dist at South Edge */ EW_DIST2 = G_distance(window.east, window.south, window.west, window.south); /* NS Dist at East edge */ NS_DIST1 = G_distance(window.east, window.north, window.east, window.south); /* NS Dist at West edge */ NS_DIST2 = G_distance(window.west, window.north, window.west, window.south); xres = ((EW_DIST1 + EW_DIST2) / 2) / window.cols; yres = ((NS_DIST1 + NS_DIST2) / 2) / window.rows; if (xres > yres) maxd = xres; else maxd = yres; } /* Look at maps given on command line */ if (vect) { for (i = 0; vect[i]; i++) ; nvects = i; Map = (struct Map_info *)G_malloc(nvects * sizeof(struct Map_info)); width = mwidth = 0; for (i = 0; i < nvects; i++) { str = strchr(vect[i], '@'); if (str) j = str - vect[i]; else j = strlen(vect[i]); if (j > width) width = j; mapset = G_find_vector2(vect[i], ""); if (!mapset) G_fatal_error(_("Vector map <%s> not found"), vect[i]); j = strlen(mapset); if (j > mwidth) mwidth = j; level = Vect_open_old(&Map[i], vect[i], mapset); if (level < 2) G_fatal_error(_("You must build topology on vector map <%s>"), vect[i]); G_verbose_message(_("Building spatial index...")); Vect_build_spatial_index(&Map[i]); } } if (!coords_opt->answer) { /* if coords are not given on command line, read them from stdin */ setvbuf(stdin, NULL, _IOLBF, 0); setvbuf(stdout, NULL, _IOLBF, 0); while (fgets(buf, sizeof(buf), stdin) != NULL) { ret = sscanf(buf, "%lf%c%lf", &xval, &ch, &yval); if (ret == 3 && (ch == ',' || ch == ' ' || ch == '\t')) { what(xval, yval, maxd, width, mwidth, topo_flag->answer, printattributes->answer, shell_flag->answer); } else { G_warning(_("Unknown input format, skipping: '%s'"), buf); continue; } } } else { /* use coords given on command line */ for (i = 0; coords_opt->answers[i] != NULL; i += 2) { xval = atof(coords_opt->answers[i]); yval = atof(coords_opt->answers[i + 1]); what(xval, yval, maxd, width, mwidth, topo_flag->answer, printattributes->answer, shell_flag->answer); } } for (i = 0; i < nvects; i++) Vect_close(&Map[i]); exit(EXIT_SUCCESS); }
/* CALCULATE THE COORDINATES OF THE TOP LEFT CORNER OF THE SAMPLING UNITS */ static int calc_unit_loc(double radius, int top, int bot, int left, int right, double ratio, int u_w, int u_l, int method, double intv, int num, int h_d, int v_d, double *ux, double *uy, int *sites, double startx, int starty, int fmask, double nx, double x, double y) { char *sites_mapset, sites_file_name[GNAME_MAX], *cmd; struct Map_info Map; struct Cell_head region; double D_u_to_a_col(), D_u_to_a_row(); int i, j, k, cnt = 0, w_w = right - left, w_l = bot - top, exp1, exp2, dx = w_w, dy = w_l, l, t, left1 = left, top1 = top, n, tmp, ulrow, ulcol, *row_buf, lap = 0; static struct line_pnts *Points; struct line_cats *Cats; int ltype; /* VARIABLES: UNITS FOR ALL DIMENSION VARIABLES IN THIS ROUTINE ARE IN PIXELS top = sampling frame top row in pixels bot = sampling frame bottom row in pixels left = sampling frame left in pixels right = sampling frame right in pixels left1 = col of left side of sampling frame or each stratum top1 = row of top side of sampling frame or each stratum l = random # cols to be added to left1 r = random # rows to be added to top1 ratio = u_w = sampling unit width in pixels u_l = sampling unit length in pixels method = method of sampling unit distribution (1-5) intv = interval between sampling units when method=3 num = number of sampling units h_d = number of horizontal strata v_d = number of vertical strata ux = uy = sites = startx = col of UL corner starting pt for strata starty = row of UL corner starting pt for strata dx = number of cols per stratum dy = number of rows per stratum w_w = width of sampling frame in cols w_l = length of sampling frame in rows */ /* if user hits Ctrl+C, abort this calculation */ setjmp(jmp); if (tag) { tag = 0; return 0; } /* for syst. noncontig. distribution */ if (method == 3) { u_w += intv; u_l += intv; } /* for stratified random distribution */ if (method == 4) { dx = (int)((double)(w_w - startx) / (double)(h_d)); dy = (int)((double)(w_l - starty) / (double)(v_d)); } /* for syst. contig. and noncontig. distribution */ else if (method == 2 || method == 3) { if (nx >= num) dx = (w_w - startx) - (num - 1) * u_w; else { dx = (w_w - startx) - (nx - 1) * u_w; dy = (w_l - starty) - (num / nx - 1) * u_l; } } if (10 > (exp1 = (int)pow(10.0, ceil(log10((double)(dx - u_w + 10)))))) exp1 = 10; if (10 > (exp2 = (int)pow(10.0, ceil(log10((double)(dy - u_l + 10)))))) exp2 = 10; /* for random nonoverlapping and stratified random */ if (method == 1 || method == 4) { fprintf(stderr, "\n 'Ctrl+C' and choose fewer units if the requested number"); fprintf(stderr, " is not reached\n"); for (i = 0; i < num; i++) { /* if Cntl+C */ if (signal(SIGINT, SIG_IGN) != SIG_IGN) signal(SIGINT, f); /* for stratified random distribution */ if (method == 4) { j = 0; if (n = i % h_d) left1 += dx; else { left1 = left + startx; if (i < h_d) top1 = top + starty; else top1 += dy; } get_rd(exp1, exp2, dx, dy, u_w, u_l, &l, &t); } /* for random nonoverlapping distribution */ if (method == 1) { /* get random numbers */ back: get_rd(exp1, exp2, dx, dy, u_w, u_l, &l, &t); if (left1 + l + u_w > right || top1 + t + u_l > bot || left1 + l < left || top1 + t < top) goto back; /* if there is a mask, check to see that the unit will be within the mask area */ if (fmask > 0) { row_buf = Rast_allocate_c_buf(); Rast_get_c_row_nomask(fmask, row_buf, t + top1); if (! (*(row_buf + l + left1) && *(row_buf + l + left1 + u_w - 1))) goto back; Rast_zero_c_buf(row_buf); Rast_get_c_row_nomask(fmask, row_buf, t + top1 + u_l - 1); if (! (*(row_buf + l + left1) && *(row_buf + l + left1 + u_w - 1))) goto back; G_free(row_buf); } /* check for sampling unit overlap */ lap = 0; for (j = 0; j < cnt; j++) { if (overlap (l + left1, t + top1, (int)ux[j], (int)uy[j], u_w, u_l)) lap = 1; } if (lap) goto back; cnt++; } /* fill the array of upper left coordinates for the sampling units */ *(ux + i) = l + left1; *(uy + i) = t + top1; /* draw the sampling units on the screen */ R_open_driver(); R_standard_color(D_translate_color("red")); if (radius) draw_circle((int)((double)(ux[i]) / x), (int)((double)(uy[i]) / y), (int)((double)(ux[i] + u_w) / x), (int)((double)(uy[i] + u_l) / y), 3); else draw_box((int)((double)(ux[i]) / x), (int)((double)(uy[i]) / y), (int)((double)(ux[i] + u_w) / x), (int)((double)(uy[i] + u_l) / y), 1); R_close_driver(); fprintf(stderr, " Distributed unit %4d of %4d requested\r", i + 1, num); } } /* for syst. contig. & syst. noncontig. */ else if (method == 2 || method == 3) { for (i = 0; i < num; i++) { *(ux + i) = left + startx + u_w * (i - nx * floor((double)i / nx)); *(uy + i) = top + starty + u_l * floor((double)i / nx); } } /* for centered over sites */ else if (method == 5) { sites_mapset = G_ask_vector_old(" Enter name of vector points map", sites_file_name); if (sites_mapset == NULL) { G_system("d.frame -e"); exit(0); } if (Vect_open_old(&Map, sites_file_name, sites_mapset) < 0) G_fatal_error(_("Unable to open vector map <%s>"), sites_file_name); /* fprintf(stderr, "\n Can't open vector points file %s\n", sites_file_name); */ *sites = 0; i = 0; n = 0; Points = Vect_new_line_struct(); /* init line_pnts struct */ Cats = Vect_new_cats_struct(); while (1) { ltype = Vect_read_next_line(&Map, Points, Cats); if (ltype == -1) G_fatal_error(_("Cannot read vector")); if (ltype == -2) break; /* EOF */ /* point features only. (GV_POINTS is pts AND centroids, GV_POINT is just pts) */ if (!(ltype & GV_POINT)) continue; ulcol = ((int)(D_u_to_a_col(Points->x[0]))) + 1 - u_w / 2; ulrow = ((int)(D_u_to_a_row(Points->y[0]))) + 1 - u_l / 2; if (ulcol <= left || ulrow <= top || ulcol + u_w - 1 > right || ulrow + u_l - 1 > bot) { fprintf(stderr, " No sampling unit over site %d at east=%8.1f north=%8.1f\n", n + 1, Points->x[0], Points->y[0]); fprintf(stderr, " as it would extend outside the map\n"); } else { *(ux + i) = ulcol - 1; *(uy + i) = ulrow - 1; i++; } n++; if (n > 250) G_fatal_error ("There are more than the maximum of 250 sites\n"); } fprintf(stderr, " Total sites with sampling units = %d\n", i); *sites = i; cmd = G_malloc(100); sprintf(cmd, "d.vect %s color=black", sites_file_name); G_system(cmd); G_free(cmd); Vect_close(&Map); G_free(Points); G_free(Cats); } return 1; }
int read_vpoints(char *name, char *mapset) { char fullname[100]; char buf[1024]; char *key, *data; double width, size, scale, rotate; int itmp, vec; int r, g, b; int ret; struct Map_info Map; vector_alloc(); /* allocate space */ sprintf(fullname, "%s in %s", name, mapset); Vect_set_open_level(2); Vect_set_fatal_error(GV_FATAL_PRINT); if (2 > Vect_open_old(&Map, name, mapset)) { error(fullname, "", "can't open vector map"); gobble_input(); return 0; } Vect_close(&Map); vec = vector.count; vector.layer[vec].type = VPOINTS; vector.layer[vec].name = G_store(name); vector.layer[vec].mapset = G_store(mapset); vector.layer[vec].ltype = GV_POINT; vector.layer[vec].masked = 0; vector.layer[vec].field = 1; vector.layer[vec].cats = NULL; vector.layer[vec].where = NULL; vector.layer[vec].width = 1.; set_color(&(vector.layer[vec].color), 0, 0, 0); set_color(&(vector.layer[vec].fcolor), 255, 0, 0); vector.layer[vec].rgbcol = NULL; vector.layer[vec].label = NULL; vector.layer[vec].lpos = -1; vector.layer[vec].symbol = G_store("basic/diamond"); vector.layer[vec].size = 6.0; vector.layer[vec].sizecol = NULL; vector.layer[vec].scale = 1.0; vector.layer[vec].rotate = 0.0; vector.layer[vec].rotcol = NULL; vector.layer[vec].epstype = 0; while (input(2, buf, help)) { if (!key_data(buf, &key, &data)) continue; if (KEY("masked")) { vector.layer[vec].masked = yesno(key, data); if (vector.layer[vec].masked) PS.mask_needed = 1; continue; } if (KEY("type")) { G_strip(data); vector.layer[vec].ltype = 0; if (strstr(data, "point")) vector.layer[vec].ltype |= GV_POINT; if (strstr(data, "centroid")) vector.layer[vec].ltype |= GV_CENTROID; continue; } if (KEY("layer")) { G_strip(data); vector.layer[vec].field = atoi(data); continue; } if (KEY("cats")) { G_strip(data); vector.layer[vec].cats = G_store(data); continue; } if (KEY("where")) { G_strip(data); vector.layer[vec].where = G_store(data); continue; } if (KEY("width")) { width = -1.; *mapset = 0; if (sscanf(data, "%lf%s", &width, mapset) < 1 || width < 0.) { width = 1.; error(key, data, "illegal width (vpoints)"); continue; } if (mapset[0] == 'i') width = width / 72.; vector.layer[vec].width = width; continue; } if (KEY("color")) { ret = G_str_to_color(data, &r, &g, &b); if (ret == 1) set_color(&(vector.layer[vec].color), r, g, b); else if (ret == 2) unset_color(&(vector.layer[vec].color)); else error(key, data, "illegal color request"); continue; } if (KEY("fcolor")) { /* fill color */ ret = G_str_to_color(data, &r, &g, &b); if (ret == 1) set_color(&(vector.layer[vec].fcolor), r, g, b); else if (ret == 2) unset_color(&(vector.layer[vec].fcolor)); else error(key, data, "illegal color request (vpoints)"); continue; } if (KEY("rgbcolumn")) { G_strip(data); vector.layer[vec].rgbcol = G_store(data); continue; } if (KEY("label")) { /* map legend label */ G_strip(data); vector.layer[vec].label = G_store(data); continue; } if (KEY("lpos")) { if (sscanf(data, "%d", &itmp) < 1 || itmp < 0) { itmp = -1; error(key, data, "illegal lpos"); continue; } vector.layer[vec].lpos = itmp; continue; } if (KEY("symbol")) { /* TODO: test here if isymbol exists */ vector.layer[vec].symbol = G_store(data); continue; } if (KEY("eps")) { char *cc; G_chop(data); vector.layer[vec].epspre = G_store(data); /* epstype: 0 - no eps, 1 - common eps, 2 - eps for each category */ vector.layer[vec].epstype = 1; /* find dynamic filename by cat number character */ /* pre is filename before the $, suf is filename after the $ */ cc = (char *)strchr(vector.layer[vec].epspre, '$'); if (cc != NULL) { *cc = '\0'; vector.layer[vec].epssuf = G_store(cc + sizeof(char)); vector.layer[vec].epstype = 2; G_debug(2, "epstype=%d, pre=[%s], suf=[%s]", vector.layer[vec].epstype, vector.layer[vec].epspre, vector.layer[vec].epssuf); } else { G_debug(2, "epstype=%d, eps file=[%s]", vector.layer[vec].epstype, vector.layer[vec].epspre); } continue; } if (KEY("size")) { if (sscanf(data, "%lf", &size) != 1 || size <= 0.0) { size = 1.0; error(key, data, "illegal size request (vpoints)"); } vector.layer[vec].size = size; continue; } /* GRASS 6.3: sizecol renamed to sizecolumn remove sizecol test and the warning in GRASS7 */ if (KEY("sizecol")) { G_warning(_("The mapping instruction <%s> will be renamed to <%s> " "in future versions of GRASS. Please use <%s> instead."), "sizecol", "sizecolumn", "sizecolumn"); } if (KEY("sizecol") || KEY("sizecolumn")) { G_strip(data); vector.layer[vec].sizecol = G_store(data); continue; } if (KEY("scale")) { if (sscanf(data, "%lf", &scale) != 1 || scale <= 0.0) { scale = 1.0; error(key, data, "illegal scale request (vpoints)"); } vector.layer[vec].scale = scale; continue; } if (KEY("rotate")) { if (sscanf(data, "%lf", &rotate) != 1) { rotate = 0.0; error(key, data, "illegal rotation request (vpoints)"); } vector.layer[vec].rotate = rotate; continue; } if (KEY("rotatecolumn")) { G_strip(data); vector.layer[vec].rotcol = G_store(data); continue; } error(key, "", "illegal request (vpoints)"); } vector.count++; return 1; }
void QgsGrassPlugin::newVector() { // QgsDebugMsg("entered."); if ( QgsGrassEdit::isRunning() ) { QMessageBox::warning( 0, tr( "Warning" ), tr( "GRASS Edit is already running." ) ); return; } bool ok; QString name; QgsGrassElementDialog dialog( qGisInterface->mainWindow() ); name = dialog.getItem( "vector", tr( "New vector name" ), tr( "New vector name" ), "", "", &ok ); if ( !ok ) return; // Create new map QgsGrass::setMapset( QgsGrass::getDefaultGisdbase(), QgsGrass::getDefaultLocation(), QgsGrass::getDefaultMapset() ); try { struct Map_info Map; Vect_open_new( &Map, name.toUtf8().data(), 0 ); #if defined(GRASS_VERSION_MAJOR) && defined(GRASS_VERSION_MINOR) && \ ( ( GRASS_VERSION_MAJOR == 6 && GRASS_VERSION_MINOR >= 4 ) || GRASS_VERSION_MAJOR > 6 ) Vect_build( &Map ); #else Vect_build( &Map, stderr ); #endif Vect_set_release_support( &Map ); Vect_close( &Map ); } catch ( QgsGrass::Exception &e ) { QMessageBox::warning( 0, tr( "Warning" ), tr( "Cannot create new vector: %1" ).arg( e.what() ) ); return; } // Open in GRASS vector provider QString uri = QgsGrass::getDefaultGisdbase() + "/" + QgsGrass::getDefaultLocation() + "/" + QgsGrass::getDefaultMapset() + "/" + name + "/0_point"; QgsVectorLayer* layer = new QgsVectorLayer( uri, name, "grass" ); if ( !layer ) { QMessageBox::warning( 0, tr( "Warning" ), tr( "New vector created but cannot be opened by data provider." ) ); return; } QgsGrassEdit *ed = new QgsGrassEdit( qGisInterface, layer, true, qGisInterface->mainWindow(), Qt::Dialog ); if ( ed->isValid() ) { ed->show(); mCanvas->refresh(); } else { QMessageBox::warning( 0, tr( "Warning" ), tr( "Cannot start editing." ) ); delete ed; } #if 0 if ( !( mProvider->startEdit() ) ) { QMessageBox::warning( 0, tr( "Warning" ), tr( "Cannot open vector for update." ) ); return; } #endif }
int main(int argc, char *argv[]) { int i, j, nlines, type, field, cat; int fd; /* struct Categories RCats; *//* TODO */ struct Cell_head window; RASTER_MAP_TYPE out_type; CELL *cell; DCELL *dcell; double drow, dcol; char buf[2000]; struct Option *vect_opt, *rast_opt, *field_opt, *col_opt, *where_opt; int Cache_size; struct order *cache; int cur_row; struct GModule *module; struct Map_info Map; struct line_pnts *Points; struct line_cats *Cats; int point; int point_cnt; /* number of points in cache */ int outside_cnt; /* points outside region */ int nocat_cnt; /* points inside region but without category */ int dupl_cnt; /* duplicate categories */ struct bound_box box; int *catexst, *cex; struct field_info *Fi; dbString stmt; dbDriver *driver; int select, norec_cnt, update_cnt, upderr_cnt, col_type; G_gisinit(argv[0]); module = G_define_module(); G_add_keyword(_("vector")); G_add_keyword(_("raster")); G_add_keyword(_("position")); G_add_keyword(_("querying")); G_add_keyword(_("attribute table")); module->description = _("Uploads raster values at positions of vector points to the table."); vect_opt = G_define_standard_option(G_OPT_V_INPUT); vect_opt->key = "vector"; vect_opt->description = _("Name of input vector points map for which to edit attribute table"); rast_opt = G_define_standard_option(G_OPT_R_INPUT); rast_opt->key = "raster"; rast_opt->description = _("Name of existing raster map to be queried"); field_opt = G_define_standard_option(G_OPT_V_FIELD); col_opt = G_define_option(); col_opt->key = "column"; col_opt->type = TYPE_STRING; col_opt->required = YES; col_opt->description = _("Column name (will be updated by raster values)"); where_opt = G_define_standard_option(G_OPT_DB_WHERE); if (G_parser(argc, argv)) exit(EXIT_FAILURE); field = atoi(field_opt->answer); db_init_string(&stmt); Points = Vect_new_line_struct(); Cats = Vect_new_cats_struct(); G_get_window(&window); Vect_region_box(&window, &box); /* T and B set to +/- PORT_DOUBLE_MAX */ /* Open vector */ Vect_set_open_level(2); Vect_open_old(&Map, vect_opt->answer, ""); Fi = Vect_get_field(&Map, field); if (Fi == NULL) G_fatal_error(_("Database connection not defined for layer %d"), field); /* Open driver */ driver = db_start_driver_open_database(Fi->driver, Fi->database); if (driver == NULL) { G_fatal_error(_("Unable to open database <%s> by driver <%s>"), Fi->database, Fi->driver); } /* Open raster */ fd = Rast_open_old(rast_opt->answer, ""); out_type = Rast_get_map_type(fd); /* TODO: Later possibly category labels */ /* if ( Rast_read_cats (name, "", &RCats) < 0 ) G_fatal_error ( "Cannot read category file"); */ /* Check column type */ col_type = db_column_Ctype(driver, Fi->table, col_opt->answer); if (col_type == -1) G_fatal_error(_("Column <%s> not found"), col_opt->answer); if (col_type != DB_C_TYPE_INT && col_type != DB_C_TYPE_DOUBLE) G_fatal_error(_("Column type not supported")); if (out_type == CELL_TYPE && col_type == DB_C_TYPE_DOUBLE) G_warning(_("Raster type is integer and column type is float")); if (out_type != CELL_TYPE && col_type == DB_C_TYPE_INT) G_warning(_("Raster type is float and column type is integer, some data lost!!")); /* Read vector points to cache */ Cache_size = Vect_get_num_primitives(&Map, GV_POINT); /* Note: Some space may be wasted (outside region or no category) */ cache = (struct order *)G_calloc(Cache_size, sizeof(struct order)); point_cnt = outside_cnt = nocat_cnt = 0; nlines = Vect_get_num_lines(&Map); G_debug(1, "Reading %d vector features fom map", nlines); for (i = 1; i <= nlines; i++) { type = Vect_read_line(&Map, Points, Cats, i); G_debug(4, "line = %d type = %d", i, type); /* check type */ if (!(type & GV_POINT)) continue; /* Points only */ /* check region */ if (!Vect_point_in_box(Points->x[0], Points->y[0], 0.0, &box)) { outside_cnt++; continue; } Vect_cat_get(Cats, field, &cat); if (cat < 0) { /* no category of given field */ nocat_cnt++; continue; } G_debug(4, " cat = %d", cat); /* Add point to cache */ drow = Rast_northing_to_row(Points->y[0], &window); dcol = Rast_easting_to_col(Points->x[0], &window); /* a special case. * if north falls at southern edge, or east falls on eastern edge, * the point will appear outside the window. * So, for these edges, bring the point inside the window */ if (drow == window.rows) drow--; if (dcol == window.cols) dcol--; cache[point_cnt].row = (int)drow; cache[point_cnt].col = (int)dcol; cache[point_cnt].cat = cat; cache[point_cnt].count = 1; point_cnt++; } Vect_set_db_updated(&Map); Vect_hist_command(&Map); Vect_close(&Map); G_debug(1, "Read %d vector points", point_cnt); /* Cache may contain duplicate categories, sort by cat, find and remove duplicates * and recalc count and decrease point_cnt */ qsort(cache, point_cnt, sizeof(struct order), by_cat); G_debug(1, "Points are sorted, starting duplicate removal loop"); for (i = 0, j = 1; j < point_cnt; j++) if (cache[i].cat != cache[j].cat) cache[++i] = cache[j]; else cache[i].count++; point_cnt = i + 1; G_debug(1, "%d vector points left after removal of duplicates", point_cnt); /* Report number of points not used */ if (outside_cnt) G_warning(_("%d points outside current region were skipped"), outside_cnt); if (nocat_cnt) G_warning(_("%d points without category were skipped"), nocat_cnt); /* Sort cache by current region row */ qsort(cache, point_cnt, sizeof(struct order), by_row); /* Allocate space for raster row */ if (out_type == CELL_TYPE) cell = Rast_allocate_c_buf(); else dcell = Rast_allocate_d_buf(); /* Extract raster values from file and store in cache */ G_debug(1, "Extracting raster values"); cur_row = -1; for (point = 0; point < point_cnt; point++) { if (cache[point].count > 1) continue; /* duplicate cats */ if (cur_row != cache[point].row) { if (out_type == CELL_TYPE) Rast_get_c_row(fd, cell, cache[point].row); else Rast_get_d_row(fd, dcell, cache[point].row); } cur_row = cache[point].row; if (out_type == CELL_TYPE) { cache[point].value = cell[cache[point].col]; } else { cache[point].dvalue = dcell[cache[point].col]; } } /* point loop */ /* Update table from cache */ G_debug(1, "Updating db table"); /* select existing categories to array (array is sorted) */ select = db_select_int(driver, Fi->table, Fi->key, NULL, &catexst); db_begin_transaction(driver); norec_cnt = update_cnt = upderr_cnt = dupl_cnt = 0; for (point = 0; point < point_cnt; point++) { if (cache[point].count > 1) { G_warning(_("More points (%d) of category %d, value set to 'NULL'"), cache[point].count, cache[point].cat); dupl_cnt++; } /* category exist in DB ? */ cex = (int *)bsearch((void *)&(cache[point].cat), catexst, select, sizeof(int), srch_cat); if (cex == NULL) { /* cat does not exist in DB */ norec_cnt++; G_warning(_("No record for category %d in table <%s>"), cache[point].cat, Fi->table); continue; } sprintf(buf, "update %s set %s = ", Fi->table, col_opt->answer); db_set_string(&stmt, buf); if (out_type == CELL_TYPE) { if (cache[point].count > 1 || Rast_is_c_null_value(&cache[point].value)) { sprintf(buf, "NULL"); } else { sprintf(buf, "%d ", cache[point].value); } } else { /* FCELL or DCELL */ if (cache[point].count > 1 || Rast_is_d_null_value(&cache[point].dvalue)) { sprintf(buf, "NULL"); } else { sprintf(buf, "%.10f", cache[point].dvalue); } } db_append_string(&stmt, buf); sprintf(buf, " where %s = %d", Fi->key, cache[point].cat); db_append_string(&stmt, buf); /* user provides where condition: */ if (where_opt->answer) { sprintf(buf, " AND %s", where_opt->answer); db_append_string(&stmt, buf); } G_debug(3, db_get_string(&stmt)); /* Update table */ if (db_execute_immediate(driver, &stmt) == DB_OK) { update_cnt++; } else { upderr_cnt++; } } G_debug(1, "Committing DB transaction"); db_commit_transaction(driver); G_free(catexst); db_close_database_shutdown_driver(driver); db_free_string(&stmt); /* Report */ G_message(_("%d categories loaded from table"), select); G_message(_("%d categories loaded from vector"), point_cnt); G_message(_("%d categories from vector missing in table"), norec_cnt); G_message(_("%d duplicate categories in vector"), dupl_cnt); if (!where_opt->answer) G_message(_("%d records updated"), update_cnt); G_message(_("%d update errors"), upderr_cnt); exit(EXIT_SUCCESS); }
int main(int argc, char *argv[]) { struct GModule *module; struct Option *in_opt, *layer_opt, *out_opt, *length_opt, *units_opt, *vertices_opt; struct Map_info In, Out; struct line_pnts *Points, *Points2; struct line_cats *Cats; int line, nlines, layer; double length = -1; int vertices = 0; double (*line_length) (); int latlon = 0; G_gisinit(argv[0]); module = G_define_module(); G_add_keyword(_("vector")); G_add_keyword(_("geometry")); module->description = _("Splits vector lines to shorter segments."); in_opt = G_define_standard_option(G_OPT_V_INPUT); layer_opt = G_define_standard_option(G_OPT_V_FIELD_ALL); out_opt = G_define_standard_option(G_OPT_V_OUTPUT); length_opt = G_define_option(); length_opt->key = "length"; length_opt->type = TYPE_DOUBLE; length_opt->required = NO; length_opt->multiple = NO; length_opt->description = _("Maximum segment length"); units_opt = G_define_option(); units_opt->key = "units"; units_opt->type = TYPE_STRING; units_opt->required = NO; units_opt->multiple = NO; units_opt->options = "meters,kilometers,feet,miles,nautmiles"; units_opt->answer = "meters"; units_opt->description = _("Length units"); vertices_opt = G_define_option(); vertices_opt->key = "vertices"; vertices_opt->type = TYPE_INTEGER; vertices_opt->required = NO; vertices_opt->multiple = NO; vertices_opt->description = _("Maximum number of vertices in segment"); if (G_parser(argc, argv)) exit(EXIT_FAILURE); if ((length_opt->answer && vertices_opt->answer) || !(length_opt->answer || vertices_opt->answer)) G_fatal_error(_("Use either length or vertices")); line_length = NULL; if (length_opt->answer) { length = atof(length_opt->answer); if (length <= 0) G_fatal_error(_("Length must be positive but is %g"), length); /* convert length to meters */ if (strcmp(units_opt->answer, "meters") == 0) /* do nothing */ ; else if (strcmp(units_opt->answer, "kilometers") == 0) length *= FROM_KILOMETERS; else if (strcmp(units_opt->answer, "feet") == 0) length *= FROM_FEET; else if (strcmp(units_opt->answer, "miles") == 0) length *= FROM_MILES; else if (strcmp(units_opt->answer, "nautmiles") == 0) length *= FROM_NAUTMILES; else G_fatal_error(_("Unknown unit %s"), units_opt->answer); /* set line length function */ if ((latlon = (G_projection() == PROJECTION_LL)) == 1) line_length = Vect_line_geodesic_length; else { double factor; line_length = Vect_line_length; /* convert length to map units */ if ((factor = G_database_units_to_meters_factor()) == 0) G_fatal_error(_("Can not get projection units")); else { /* meters to units */ length = length / factor; } } G_verbose_message(_("length in %s: %g"), (latlon ? "meters" : "map units"), length); } if (vertices_opt->answer) { vertices = atoi(vertices_opt->answer); if (vertices < 2) G_fatal_error(_("Number of vertices must be at least 2")); } Vect_set_open_level(2); Vect_open_old2(&In, in_opt->answer, "", layer_opt->answer); layer = Vect_get_field_number(&In, layer_opt->answer); Vect_open_new(&Out, out_opt->answer, Vect_is_3d(&In)); Vect_copy_head_data(&In, &Out); Vect_hist_copy(&In, &Out); Vect_hist_command(&Out); Vect_copy_tables(&In, &Out, layer); Points = Vect_new_line_struct(); Points2 = Vect_new_line_struct(); Cats = Vect_new_cats_struct(); nlines = Vect_get_num_lines(&In); for (line = 1; line <= nlines; line++) { int ltype; G_percent(line, nlines, 1); if (!Vect_line_alive(&In, line)) continue; ltype = Vect_read_line(&In, Points, Cats, line); if (layer != -1 && !Vect_cat_get(Cats, layer, NULL)) continue; if (ltype & GV_LINES) { if (length > 0) { double l, from, to, step; l = line_length(Points); if (l <= length) { Vect_write_line(&Out, ltype, Points, Cats); } else { int n, i; n = ceil(l / length); if (latlon) l = Vect_line_length(Points); step = l / n; from = 0.; for (i = 0; i < n; i++) { int ret; double x, y, z; if (i == n - 1) { to = l; /* to be sure that it goes to end */ } else { to = from + step; } ret = Vect_line_segment(Points, from, to, Points2); if (ret == 0) { G_warning(_("Unable to make line segment: %f - %f (line length = %f)"), from, to, l); continue; } /* To be sure that the coordinates are identical */ if (i > 0) { Points2->x[0] = x; Points2->y[0] = y; Points2->z[0] = z; } if (i == n - 1) { Points2->x[Points2->n_points - 1] = Points->x[Points->n_points - 1]; Points2->y[Points2->n_points - 1] = Points->y[Points->n_points - 1]; Points2->z[Points2->n_points - 1] = Points->z[Points->n_points - 1]; } Vect_write_line(&Out, ltype, Points2, Cats); /* last point */ x = Points2->x[Points2->n_points - 1]; y = Points2->y[Points2->n_points - 1]; z = Points2->z[Points2->n_points - 1]; from += step; } } } else { int start = 0; /* number of coordinates written */ while (start < Points->n_points - 1) { int i, v; Vect_reset_line(Points2); for (i = 0; i < vertices; i++) { v = start + i; if (v == Points->n_points) break; Vect_append_point(Points2, Points->x[v], Points->y[v], Points->z[v]); } Vect_write_line(&Out, ltype, Points2, Cats); start = v; } } } else { Vect_write_line(&Out, ltype, Points, Cats); } } Vect_close(&In); Vect_build(&Out); Vect_close(&Out); exit(EXIT_SUCCESS); }
bool GRASS_EXPORT QgsGrass::mapRegion( int type, QString gisbase, QString location, QString mapset, QString map, struct Cell_head *window ) { QgsDebugMsg( "entered." ); QgsDebugMsg( QString( "map = %1" ).arg( map ) ); QgsDebugMsg( QString( "mapset = %1" ).arg( mapset ) ); QgsGrass::setLocation( gisbase, location ); if ( type == Raster ) { if ( G_get_cellhd( map.toUtf8().data(), mapset.toUtf8().data(), window ) < 0 ) { QMessageBox::warning( 0, QObject::tr( "Warning" ), QObject::tr( "Cannot read raster map region" ) ); return false; } } else if ( type == Vector ) { // Get current projection region( gisbase, location, mapset, window ); struct Map_info Map; int level = Vect_open_old_head( &Map, map.toUtf8().data(), mapset.toUtf8().data() ); if ( level < 2 ) { QMessageBox::warning( 0, QObject::tr( "Warning" ), QObject::tr( "Cannot read vector map region" ) ); return false; } BOUND_BOX box; Vect_get_map_box( &Map, &box ); window->north = box.N; window->south = box.S; window->west = box.W; window->east = box.E; window->top = box.T; window->bottom = box.B; // Is this optimal ? window->ns_res = ( window->north - window->south ) / 1000; window->ew_res = window->ns_res; if ( window->top > window->bottom ) { window->tb_res = ( window->top - window->bottom ) / 10; } else { window->top = window->bottom + 1; window->tb_res = 1; } G_adjust_Cell_head3( window, 0, 0, 0 ); Vect_close( &Map ); } else if ( type == Region ) { if ( G__get_window( window, ( char * ) "windows", map.toUtf8().data(), mapset.toUtf8().data() ) != NULL ) { QMessageBox::warning( 0, QObject::tr( "Warning" ), QObject::tr( "Cannot read region" ) ); return false; } } return true; }
int execute_random(struct rr_state *theState) { long nt; long nc; struct Cell_head window; int nrows, ncols, row, col; int infd, cinfd, outfd; struct Map_info Out; struct field_info *fi; dbTable *table; dbColumn *column; dbString sql; dbDriver *driver; struct line_pnts *Points; struct line_cats *Cats; int cat; RASTER_MAP_TYPE type; int do_check; G_get_window(&window); nrows = Rast_window_rows(); ncols = Rast_window_cols(); /* open the data files, input raster should be set-up already */ if ((infd = theState->fd_old) < 0) G_fatal_error(_("Unable to open raster map <%s>"), theState->inraster); if (theState->docover == TRUE) { if ((cinfd = theState->fd_cold) < 0) G_fatal_error(_("Unable to open raster map <%s>"), theState->inrcover); } if (theState->outraster != NULL) { if (theState->docover == TRUE) type = theState->cover.type; else type = theState->buf.type; outfd = Rast_open_new(theState->outraster, type); theState->fd_new = outfd; } if (theState->outvector) { if (Vect_open_new(&Out, theState->outvector, theState->z_geometry) < 0) G_fatal_error(_("Unable to create vector map <%s>"), theState->outvector); Vect_hist_command(&Out); fi = Vect_default_field_info(&Out, 1, NULL, GV_1TABLE); driver = db_start_driver_open_database(fi->driver, Vect_subst_var(fi->database, &Out)); if (!driver) G_fatal_error(_("Unable to open database <%s> by driver <%s>"), Vect_subst_var(fi->database, &Out), fi->driver); db_set_error_handler_driver(driver); Vect_map_add_dblink(&Out, 1, NULL, fi->table, GV_KEY_COLUMN, fi->database, fi->driver); if (theState->docover == TRUE) table = db_alloc_table(3); else table = db_alloc_table(2); db_set_table_name(table, fi->table); column = db_get_table_column(table, 0); db_set_column_name(column, GV_KEY_COLUMN); db_set_column_sqltype(column, DB_SQL_TYPE_INTEGER); column = db_get_table_column(table, 1); db_set_column_name(column, "value"); db_set_column_sqltype(column, DB_SQL_TYPE_DOUBLE_PRECISION); if (theState->docover == TRUE) { column = db_get_table_column(table, 2); db_set_column_name(column, "covervalue"); db_set_column_sqltype(column, DB_SQL_TYPE_DOUBLE_PRECISION); } if (db_create_table(driver, table) != DB_OK) G_warning(_("Cannot create new table")); db_begin_transaction(driver); Points = Vect_new_line_struct(); Cats = Vect_new_cats_struct(); db_init_string(&sql); } if (theState->outvector && theState->outraster) G_message(_("Writing raster map <%s> and vector map <%s> ..."), theState->outraster, theState->outvector); else if (theState->outraster) G_message(_("Writing raster map <%s> ..."), theState->outraster); else if (theState->outvector) G_message(_("Writing vector map <%s> ..."), theState->outvector); G_percent(0, theState->nRand, 2); init_rand(); nc = (theState->use_nulls) ? theState->nCells : theState->nCells - theState->nNulls; nt = theState->nRand; /* Number of points to generate */ cat = 1; /* Execute for loop for every row if nt>1 */ for (row = 0; row < nrows && nt; row++) { Rast_get_row(infd, theState->buf.data.v, row, theState->buf.type); if (theState->docover == TRUE) { Rast_get_row(cinfd, theState->cover.data.v, row, theState->cover.type); } for (col = 0; col < ncols && nt; col++) { do_check = 0; if (theState->use_nulls || !is_null_value(theState->buf, col)) do_check = 1; if (do_check && theState->docover == TRUE) { /* skip no data cover points */ if (!theState->use_nulls && is_null_value(theState->cover, col)) do_check = 0; } if (do_check && make_rand() % nc < nt) { nt--; if (is_null_value(theState->buf, col)) cpvalue(&theState->nulls, 0, &theState->buf, col); if (theState->docover == TRUE) { if (is_null_value(theState->cover, col)) cpvalue(&theState->cnulls, 0, &theState->cover, col); } if (theState->outvector) { double x, y, val, coverval; char buf[500]; Vect_reset_line(Points); Vect_reset_cats(Cats); x = window.west + (col + .5) * window.ew_res; y = window.north - (row + .5) * window.ns_res; val = cell_as_dbl(&theState->buf, col); if (theState->docover == 1) coverval = cell_as_dbl(&theState->cover, col); if (theState->z_geometry) Vect_append_point(Points, x, y, val); else Vect_append_point(Points, x, y, 0.0); Vect_cat_set(Cats, 1, cat); Vect_write_line(&Out, GV_POINT, Points, Cats); if (theState->docover == 1) if (is_null_value(theState->cover, col)) sprintf(buf, "insert into %s values ( %d, %f, NULL )", fi->table, cat, val); else sprintf(buf, "insert into %s values ( %d, %f, %f )", fi->table, cat, val, coverval); else sprintf(buf, "insert into %s values ( %d, %f )", fi->table, cat, val); db_set_string(&sql, buf); if (db_execute_immediate(driver, &sql) != DB_OK) G_fatal_error(_("Cannot insert new record: %s"), db_get_string(&sql)); cat++; } G_percent((theState->nRand - nt), theState->nRand, 2); } else { set_to_null(&theState->buf, col); if (theState->docover == 1) set_to_null(&theState->cover, col); } if (do_check) nc--; } while (col < ncols) { set_to_null(&theState->buf, col); if (theState->docover == 1) set_to_null(&theState->cover, col); col++; } if (theState->outraster) { if (theState->docover == 1) Rast_put_row(outfd, theState->cover.data.v, theState->cover.type); else Rast_put_row(outfd, theState->buf.data.v, theState->buf.type); } } /* Catch any remaining rows in the window */ if (theState->outraster && row < nrows) { for (col = 0; col < ncols; col++) { if (theState->docover == 1) set_to_null(&theState->cover, col); else set_to_null(&theState->buf, col); } for (; row < nrows; row++) { if (theState->docover == 1) Rast_put_row(outfd, theState->cover.data.v, theState->cover.type); else Rast_put_row(outfd, theState->buf.data.v, theState->buf.type); } } if (nt > 0) G_warning(_("Only [%ld] random points created"), theState->nRand - nt); /* close files */ Rast_close(infd); if (theState->docover == TRUE) Rast_close(cinfd); if (theState->outvector) { db_commit_transaction(driver); if (db_create_index2(driver, fi->table, GV_KEY_COLUMN) != DB_OK) G_warning(_("Unable to create index")); 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_close_database_shutdown_driver(driver); if (theState->notopol != 1) Vect_build(&Out); Vect_close(&Out); } if (theState->outraster) Rast_close(outfd); return 0; } /* execute_random() */
int main(int argc, char *argv[]) { struct Option *vector_opt, *seed_opt, *flowlines_opt, *flowacc_opt, *sampled_opt, *scalar_opt, *unit_opt, *step_opt, *limit_opt, *skip_opt, *dir_opt, *error_opt; struct Flag *table_fl; struct GModule *module; RASTER3D_Region region; RASTER3D_Map *flowacc, *sampled; struct Integration integration; struct Seed seed; struct Gradient_info gradient_info; struct Map_info seed_Map; struct line_pnts *seed_points; struct line_cats *seed_cats; struct Map_info fl_map; struct line_cats *fl_cats; /* for flowlines */ struct line_pnts *fl_points; /* for flowlines */ struct field_info *finfo; dbDriver *driver; int cat; /* cat of flowlines */ int if_table; int i, r, c, d; char *desc; int n_seeds, seed_count, ltype; int skip[3]; G_gisinit(argv[0]); module = G_define_module(); G_add_keyword(_("raster3d")); G_add_keyword(_("hydrology")); G_add_keyword(_("voxel")); module->description = _("Computes 3D flow lines and 3D flow accumulation."); scalar_opt = G_define_standard_option(G_OPT_R3_INPUT); scalar_opt->required = NO; scalar_opt->guisection = _("Input"); vector_opt = G_define_standard_option(G_OPT_R3_INPUTS); vector_opt->key = "vector_field"; vector_opt->required = NO; vector_opt->description = _("Names of three 3D raster maps describing " "x, y, z components of vector field"); vector_opt->guisection = _("Input"); seed_opt = G_define_standard_option(G_OPT_V_INPUT); seed_opt->required = NO; seed_opt->key = "seed_points"; seed_opt->description = _("If no map is provided, " "flow lines are generated " "from each cell of the input 3D raster"); seed_opt->label = _("Name of vector map with points " "from which flow lines are generated"); seed_opt->guisection = _("Input"); flowlines_opt = G_define_standard_option(G_OPT_V_OUTPUT); flowlines_opt->key = "flowline"; flowlines_opt->required = NO; flowlines_opt->description = _("Name for vector map of flow lines"); flowlines_opt->guisection = _("Output"); flowacc_opt = G_define_standard_option(G_OPT_R3_OUTPUT); flowacc_opt->key = "flowaccumulation"; flowacc_opt->required = NO; flowacc_opt->description = _("Name for output flowaccumulation 3D raster"); flowacc_opt->guisection = _("Output"); sampled_opt = G_define_standard_option(G_OPT_R3_INPUT); sampled_opt->key = "sampled"; sampled_opt->required = NO; sampled_opt->label = _("Name for 3D raster sampled by flowlines"); sampled_opt->description = _("Values of this 3D raster will be stored " "as attributes of flowlines segments"); unit_opt = G_define_option(); unit_opt->key = "unit"; unit_opt->type = TYPE_STRING; unit_opt->required = NO; unit_opt->answer = "cell"; unit_opt->options = "time,length,cell"; desc = NULL; G_asprintf(&desc, "time;%s;" "length;%s;" "cell;%s", _("elapsed time"), _("length in map units"), _("length in cells (voxels)")); unit_opt->descriptions = desc; unit_opt->label = _("Unit of integration step"); unit_opt->description = _("Default unit is cell"); unit_opt->guisection = _("Integration"); step_opt = G_define_option(); step_opt->key = "step"; step_opt->type = TYPE_DOUBLE; step_opt->required = NO; step_opt->answer = "0.25"; step_opt->label = _("Integration step in selected unit"); step_opt->description = _("Default step is 0.25 cell"); step_opt->guisection = _("Integration"); limit_opt = G_define_option(); limit_opt->key = "limit"; limit_opt->type = TYPE_INTEGER; limit_opt->required = NO; limit_opt->answer = "2000"; limit_opt->description = _("Maximum number of steps"); limit_opt->guisection = _("Integration"); error_opt = G_define_option(); error_opt->key = "max_error"; error_opt->type = TYPE_DOUBLE; error_opt->required = NO; error_opt->answer = "1e-5"; error_opt->label = _("Maximum error of integration"); error_opt->description = _("Influences step, increase maximum error " "to allow bigger steps"); error_opt->guisection = _("Integration"); skip_opt = G_define_option(); skip_opt->key = "skip"; skip_opt->type = TYPE_INTEGER; skip_opt->required = NO; skip_opt->multiple = YES; skip_opt->description = _("Number of cells between flow lines in x, y and z direction"); dir_opt = G_define_option(); dir_opt->key = "direction"; dir_opt->type = TYPE_STRING; dir_opt->required = NO; dir_opt->multiple = NO; dir_opt->options = "up,down,both"; dir_opt->answer = "down"; dir_opt->description = _("Compute flowlines upstream, " "downstream or in both direction."); table_fl = G_define_flag(); table_fl->key = 'a'; table_fl->description = _("Create and fill attribute table"); G_option_required(scalar_opt, vector_opt, NULL); G_option_exclusive(scalar_opt, vector_opt, NULL); G_option_required(flowlines_opt, flowacc_opt, NULL); G_option_requires(seed_opt, flowlines_opt, NULL); G_option_requires(table_fl, flowlines_opt, NULL); G_option_requires(sampled_opt, table_fl, NULL); if (G_parser(argc, argv)) exit(EXIT_FAILURE); driver = NULL; finfo = NULL; if_table = table_fl->answer ? TRUE : FALSE; check_vector_input_maps(vector_opt, seed_opt); Rast3d_init_defaults(); Rast3d_get_window(®ion); /* set up integration variables */ if (step_opt->answer) { integration.step = atof(step_opt->answer); integration.unit = unit_opt->answer; } else { integration.unit = "cell"; integration.step = 0.25; } integration.max_error = atof(error_opt->answer); integration.max_step = 5 * integration.step; integration.min_step = integration.step / 5; integration.limit = atof(limit_opt->answer); if (strcmp(dir_opt->answer, "up") == 0) integration.direction_type = FLOWDIR_UP; else if (strcmp(dir_opt->answer, "down") == 0) integration.direction_type = FLOWDIR_DOWN; else integration.direction_type = FLOWDIR_BOTH; /* cell size is the diagonal */ integration.cell_size = sqrt(region.ns_res * region.ns_res + region.ew_res * region.ew_res + region.tb_res * region.tb_res); /* set default skip if needed */ if (skip_opt->answers) { for (i = 0; i < 3; i++) { if (skip_opt->answers[i] != NULL) { skip[i] = atoi(skip_opt->answers[i]); } else { G_fatal_error(_("Please provide 3 integer values for skip option.")); } } } else { skip[0] = fmax(1, region.cols / 10); skip[1] = fmax(1, region.rows / 10); skip[2] = fmax(1, region.depths / 10); } /* open raster 3D maps of velocity components */ gradient_info.initialized = FALSE; load_input_raster3d_maps(scalar_opt, vector_opt, &gradient_info, ®ion); /* open new 3D raster map of flowacumulation */ if (flowacc_opt->answer) { flowacc = Rast3d_open_new_opt_tile_size(flowacc_opt->answer, RASTER3D_USE_CACHE_DEFAULT, ®ion, FCELL_TYPE, 32); if (!flowacc) Rast3d_fatal_error(_("Unable to open 3D raster map <%s>"), flowacc_opt->answer); init_flowaccum(®ion, flowacc); } /* open 3D raster map used for sampling */ if (sampled_opt->answer) { sampled = Rast3d_open_cell_old(sampled_opt->answer, G_find_raster3d(sampled_opt->answer, ""), ®ion, RASTER3D_TILE_SAME_AS_FILE, RASTER3D_USE_CACHE_DEFAULT); if (!sampled) Rast3d_fatal_error(_("Unable to open 3D raster map <%s>"), sampled_opt->answer); } else sampled = NULL; /* open new vector map of flowlines */ if (flowlines_opt->answer) { fl_cats = Vect_new_cats_struct(); fl_points = Vect_new_line_struct(); if (Vect_open_new(&fl_map, flowlines_opt->answer, TRUE) < 0) G_fatal_error(_("Unable to create vector map <%s>"), flowlines_opt->answer); Vect_hist_command(&fl_map); if (if_table) { create_table(&fl_map, &finfo, &driver, gradient_info.compute_gradient, sampled ? 1 : 0); } } n_seeds = 0; /* open vector map of seeds */ if (seed_opt->answer) { if (Vect_open_old2(&seed_Map, seed_opt->answer, "", "1") < 0) G_fatal_error(_("Unable to open vector map <%s>"), seed_opt->answer); if (!Vect_is_3d(&seed_Map)) G_fatal_error(_("Vector map <%s> is not 3D"), seed_opt->answer); n_seeds = Vect_get_num_primitives(&seed_Map, GV_POINT); } if (flowacc_opt->answer || (!seed_opt->answer && flowlines_opt->answer)) { if (flowacc_opt->answer) n_seeds += region.cols * region.rows * region.depths; else { n_seeds += ceil(region.cols / (double)skip[0]) * ceil(region.rows / (double)skip[1]) * ceil(region.depths / (double)skip[2]); } } G_debug(1, "Number of seeds is %d", n_seeds); seed_count = 0; cat = 1; if (seed_opt->answer) { seed_points = Vect_new_line_struct(); seed_cats = Vect_new_cats_struct(); /* compute flowlines from vector seed map */ while (TRUE) { ltype = Vect_read_next_line(&seed_Map, seed_points, seed_cats); if (ltype == -1) { Vect_close(&seed_Map); G_fatal_error(_("Error during reading seed vector map")); } else if (ltype == -2) { break; } else if (ltype == GV_POINT) { seed.x = seed_points->x[0]; seed.y = seed_points->y[0]; seed.z = seed_points->z[0]; seed.flowline = TRUE; seed.flowaccum = FALSE; } G_percent(seed_count, n_seeds, 1); if (integration.direction_type == FLOWDIR_UP || integration.direction_type == FLOWDIR_BOTH) { integration.actual_direction = FLOWDIR_UP; compute_flowline(®ion, &seed, &gradient_info, flowacc, sampled, &integration, &fl_map, fl_cats, fl_points, &cat, if_table, finfo, driver); } if (integration.direction_type == FLOWDIR_DOWN || integration.direction_type == FLOWDIR_BOTH) { integration.actual_direction = FLOWDIR_DOWN; compute_flowline(®ion, &seed, &gradient_info, flowacc, sampled, &integration, &fl_map, fl_cats, fl_points, &cat, if_table, finfo, driver); } seed_count++; } Vect_destroy_line_struct(seed_points); Vect_destroy_cats_struct(seed_cats); Vect_close(&seed_Map); } if (flowacc_opt->answer || (!seed_opt->answer && flowlines_opt->answer)) { /* compute flowlines from points on grid */ for (r = region.rows; r > 0; r--) { for (c = 0; c < region.cols; c++) { for (d = 0; d < region.depths; d++) { seed.x = region.west + c * region.ew_res + region.ew_res / 2; seed.y = region.south + r * region.ns_res - region.ns_res / 2; seed.z = region.bottom + d * region.tb_res + region.tb_res / 2; seed.flowline = FALSE; seed.flowaccum = FALSE; if (flowacc_opt->answer) seed.flowaccum = TRUE; if (flowlines_opt->answer && !seed_opt->answer && (c % skip[0] == 0) && (r % skip[1] == 0) && (d % skip[2] == 0)) seed.flowline = TRUE; if (seed.flowaccum || seed.flowline) { G_percent(seed_count, n_seeds, 1); if (integration.direction_type == FLOWDIR_UP || integration.direction_type == FLOWDIR_BOTH) { integration.actual_direction = FLOWDIR_UP; compute_flowline(®ion, &seed, &gradient_info, flowacc, sampled, &integration, &fl_map, fl_cats, fl_points, &cat, if_table, finfo, driver); } if (integration.direction_type == FLOWDIR_DOWN || integration.direction_type == FLOWDIR_BOTH) { integration.actual_direction = FLOWDIR_DOWN; compute_flowline(®ion, &seed, &gradient_info, flowacc, sampled, &integration, &fl_map, fl_cats, fl_points, &cat, if_table, finfo, driver); } seed_count++; } } } } } G_percent(1, 1, 1); if (flowlines_opt->answer) { if (if_table) { db_commit_transaction(driver); db_close_database_shutdown_driver(driver); } Vect_destroy_line_struct(fl_points); Vect_destroy_cats_struct(fl_cats); Vect_build(&fl_map); Vect_close(&fl_map); } if (flowacc_opt->answer) Rast3d_close(flowacc); return EXIT_SUCCESS; }
int main(int argc, char **argv) { double radius; double fisher, david, douglas, lloyd, lloydip, morisita; int i, nquads, *counts; struct Cell_head window; struct GModule *module; struct { struct Option *input, *field, *output, *n, *r; } parm; struct { struct Flag *g; } flag; COOR *quads; struct Map_info Map; G_gisinit(argv[0]); module = G_define_module(); G_add_keyword(_("vector")); G_add_keyword(_("statistics")); G_add_keyword(_("point pattern")); module->description = _("Indices for quadrat counts of vector point lists."); parm.input = G_define_standard_option(G_OPT_V_INPUT); parm.field = G_define_standard_option(G_OPT_V_FIELD_ALL); parm.output = G_define_standard_option(G_OPT_V_OUTPUT); parm.output->required = NO; parm.output->description = _("Name for output quadrat centers map (number of points is written as category)"); parm.n = G_define_option(); parm.n->key = "nquadrats"; parm.n->type = TYPE_INTEGER; parm.n->required = YES; parm.n->description = _("Number of quadrats"); parm.r = G_define_option(); parm.r->key = "radius"; parm.r->type = TYPE_DOUBLE; parm.r->required = YES; parm.r->description = _("Quadrat radius"); flag.g = G_define_flag(); flag.g->key = 'g'; flag.g->description = _("Print results in shell script style"); if (G_parser(argc, argv)) exit(EXIT_FAILURE); sscanf(parm.n->answer, "%d", &nquads); sscanf(parm.r->answer, "%lf", &radius); G_get_window(&window); /* Open input */ Vect_set_open_level(2); if (Vect_open_old2(&Map, parm.input->answer, "", parm.field->answer) < 0) G_fatal_error(_("Unable to open vector map <%s>"), parm.input->answer); /* Get the quadrats */ G_message(_("Finding quadrats...")); quads = find_quadrats(nquads, radius, window); /* Get the counts per quadrat */ G_message(_("Counting points quadrats...")); counts = (int *)G_malloc(nquads * (sizeof(int))); count_sites(quads, nquads, counts, radius, &Map, Vect_get_field_number(&Map, parm.field->answer)); Vect_close(&Map); /* output if requested */ if (parm.output->answer) { struct Map_info Out; struct line_pnts *Points; struct line_cats *Cats; Points = Vect_new_line_struct(); Cats = Vect_new_cats_struct(); if (Vect_open_new(&Out, parm.output->answer, 0) < 0) G_fatal_error(_("Unable to create vector map <%s>"), parm.output->answer); Vect_hist_command(&Out); for (i = 0; i < nquads; i++) { Vect_reset_line(Points); Vect_reset_cats(Cats); Vect_append_point(Points, quads[i].x, quads[i].y, 0.0); Vect_cat_set(Cats, 1, counts[i]); Vect_write_line(&Out, GV_POINT, Points, Cats); } Vect_build(&Out); Vect_close(&Out); } /* Indices if requested */ qindices(counts, nquads, &fisher, &david, &douglas, &lloyd, &lloydip, &morisita); if (!flag.g->answer) { fprintf(stdout, "-----------------------------------------------------------\n"); fprintf(stdout, "Index Realization\n"); fprintf(stdout, "-----------------------------------------------------------\n"); fprintf(stdout, "Fisher el al (1922) Relative Variance %g\n", fisher); fprintf(stdout, "David & Moore (1954) Index of Cluster Size %g\n", david); fprintf(stdout, "Douglas (1975) Index of Cluster Frequency %g\n", douglas); fprintf(stdout, "Lloyd (1967) \"mean crowding\" %g\n", lloyd); fprintf(stdout, "Lloyd (1967) Index of patchiness %g\n", lloydip); fprintf(stdout, "Morisita's (1959) I (variability b/n patches) %g\n", morisita); fprintf(stdout, "-----------------------------------------------------------\n"); } else { fprintf(stdout, "fisher=%g\n", fisher); fprintf(stdout, "david=%g\n", david); fprintf(stdout, "douglas=%g\n", douglas); fprintf(stdout, "lloyd=%g\n", lloyd); fprintf(stdout, "lloydip=%g\n", lloydip); fprintf(stdout, "morisita=%g\n", morisita); } exit(EXIT_SUCCESS); }
int main(int argc, char **argv) { int ret, level; int stat, type, display; int chcat; int has_color, has_fcolor; struct color_rgb color, fcolor; double size; int default_width; double width_scale; double minreg, maxreg, reg; char map_name[GNAME_MAX]; struct GModule *module; struct Option *map_opt; struct Option *color_opt, *fcolor_opt, *rgbcol_opt, *zcol_opt; struct Option *type_opt, *display_opt; struct Option *icon_opt, *size_opt, *sizecolumn_opt, *rotcolumn_opt; struct Option *where_opt; struct Option *field_opt, *cat_opt, *lfield_opt; struct Option *lcolor_opt, *bgcolor_opt, *bcolor_opt; struct Option *lsize_opt, *font_opt, *enc_opt, *xref_opt, *yref_opt; struct Option *attrcol_opt, *maxreg_opt, *minreg_opt; struct Option *width_opt, *wcolumn_opt, *wscale_opt; struct Option *leglab_opt; struct Option *icon_line_opt, *icon_area_opt; struct Flag *id_flag, *cats_acolors_flag, *sqrt_flag, *legend_flag; char *desc; struct cat_list *Clist; LATTR lattr; struct Map_info Map; struct Cell_head window; struct bound_box box; double overlap; stat = 0; /* Initialize the GIS calls */ G_gisinit(argv[0]); module = G_define_module(); G_add_keyword(_("display")); G_add_keyword(_("graphics")); G_add_keyword(_("vector")); module->description = _("Displays user-specified vector map " "in the active graphics frame."); map_opt = G_define_standard_option(G_OPT_V_MAP); field_opt = G_define_standard_option(G_OPT_V_FIELD_ALL); field_opt->answer = "1"; field_opt->guisection = _("Selection"); display_opt = G_define_option(); display_opt->key = "display"; display_opt->type = TYPE_STRING; display_opt->required = YES; display_opt->multiple = YES; display_opt->answer = "shape"; display_opt->options = "shape,cat,topo,vert,dir,zcoor"; display_opt->description = _("Display"); desc = NULL; G_asprintf(&desc, "shape;%s;cat;%s;topo;%s;vert;%s;dir;%s;zcoor;%s", _("Display geometry of features"), _("Display category numbers of features"), _("Display topology information (nodes, edges)"), _("Display vertices of features"), _("Display direction of linear features"), _("Display z-coordinate of features (only for 3D vector maps)")); display_opt->descriptions = desc; /* Query */ type_opt = G_define_standard_option(G_OPT_V_TYPE); type_opt->answer = "point,line,area,face"; type_opt->options = "point,line,boundary,centroid,area,face"; type_opt->guisection = _("Selection"); cat_opt = G_define_standard_option(G_OPT_V_CATS); cat_opt->guisection = _("Selection"); where_opt = G_define_standard_option(G_OPT_DB_WHERE); where_opt->guisection = _("Selection"); /* Colors */ color_opt = G_define_standard_option(G_OPT_CN); color_opt->label = _("Feature color"); color_opt->guisection = _("Colors"); fcolor_opt = G_define_standard_option(G_OPT_CN); fcolor_opt->key = "fill_color"; fcolor_opt->answer = "200:200:200"; fcolor_opt->label = _("Area fill color"); fcolor_opt->guisection = _("Colors"); rgbcol_opt = G_define_standard_option(G_OPT_DB_COLUMN); rgbcol_opt->key = "rgb_column"; rgbcol_opt->guisection = _("Colors"); rgbcol_opt->label = _("Colorize features according color definition column"); rgbcol_opt->description = _("Color definition in R:G:B form"); zcol_opt = G_define_standard_option(G_OPT_M_COLR); zcol_opt->key = "zcolor"; zcol_opt->description = _("Colorize point or area features according to z-coordinate"); zcol_opt->guisection = _("Colors"); /* Lines */ width_opt = G_define_option(); width_opt->key = "width"; width_opt->type = TYPE_INTEGER; width_opt->answer = "0"; width_opt->guisection = _("Lines"); width_opt->description = _("Line width"); wcolumn_opt = G_define_standard_option(G_OPT_DB_COLUMN); wcolumn_opt->key = "width_column"; wcolumn_opt->guisection = _("Lines"); wcolumn_opt->label = _("Name of numeric column containing line width"); wcolumn_opt->description = _("These values will be scaled by width_scale"); wscale_opt = G_define_option(); wscale_opt->key = "width_scale"; wscale_opt->type = TYPE_DOUBLE; wscale_opt->answer = "1"; wscale_opt->guisection = _("Lines"); wscale_opt->description = _("Scale factor for width_column"); /* Symbols */ icon_opt = G_define_option(); icon_opt->key = "icon"; icon_opt->type = TYPE_STRING; icon_opt->required = NO; icon_opt->multiple = NO; icon_opt->guisection = _("Symbols"); icon_opt->answer = "basic/x"; /* This could also use ->gisprompt = "old,symbol,symbol" instead of ->options */ icon_opt->options = icon_files(); icon_opt->description = _("Point and centroid symbol"); size_opt = G_define_option(); size_opt->key = "size"; size_opt->type = TYPE_DOUBLE; size_opt->answer = "5"; size_opt->guisection = _("Symbols"); size_opt->label = _("Symbol size"); size_opt->description = _("When used with the size_column option this becomes the scale factor"); sizecolumn_opt = G_define_standard_option(G_OPT_DB_COLUMN); sizecolumn_opt->key = "size_column"; sizecolumn_opt->guisection = _("Symbols"); sizecolumn_opt->description = _("Name of numeric column containing symbol size"); rotcolumn_opt = G_define_standard_option(G_OPT_DB_COLUMN); rotcolumn_opt->key = "rotation_column"; rotcolumn_opt->guisection = _("Symbols"); rotcolumn_opt->label = _("Name of numeric column containing symbol rotation angle"); rotcolumn_opt->description = _("Measured in degrees CCW from east"); icon_area_opt = G_define_option(); icon_area_opt->key = "icon_area"; icon_area_opt->type = TYPE_STRING; icon_area_opt->required = NO; icon_area_opt->multiple = NO; icon_area_opt->guisection = _("Legend"); icon_area_opt->answer = "legend/area"; icon_area_opt->options = icon_files(); icon_area_opt->description = _("Area/boundary symbol for legend"); icon_line_opt = G_define_option(); icon_line_opt->key = "icon_line"; icon_line_opt->type = TYPE_STRING; icon_line_opt->required = NO; icon_line_opt->multiple = NO; icon_line_opt->guisection = _("Legend"); icon_line_opt->answer = "legend/line"; icon_line_opt->options = icon_files(); icon_line_opt->description = _("Line symbol for legend"); leglab_opt = G_define_option(); leglab_opt->key = "legend_label"; leglab_opt->type = TYPE_STRING; leglab_opt->guisection = _("Legend"); leglab_opt->description = _("Label to display after symbol in vector legend"); /* Labels */ lfield_opt = G_define_standard_option(G_OPT_V_FIELD); lfield_opt->key = "label_layer"; lfield_opt->required = NO; lfield_opt->guisection = _("Labels"); lfield_opt->label = _("Layer number for labels (default: the given layer number)"); attrcol_opt = G_define_standard_option(G_OPT_DB_COLUMN); attrcol_opt->key = "attribute_column"; attrcol_opt->multiple = NO; /* or fix attr.c, around line 102 */ attrcol_opt->guisection = _("Labels"); attrcol_opt->description = _("Name of column to be displayed as a label"); lcolor_opt = G_define_standard_option(G_OPT_C); lcolor_opt->key = "label_color"; lcolor_opt->answer = "red"; lcolor_opt->label = _("Label color"); lcolor_opt->guisection = _("Labels"); bgcolor_opt = G_define_standard_option(G_OPT_CN); bgcolor_opt->key = "label_bgcolor"; bgcolor_opt->answer = "none"; bgcolor_opt->guisection = _("Labels"); bgcolor_opt->label = _("Label background color"); bcolor_opt = G_define_standard_option(G_OPT_CN); bcolor_opt->key = "label_bcolor"; bcolor_opt->type = TYPE_STRING; bcolor_opt->answer = "none"; bcolor_opt->guisection = _("Labels"); bcolor_opt->label = _("Label border color"); lsize_opt = G_define_option(); lsize_opt->key = "label_size"; lsize_opt->type = TYPE_INTEGER; lsize_opt->answer = "8"; lsize_opt->guisection = _("Labels"); lsize_opt->description = _("Label size (pixels)"); font_opt = G_define_option(); font_opt->key = "font"; font_opt->type = TYPE_STRING; font_opt->guisection = _("Labels"); font_opt->description = _("Font name"); enc_opt = G_define_option(); enc_opt->key = "encoding"; enc_opt->type = TYPE_STRING; enc_opt->guisection = _("Labels"); enc_opt->description = _("Text encoding"); xref_opt = G_define_option(); xref_opt->key = "xref"; xref_opt->type = TYPE_STRING; xref_opt->guisection = _("Labels"); xref_opt->answer = "left"; xref_opt->options = "left,center,right"; xref_opt->description = _("Label horizontal justification"); yref_opt = G_define_option(); yref_opt->key = "yref"; yref_opt->type = TYPE_STRING; yref_opt->guisection = _("Labels"); yref_opt->answer = "center"; yref_opt->options = "top,center,bottom"; yref_opt->description = _("Label vertical justification"); minreg_opt = G_define_option(); minreg_opt->key = "minreg"; minreg_opt->type = TYPE_DOUBLE; minreg_opt->required = NO; minreg_opt->description = _("Minimum region size (average from height and width) " "when map is displayed"); maxreg_opt = G_define_option(); maxreg_opt->key = "maxreg"; maxreg_opt->type = TYPE_DOUBLE; maxreg_opt->required = NO; maxreg_opt->description = _("Maximum region size (average from height and width) " "when map is displayed"); /* Colors */ cats_acolors_flag = G_define_flag(); cats_acolors_flag->key = 'c'; cats_acolors_flag->guisection = _("Colors"); cats_acolors_flag->description = _("Random colors according to category number " "(or layer number if 'layer=-1' is given)"); /* Query */ id_flag = G_define_flag(); id_flag->key = 'i'; id_flag->guisection = _("Selection"); id_flag->description = _("Use values from 'cats' option as feature id"); sqrt_flag = G_define_flag(); sqrt_flag->key = 'r'; sqrt_flag->label = _("Use square root of the value of size_column"); sqrt_flag->description = _("This makes circle areas proportionate to the size_column values " "instead of circle radius"); sqrt_flag->guisection = _("Symbols"); legend_flag = G_define_flag(); legend_flag->key = 's'; legend_flag->label = _("Do not show this layer in vector legend"); legend_flag->guisection = _("Legend"); /* Check command line */ if (G_parser(argc, argv)) exit(EXIT_FAILURE); D_open_driver(); G_get_set_window(&window); /* Check min/max region */ reg = ((window.east - window.west) + (window.north - window.south)) / 2; if (minreg_opt->answer) { minreg = atof(minreg_opt->answer); if (reg < minreg) { G_important_message(_("Region size is lower than minreg, nothing displayed")); exit(EXIT_SUCCESS); } } if (maxreg_opt->answer) { maxreg = atof(maxreg_opt->answer); if (reg > maxreg) { G_important_message(_("Region size is greater than maxreg, nothing displayed")); exit(EXIT_SUCCESS); } } strcpy(map_name, map_opt->answer); default_width = atoi(width_opt->answer); if (default_width < 0) default_width = 0; width_scale = atof(wscale_opt->answer); if (cats_acolors_flag->answer && rgbcol_opt->answer) { G_warning(_("The -%c flag and <%s> option cannot be used together, " "the -%c flag will be ignored!"), cats_acolors_flag->key, rgbcol_opt->key, cats_acolors_flag->key); cats_acolors_flag->answer = FALSE; } color = G_standard_color_rgb(WHITE); has_color = option_to_color(&color, color_opt->answer); fcolor = G_standard_color_rgb(WHITE); has_fcolor = option_to_color(&fcolor, fcolor_opt->answer); size = atof(size_opt->answer); /* if where_opt was specified select categories from db * otherwise parse cat_opt */ Clist = Vect_new_cat_list(); Clist->field = atoi(field_opt->answer); /* open vector */ level = Vect_open_old2(&Map, map_name, "", field_opt->answer); chcat = 0; if (where_opt->answer) { if (Clist->field < 1) G_fatal_error(_("Option <%s> must be > 0"), field_opt->key); chcat = 1; option_to_where(&Map, Clist, where_opt->answer); } else if (cat_opt->answer) { if (Clist->field < 1 && !id_flag->answer) G_fatal_error(_("Option <%s> must be > 0"), field_opt->key); chcat = 1; ret = Vect_str_to_cat_list(cat_opt->answer, Clist); if (ret > 0) G_warning(n_("%d error in cat option", "%d errors in cat option", ret), ret); } type = Vect_option_to_types(type_opt); display = option_to_display(display_opt); /* labels */ options_to_lattr(&lattr, lfield_opt->answer, lcolor_opt->answer, bgcolor_opt->answer, bcolor_opt->answer, atoi(lsize_opt->answer), font_opt->answer, enc_opt->answer, xref_opt->answer, yref_opt->answer); D_setup(0); D_set_reduction(1.0); G_verbose_message(_("Plotting...")); if (level >= 2) Vect_get_map_box(&Map, &box); if (level >= 2 && (window.north < box.S || window.south > box.N || window.east < box.W || window.west > G_adjust_easting(box.E, &window))) { G_warning(_("The bounding box of the map is outside the current region, " "nothing drawn")); } else { overlap = G_window_percentage_overlap(&window, box.N, box.S, box.E, box.W); G_debug(1, "overlap = %f \n", overlap); if (overlap < 1) Vect_set_constraint_region(&Map, window.north, window.south, window.east, window.west, PORT_DOUBLE_MAX, -PORT_DOUBLE_MAX); /* default line width */ if (!wcolumn_opt->answer) D_line_width(default_width); if (display & DISP_SHAPE) { stat += display_shape(&Map, type, Clist, &window, has_color ? &color : NULL, has_fcolor ? &fcolor : NULL, chcat, icon_opt->answer, size, sizecolumn_opt->answer, sqrt_flag->answer ? TRUE : FALSE, rotcolumn_opt->answer, id_flag->answer ? TRUE : FALSE, cats_acolors_flag->answer ? TRUE : FALSE, rgbcol_opt->answer, default_width, wcolumn_opt->answer, width_scale, zcol_opt->answer); if (wcolumn_opt->answer) D_line_width(default_width); } if (has_color) { D_RGB_color(color.r, color.g, color.b); if (display & DISP_DIR) stat += display_dir(&Map, type, Clist, chcat, size); } if (!legend_flag->answer) { write_into_legfile(&Map, type, leglab_opt->answer, map_name, icon_opt->answer, size_opt->answer, color_opt->answer, fcolor_opt->answer, width_opt->answer, icon_area_opt->answer, icon_line_opt->answer, sizecolumn_opt->answer); } /* reset line width: Do we need to get line width from display * driver (not implemented)? It will help restore previous line * width (not just 0) determined by another module (e.g., * d.linewidth). */ if (!wcolumn_opt->answer) D_line_width(0); if (display & DISP_CAT) stat += display_label(&Map, type, Clist, &lattr, chcat); if (attrcol_opt->answer) stat += display_attr(&Map, type, attrcol_opt->answer, Clist, &lattr, chcat); if (display & DISP_ZCOOR) stat += display_zcoor(&Map, type, &lattr); if (display & DISP_VERT) stat += display_vert(&Map, type, &lattr, size); if (display & DISP_TOPO) stat += display_topo(&Map, type, &lattr, size); } D_save_command(G_recreate_command()); D_close_driver(); Vect_close(&Map); Vect_destroy_cat_list(Clist); if (stat != 0) { G_fatal_error(_("Rendering failed")); } G_done_msg(" "); exit(EXIT_SUCCESS); }
int main(int argc, char *argv[]) { int i, type, stat; int day, yr, Out_proj; int out_zone = 0; int overwrite; /* overwrite output map */ const char *mapset; const char *omap_name, *map_name, *iset_name, *iloc_name; struct pj_info info_in; struct pj_info info_out; const char *gbase; char date[40], mon[4]; struct GModule *module; struct Option *omapopt, *mapopt, *isetopt, *ilocopt, *ibaseopt, *smax; struct Key_Value *in_proj_keys, *in_unit_keys; struct Key_Value *out_proj_keys, *out_unit_keys; struct line_pnts *Points, *Points2; struct line_cats *Cats; struct Map_info Map; struct Map_info Out_Map; struct bound_box src_box, tgt_box; int nowrap = 0, recommend_nowrap = 0; double lmax; struct { struct Flag *list; /* list files in source location */ struct Flag *transformz; /* treat z as ellipsoidal height */ struct Flag *wrap; /* latlon output: wrap to 0,360 */ struct Flag *no_topol; /* do not build topology */ } flag; G_gisinit(argv[0]); module = G_define_module(); G_add_keyword(_("vector")); G_add_keyword(_("projection")); G_add_keyword(_("transformation")); G_add_keyword(_("import")); module->description = _("Re-projects a vector map from one location to the current location."); /* set up the options and flags for the command line parser */ ilocopt = G_define_standard_option(G_OPT_M_LOCATION); ilocopt->required = YES; ilocopt->label = _("Location containing input vector map"); ilocopt->guisection = _("Source"); isetopt = G_define_standard_option(G_OPT_M_MAPSET); isetopt->label = _("Mapset containing input vector map"); isetopt->description = _("Default: name of current mapset"); isetopt->guisection = _("Source"); mapopt = G_define_standard_option(G_OPT_V_INPUT); mapopt->required = NO; mapopt->label = _("Name of input vector map to re-project"); mapopt->description = NULL; mapopt->guisection = _("Source"); ibaseopt = G_define_standard_option(G_OPT_M_DBASE); ibaseopt->label = _("Path to GRASS database of input location"); smax = G_define_option(); smax->key = "smax"; smax->type = TYPE_DOUBLE; smax->required = NO; smax->answer = "10000"; smax->label = _("Maximum segment length in meters in output vector map"); smax->description = _("Increases accuracy of reprojected shapes, disable with smax=0"); smax->guisection = _("Target"); omapopt = G_define_standard_option(G_OPT_V_OUTPUT); omapopt->required = NO; omapopt->description = _("Name for output vector map (default: input)"); omapopt->guisection = _("Target"); flag.list = G_define_flag(); flag.list->key = 'l'; flag.list->description = _("List vector maps in input mapset and exit"); flag.transformz = G_define_flag(); flag.transformz->key = 'z'; flag.transformz->description = _("3D vector maps only"); flag.transformz->label = _("Assume z coordinate is ellipsoidal height and " "transform if possible"); flag.transformz->guisection = _("Target"); flag.wrap = G_define_flag(); flag.wrap->key = 'w'; flag.wrap->description = _("Latlon output only, default is -180,180"); flag.wrap->label = _("Disable wrapping to -180,180 for latlon output"); flag.transformz->guisection = _("Target"); flag.no_topol = G_define_flag(); flag.no_topol->key = 'b'; flag.no_topol->label = _("Do not build vector topology"); flag.no_topol->description = _("Recommended for massive point projection"); /* The parser checks if the map already exists in current mapset, we switch out the check and do it in the module after the parser */ overwrite = G_check_overwrite(argc, argv); if (G_parser(argc, argv)) exit(EXIT_FAILURE); /* start checking options and flags */ /* set input vector map name and mapset */ map_name = mapopt->answer; if (omapopt->answer) omap_name = omapopt->answer; else omap_name = map_name; if (omap_name && !flag.list->answer && !overwrite && G_find_vector2(omap_name, G_mapset())) G_fatal_error(_("option <%s>: <%s> exists. To overwrite, use the --overwrite flag"), omapopt->key, omap_name); if (isetopt->answer) iset_name = isetopt->answer; else iset_name = G_store(G_mapset()); iloc_name = ilocopt->answer; if (ibaseopt->answer) gbase = ibaseopt->answer; else gbase = G_store(G_gisdbase()); if (!ibaseopt->answer && strcmp(iloc_name, G_location()) == 0) G_fatal_error(_("Input and output locations can not be the same")); lmax = atof(smax->answer); if (lmax < 0) lmax = 0; Out_proj = G_projection(); if (Out_proj == PROJECTION_LL && flag.wrap->answer) nowrap = 1; G_begin_distance_calculations(); /* Change the location here and then come back */ select_target_env(); G_setenv_nogisrc("GISDBASE", gbase); G_setenv_nogisrc("LOCATION_NAME", iloc_name); stat = G_mapset_permissions(iset_name); if (stat >= 0) { /* yes, we can access the mapset */ /* if requested, list the vector maps in source location - MN 5/2001 */ if (flag.list->answer) { int i; char **list; G_verbose_message(_("Checking location <%s> mapset <%s>"), iloc_name, iset_name); list = G_list(G_ELEMENT_VECTOR, G_getenv_nofatal("GISDBASE"), G_getenv_nofatal("LOCATION_NAME"), iset_name); if (list[0]) { for (i = 0; list[i]; i++) { fprintf(stdout, "%s\n", list[i]); } fflush(stdout); } else { G_important_message(_("No vector maps found")); } exit(EXIT_SUCCESS); /* leave v.proj after listing */ } if (mapopt->answer == NULL) { G_fatal_error(_("Required parameter <%s> not set"), mapopt->key); } G_setenv_nogisrc("MAPSET", iset_name); /* Make sure map is available */ mapset = G_find_vector2(map_name, iset_name); if (mapset == NULL) G_fatal_error(_("Vector map <%s> in location <%s> mapset <%s> not found"), map_name, iloc_name, iset_name); /*** Get projection info for input mapset ***/ in_proj_keys = G_get_projinfo(); if (in_proj_keys == NULL) exit(EXIT_FAILURE); /* apparently the +over switch must be set in the input projection, * not the output latlon projection */ if (Out_proj == PROJECTION_LL && nowrap == 1) G_set_key_value("+over", "defined", in_proj_keys); in_unit_keys = G_get_projunits(); if (in_unit_keys == NULL) exit(EXIT_FAILURE); if (pj_get_kv(&info_in, in_proj_keys, in_unit_keys) < 0) exit(EXIT_FAILURE); Vect_set_open_level(1); G_debug(1, "Open old: location: %s mapset : %s", G_location_path(), G_mapset()); if (Vect_open_old(&Map, map_name, mapset) < 0) G_fatal_error(_("Unable to open vector map <%s>"), map_name); } else if (stat < 0) { /* allow 0 (i.e. denied permission) */ /* need to be able to read from others */ if (stat == 0) G_fatal_error(_("Mapset <%s> in input location <%s> - permission denied"), iset_name, iloc_name); else G_fatal_error(_("Mapset <%s> in input location <%s> not found"), iset_name, iloc_name); } select_current_env(); /****** get the output projection parameters ******/ out_proj_keys = G_get_projinfo(); if (out_proj_keys == NULL) exit(EXIT_FAILURE); out_unit_keys = G_get_projunits(); if (out_unit_keys == NULL) exit(EXIT_FAILURE); if (pj_get_kv(&info_out, out_proj_keys, out_unit_keys) < 0) exit(EXIT_FAILURE); G_free_key_value(in_proj_keys); G_free_key_value(in_unit_keys); G_free_key_value(out_proj_keys); G_free_key_value(out_unit_keys); if (G_verbose() == G_verbose_max()) { pj_print_proj_params(&info_in, &info_out); } /* Initialize the Point / Cat structure */ Points = Vect_new_line_struct(); Points2 = Vect_new_line_struct(); Cats = Vect_new_cats_struct(); /* test if latlon wrapping to -180,180 should be disabled */ if (Out_proj == PROJECTION_LL && nowrap == 0) { int first = 1, counter = 0; double x, y; /* Cycle through all lines */ Vect_rewind(&Map); while (1) { type = Vect_read_next_line(&Map, Points, Cats); /* read line */ if (type == 0) continue; /* Dead */ if (type == -1) G_fatal_error(_("Reading input vector map")); if (type == -2) break; if (first && Points->n_points > 0) { first = 0; src_box.E = src_box.W = Points->x[0]; src_box.N = src_box.S = Points->y[0]; src_box.T = src_box.B = Points->z[0]; } for (i = 0; i < Points->n_points; i++) { if (src_box.E < Points->x[i]) src_box.E = Points->x[i]; if (src_box.W > Points->x[i]) src_box.W = Points->x[i]; if (src_box.N < Points->y[i]) src_box.N = Points->y[i]; if (src_box.S > Points->y[i]) src_box.S = Points->y[i]; } counter++; } if (counter == 0) { G_warning(_("Input vector map <%s> is empty"), omap_name); exit(EXIT_SUCCESS); } /* NW corner */ x = src_box.W; y = src_box.N; if (pj_do_transform(1, &x, &y, NULL, &info_in, &info_out) < 0) { G_fatal_error(_("Error in pj_do_transform")); } tgt_box.E = x; tgt_box.W = x; tgt_box.N = y; tgt_box.S = y; /* SW corner */ x = src_box.W; y = src_box.S; if (pj_do_transform(1, &x, &y, NULL, &info_in, &info_out) < 0) { G_fatal_error(_("Error in pj_do_transform")); } if (tgt_box.W > x) tgt_box.W = x; if (tgt_box.E < x) tgt_box.E = x; if (tgt_box.N < y) tgt_box.N = y; if (tgt_box.S > y) tgt_box.S = y; /* NE corner */ x = src_box.E; y = src_box.N; if (pj_do_transform(1, &x, &y, NULL, &info_in, &info_out) < 0) { G_fatal_error(_("Error in pj_do_transform")); } if (tgt_box.W > x) { tgt_box.E = x + 360; recommend_nowrap = 1; } if (tgt_box.N < y) tgt_box.N = y; if (tgt_box.S > y) tgt_box.S = y; /* SE corner */ x = src_box.E; y = src_box.S; if (pj_do_transform(1, &x, &y, NULL, &info_in, &info_out) < 0) { G_fatal_error(_("Error in pj_do_transform")); } if (tgt_box.W > x) { if (tgt_box.E < x + 360) tgt_box.E = x + 360; recommend_nowrap = 1; } if (tgt_box.N < y) tgt_box.N = y; if (tgt_box.S > y) tgt_box.S = y; } G_debug(1, "Open new: location: %s mapset : %s", G_location_path(), G_mapset()); if (Vect_open_new(&Out_Map, omap_name, Vect_is_3d(&Map)) < 0) G_fatal_error(_("Unable to create vector map <%s>"), omap_name); Vect_set_error_handler_io(NULL, &Out_Map); /* register standard i/o error handler */ Vect_copy_head_data(&Map, &Out_Map); Vect_hist_copy(&Map, &Out_Map); Vect_hist_command(&Out_Map); out_zone = info_out.zone; Vect_set_zone(&Out_Map, out_zone); /* Read and write header info */ sprintf(date, "%s", G_date()); sscanf(date, "%*s%s%d%*s%d", mon, &day, &yr); if (yr < 2000) yr = yr - 1900; else yr = yr - 2000; sprintf(date, "%s %d %d", mon, day, yr); Vect_set_date(&Out_Map, date); /* line densification works only with vector topology */ if (Map.format != GV_FORMAT_NATIVE) lmax = 0; /* Cycle through all lines */ Vect_rewind(&Map); i = 0; G_message(_("Reprojecting primitives ...")); while (TRUE) { ++i; G_progress(i, 1e3); type = Vect_read_next_line(&Map, Points, Cats); /* read line */ if (type == 0) continue; /* Dead */ if (type == -1) G_fatal_error(_("Reading input vector map")); if (type == -2) break; Vect_line_prune(Points); if (lmax > 0 && (type & GV_LINES) && Points->n_points > 1) { double x1, y1, z1, x2, y2, z2; double dx, dy, dz; double l; int i, n; Vect_reset_line(Points2); for (i = 0; i < Points->n_points - 1; i++) { x1 = Points->x[i]; y1 = Points->y[i]; z1 = Points->z[i]; n = i + 1; x2 = Points->x[n]; y2 = Points->y[n]; z2 = Points->z[n]; dx = x2 - x1; dy = y2 - y1; dz = z2 - z1; if (pj_do_transform(1, &x1, &y1, flag.transformz->answer ? &z1 : NULL, &info_in, &info_out) < 0) { G_fatal_error(_("Unable to re-project vector map <%s> from <%s>"), Vect_get_full_name(&Map), ilocopt->answer); } if (pj_do_transform(1, &x2, &y2, flag.transformz->answer ? &z2 : NULL, &info_in, &info_out) < 0) { G_fatal_error(_("Unable to re-project vector map <%s> from <%s>"), Vect_get_full_name(&Map), ilocopt->answer); } Vect_append_point(Points2, x1, y1, z1); l = G_distance(x1, y1, x2, y2); if (l > lmax) { int j; double x, y, z; x1 = Points->x[i]; y1 = Points->y[i]; z1 = Points->z[i]; n = ceil(l / lmax); for (j = 1; j < n; j++) { x = x1 + dx * j / n; y = y1 + dy * j / n; z = z1 + dz * j / n; if (pj_do_transform(1, &x, &y, flag.transformz->answer ? &z : NULL, &info_in, &info_out) < 0) { G_fatal_error(_("Unable to re-project vector map <%s> from <%s>"), Vect_get_full_name(&Map), ilocopt->answer); } Vect_append_point(Points2, x, y, z); } } } Vect_append_point(Points2, x2, y2, z2); Vect_write_line(&Out_Map, type, Points2, Cats); /* write line */ } else { if (pj_do_transform(Points->n_points, Points->x, Points->y, flag.transformz->answer ? Points->z : NULL, &info_in, &info_out) < 0) { G_fatal_error(_("Unable to re-project vector map <%s> from <%s>"), Vect_get_full_name(&Map), ilocopt->answer); } Vect_write_line(&Out_Map, type, Points, Cats); /* write line */ } } /* end lines section */ G_progress(1, 1); /* Copy tables */ if (Vect_copy_tables(&Map, &Out_Map, 0)) G_warning(_("Failed to copy attribute table to output map")); Vect_close(&Map); if (!flag.no_topol->answer) Vect_build(&Out_Map); Vect_close(&Out_Map); if (recommend_nowrap) G_important_message(_("Try to disable wrapping to -180,180 " "if topological errors occurred")); exit(EXIT_SUCCESS); }
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); }
// Slot called when the "Add GRASS vector layer" menu item is triggered void QgsGrassPlugin::addVector() { // QgsDebugMsg("entered."); QString uri; QgsGrassSelect *sel = new QgsGrassSelect( qGisInterface->mainWindow(), QgsGrassSelect::VECTOR ); if ( sel->exec() ) { uri = sel->gisdbase + "/" + sel->location + "/" + sel->mapset + "/" + sel->map + "/" + sel->layer; } // QgsDebugMsg(QString("plugin URI: %1").arg(uri)); if ( uri.length() == 0 ) { // QgsDebugMsg("Nothing was selected"); return; } else { // QgsDebugMsg("Add new vector layer"); // create vector name: vector layer QString name = sel->map; QString field; QString type; if ( !sel->layer.startsWith( "topo_" ) ) { QRegExp rx( "(\\d+)_(.+)" ); if ( rx.indexIn( sel->layer ) != -1 ) { field = rx.cap( 1 ); type = rx.cap( 2 ); } } // Set location QgsGrass::setLocation( sel->gisdbase, sel->location ); /* Open vector */ try { //Vect_set_open_level( 2 ); struct Map_info map; int level = Vect_open_old_head( &map, sel->map.toUtf8().data(), sel->mapset.toUtf8().data() ); if ( level == 1 ) { QgsDebugMsg( "Cannot open vector on level 2" ); QMessageBox::warning( 0, tr( "Warning" ), tr( "Cannot open vector %1 in mapset %2 on level 2 (topology not available, try to rebuild topology using v.build module)." ).arg( sel->map ).arg( sel->mapset ) ); Vect_close( &map ); return; } else if ( level < 1 ) { QgsDebugMsg( "Cannot open vector" ); QMessageBox::warning( 0, tr( "Warning" ), tr( "Cannot open vector %1 in mapset %2" ).arg( sel->map ).arg( sel->mapset ) ); return; } if ( level >= 2 ) { if ( !sel->layer.startsWith( "topo_" ) ) { // Count layers int cnt = 0; int ncidx = Vect_cidx_get_num_fields( &map ); for ( int i = 0; i < ncidx; i++ ) { int field = Vect_cidx_get_field_number( &map, i ); if ( Vect_cidx_get_type_count( &map, field, GV_POINT | GV_LINE | GV_AREA ) > 0 || ( field > 1 && Vect_cidx_get_type_count( &map, field, GV_BOUNDARY ) ) ) { cnt++; } } if ( cnt > 1 ) { name.append( " " + field ); // No need to ad type, the type is obvious from the legend } } else { name.append( " " + sel->layer ); } } Vect_close( &map ); } catch ( QgsGrass::Exception &e ) { QMessageBox::warning( 0, tr( "Warning" ), tr( "Cannot open GRASS vector:\n %1" ).arg( e.what() ) ); } qGisInterface->addVectorLayer( uri, name, "grass" ); } }
int main(int argc, char **argv) { struct Flag *printattributes, *topo_flag, *shell_flag; struct Option *map_opt, *field_opt, *coords_opt, *maxdistance; struct Cell_head window; struct GModule *module; char buf[2000]; int i, level, ret; int *field; double xval, yval, xres, yres, maxd, x; double EW_DIST1, EW_DIST2, NS_DIST1, NS_DIST2; char nsres[30], ewres[30]; char ch; /* Initialize the GIS calls */ G_gisinit(argv[0]); module = G_define_module(); G_add_keyword(_("vector")); G_add_keyword(_("position")); G_add_keyword(_("querying")); module->description = _("Queries a vector map at given locations."); map_opt = G_define_standard_option(G_OPT_V_MAPS); field_opt = G_define_standard_option(G_OPT_V_FIELD_ALL); coords_opt = G_define_standard_option(G_OPT_M_EN); coords_opt->label = _("Coordinates for query"); coords_opt->description = _("If not given read from standard input"); maxdistance = G_define_option(); maxdistance->type = TYPE_DOUBLE; maxdistance->key = "distance"; maxdistance->answer = "0"; maxdistance->multiple = NO; maxdistance->description = _("Query threshold distance"); topo_flag = G_define_flag(); topo_flag->key = 'd'; topo_flag->description = _("Print topological information (debugging)"); topo_flag->guisection = _("Print"); printattributes = G_define_flag(); printattributes->key = 'a'; printattributes->description = _("Print attribute information"); printattributes->guisection = _("Print"); shell_flag = G_define_flag(); shell_flag->key = 'g'; shell_flag->description = _("Print the stats in shell script style"); shell_flag->guisection = _("Print"); if (G_parser(argc, argv)) exit(EXIT_FAILURE); if (map_opt->answers && map_opt->answers[0]) vect = map_opt->answers; maxd = atof(maxdistance->answer); /* * fprintf(stdout, maxdistance->answer); * fprintf(stdout, "Maxd is %f", maxd); * fprintf(stdout, xcoord->answer); * fprintf(stdout, "xval is %f", xval); * fprintf(stdout, ycoord->answer); * fprintf(stdout, "yval is %f", yval); */ if (maxd == 0.0) { G_get_window(&window); x = window.proj; G_format_resolution(window.ew_res, ewres, x); G_format_resolution(window.ns_res, nsres, x); EW_DIST1 = G_distance(window.east, window.north, window.west, window.north); /* EW Dist at South Edge */ EW_DIST2 = G_distance(window.east, window.south, window.west, window.south); /* NS Dist at East edge */ NS_DIST1 = G_distance(window.east, window.north, window.east, window.south); /* NS Dist at West edge */ NS_DIST2 = G_distance(window.west, window.north, window.west, window.south); xres = ((EW_DIST1 + EW_DIST2) / 2) / window.cols; yres = ((NS_DIST1 + NS_DIST2) / 2) / window.rows; if (xres > yres) maxd = xres; else maxd = yres; } /* Look at maps given on command line */ if (vect) { for (i = 0; vect[i]; i++) ; nvects = i; for (i = 0; field_opt->answers[i]; i++) ; if (nvects != i) G_fatal_error(_("Number of given vector maps (%d) differs from number of layers (%d)"), nvects, i); Map = (struct Map_info *) G_malloc(nvects * sizeof(struct Map_info)); field = (int *) G_malloc(nvects * sizeof(int)); for (i = 0; i < nvects; i++) { level = Vect_open_old2(&Map[i], vect[i], "", field_opt->answers[i]); if (level < 2) G_fatal_error(_("You must build topology on vector map <%s>"), vect[i]); field[i] = Vect_get_field_number(&Map[i], field_opt->answers[i]); } } if (!coords_opt->answer) { /* read them from stdin */ setvbuf(stdin, NULL, _IOLBF, 0); setvbuf(stdout, NULL, _IOLBF, 0); while (fgets(buf, sizeof(buf), stdin) != NULL) { ret = sscanf(buf, "%lf%c%lf", &xval, &ch, &yval); if (ret == 3 && (ch == ',' || ch == ' ' || ch == '\t')) { what(xval, yval, maxd, topo_flag->answer, printattributes->answer, shell_flag->answer, field); } else { G_warning(_("Unknown input format, skipping: '%s'"), buf); continue; } } } else { /* use coords given on command line */ for (i = 0; coords_opt->answers[i] != NULL; i += 2) { xval = atof(coords_opt->answers[i]); yval = atof(coords_opt->answers[i + 1]); what(xval, yval, maxd, topo_flag->answer, printattributes->answer, shell_flag->answer, field); } } for (i = 0; i < nvects; i++) Vect_close(&Map[i]); exit(EXIT_SUCCESS); }
int close_streamvect(char *stream_vect) { int r, c, r_nbr, c_nbr, done; GW_LARGE_INT i; CELL stream_id, stream_nbr; ASP_FLAG af; int next_node; struct sstack { int stream_id; int next_trib; } *nodestack; int top = 0, stack_step = 1000; int asp_r[9] = { 0, -1, -1, -1, 0, 1, 1, 1, 0 }; int asp_c[9] = { 0, 1, 0, -1, -1, -1, 0, 1, 1 }; struct Map_info Out; static struct line_pnts *Points; struct line_cats *Cats; dbDriver *driver; dbHandle handle; dbString table_name, dbsql, valstr; struct field_info *Fi; char *cat_col_name = "cat", buf[2000]; struct Cell_head window; double north_offset, west_offset, ns_res, ew_res; int next_cat; G_message(_("Writing vector map <%s>..."), stream_vect); if (Vect_open_new(&Out, stream_vect, 0) < 0) G_fatal_error(_("Unable to create vector map <%s>"), stream_vect); nodestack = (struct sstack *)G_malloc(stack_step * sizeof(struct sstack)); Points = Vect_new_line_struct(); Cats = Vect_new_cats_struct(); G_get_set_window(&window); ns_res = window.ns_res; ew_res = window.ew_res; north_offset = window.north - 0.5 * ns_res; west_offset = window.west + 0.5 * ew_res; next_cat = n_stream_nodes + 1; for (i = 0; i < n_outlets; i++, next_cat++) { G_percent(i, n_outlets, 2); r = outlets[i].r; c = outlets[i].c; cseg_get(&stream, &stream_id, r, c); if (!stream_id) continue; Vect_reset_line(Points); Vect_reset_cats(Cats); /* outlet */ Vect_cat_set(Cats, 1, stream_id); Vect_cat_set(Cats, 2, 2); Vect_append_point(Points, west_offset + c * ew_res, north_offset - r * ns_res, 0); Vect_write_line(&Out, GV_POINT, Points, Cats); /* add root node to stack */ G_debug(3, "add root node"); top = 0; nodestack[top].stream_id = stream_id; nodestack[top].next_trib = 0; /* depth first post order traversal */ G_debug(3, "traverse"); while (top >= 0) { done = 1; stream_id = nodestack[top].stream_id; G_debug(3, "stream_id %d", stream_id); if (nodestack[top].next_trib < stream_node[stream_id].n_trib) { /* add to stack */ next_node = stream_node[stream_id].trib[nodestack[top].next_trib]; G_debug(3, "add to stack: next %d, trib %d, n trib %d", next_node, nodestack[top].next_trib, stream_node[stream_id].n_trib); nodestack[top].next_trib++; top++; if (top >= stack_step) { /* need more space */ stack_step += 1000; nodestack = (struct sstack *)G_realloc(nodestack, stack_step * sizeof(struct sstack)); } nodestack[top].next_trib = 0; nodestack[top].stream_id = next_node; done = 0; G_debug(3, "go further down"); } if (done) { G_debug(3, "write stream segment"); Vect_reset_line(Points); Vect_reset_cats(Cats); r_nbr = stream_node[stream_id].r; c_nbr = stream_node[stream_id].c; cseg_get(&stream, &stream_nbr, r_nbr, c_nbr); if (stream_nbr <= 0) G_fatal_error(_("Stream id %d not set, top is %d, parent is %d"), stream_id, top, nodestack[top - 1].stream_id); Vect_cat_set(Cats, 1, stream_id); if (stream_node[stream_id].n_trib == 0) Vect_cat_set(Cats, 2, 0); else Vect_cat_set(Cats, 2, 1); Vect_append_point(Points, west_offset + c_nbr * ew_res, north_offset - r_nbr * ns_res, 0); Vect_write_line(&Out, GV_POINT, Points, Cats); seg_get(&aspflag, (char *)&af, r_nbr, c_nbr); while (af.asp > 0) { r_nbr = r_nbr + asp_r[(int)af.asp]; c_nbr = c_nbr + asp_c[(int)af.asp]; cseg_get(&stream, &stream_nbr, r_nbr, c_nbr); if (stream_nbr <= 0) G_fatal_error(_("Stream id not set while tracing")); Vect_append_point(Points, west_offset + c_nbr * ew_res, north_offset - r_nbr * ns_res, 0); if (stream_nbr != stream_id) { /* first point of parent stream */ break; } seg_get(&aspflag, (char *)&af, r_nbr, c_nbr); } Vect_write_line(&Out, GV_LINE, Points, Cats); top--; } } } G_percent(n_outlets, n_outlets, 1); /* finish it */ G_message(_("Writing attribute data...")); /* Prepeare strings for use in db_* calls */ db_init_string(&dbsql); db_init_string(&valstr); db_init_string(&table_name); db_init_handle(&handle); /* Preparing database for use */ /* Create database for new vector map */ Fi = Vect_default_field_info(&Out, 1, NULL, GV_1TABLE); driver = db_start_driver_open_database(Fi->driver, Vect_subst_var(Fi->database, &Out)); if (driver == NULL) { G_fatal_error(_("Unable to start driver <%s>"), Fi->driver); } db_set_error_handler_driver(driver); G_debug(1, "table: %s", Fi->table); G_debug(1, "driver: %s", Fi->driver); G_debug(1, "database: %s", Fi->database); sprintf(buf, "create table %s (%s integer, stream_type varchar(20), type_code integer)", Fi->table, cat_col_name); db_set_string(&dbsql, buf); if (db_execute_immediate(driver, &dbsql) != DB_OK) { db_close_database(driver); db_shutdown_driver(driver); G_fatal_error(_("Unable to create table: '%s'"), db_get_string(&dbsql)); } if (db_create_index2(driver, Fi->table, cat_col_name) != DB_OK) G_warning(_("Unable to create index on table <%s>"), Fi->table); 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); /* stream nodes */ for (i = 1; i <= n_stream_nodes; i++) { sprintf(buf, "insert into %s values ( %lld, \'%s\', %d )", Fi->table, i, (stream_node[i].n_trib > 0 ? "intermediate" : "start"), (stream_node[i].n_trib > 0)); db_set_string(&dbsql, buf); if (db_execute_immediate(driver, &dbsql) != DB_OK) { db_close_database(driver); db_shutdown_driver(driver); G_fatal_error(_("Unable to insert new row: '%s'"), db_get_string(&dbsql)); } } db_commit_transaction(driver); db_close_database_shutdown_driver(driver); Vect_map_add_dblink(&Out, 1, NULL, Fi->table, cat_col_name, Fi->database, Fi->driver); G_debug(1, "close vector"); Vect_hist_command(&Out); Vect_build(&Out); Vect_close(&Out); G_free(nodestack); return 1; }
QStringList QgsGrassSelect::vectorLayers( QString gisdbase, QString location, QString mapset, QString mapName ) { QStringList list; // Set location QgsGrass::setLocation( gisdbase, location ); /* Open vector */ QgsGrass::resetError(); //Vect_set_open_level( 2 ); struct Map_info map; int level = -1; try { level = Vect_open_old_head( &map, ( char * ) mapName.toUtf8().data(), ( char * ) mapset.toUtf8().data() ); } catch ( QgsGrass::Exception &e ) { Q_UNUSED( e ); QgsDebugMsg( QString( "Cannot open GRASS vector: %1" ).arg( e.what() ) ); return list; } if ( level == 1 ) { QgsDebugMsg( "Cannot open vector on level 2" ); QMessageBox::warning( 0, tr( "Warning" ), tr( "Cannot open vector %1 in mapset %2 on level 2 (topology not available, try to rebuild topology using v.build module)." ).arg( mapName ).arg( mapset ) ); // Vect_close here is correct, it should work, but it seems to cause // crash on win http://trac.osgeo.org/qgis/ticket/2003 // disabled on win test it #if !defined(WIN32) Vect_close( &map ); #endif return list; } else if ( level < 1 ) { QgsDebugMsg( "Cannot open vector" ); QMessageBox::warning( 0, tr( "Warning" ), tr( "Cannot open vector %1 in mapset %2" ).arg( mapName ).arg( mapset ) ); return list; } QgsDebugMsg( "GRASS vector successfully opened" ); // Get layers int ncidx = Vect_cidx_get_num_fields( &map ); for ( int i = 0; i < ncidx; i++ ) { int field = Vect_cidx_get_field_number( &map, i ); QString fs; fs.sprintf( "%d", field ); QgsDebugMsg( QString( "i = %1 layer = %2" ).arg( i ).arg( field ) ); /* Points */ int npoints = Vect_cidx_get_type_count( &map, field, GV_POINT ); if ( npoints > 0 ) { QString l = fs + "_point"; list.append( l ); } /* Lines */ /* Lines without category appears in layer 0, but not boundaries */ int tp; if ( field == 0 ) tp = GV_LINE; else tp = GV_LINE | GV_BOUNDARY; int nlines = Vect_cidx_get_type_count( &map, field, tp ); if ( nlines > 0 ) { QString l = fs + "_line"; list.append( l ); } /* Polygons */ int nareas = Vect_cidx_get_type_count( &map, field, GV_AREA ); if ( nareas > 0 ) { QString l = fs + "_polygon"; list.append( l ); } } Vect_close( &map ); return list; }
int main(int argc, char *argv[]) { struct GModule *module; struct Option *map_opt, *type_opt, *field_opt, *col_opt, *where_opt, *percentile; struct Flag *shell_flag, *extended; struct Map_info Map; struct field_info *Fi; dbDriver *Driver; dbCatValArray Cvarr; struct line_pnts *Points; struct line_cats *Cats; int otype, ofield; int compatible = 1; /* types are compatible: point+centroid or line+boundary or area */ int nrec, ctype, nlines, line, nareas, area; int nmissing = 0; /* number of missing atttributes */ int nnull = 0; /* number of null values */ int first = 1; /* Statistics */ int count = 0; /* number of features with non-null attribute */ double sum = 0.0; double sumsq = 0.0; double sumcb = 0.0; double sumqt = 0.0; double sum_abs = 0.0; double min = 0.0 / 0.0; /* init as nan */ double max = 0.0 / 0.0; double mean, mean_abs, pop_variance, sample_variance, pop_stdev, sample_stdev, pop_coeff_variation, kurtosis, skewness; double total_size = 0.0; /* total size: length/area */ /* Extended statistics */ int perc; module = G_define_module(); G_add_keyword(_("vector")); G_add_keyword(_("statistics")); module->label = _("Calculates univariate statistics for attribute."); module->description = _("Variance and standard " "deviation is calculated only for points if specified."); map_opt = G_define_standard_option(G_OPT_V_MAP); field_opt = G_define_standard_option(G_OPT_V_FIELD); type_opt = G_define_standard_option(G_OPT_V_TYPE); type_opt->options = "point,line,boundary,centroid,area"; col_opt = G_define_standard_option(G_OPT_DB_COLUMN); col_opt->required = YES; where_opt = G_define_standard_option(G_OPT_DB_WHERE); percentile = G_define_option(); percentile->key = "percentile"; percentile->type = TYPE_INTEGER; percentile->required = NO; percentile->options = "0-100"; percentile->answer = "90"; percentile->description = _("Percentile to calculate (requires extended statistics flag)"); shell_flag = G_define_flag(); shell_flag->key = 'g'; shell_flag->description = _("Print the stats in shell script style"); extended = G_define_flag(); extended->key = 'e'; extended->description = _("Calculate extended statistics"); G_gisinit(argv[0]); if (G_parser(argc, argv)) exit(EXIT_FAILURE); otype = Vect_option_to_types(type_opt); perc = atoi(percentile->answer); Points = Vect_new_line_struct(); Cats = Vect_new_cats_struct(); /* open input vector */ Vect_set_open_level(2); Vect_open_old2(&Map, map_opt->answer, "", field_opt->answer); ofield = Vect_get_field_number(&Map, field_opt->answer); /* Check if types are compatible */ if ((otype & GV_POINTS) && ((otype & GV_LINES) || (otype & GV_AREA))) compatible = 0; if ((otype & GV_LINES) && (otype & GV_AREA)) compatible = 0; if (!compatible) { G_warning(_("Incompatible vector type(s) specified, only number of features, minimum, maximum and range " "can be calculated")); } if (extended->answer && !(otype & GV_POINTS)) { G_warning(_("Extended statistics is currently supported only for points/centroids")); } /* Read attributes */ db_CatValArray_init(&Cvarr); Fi = Vect_get_field(&Map, ofield); if (Fi == NULL) { G_fatal_error(_(" Database connection not defined for layer <%s>"), field_opt->answer); } Driver = db_start_driver_open_database(Fi->driver, Fi->database); if (Driver == NULL) G_fatal_error("Unable to open database <%s> by driver <%s>", Fi->database, Fi->driver); /* Note do not check if the column exists in the table because it may be an expression */ nrec = db_select_CatValArray(Driver, Fi->table, Fi->key, col_opt->answer, where_opt->answer, &Cvarr); G_debug(2, "nrec = %d", nrec); ctype = Cvarr.ctype; if (ctype != DB_C_TYPE_INT && ctype != DB_C_TYPE_DOUBLE) G_fatal_error(_("Column type not supported")); if (nrec < 0) G_fatal_error(_("Unable to select data from table")); db_close_database_shutdown_driver(Driver); /* Lines */ nlines = Vect_get_num_lines(&Map); for (line = 1; line <= nlines; line++) { int i, type; G_debug(3, "line = %d", line); type = Vect_read_line(&Map, Points, Cats, line); if (!(type & otype)) continue; for (i = 0; i < Cats->n_cats; i++) { if (Cats->field[i] == ofield) { double val; dbCatVal *catval; G_debug(3, "cat = %d", Cats->cat[i]); if (db_CatValArray_get_value(&Cvarr, Cats->cat[i], &catval) != DB_OK) { G_debug(3, "No record for cat = %d", Cats->cat[i]); nmissing++; continue; } if (catval->isNull) { G_debug(3, "NULL value for cat = %d", Cats->cat[i]); nnull++; continue; } if (ctype == DB_C_TYPE_INT) { val = catval->val.i; } else if (ctype == DB_C_TYPE_DOUBLE) { val = catval->val.d; } count++; if (first) { max = val; min = val; first = 0; } else { if (val > max) max = val; if (val < min) min = val; } if (compatible) { if (type & GV_POINTS) { sum += val; sumsq += val * val; sumcb += val * val * val; sumqt += val * val * val * val; sum_abs += fabs(val); } else { /* GV_LINES */ double l; l = Vect_line_length(Points); sum += l * val; sumsq += l * val * val; sumcb += l * val * val * val; sumqt += l * val * val * val * val; sum_abs += l * fabs(val); total_size += l; } } G_debug(3, "sum = %f total_size = %f", sum, total_size); } } } if (otype & GV_AREA) { nareas = Vect_get_num_areas(&Map); for (area = 1; area <= nareas; area++) { int i, centr; G_debug(3, "area = %d", area); centr = Vect_get_area_centroid(&Map, area); if (centr < 1) continue; G_debug(3, "centr = %d", centr); Vect_read_line(&Map, NULL, Cats, centr); for (i = 0; i < Cats->n_cats; i++) { if (Cats->field[i] == ofield) { double val; dbCatVal *catval; G_debug(3, "cat = %d", Cats->cat[i]); if (db_CatValArray_get_value (&Cvarr, Cats->cat[i], &catval) != DB_OK) { G_debug(3, "No record for cat = %d", Cats->cat[i]); nmissing++; continue; } if (catval->isNull) { G_debug(3, "NULL value for cat = %d", Cats->cat[i]); nnull++; continue; } if (ctype == DB_C_TYPE_INT) { val = catval->val.i; } else if (ctype == DB_C_TYPE_DOUBLE) { val = catval->val.d; } count++; if (first) { max = val; min = val; first = 0; } else { if (val > max) max = val; if (val < min) min = val; } if (compatible) { double a; a = Vect_get_area_area(&Map, area); sum += a * val; sumsq += a * val * val; sumcb += a * val * val * val; sumqt += a * val * val * val * val; sum_abs += a * fabs(val); total_size += a; } G_debug(4, "sum = %f total_size = %f", sum, total_size); } } } } G_debug(2, "sum = %f total_size = %f", sum, total_size); if (compatible) { if ((otype & GV_LINES) || (otype & GV_AREA)) { mean = sum / total_size; mean_abs = sum_abs / total_size; /* Roger Bivand says it is wrong see GRASS devel list 7/2004 */ /* pop_variance = (sumsq - sum*sum/total_size)/total_size; pop_stdev = sqrt(pop_variance); */ } else { double n = count; mean = sum / count; mean_abs = sum_abs / count; pop_variance = (sumsq - sum * sum / count) / count; pop_stdev = sqrt(pop_variance); pop_coeff_variation = pop_stdev / (sqrt(sum * sum) / count); sample_variance = (sumsq - sum * sum / count) / (count - 1); sample_stdev = sqrt(sample_variance); kurtosis = (sumqt / count - 4 * sum * sumcb / (n * n) + 6 * sum * sum * sumsq / (n * n * n) - 3 * sum * sum * sum * sum / (n * n * n * n)) / (sample_stdev * sample_stdev * sample_stdev * sample_stdev) - 3; skewness = (sumcb / n - 3 * sum * sumsq / (n * n) + 2 * sum * sum * sum / (n * n * n)) / (sample_stdev * sample_stdev * sample_stdev); } } G_debug(3, "otype %d:", otype); if (shell_flag->answer) { fprintf(stdout, "n=%d\n", count); fprintf(stdout, "nmissing=%d\n", nmissing); fprintf(stdout, "nnull=%d\n", nnull); if (count > 0) { fprintf(stdout, "min=%g\n", min); fprintf(stdout, "max=%g\n", max); fprintf(stdout, "range=%g\n", max - min); if (compatible && (otype & GV_POINTS)) { fprintf(stdout, "mean=%g\n", mean); fprintf(stdout, "mean_abs=%g\n", mean_abs); fprintf(stdout, "population_stddev=%g\n", pop_stdev); fprintf(stdout, "population_variance=%g\n", pop_variance); fprintf(stdout, "population_coeff_variation=%g\n", pop_coeff_variation); if (otype & GV_POINTS) { fprintf(stdout, "sample_stddev=%g\n", sample_stdev); fprintf(stdout, "sample_variance=%g\n", sample_variance); fprintf(stdout, "kurtosis=%g\n", kurtosis); fprintf(stdout, "skewness=%g\n", skewness); } } } } else { fprintf(stdout, "number of features with non NULL attribute: %d\n", count); fprintf(stdout, "number of missing attributes: %d\n", nmissing); fprintf(stdout, "number of NULL attributes: %d\n", nnull); if (count > 0) { fprintf(stdout, "minimum: %g\n", min); fprintf(stdout, "maximum: %g\n", max); fprintf(stdout, "range: %g\n", max - min); if (compatible && (otype & GV_POINTS)) { fprintf(stdout, "mean: %g\n", mean); fprintf(stdout, "mean of absolute values: %g\n", mean_abs); fprintf(stdout, "population standard deviation: %g\n", pop_stdev); fprintf(stdout, "population variance: %g\n", pop_variance); fprintf(stdout, "population coefficient of variation: %g\n", pop_coeff_variation); if (otype & GV_POINTS) { fprintf(stdout, "sample standard deviation: %g\n", sample_stdev); fprintf(stdout, "sample variance: %g\n", sample_variance); fprintf(stdout, "kurtosis: %g\n", kurtosis); fprintf(stdout, "skewness: %g\n", skewness); } } } } /* TODO: mode, skewness, kurtosis */ if (extended->answer && compatible && (otype & GV_POINTS) && count > 0) { double quartile_25 = 0.0, quartile_75 = 0.0, quartile_perc = 0.0; double median = 0.0; int qpos_25, qpos_75, qpos_perc; qpos_25 = (int)(count * 0.25 - 0.5); qpos_75 = (int)(count * 0.75 - 0.5); qpos_perc = (int)(count * perc / 100. - 0.5); if (db_CatValArray_sort_by_value(&Cvarr) != DB_OK) G_fatal_error(_("Cannot sort the key/value array")); if (Cvarr.ctype == DB_C_TYPE_INT) { quartile_25 = (Cvarr.value[qpos_25]).val.i; if (count % 2) /* odd */ median = (Cvarr.value[(int)(count / 2)]).val.i; else /* even */ median = ((Cvarr.value[count / 2 - 1]).val.i + (Cvarr.value[count / 2]).val.i) / 2.0; quartile_75 = (Cvarr.value[qpos_75]).val.i; quartile_perc = (Cvarr.value[qpos_perc]).val.i; } else { /* must be DB_C_TYPE_DOUBLE */ quartile_25 = (Cvarr.value[qpos_25]).val.d; if (count % 2) /* odd */ median = (Cvarr.value[(int)(count / 2)]).val.d; else /* even */ median = ((Cvarr.value[count / 2 - 1]).val.d + (Cvarr.value[count / 2]).val.d) / 2.0; quartile_75 = (Cvarr.value[qpos_75]).val.d; quartile_perc = (Cvarr.value[qpos_perc]).val.d; } if (shell_flag->answer) { fprintf(stdout, "first_quartile=%g\n", quartile_25); fprintf(stdout, "median=%g\n", median); fprintf(stdout, "third_quartile=%g\n", quartile_75); fprintf(stdout, "percentile_%d=%g\n", perc, quartile_perc); } else { fprintf(stdout, "1st quartile: %g\n", quartile_25); if (count % 2) fprintf(stdout, "median (odd number of cells): %g\n", median); else fprintf(stdout, "median (even number of cells): %g\n", median); fprintf(stdout, "3rd quartile: %g\n", quartile_75); if (perc % 10 == 1 && perc != 11) fprintf(stdout, "%dst percentile: %g\n", perc, quartile_perc); else if (perc % 10 == 2 && perc != 12) fprintf(stdout, "%dnd percentile: %g\n", perc, quartile_perc); else if (perc % 10 == 3 && perc != 13) fprintf(stdout, "%drd percentile: %g\n", perc, quartile_perc); else fprintf(stdout, "%dth percentile: %g\n", perc, quartile_perc); } } Vect_close(&Map); exit(EXIT_SUCCESS); }
int main(int argc, char *argv[]) { struct GModule *module; struct { struct Flag *r, *w, *l, *g, *a, *n, *c; } flag; struct { struct Option *map, *field, *colr, *rast, *volume, *rules, *attrcol, *rgbcol, *range, *use; } opt; int layer; int overwrite, remove, is_from_stdin, stat, have_colors, convert, use; const char *mapset, *cmapset; const char *style, *rules, *cmap, *attrcolumn, *rgbcolumn; char *name; struct Map_info Map; struct FPRange range; struct Colors colors, colors_tmp; /* struct Cell_stats statf; */ G_gisinit(argv[0]); module = G_define_module(); G_add_keyword(_("vector")); G_add_keyword(_("color table")); module->description = _("Creates/modifies the color table associated with a vector map."); opt.map = G_define_standard_option(G_OPT_V_MAP); opt.field = G_define_standard_option(G_OPT_V_FIELD); opt.use = G_define_option(); opt.use->key = "use"; opt.use->type = TYPE_STRING; opt.use->required = YES; opt.use->multiple = NO; opt.use->options = "attr,cat,z"; opt.use->description = _("Source values"); G_asprintf((char **) &(opt.use->descriptions), "attr;%s;cat;%s;z;%s", _("read values from attribute table (requires <column> option)"), _("use category values"), _("use z coordinate (3D points or centroids only)")); opt.use->answer = "cat"; opt.attrcol = G_define_standard_option(G_OPT_DB_COLUMN); opt.attrcol->label = _("Name of column containing numeric data"); opt.attrcol->description = _("Required for use=attr"); opt.attrcol->guisection = _("Define"); opt.range = G_define_option(); opt.range->key = "range"; opt.range->type = TYPE_DOUBLE; opt.range->required = NO; opt.range->label = _("Manually set range (refers to 'column' option)"); opt.range->description = _("Ignored when 'rules' given"); opt.range->key_desc = "min,max"; opt.colr = G_define_standard_option(G_OPT_M_COLR); opt.colr->guisection = _("Define"); opt.rast = G_define_standard_option(G_OPT_R_INPUT); opt.rast->key = "raster"; opt.rast->required = NO; opt.rast->description = _("Raster map from which to copy color table"); opt.rast->guisection = _("Define"); opt.volume = G_define_standard_option(G_OPT_R3_INPUT); opt.volume->key = "raster_3d"; opt.volume->required = NO; opt.volume->description = _("3D raster map from which to copy color table"); opt.volume->guisection = _("Define"); opt.rules = G_define_standard_option(G_OPT_F_INPUT); opt.rules->key = "rules"; opt.rules->required = NO; opt.rules->description = _("Path to rules file"); opt.rules->guisection = _("Define"); opt.rgbcol = G_define_standard_option(G_OPT_DB_COLUMN); opt.rgbcol->key = "rgb_column"; opt.rgbcol->label = _("Name of color column to populate RGB values"); opt.rgbcol->description = _("If not given writes color table"); flag.r = G_define_flag(); flag.r->key = 'r'; flag.r->description = _("Remove existing color table"); flag.r->guisection = _("Remove"); flag.w = G_define_flag(); flag.w->key = 'w'; flag.w->description = _("Only write new color table if it does not already exist"); flag.l = G_define_flag(); flag.l->key = 'l'; flag.l->description = _("List available rules then exit"); flag.l->suppress_required = YES; flag.l->guisection = _("Print"); flag.n = G_define_flag(); flag.n->key = 'n'; flag.n->description = _("Invert colors"); flag.n->guisection = _("Define"); flag.g = G_define_flag(); flag.g->key = 'g'; flag.g->description = _("Logarithmic scaling"); flag.g->guisection = _("Define"); flag.a = G_define_flag(); flag.a->key = 'a'; flag.a->description = _("Logarithmic-absolute scaling"); flag.a->guisection = _("Define"); flag.c = G_define_flag(); flag.c->key = 'c'; flag.c->label = _("Convert color rules from RGB values to color table"); flag.c->description = _("Option 'rgb_column' with valid RGB values required"); /* TODO ? flag.e = G_define_flag(); flag.e->key = 'e'; flag.e->description = _("Histogram equalization"); flag.e->guisection = _("Define"); */ if (G_parser(argc, argv)) exit(EXIT_FAILURE); if (flag.l->answer) { G_list_color_rules(stdout); return EXIT_SUCCESS; } overwrite = !flag.w->answer; remove = flag.r->answer; name = opt.map->answer; style = opt.colr->answer; rules = opt.rules->answer; attrcolumn = opt.attrcol->answer; rgbcolumn = opt.rgbcol->answer; convert = flag.c->answer; use = USE_CAT; if (opt.use->answer) { switch (opt.use->answer[0]) { case 'a': use = USE_ATTR; break; case 'c': use = USE_CAT; break; case 'z': use = USE_Z; break; default: break; } } G_debug(1, "use=%d", use); if (!name) G_fatal_error(_("No vector map specified")); if (use == USE_ATTR && !attrcolumn) G_fatal_error(_("Option <%s> required"), opt.attrcol->key); if (use != USE_ATTR && attrcolumn) { G_important_message(_("Option <%s> given, assuming <use=attr>..."), opt.attrcol->key); use = USE_ATTR; } if (opt.rast->answer && opt.volume->answer) G_fatal_error(_("%s= and %s= are mutually exclusive"), opt.rast->key, opt.volume->key); cmap = NULL; if (opt.rast->answer) cmap = opt.rast->answer; if (opt.volume->answer) cmap = opt.volume->answer; if (!cmap && !style && !rules && !remove && !convert) G_fatal_error(_("One of -%c, -%c or %s=, %s= or %s= " "must be specified"), flag.r->key, flag.c->key, opt.colr->key, opt.rast->key, opt.rules->key); if (!!style + !!cmap + !!rules > 1) G_fatal_error(_("%s=, %s= and %s= are mutually exclusive"), opt.colr->key, opt.rules->key, opt.rast->key); if (flag.g->answer && flag.a->answer) G_fatal_error(_("-%c and -%c are mutually exclusive"), flag.g->key, flag.a->key); if (flag.c->answer && !rgbcolumn) G_fatal_error(_("%s= required for -%c"), opt.rgbcol->key, flag.c->key); is_from_stdin = rules && strcmp(rules, "-") == 0; if (is_from_stdin) G_fatal_error(_("Reading rules from standard input is not implemented yet, please provide path to rules file instead.")); mapset = G_find_vector(name, ""); if (!mapset) G_fatal_error(_("Vector map <%s> not found"), name); stat = -1; if (remove) { stat = Vect_remove_colors(name, mapset); if (stat < 0) G_fatal_error(_("Unable to remove color table of vector map <%s>"), name); if (stat == 0) G_warning(_("Color table of vector map <%s> not found"), name); return EXIT_SUCCESS; } G_suppress_warnings(TRUE); have_colors = Vect_read_colors(name, mapset, NULL); if (have_colors > 0 && !overwrite) { G_fatal_error(_("Color table exists. Exiting.")); } G_suppress_warnings(FALSE); /* open map and get min/max values */ Vect_set_open_level(1); /* no topology required */ if (Vect_open_old2(&Map, name, mapset, opt.field->answer) < 0) G_fatal_error(_("Unable to open vector map <%s>"), name); Vect_set_error_handler_io(&Map, NULL); if (use == USE_Z && !Vect_is_3d(&Map)) G_fatal_error(_("Vector map <%s> is not 3D"), Vect_get_full_name(&Map)); layer = Vect_get_field_number(&Map, opt.field->answer); if (layer < 1) G_fatal_error(_("Layer <%s> not found"), opt.field->answer); if (opt.range->answer) { range.min = atof(opt.range->answers[0]); range.max = atof(opt.range->answers[1]); if (range.min > range.max) G_fatal_error(_("Option <%s>: min must be greater or equal to max"), opt.range->key); } Rast_init_colors(&colors); if (is_from_stdin) { G_fatal_error(_("Reading color rules from standard input is currently not supported")); /* if (!read_color_rules(stdin, &colors, min, max, fp)) exit(EXIT_FAILURE); */ } else if (style || rules) { if (style && !G_find_color_rule(style)) G_fatal_error(_("Color table <%s> not found"), style); if (use == USE_CAT) { scan_cats(&Map, layer, style, rules, opt.range->answer ? &range : NULL, &colors); } else if (use == USE_Z) { scan_z(&Map, layer, style, rules, opt.range->answer ? &range : NULL, &colors); } else { scan_attr(&Map, layer, attrcolumn, style, rules, opt.range->answer ? &range : NULL, &colors); } } else { /* use color from another map (cmap) */ if (opt.rast->answer) { cmapset = G_find_raster2(cmap, ""); if (!cmapset) G_fatal_error(_("Raster map <%s> not found"), cmap); if (Rast_read_colors(cmap, cmapset, &colors) < 0) G_fatal_error(_("Unable to read color table for raster map <%s>"), cmap); } else if (opt.volume->answer) { cmapset = G_find_raster3d(cmap, ""); if (!cmapset) G_fatal_error(_("3D raster map <%s> not found"), cmap); if (Rast3d_read_colors(cmap, cmapset, &colors) < 0) G_fatal_error(_("Unable to read color table for 3D raster map <%s>"), cmap); } } if (flag.n->answer) Rast_invert_colors(&colors); /* TODO ? if (flag.e->answer) { if (!have_stats) have_stats = get_stats(name, mapset, &statf); Rast_histogram_eq_colors(&colors_tmp, &colors, &statf); colors = colors_tmp; } */ if (flag.g->answer) { Rast_log_colors(&colors_tmp, &colors, 100); colors = colors_tmp; } if (flag.a->answer) { Rast_abs_log_colors(&colors_tmp, &colors, 100); colors = colors_tmp; } G_important_message(_("Writing color rules...")); if (style || rules || opt.rast->answer || opt.volume->answer) { if (rgbcolumn) write_rgb_values(&Map, layer, rgbcolumn, &colors); else Vect_write_colors(name, mapset, &colors); } if (convert) { /* convert RGB values to color tables */ rgb2colr(&Map, layer, rgbcolumn, &colors); Vect_write_colors(name, mapset, &colors); } Vect_close(&Map); G_message(_("Color table for vector map <%s> set to '%s'"), G_fully_qualified_name(name, mapset), is_from_stdin || convert ? "rules" : style ? style : rules ? rules : cmap); exit(EXIT_SUCCESS); }
int main(int argc, char *argv[]) { int i, cat, with_z, more, ctype, nrows; char buf[DB_SQL_MAX]; int count; double coor[3]; int ncoor; struct Option *driver_opt, *database_opt, *table_opt; struct Option *xcol_opt, *ycol_opt, *zcol_opt, *keycol_opt, *where_opt, *outvect; struct Flag *same_table_flag; struct GModule *module; struct Map_info Map; struct line_pnts *Points; struct line_cats *Cats; dbString sql; dbDriver *driver; dbCursor cursor; dbTable *table; dbColumn *column; dbValue *value; struct field_info *fi; G_gisinit(argv[0]); module = G_define_module(); G_add_keyword(_("vector")); G_add_keyword(_("import")); G_add_keyword(_("database")); G_add_keyword(_("points")); module->description = _("Creates new vector (points) map from database table containing coordinates."); table_opt = G_define_standard_option(G_OPT_DB_TABLE); table_opt->required = YES; table_opt->description = _("Input table name"); driver_opt = G_define_standard_option(G_OPT_DB_DRIVER); driver_opt->options = db_list_drivers(); driver_opt->answer = (char *)db_get_default_driver_name(); driver_opt->guisection = _("Input DB"); database_opt = G_define_standard_option(G_OPT_DB_DATABASE); database_opt->answer = (char *)db_get_default_database_name(); database_opt->guisection = _("Input DB"); xcol_opt = G_define_standard_option(G_OPT_DB_COLUMN); xcol_opt->key = "x"; xcol_opt->required = YES; xcol_opt->description = _("Name of column containing x coordinate"); ycol_opt = G_define_standard_option(G_OPT_DB_COLUMN); ycol_opt->key = "y"; ycol_opt->required = YES; ycol_opt->description = _("Name of column containing y coordinate"); zcol_opt = G_define_standard_option(G_OPT_DB_COLUMN); zcol_opt->key = "z"; zcol_opt->description = _("Name of column containing z coordinate"); zcol_opt->guisection = _("3D output"); keycol_opt = G_define_standard_option(G_OPT_DB_COLUMN); keycol_opt->key = "key"; keycol_opt->required = NO; keycol_opt->label = _("Name of column containing category number"); keycol_opt->description = _("Must refer to an integer column"); where_opt = G_define_standard_option(G_OPT_DB_WHERE); where_opt->guisection = _("Selection"); outvect = G_define_standard_option(G_OPT_V_OUTPUT); same_table_flag = G_define_flag(); same_table_flag->key = 't'; same_table_flag->description = _("Use imported table as attribute table for new map"); if (G_parser(argc, argv)) exit(EXIT_FAILURE); if (zcol_opt->answer) { with_z = WITH_Z; ncoor = 3; } else { with_z = WITHOUT_Z; ncoor = 2; } Points = Vect_new_line_struct(); Cats = Vect_new_cats_struct(); db_init_string(&sql); if (G_get_overwrite()) { /* We don't want to delete the input table when overwriting the output * vector. */ char name[GNAME_MAX], mapset[GMAPSET_MAX]; if (!G_name_is_fully_qualified(outvect->answer, name, mapset)) { strcpy(name, outvect->answer); strcpy(mapset, G_mapset()); } Vect_set_open_level(1); /* no topo needed */ if (strcmp(mapset, G_mapset()) == 0 && G_find_vector2(name, mapset) && Vect_open_old(&Map, name, mapset) >= 0) { int num_dblinks; num_dblinks = Vect_get_num_dblinks(&Map); for (i = 0; i < num_dblinks; i++) { if ((fi = Vect_get_dblink(&Map, i)) != NULL && strcmp(fi->driver, driver_opt->answer) == 0 && strcmp(fi->database, database_opt->answer) == 0 && strcmp(fi->table, table_opt->answer) == 0) G_fatal_error(_("Vector map <%s> cannot be overwritten " "because input table <%s> is linked to " "this map."), outvect->answer, table_opt->answer); } Vect_close(&Map); } } if (Vect_open_new(&Map, outvect->answer, with_z) < 0) G_fatal_error(_("Unable to create vector map <%s>"), outvect->answer); Vect_set_error_handler_io(NULL, &Map); Vect_hist_command(&Map); fi = Vect_default_field_info(&Map, 1, NULL, GV_1TABLE); /* Open driver */ driver = db_start_driver_open_database(driver_opt->answer, database_opt->answer); if (driver == NULL) { G_fatal_error(_("Unable to open database <%s> by driver <%s>"), fi->database, fi->driver); } db_set_error_handler_driver(driver); /* check if target table already exists */ G_debug(3, "Output vector table <%s>, driver: <%s>, database: <%s>", outvect->answer, db_get_default_driver_name(), db_get_default_database_name()); if (!same_table_flag->answer && db_table_exists(db_get_default_driver_name(), db_get_default_database_name(), outvect->answer) == 1) G_fatal_error(_("Output vector map, table <%s> (driver: <%s>, database: <%s>) " "already exists"), outvect->answer, db_get_default_driver_name(), db_get_default_database_name()); if (keycol_opt->answer) { int coltype; coltype = db_column_Ctype(driver, table_opt->answer, keycol_opt->answer); if (coltype == -1) G_fatal_error(_("Column <%s> not found in table <%s>"), keycol_opt->answer, table_opt->answer); if (coltype != DB_C_TYPE_INT) G_fatal_error(_("Data type of key column must be integer")); } else { if (same_table_flag->answer) { G_fatal_error(_("Option <%s> must be specified when -%c flag is given"), keycol_opt->key, same_table_flag->key); } if (strcmp(db_get_default_driver_name(), "sqlite") != 0) G_fatal_error(_("Unable to define key column. This operation is not supported " "by <%s> driver. You need to define <%s> option."), fi->driver, keycol_opt->key); } /* Open select cursor */ sprintf(buf, "SELECT %s, %s", xcol_opt->answer, ycol_opt->answer); db_set_string(&sql, buf); if (with_z) { sprintf(buf, ", %s", zcol_opt->answer); db_append_string(&sql, buf); } if (keycol_opt->answer) { sprintf(buf, ", %s", keycol_opt->answer); db_append_string(&sql, buf); } sprintf(buf, " FROM %s", table_opt->answer); db_append_string(&sql, buf); if (where_opt->answer) { sprintf(buf, " WHERE %s", where_opt->answer); db_append_string(&sql, buf); } G_debug(2, "SQL: %s", db_get_string(&sql)); if (db_open_select_cursor(driver, &sql, &cursor, DB_SEQUENTIAL) != DB_OK) { G_fatal_error(_("Unable to open select cursor: '%s'"), db_get_string(&sql)); } table = db_get_cursor_table(&cursor); nrows = db_get_num_rows(&cursor); G_debug(2, "%d points selected", nrows); count = cat = 0; G_message(_("Writing features...")); while (db_fetch(&cursor, DB_NEXT, &more) == DB_OK && more) { G_percent(count, nrows, 2); /* key column */ if (keycol_opt->answer) { column = db_get_table_column(table, with_z ? 3 : 2); ctype = db_sqltype_to_Ctype(db_get_column_sqltype(column)); if (ctype != DB_C_TYPE_INT) G_fatal_error(_("Key column must be integer")); value = db_get_column_value(column); cat = db_get_value_int(value); } else { cat++; } /* coordinates */ for (i = 0; i < ncoor; i++) { column = db_get_table_column(table, i); ctype = db_sqltype_to_Ctype(db_get_column_sqltype(column)); if (ctype != DB_C_TYPE_INT && ctype != DB_C_TYPE_DOUBLE) G_fatal_error(_("x/y/z column must be integer or double")); value = db_get_column_value(column); if (ctype == DB_C_TYPE_INT) coor[i] = (double)db_get_value_int(value); else coor[i] = db_get_value_double(value); } Vect_reset_line(Points); Vect_reset_cats(Cats); Vect_append_point(Points, coor[0], coor[1], coor[2]); Vect_cat_set(Cats, 1, cat); Vect_write_line(&Map, GV_POINT, Points, Cats); count++; } G_percent(1, 1, 1); /* close connection to input DB before copying attributes */ db_close_database_shutdown_driver(driver); /* Copy table */ if (!same_table_flag->answer) { G_message(_("Copying attributes...")); if (DB_FAILED == db_copy_table_where(driver_opt->answer, database_opt->answer, table_opt->answer, fi->driver, fi->database, fi->table, where_opt->answer)) { /* where can be NULL */ G_warning(_("Unable to copy table")); } else { Vect_map_add_dblink(&Map, 1, NULL, fi->table, keycol_opt->answer ? keycol_opt->answer : GV_KEY_COLUMN, fi->database, fi->driver); } if (!keycol_opt->answer) { /* TODO: implement for all DB drivers in generic way if * possible */ driver = db_start_driver_open_database(fi->driver, fi->database); if (driver == NULL) { G_fatal_error(_("Unable to open database <%s> by driver <%s>"), fi->database, fi->driver); } db_set_error_handler_driver(driver); /* add key column */ sprintf(buf, "ALTER TABLE %s ADD COLUMN %s INTEGER", fi->table, GV_KEY_COLUMN); db_set_string(&sql, buf); if (db_execute_immediate(driver, &sql) != DB_OK) { G_fatal_error(_("Unable to add key column <%s>: " "SERIAL type is not supported by <%s>"), GV_KEY_COLUMN, fi->driver); } /* update key column */ sprintf(buf, "UPDATE %s SET %s = _ROWID_", fi->table, GV_KEY_COLUMN); db_set_string(&sql, buf); if (db_execute_immediate(driver, &sql) != DB_OK) { G_fatal_error(_("Failed to update key column <%s>"), GV_KEY_COLUMN); } } } else { /* do not copy attributes, link original table */ Vect_map_add_dblink(&Map, 1, NULL, table_opt->answer, keycol_opt->answer ? keycol_opt->answer : GV_KEY_COLUMN, database_opt->answer, driver_opt->answer); } Vect_build(&Map); Vect_close(&Map); G_done_msg(_n("%d point written to vector map.", "%d points written to vector map.", count), count); return (EXIT_SUCCESS); }
int main(int argc, char *argv[]) { char *p; int i, j, k; int method, half, use_catno; const char *mapset; struct GModule *module; struct Option *point_opt, /* point vector */ *area_opt, /* area vector */ *point_type_opt, /* point type */ *point_field_opt, /* point layer */ *area_field_opt, /* area layer */ *method_opt, /* stats method */ *point_column_opt, /* point column for stats */ *count_column_opt, /* area column for point count */ *stats_column_opt, /* area column for stats result */ *fs_opt; /* field separator for printed output */ struct Flag *print_flag; char *fs; struct Map_info PIn, AIn; int point_type, point_field, area_field; struct line_pnts *Points; struct line_cats *ACats, *PCats; AREA_CAT *Area_cat; int pline, ptype, count; int area, nareas, nacats, nacatsalloc; int ctype, nrec; struct field_info *PFi, *AFi; dbString stmt; dbDriver *Pdriver, *Adriver; char buf[2000]; int update_ok, update_err; struct boxlist *List; struct bound_box box; dbCatValArray cvarr; dbColumn *column; struct pvalcat { double dval; int catno; } *pvalcats; int npvalcats, npvalcatsalloc; stat_func *statsvalue = NULL; double result; column = NULL; G_gisinit(argv[0]); module = G_define_module(); G_add_keyword(_("vector")); G_add_keyword(_("attribute table")); G_add_keyword(_("database")); G_add_keyword(_("univariate statistics")); G_add_keyword(_("zonal statistics")); module->description = _("Count points in areas, calculate statistics from point attributes."); point_opt = G_define_standard_option(G_OPT_V_INPUT); point_opt->key = "points"; point_opt->description = _("Name of existing vector map with points"); /* point_opt->guisection = _("Required"); */ area_opt = G_define_standard_option(G_OPT_V_INPUT); area_opt->key = "areas"; area_opt->description = _("Name of existing vector map with areas"); /* area_opt->guisection = _("Required"); */ point_type_opt = G_define_standard_option(G_OPT_V_TYPE); point_type_opt->key = "type"; point_type_opt->options = "point,centroid"; point_type_opt->answer = "point"; point_type_opt->label = _("Feature type"); point_type_opt->required = NO; point_field_opt = G_define_standard_option(G_OPT_V_FIELD); point_field_opt->key = "player"; point_field_opt->label = _("Layer number for points map"); area_field_opt = G_define_standard_option(G_OPT_V_FIELD); area_field_opt->key = "alayer"; area_field_opt->label = _("Layer number for area map"); method_opt = G_define_option(); method_opt->key = "method"; method_opt->type = TYPE_STRING; method_opt->required = NO; method_opt->multiple = NO; p = G_malloc(1024); for (i = 0; menu[i].name; i++) { if (i) strcat(p, ","); else *p = 0; strcat(p, menu[i].name); } method_opt->options = p; method_opt->description = _("Method for aggregate statistics"); point_column_opt = G_define_standard_option(G_OPT_DB_COLUMN); point_column_opt->key = "pcolumn"; point_column_opt->required = NO; point_column_opt->multiple = NO; point_column_opt->label = _("Column name of points map to use for statistics"); point_column_opt->description = _("Column of points map must be numeric"); count_column_opt = G_define_option(); count_column_opt->key = "ccolumn"; count_column_opt->type = TYPE_STRING; count_column_opt->required = NO; count_column_opt->multiple = NO; count_column_opt->label = _("Column name to upload points count"); count_column_opt->description = _("Column to hold points count, must be of type integer, will be created if not existing"); stats_column_opt = G_define_option(); stats_column_opt->key = "scolumn"; stats_column_opt->type = TYPE_STRING; stats_column_opt->required = NO; stats_column_opt->multiple = NO; stats_column_opt->label = _("Column name to upload statistics"); stats_column_opt->description = _("Column to hold statistics, must be of type double, will be created if not existing"); fs_opt = G_define_standard_option(G_OPT_F_SEP); print_flag = G_define_flag(); print_flag->key = 'p'; print_flag->label = _("Print output to stdout, do not update attribute table"); print_flag->description = _("First column is always area category"); if (G_parser(argc, argv)) exit(EXIT_FAILURE); point_type = Vect_option_to_types(point_type_opt); point_field = atoi(point_field_opt->answer); area_field = atoi(area_field_opt->answer); if (print_flag->answer) /* get field separator */ fs = G_option_to_separator(fs_opt); else fs = NULL; /* check for stats */ if (method_opt->answer) { if (!point_column_opt->answer) { G_fatal_error("Method but no point column selected"); } if (!print_flag->answer && !stats_column_opt->answer) G_fatal_error("Name for stats column is missing"); } if (point_column_opt->answer) { if (!method_opt->answer) G_fatal_error("No method for statistics selected"); if (!print_flag->answer && !stats_column_opt->answer) G_fatal_error("Name for stats column is missing"); } /* Open points vector */ if ((mapset = G_find_vector2(point_opt->answer, "")) == NULL) G_fatal_error(_("Vector map <%s> not found"), point_opt->answer); Vect_set_open_level(2); if (Vect_open_old(&PIn, point_opt->answer, mapset) < 0) G_fatal_error(_("Unable to open vector map <%s>"), point_opt->answer); /* Open areas vector */ if ((mapset = G_find_vector2(area_opt->answer, "")) == NULL) G_fatal_error(_("Vector map <%s> not found"), area_opt->answer); if (!print_flag->answer && strcmp(mapset, G_mapset()) != 0) G_fatal_error(_("Vector map <%s> is not in user mapset and cannot be updated"), area_opt->answer); Vect_set_open_level(2); if (Vect_open_old(&AIn, area_opt->answer, mapset) < 0) G_fatal_error(_("Unable to open vector map <%s>"), area_opt->answer); method = -1; use_catno = 0; half = 0; if (method_opt->answer) { /* get the method */ for (method = 0; (p = menu[method].name); method++) if ((strcmp(p, method_opt->answer) == 0)) break; if (!p) { G_warning(_("<%s=%s> unknown %s"), method_opt->key, method_opt->answer, method_opt->answer); G_usage(); exit(EXIT_FAILURE); } /* establish the statsvalue routine */ statsvalue = menu[method].method; /* category number of lowest/highest value */ if ((strcmp(menu[method].name, menu[5].name) == 0) || (strcmp(menu[method].name, menu[7].name) == 0)) use_catno = 1; G_debug(1, "method: %s, use cat value: %s", menu[method].name, (use_catno == 1 ? "yes" : "no")); } /* Open database driver */ db_init_string(&stmt); Adriver = NULL; if (!print_flag->answer) { AFi = Vect_get_field(&AIn, area_field); if (AFi == NULL) G_fatal_error(_("Database connection not defined for layer %d"), area_field); Adriver = db_start_driver_open_database(AFi->driver, AFi->database); if (Adriver == NULL) G_fatal_error(_("Unable to open database <%s> with driver <%s>"), AFi->database, AFi->driver); if (!count_column_opt->answer) G_fatal_error(_("ccolumn is required to upload point counts")); /* check if count column exists */ G_debug(1, "check if count column exists"); db_get_column(Adriver, AFi->table, count_column_opt->answer, &column); if (column) { /* check count column type */ if (db_column_Ctype(Adriver, AFi->table, count_column_opt->answer) != DB_C_TYPE_INT) G_fatal_error(_("ccolumn must be of type integer")); db_free_column(column); column = NULL; } else { /* create count column */ /* db_add_column() exists but is not implemented, * see lib/db/stubs/add_col.c */ sprintf(buf, "alter table %s add column %s integer", AFi->table, count_column_opt->answer); db_set_string(&stmt, buf); if (db_execute_immediate(Adriver, &stmt) != DB_OK) G_fatal_error(_("Unable to add column <%s>"), count_column_opt->answer); } if (method_opt->answer) { if (!stats_column_opt->answer) G_fatal_error(_("scolumn is required to upload point stats")); /* check if stats column exists */ G_debug(1, "check if stats column exists"); db_get_column(Adriver, AFi->table, stats_column_opt->answer, &column); if (column) { /* check stats column type */ if (db_column_Ctype (Adriver, AFi->table, stats_column_opt->answer) != DB_C_TYPE_DOUBLE) G_fatal_error(_("scolumn must be of type double")); db_free_column(column); column = NULL; } else { /* create stats column */ /* db_add_column() exists but is not implemented, * see lib/db/stubs/add_col.c */ sprintf(buf, "alter table %s add column %s double", AFi->table, stats_column_opt->answer); db_set_string(&stmt, buf); if (db_execute_immediate(Adriver, &stmt) != DB_OK) G_fatal_error(_("Unable to add column <%s>"), stats_column_opt->answer); } } } else AFi = NULL; Pdriver = NULL; if (method_opt->answer) { G_verbose_message(_("collecting attributes from points vector...")); PFi = Vect_get_field(&PIn, point_field); if (PFi == NULL) G_fatal_error(_("Database connection not defined for layer %d"), point_field); Pdriver = db_start_driver_open_database(PFi->driver, PFi->database); if (Pdriver == NULL) G_fatal_error(_("Unable to open database <%s> with driver <%s>"), PFi->database, PFi->driver); /* check if point column exists */ db_get_column(Pdriver, PFi->table, point_column_opt->answer, &column); if (column) { db_free_column(column); column = NULL; } else { G_fatal_error(_("Column <%s> not found in table <%s>"), point_column_opt->answer, PFi->table); } /* Check column type */ ctype = db_column_Ctype(Pdriver, PFi->table, point_column_opt->answer); if (ctype == DB_C_TYPE_INT) half = menu[method].half; else if (ctype == DB_C_TYPE_DOUBLE) half = 0; else G_fatal_error(_("column for points vector must be numeric")); db_CatValArray_init(&cvarr); nrec = db_select_CatValArray(Pdriver, PFi->table, PFi->key, point_column_opt->answer, NULL, &cvarr); G_debug(1, "selected values = %d", nrec); db_close_database_shutdown_driver(Pdriver); } Points = Vect_new_line_struct(); ACats = Vect_new_cats_struct(); PCats = Vect_new_cats_struct(); List = Vect_new_boxlist(0); /* Allocate space ( may be more than needed (duplicate cats and elements without cats) ) */ if ((nareas = Vect_get_num_areas(&AIn)) <= 0) G_fatal_error("No areas in area input vector"); nacatsalloc = nareas; Area_cat = (AREA_CAT *) G_calloc(nacatsalloc, sizeof(AREA_CAT)); /* Read all cats from 'area' */ nacats = 0; for (area = 1; area <= nareas; area++) { Vect_get_area_cats(&AIn, area, ACats); if (ACats->n_cats <= 0) continue; for (i = 0; i < ACats->n_cats; i++) { if (ACats->field[i] == area_field) { Area_cat[nacats].area_cat = ACats->cat[i]; Area_cat[nacats].count = 0; Area_cat[nacats].nvalues = 0; Area_cat[nacats].nalloc = 0; nacats++; if (nacats >= nacatsalloc) { nacatsalloc += 100; Area_cat = (AREA_CAT *) G_realloc(Area_cat, nacatsalloc * sizeof(AREA_CAT)); } } } } G_debug(1, "%d cats loaded from vector (including duplicates)", nacats); /* Sort by category */ qsort((void *)Area_cat, nacats, sizeof(AREA_CAT), cmp_area); /* remove duplicate categories */ for (i = 1; i < nacats; i++) { if (Area_cat[i].area_cat == Area_cat[i - 1].area_cat) { for (j = i; j < nacats - 1; j++) { Area_cat[j].area_cat = Area_cat[j + 1].area_cat; } nacats--; } } G_debug(1, "%d cats loaded from vector (unique)", nacats); /* Go through all areas in area vector and find points in points vector * falling into the area */ npvalcatsalloc = 10; npvalcats = 0; pvalcats = (struct pvalcat *)G_calloc(npvalcatsalloc, sizeof(struct pvalcat)); G_message(_("Selecting points for each area...")); count = 0; for (area = 1; area <= nareas; area++) { dbCatVal *catval; G_debug(3, "area = %d", area); G_percent(area, nareas, 2); Vect_get_area_cats(&AIn, area, ACats); if (ACats->n_cats <= 0) continue; /* select points by box */ Vect_get_area_box(&AIn, area, &box); box.T = PORT_DOUBLE_MAX; box.B = -PORT_DOUBLE_MAX; Vect_select_lines_by_box(&PIn, &box, point_type, List); G_debug(4, "%d points selected by box", List->n_values); /* For each point in box check if it is in the area */ for (i = 0; i < List->n_values; i++) { pline = List->id[i]; G_debug(4, "%d: point %d", i, pline); ptype = Vect_read_line(&PIn, Points, PCats, pline); if (!(ptype & point_type)) continue; /* point in area */ if (Vect_point_in_area(Points->x[0], Points->y[0], &AIn, area, &box)) { AREA_CAT *area_info, search_ai; int tmp_cat; /* stats on point column */ if (method_opt->answer) { npvalcats = 0; tmp_cat = -1; for (j = 0; j < PCats->n_cats; j++) { if (PCats->field[j] == point_field) { if (tmp_cat >= 0) G_debug(3, "More cats found in point layer (point=%d)", pline); tmp_cat = PCats->cat[j]; /* find cat in array */ db_CatValArray_get_value(&cvarr, tmp_cat, &catval); if (catval) { pvalcats[npvalcats].catno = tmp_cat; switch (cvarr.ctype) { case DB_C_TYPE_INT: pvalcats[npvalcats].dval = catval->val.i; npvalcats++; break; case DB_C_TYPE_DOUBLE: pvalcats[npvalcats].dval = catval->val.d; npvalcats++; break; } if (npvalcats >= npvalcatsalloc) { npvalcatsalloc += 10; pvalcats = (struct pvalcat *)G_realloc(pvalcats, npvalcatsalloc * sizeof (struct pvalcat)); } } } } } /* update count for all area cats of given field */ search_ai.area_cat = -1; for (j = 0; j < ACats->n_cats; j++) { if (ACats->field[j] == area_field) { if (search_ai.area_cat >= 0) G_debug(3, "More cats found in area layer (area=%d)", area); search_ai.area_cat = ACats->cat[j]; /* find cat in array */ area_info = (AREA_CAT *) bsearch((void *)&search_ai, Area_cat, nacats, sizeof(AREA_CAT), cmp_area); if (area_info->area_cat != search_ai.area_cat) G_fatal_error(_("could not find area category %d"), search_ai.area_cat); /* each point is counted once, also if it has * more than one category or no category * OK? */ area_info->count++; if (method_opt->answer) { /* ensure enough space */ if (area_info->nvalues + npvalcats >= area_info->nalloc) { if (area_info->nalloc == 0) { area_info->nalloc = npvalcats + 10; area_info->values = (double *)G_calloc(area_info->nalloc, sizeof(double)); area_info->cats = (int *)G_calloc(area_info->nalloc, sizeof(int)); } else area_info->nalloc += area_info->nvalues + npvalcats + 10; area_info->values = (double *)G_realloc(area_info->values, area_info->nalloc * sizeof(double)); area_info->cats = (int *)G_realloc(area_info->cats, area_info->nalloc * sizeof(int)); } for (k = 0; k < npvalcats; k++) { area_info->cats[area_info->nvalues] = pvalcats[k].catno; area_info->values[area_info->nvalues] = pvalcats[k].dval; area_info->nvalues++; } } } } count++; } } /* next point in box */ } /* next area */ G_debug(1, "count = %d", count); /* release catval array */ if (method_opt->answer) db_CatValArray_free(&cvarr); Vect_close(&PIn); /* Update table or print to stdout */ if (print_flag->answer) { /* print header */ fprintf(stdout, "area_cat%scount", fs); if (method_opt->answer) fprintf(stdout, "%s%s", fs, menu[method].name); fprintf(stdout, "\n"); } else { G_message("Updating attributes for area vector..."); update_err = update_ok = 0; } if (Adriver) db_begin_transaction(Adriver); for (i = 0; i < nacats; i++) { if (!print_flag->answer) G_percent(i, nacats, 2); result = 0; if (Area_cat[i].count > 0 && method_opt->answer) { /* get stats */ statsvalue(&result, Area_cat[i].values, Area_cat[i].nvalues, NULL); if (half) result += 0.5; else if (use_catno) result = Area_cat[i].cats[(int)result]; } if (print_flag->answer) { fprintf(stdout, "%d%s%d", Area_cat[i].area_cat, fs, Area_cat[i].count); if (method_opt->answer) { if (Area_cat[i].count > 0) fprintf(stdout, "%s%.15g", fs, result); else fprintf(stdout, "%snull", fs); } fprintf(stdout, "\n"); } else { sprintf(buf, "update %s set %s = %d", AFi->table, count_column_opt->answer, Area_cat[i].count); db_set_string(&stmt, buf); if (method_opt->answer) { if (Area_cat[i].count > 0) sprintf(buf, " , %s = %.15g", stats_column_opt->answer, result); else sprintf(buf, " , %s = null", stats_column_opt->answer); db_append_string(&stmt, buf); } sprintf(buf, " where %s = %d", AFi->key, Area_cat[i].area_cat); db_append_string(&stmt, buf); G_debug(2, "SQL: %s", db_get_string(&stmt)); if (db_execute_immediate(Adriver, &stmt) == DB_OK) { update_ok++; } else { update_err++; } } } if (Adriver) db_commit_transaction(Adriver); if (!print_flag->answer) { G_percent(nacats, nacats, 2); db_close_database_shutdown_driver(Adriver); db_free_string(&stmt); G_message(_("%d records updated"), update_ok); if (update_err > 0) G_message(_("%d update errors"), update_err); Vect_set_db_updated(&AIn); } Vect_close(&AIn); G_done_msg(" "); exit(EXIT_SUCCESS); }