int main(int argc, char *argv[]) { struct GModule *module; struct { struct Option *base, *cover, *output; } parm; char *basemap, *base_mapset; char *covermap, *cover_mapset; char *outmap; char command[1024]; struct Categories cover_cats; FILE *stats_fd, *reclass_fd; int first; long basecat, covercat, catb, catc; double area; struct stats stats; G_gisinit(argv[0]); module = G_define_module(); module->keywords = _("raster, statistics"); module->description = _("Finds the median of values in a cover map within " "areas assigned the same category value in a " "user-specified base map."); parm.base = G_define_standard_option(G_OPT_R_INPUT); parm.base->key = "base"; parm.base->description = _("Name of base raster map"); parm.cover = G_define_standard_option(G_OPT_R_INPUT); parm.cover->key = "cover"; parm.cover->description = _("Name of cover raster map"); parm.output = G_define_standard_option(G_OPT_R_OUTPUT); if (G_parser(argc, argv)) exit(EXIT_FAILURE); basemap = parm.base->answer; covermap = parm.cover->answer; outmap = parm.output->answer; base_mapset = G_find_cell2(basemap, ""); if (base_mapset == NULL) G_fatal_error(_("Base raster map <%s> not found"), basemap); cover_mapset = G_find_cell2(covermap, ""); if (cover_mapset == NULL) G_fatal_error(_("Raster map <%s> not found"), covermap); if (G_legal_filename(outmap) < 0) G_fatal_error(_("<%s> is an illegal file name"), outmap); if (strcmp(G_mapset(), base_mapset) == 0 && strcmp(basemap, outmap) == 0) G_fatal_error(_("Base map and output map <%s> must be different"), outmap); if (G_read_cats(covermap, cover_mapset, &cover_cats) < 0) G_fatal_error(_("Unable to read category labels of raster map <%s>"), covermap); strcpy(command, "r.stats -an \""); strcat(command, G_fully_qualified_name(basemap, base_mapset)); strcat(command, ","); strcat(command, G_fully_qualified_name(covermap, cover_mapset)); strcat(command, "\""); /* strcpy (command,"cat /tmp/t"); */ G_debug(3, "command: %s", command); stats_fd = popen(command, "r"); G_debug(3, "r.reclass i=\"%s\" o=\"%s\"", G_fully_qualified_name(basemap, base_mapset), outmap); sprintf(command, "r.reclass i=\"%s\" o=\"%s\"", G_fully_qualified_name(basemap, base_mapset), outmap); reclass_fd = popen(command, "w"); first = 1; while (read_stats(stats_fd, &basecat, &covercat, &area)) { if (first) { stats.n = 0; stats.nalloc = 16; stats.cat = (long *) G_calloc(stats.nalloc, sizeof(long)); stats.area = (double *) G_calloc(stats.nalloc, sizeof(double)); first = 0; catb = basecat; } if (basecat != catb) { catc = median(&stats); write_reclass(reclass_fd, catb, catc, G_get_cat(catc, &cover_cats)); catb = basecat; stats.n = 0; } stats.n++; if (stats.n > stats.nalloc) { stats.nalloc *= 2; stats.cat = (long *) G_realloc(stats.cat, stats.nalloc * sizeof(long)); stats.area = (double *) G_realloc(stats.area, stats.nalloc * sizeof(double)); } stats.cat[stats.n - 1] = covercat; stats.area[stats.n - 1] = area; } if (!first) { catc = median(&stats); write_reclass(reclass_fd, catb, catc, G_get_cat(catc, &cover_cats)); } pclose(stats_fd); pclose(reclass_fd); exit(EXIT_SUCCESS); }
/* This function is for floating point maps */ void do_report_DCELL ( char *map, char *mapset, char *sites, int precision, int null_flag, int uncat_flag, int all_flag, int quiet_flag, int skip_flag, char *logfile, int background, int gain, int show_progress) { DCELL *dcellbuf; DCELL dvalue; CELL value; struct Cell_head region; struct Categories categories; GT_Row_cache_t *cache; unsigned long row_idx, col_idx; short error; int fd; unsigned long i,j,k; unsigned long no_sites; FILE *lp; unsigned long nrows, ncols; unsigned long *share_smp = NULL; /* array that keeps percentage of sites */ double total = 0; double map_total = 0; double kvamme_gain; long null_count = 0; /* keeps count of sites on NULL cells */ long nocat_count = 0; /* sites on cells outside category range */ /* category counts and descriptions */ int cats; char **cats_description; /* category labels */ long *cat_count; /* category counts */ long null_count_map; /* number of NULL cells in input map */ long nocat_count_map; /* number of cells that do not fall into the category range [0 .. n] */ int debug_mode = 0; /* 1 to enable writing additional output to logfile */ time_t systime; char errmsg [200]; struct Map_info in_vect_map; struct line_pnts *vect_points; double x,y,z; int n_points = 1; int cur_type; /* get current region */ G_get_window (®ion); nrows = G_window_rows (); ncols = G_window_cols (); /* check logfile */ if (logfile != NULL) { debug_mode = 1; if ( !G_legal_filename (logfile) ) { delete_tmpfile (map); G_fatal_error ("Please specify a legal filename for the logfile.\n"); } /* attempt to write to logfile */ if ( (lp = fopen ( logfile, "w+" ) ) == NULL ) { delete_tmpfile (map); G_fatal_error ("Could not create logfile.\n"); } /* we want unbuffered output for the logfile */ setvbuf (lp,NULL,_IONBF,0); fprintf (lp,"This is s.report, version %.2f\n",PROGVERSION); systime = time (NULL); fprintf (lp,"Started on %s",ctime(&systime)); fprintf (lp,"\tlocation = %s\n",G_location()); fprintf (lp,"\tmapset = %s\n",G_mapset()); fprintf (lp,"\tinput map = %s\n",map); fprintf (lp,"\tsample file = %s\n",sites); } else { /* log output to stderr by default */ lp = stderr; } if (1 > Vect_open_old (&in_vect_map, sites, "")) { sprintf (errmsg, "Could not open input map %s.\n", sites); delete_tmpfile (map); G_fatal_error (errmsg); } vect_points = Vect_new_line_struct (); if (all_flag != 1) { Vect_set_constraint_region (&in_vect_map, region.north, region.south, region.east, region.west, 0.0, 0.0); } /* get total number of sampling points */ i = 0; while ((cur_type = Vect_read_next_line (&in_vect_map, vect_points, NULL) > 0)) { i ++; } no_sites = i; /* store this for later use */ /* open raster map */ fd = G_open_cell_old (map, G_find_cell (map, "")); if (fd < 0) { delete_tmpfile (map); G_fatal_error ("Could not open raster map for reading!\n"); } /* allocate a cache and a raster buffer */ cache = (GT_Row_cache_t *) G_malloc (sizeof (GT_Row_cache_t)); GT_RC_open (cache, CACHESIZE, fd, DCELL_TYPE); dcellbuf = G_allocate_raster_buf (DCELL_TYPE); cats = GT_get_stats (map,mapset,&null_count_map, &nocat_count_map, show_progress); if ( cats < 2 ) { delete_tmpfile (map); G_fatal_error ("Input map must have at least two categories."); } /* get category labels and counts */ cats_description = GT_get_labels (map,mapset); if (cats_description == NULL) { delete_tmpfile (map); G_fatal_error ("Could not read category labels from input map."); } cat_count = GT_get_f_counts (map,mapset, show_progress); if (cat_count == NULL) { delete_tmpfile (map); G_fatal_error ("Could not count categories in input map."); } /* now read category structure of input map*/ G_init_cats (0, "", &categories); error = G_read_cats (map, mapset, &categories); if (error != 0) { /* no categories found */ delete_tmpfile (map); G_fatal_error ("Could not read category information of input map."); } /* allocate a double array to hold statistics */ share_smp = (unsigned long *) G_malloc ((signed) (cats * sizeof (unsigned long))); for (i = 0; i < cats; i++) { share_smp[i] = 0; } /* count raster values under sites */ /* re-open sites file with samples */ i = 0; k = 0; /* progress counter for status display */ Vect_rewind (&in_vect_map); if ( !quiet_flag ) { fprintf (stdout, "Counting sample: \n"); fflush (stdout); } /* we MUST not set constraints so that no raster values outside the current region are accessed, which would give an "illegal cache request" error */ Vect_set_constraint_region (&in_vect_map, region.north, region.south, region.east, region.west, 0.0, 0.0); while ((cur_type = Vect_read_next_line (&in_vect_map, vect_points, NULL) > 0)) { Vect_copy_pnts_to_xyz (vect_points, &x, &y, &z, &n_points); k ++; if ( !quiet_flag ) { G_percent ((signed) k, (signed) no_sites, 1); } /* get raster row with same northing as sample and perform * quantification */ row_idx = (long) G_northing_to_row (y, ®ion); col_idx = (long) G_easting_to_col (x, ®ion); dcellbuf = GT_RC_get (cache, (signed) row_idx); /* now read the raster value under the current site */ if (G_is_d_null_value (&dcellbuf[col_idx]) == 0) { dvalue = dcellbuf[col_idx]; value = G_quant_get_cell_value (&categories.q, dvalue); if ( ! (( value < 0 ) || ( value > cats -1)) ) { share_smp [value] ++; i ++; } else { if ( uncat_flag ) { /* also keep count of sites on uncategorised cells? */ i ++; nocat_count++; } } } if (G_is_d_null_value (&dcellbuf[col_idx]) == 1) { /* got a NULL value under this site */ if (null_flag) { /* only count this, if null flag is set */ null_count ++; i ++; } } } Vect_close (&in_vect_map); fprintf (lp,"\n"); if ( background ) { fprintf (lp,"Distribution of categories under %lu points (%lu in region) and in input map:\n",i,no_sites); } else { fprintf (lp,"Distribution of categories under %lu points (%lu in region):\n",i,no_sites); } /* determine starting value for total of sites analysed */ total = 0; for ( j=0; j < cats; j ++) { total = total + share_smp[j]; map_total = map_total + cat_count[j]; } if (null_flag) { /* add NULL values to total */ total = total + null_count; map_total = map_total + null_count_map; } if (uncat_flag) { /* add uncategorised cells to total */ total = total + nocat_count; map_total = map_total + nocat_count_map; } /* Now display those values which the user has chosen */ if ( (background) && (gain) ) { fprintf (lp,"Cat.\tPts.\t(%%)\tMap\t(%%)\tGain\tDescription\n"); } if ( (background) && (!gain) ) { fprintf (lp,"Cat.\tPts.\t(%%)\tMap\t(%%)\tDescription\n"); } if ( (!background) && (gain) ) { fprintf (lp,"Cat.\tPts.\t(%%)\tGain\tDescription\n"); } if ( (!background) && (!gain) ) { fprintf (lp,"Cat.\tPts.\t(%%)\tDescription\n"); } for ( j = 0; j < cats; j ++) { /* if skip_flag is not set: only show categories that have count > 0 */ if ((skip_flag == 1) || ((skip_flag == 0) && (share_smp[j] > 0))) { if ( (background) && (gain) ) { /* Kvamme's Gain = 1 - (%area/%sites) */ kvamme_gain = gstats_gain_K(((double) share_smp[j]*(100/total)), ((double) cat_count[j]*(100/map_total))); fprintf (lp, "%lu\t%6lu\t%6.2f\t%8lu %6.2f\t%6.2f\t%s\n", j, share_smp[j], (float) share_smp[j]*(100/total), cat_count[j], (float) cat_count[j]*(100/map_total), kvamme_gain, cats_description[j]); } if ( (background) && (!gain) ) { fprintf (lp, "%lu\t%6lu\t%6.2f\t%8lu %6.2f\t%s\n", j, share_smp[j], (float) share_smp[j]*(100/total), cat_count[j], (float) cat_count[j]*(100/map_total), cats_description[j]); } if ( (!background) && (gain) ) { kvamme_gain = 1 - ( ((float) cat_count[j]*(100/map_total)) / ((float) share_smp[j]*(100/total)) ); fprintf (lp, "%lu\t%6lu\t%6.2f\t%6.2f\t%s\n", j, share_smp[j], (float) share_smp[j]*(100/total), kvamme_gain, cats_description[j]); } if ( (!background) && (!gain) ) { fprintf (lp, "%lu\t%6lu\t%6.2f\t%s\n", j, share_smp[j], (float) share_smp[j]*(100/total), cats_description[j]); } } } if (null_flag) { if ( background ) { fprintf (lp,"NULL\t%6lu\t%6.2f\t%8lu %6.2f\n",null_count, (float) null_count * 100 / total ,null_count_map, (float) null_count_map * 100 / map_total); } else { fprintf (lp,"NULL\t%6lu\t%6.2f\n",null_count, (float) null_count * 100 / total); } } if (uncat_flag) { if ( background ) { fprintf (lp,"NOCAT\t%6lu\t%6.2f\t%8lu %6.2f\n",nocat_count, (float) nocat_count * 100 / total ,nocat_count_map, (float) nocat_count_map * 100 / map_total); } else { fprintf (lp,"NOCAT\t%6lu\t%6.2f\n",nocat_count, (float) nocat_count * 100 / total); } } if ( background) { fprintf (lp,"TOTAL\t%6lu\t%6.2f\t%8lu %6.2f\n",(long) total, (float) 100, (long) map_total, (float) 100); } else { fprintf (lp,"TOTAL\t%6lu\t%6.2f\n",(long) total, (float) 100); } /* close cache and sites file; free buffers. */ GT_RC_close (cache); G_free (dcellbuf); G_free (cache); }
int main(int argc, char **argv) { char *mapset; int o_method; struct GModule *module; struct Option *method, *basemap, *covermap, *outputmap; struct Flag *flag_c; struct Categories cats; char methods[1024]; G_gisinit(argv[0]); module = G_define_module(); module->keywords = _("raster, statistics"); module->description = _("Calculates category or object oriented statistics."); basemap = G_define_standard_option(G_OPT_R_BASE); covermap = G_define_standard_option(G_OPT_R_COVER); for (o_method = 0; menu[o_method].name; o_method++) { if (o_method) strcat(methods, ","); else *(methods) = 0; strcat(methods, menu[o_method].name); } method = G_define_option(); method->key = "method"; method->type = TYPE_STRING; method->required = YES; method->description = _("Method of object-based statistic"); method->options = methods; outputmap = G_define_standard_option(G_OPT_R_OUTPUT); outputmap->description = _("Resultant raster map (not used with 'distribution')"); outputmap->required = NO; flag_c = G_define_flag(); flag_c->key = 'c'; flag_c->description = _("Cover values extracted from the category labels of the cover map"); if (G_parser(argc, argv)) exit(EXIT_FAILURE); if ((mapset = G_find_cell2(basemap->answer, "")) == 0) G_fatal_error(_("Raster map <%s> not found"), basemap->answer); if (G_raster_map_is_fp(basemap->answer, mapset) != 0) G_fatal_error(_("This module currently only works for integer (CELL) maps")); if ((mapset = G_find_cell2(covermap->answer, "")) == 0) G_fatal_error(_("Raster map <%s> not found"), covermap->answer); if (G_raster_map_is_fp(covermap->answer, mapset) != 0) G_fatal_error(_("This module currently only works for integer (CELL) maps")); if (G_read_cats(covermap->answer, mapset, &cats) < 0) { G_fatal_error(_("Unable to read category file of raster map <%s@%s>"), covermap->answer, mapset); } for (o_method = 0; menu[o_method].name; o_method++) if (strcmp(menu[o_method].name, method->answer) == 0) break; if (!menu[o_method].name) { G_warning(_("<%s=%s> unknown %s"), method->key, method->answer, method->key); G_usage(); exit(EXIT_FAILURE); } switch (menu[o_method].val) { case DISTRIB: if (outputmap->answer != NULL) G_warning(_("Output map <%s> ignored"), outputmap->answer); o_distrib(basemap->answer, covermap->answer, outputmap->answer, flag_c->answer); break; case AVERAGE: is_ok(method->answer, outputmap->answer); o_average(basemap->answer, covermap->answer, outputmap->answer, flag_c->answer, &cats); break; case MODE: is_ok(method->answer, outputmap->answer); o_mode(basemap->answer, covermap->answer, outputmap->answer, flag_c->answer, &cats); break; case ADEV: is_ok(method->answer, outputmap->answer); o_adev(basemap->answer, covermap->answer, outputmap->answer, flag_c->answer, &cats); break; case SDEV: is_ok(method->answer, outputmap->answer); o_sdev(basemap->answer, covermap->answer, outputmap->answer, flag_c->answer, &cats); break; case VARIANC: is_ok(method->answer, outputmap->answer); o_var(basemap->answer, covermap->answer, outputmap->answer, flag_c->answer, &cats); break; case SKEWNES: is_ok(method->answer, outputmap->answer); o_skew(basemap->answer, covermap->answer, outputmap->answer, flag_c->answer, &cats); break; case KURTOSI: is_ok(method->answer, outputmap->answer); o_kurt(basemap->answer, covermap->answer, outputmap->answer, flag_c->answer, &cats); break; case MEDIAN: is_ok(method->answer, outputmap->answer); o_median(basemap->answer, covermap->answer, outputmap->answer, flag_c->answer, &cats); break; case MIN: is_ok(method->answer, outputmap->answer); o_min(basemap->answer, covermap->answer, outputmap->answer, flag_c->answer, &cats); break; case MAX: is_ok(method->answer, outputmap->answer); o_max(basemap->answer, covermap->answer, outputmap->answer, flag_c->answer, &cats); break; case SUM: is_ok(method->answer, outputmap->answer); o_sum(basemap->answer, covermap->answer, outputmap->answer, flag_c->answer, &cats); break; case DIV: is_ok(method->answer, outputmap->answer); o_divr(basemap->answer, covermap->answer, outputmap->answer, flag_c->answer, &cats); break; default: G_fatal_error(_("Not yet implemented!")); } return 0; }
/*! \brief Get categories/labels Formats label as in d.what.rast -> (catval) catlabel \param filename raster map name \param drow \param dcol \param catstr category string \return 1 on success \return 0 on failure */ int Gs_get_cat_label(const char *filename, int drow, int dcol, char *catstr) { struct Categories cats; const char *mapset; CELL *buf; DCELL *dbuf; RASTER_MAP_TYPE map_type; int fd; if ((mapset = G_find_cell2(filename, "")) == NULL) { G_warning(_("Raster map <%s> not found"), filename); return 0; } if (-1 != G_read_cats(filename, mapset, &cats)) { fd = G_open_cell_old(filename, mapset); map_type = G_get_raster_map_type(fd); if (map_type == CELL_TYPE) { buf = G_allocate_c_raster_buf(); if (G_get_c_raster_row(fd, buf, drow) < 0) { sprintf(catstr, "error"); } else if (G_is_c_null_value(&buf[dcol])) { sprintf(catstr, "(NULL) %s", G_get_c_raster_cat(&buf[dcol], &cats)); } else { sprintf(catstr, "(%d) %s", buf[dcol], G_get_c_raster_cat(&buf[dcol], &cats)); } G_free(buf); } else { /* fp map */ dbuf = G_allocate_d_raster_buf(); if (G_get_d_raster_row(fd, dbuf, drow) < 0) { sprintf(catstr, "error"); } else if (G_is_d_null_value(&dbuf[dcol])) { sprintf(catstr, "(NULL) %s", G_get_d_raster_cat(&dbuf[dcol], &cats)); } else { sprintf(catstr, "(%g) %s", dbuf[dcol], G_get_d_raster_cat(&dbuf[dcol], &cats)); } G_free(dbuf); } } else { strcpy(catstr, "no category label"); } /* TODO: may want to keep these around for multiple queries */ G_free_cats(&cats); G_close_cell(fd); return (1); }
int main(int argc, char *argv[]) { struct GModule *module; struct { struct Option *base, *cover, *output; } parm; char *basemap, *base_mapset; char *covermap, *cover_mapset; char *outmap; char command[1024]; struct Categories cover_cats; FILE *stats, *reclass; int first; long basecat, covercat, catb, catc; double value, max; G_gisinit(argv[0]); module = G_define_module(); module->keywords = _("raster, statistics"); module->description = _("Finds the mode of values in a cover map within " "areas assigned the same category value in a " "user-specified base map."); parm.base = G_define_option(); parm.base->key = "base"; parm.base->description = _("Base map to be reclassified"); parm.base->required = YES; parm.base->type = TYPE_STRING; parm.base->gisprompt = "old,cell,raster"; parm.cover = G_define_option(); parm.cover->key = "cover"; parm.cover->description = _("Coverage map"); parm.cover->required = YES; parm.cover->type = TYPE_STRING; parm.cover->gisprompt = "old,cell,raster"; parm.output = G_define_option(); parm.output->key = "output"; parm.output->description = _("Output map"); parm.output->required = YES; parm.output->type = TYPE_STRING; parm.output->gisprompt = "new,cell,raster"; if (G_parser(argc, argv)) exit(1); basemap = parm.base->answer; covermap = parm.cover->answer; outmap = parm.output->answer; base_mapset = G_find_cell2(basemap, ""); if (base_mapset == NULL) { G_fatal_error(_("%s: base raster map not found"), basemap); } cover_mapset = G_find_cell2(covermap, ""); if (cover_mapset == NULL) { G_fatal_error(_("%s: cover raster map not found"), covermap); } if (G_legal_filename(outmap) < 0) { G_fatal_error(_("<%s> is an illegal file name"), outmap); } if (strcmp(G_mapset(), base_mapset) == 0 && strcmp(basemap, outmap) == 0) { G_fatal_error(_("%s: base map and output map must be different"), outmap); } if (G_read_cats(covermap, cover_mapset, &cover_cats) < 0) { G_fatal_error(_("%s: Unable to read category labels"), covermap); } strcpy(command, "r.stats -an \""); strcat(command, G_fully_qualified_name(basemap, base_mapset)); strcat(command, ","); strcat(command, G_fully_qualified_name(covermap, cover_mapset)); strcat(command, "\""); /* printf(command); */ stats = popen(command, "r"); sprintf(command, "r.reclass i=\"%s\" o=\"%s\"", G_fully_qualified_name(basemap, base_mapset), outmap); /* printf(command); */ reclass = popen(command, "w"); first = 1; while (read_stats(stats, &basecat, &covercat, &value)) { if (first) { first = 0; catb = basecat; catc = covercat; max = value; } if (basecat != catb) { write_reclass(reclass, catb, catc, G_get_cat(catc, &cover_cats)); catb = basecat; catc = covercat; max = value; } if (value > max) { catc = covercat; max = value; } } if (first) { catb = catc = 0; } write_reclass(reclass, catb, catc, G_get_cat(catc, &cover_cats)); pclose(stats); pclose(reclass); exit(0); }
int main(int argc, char *argv[]) { char name[128]; char *mapset; struct Categories cats; int vector = 0; int stat; /* Initialize GIS engine */ G_gisinit(argv[0]); if (argc > 1 && (strcmp(argv[1], "-v") == 0)) { vector = 1; argc--; argv++; } if (argc < 2) { if (vector) mapset = G_ask_vector_in_mapset(_("Which vector map needs " "updated categories?"), name); else mapset = G_ask_cell_in_mapset(_("Which raster map needs " "updated categories?"), name); if (mapset == NULL) G_fatal_error(_("%s map <%s> not found"), vector ? "Vector" : "Raster", name); } else { strncpy(name, argv[1], sizeof(name)); mapset = (vector) ? G_find_vector2(name, G_mapset()) : G_find_cell2(name, G_mapset()); if (mapset == NULL) G_fatal_error(_("%s map <%s> not found"), vector ? "Vector" : "Raster", name); } stat = (vector) ? G_read_vector_cats(name, mapset, &cats) : G_read_cats(name, mapset, &cats); if (stat < 0) G_init_cats((CELL) 0, "", &cats); if (!vector && G_raster_map_is_fp(name, mapset)) { if (E_edit_fp_cats(name, &cats) < 0) { G_message(_("Category file for <%s> not updated"), name); return EXIT_SUCCESS; } } else { if (E_edit_cats(name, &cats, stat < 0) < 0) { G_message(_("Category file for <%s> not updated"), name); return EXIT_SUCCESS; } } if (vector) G_write_vector_cats(name, &cats); else G_write_cats(name, &cats); G_message(_("Category file for <%s> updated"), name); return EXIT_SUCCESS; }