int digitize(FILE * fd) { int any; struct Categories labels; G_init_cats((CELL) 0, "", &labels); any = 0; for (;;) { switch (get_type()) { case 'A': /* area */ if (get_area(fd, &labels)) any = 1; break; case 'C': /* circle */ if (get_circle(fd, &labels)) any = 1; break; case 'L': /* line */ if (get_line(fd, &labels)) any = 1; break; case 'X': /* done */ return any; case 'Q': /* exit without saving */ if (G_yes(_("Quit without creating a map?? "), 0)) return 0; } } }
static int close_new(int fd, int ok) { struct fileinfo *fcb = &G__.fileinfo[fd]; int stat; struct Categories cats; struct History hist; char path[GPATH_MAX]; CELL cell_min, cell_max; int row, i, open_mode; if (ok) { switch (fcb->open_mode) { case OPEN_NEW_COMPRESSED: G_debug(1, "close %s compressed", fcb->name); break; case OPEN_NEW_UNCOMPRESSED: G_debug(1, "close %s uncompressed", fcb->name); break; case OPEN_NEW_RANDOM: G_debug(1, "close %s random", fcb->name); break; } if (fcb->open_mode != OPEN_NEW_RANDOM && fcb->cur_row < fcb->cellhd.rows) { G_zero_raster_buf(fcb->data, fcb->map_type); for (row = fcb->cur_row; row < fcb->cellhd.rows; row++) G_put_raster_row(fd, fcb->data, fcb->map_type); G_free(fcb->data); fcb->data = NULL; } /* create path : full null file name */ G__make_mapset_element_misc("cell_misc", fcb->name); G__file_name_misc(path, "cell_misc", NULL_FILE, fcb->name, G_mapset()); remove(path); if (fcb->null_cur_row > 0) { /* if temporary NULL file exists, write it into cell_misc/name/null */ int null_fd; null_fd = G__open_null_write(fd); if (null_fd <= 0) return -1; if (null_fd < 1) return -1; /* first finish writing null file */ /* write out the rows stored in memory */ for (row = fcb->min_null_row; row < fcb->null_cur_row; row++) G__write_null_bits(null_fd, fcb->NULL_ROWS[row - fcb->min_null_row], row, fcb->cellhd.cols, fd); /* write missing rows */ if (fcb->open_mode != OPEN_NEW_RANDOM && fcb->null_cur_row < fcb->cellhd.rows) { G__init_null_bits(fcb->null_work_buf, fcb->cellhd.cols); for (row = fcb->null_cur_row; row < fcb->cellhd.rows; row++) G__write_null_bits(null_fd, fcb->null_work_buf, row, fcb->cellhd.cols, fd); } close(null_fd); if (rename(fcb->null_temp_name, path)) { G_warning(_("closecell: can't move %s\nto null file %s"), fcb->null_temp_name, path); stat = -1; } else { remove(fcb->null_temp_name); } } else { remove(fcb->null_temp_name); remove(path); } /* null_cur_row > 0 */ if (fcb->open_mode == OPEN_NEW_COMPRESSED) { /* auto compression */ fcb->row_ptr[fcb->cellhd.rows] = lseek(fd, 0L, SEEK_CUR); G__write_row_ptrs(fd); } if (fcb->map_type != CELL_TYPE) { /* floating point map */ int cell_fd; if (G__write_fp_format(fd) != 0) { G_warning(_("Error writing floating point format file for map %s"), fcb->name); stat = -1; } /* now write 0-length cell file */ G__make_mapset_element("cell"); cell_fd = creat(G__file_name(path, "cell", fcb->name, fcb->mapset), 0666); close(cell_fd); strcpy(CELL_DIR, "fcell"); } else { /* remove fcell/name file */ G__file_name(path, "fcell", fcb->name, fcb->mapset); remove(path); /* remove cell_misc/name/f_format */ G__file_name_misc(path, "cell_misc", FORMAT_FILE, fcb->name, fcb->mapset); remove(path); strcpy(CELL_DIR, "cell"); close(fd); } } /* ok */ /* NOW CLOSE THE FILE DESCRIPTOR */ close(fd); /* remember open_mode */ open_mode = fcb->open_mode; fcb->open_mode = -1; if (fcb->data != NULL) G_free(fcb->data); if (fcb->null_temp_name != NULL) { G_free(fcb->null_temp_name); fcb->null_temp_name = NULL; } /* if the cell file was written to a temporary file * move this temporary file into the cell file * if the move fails, tell the user, but go ahead and create * the support files */ stat = 1; if (ok && (fcb->temp_name != NULL)) { G__file_name(path, CELL_DIR, fcb->name, fcb->mapset); remove(path); if (rename(fcb->temp_name, path)) { G_warning(_("closecell: can't move %s\nto cell file %s"), fcb->temp_name, path); stat = -1; } else { remove(fcb->temp_name); } } if (fcb->temp_name != NULL) { G_free(fcb->temp_name); } if (ok) { /* remove color table */ G_remove_colors(fcb->name, ""); /* create a history file */ G_short_history(fcb->name, "raster", &hist); G_write_history(fcb->name, &hist); /* write the range */ if (fcb->map_type == CELL_TYPE) { G_write_range(fcb->name, &fcb->range); G__remove_fp_range(fcb->name); } /*NOTE: int range for floating point maps is not written out */ else { /* if(fcb->map_type != CELL_TYPE) */ G_write_fp_range(fcb->name, &fcb->fp_range); G_construct_default_range(&fcb->range); /* this range will be used to add default rule to quant structure */ } if (fcb->map_type != CELL_TYPE) fcb->cellhd.format = -1; else /* CELL map */ fcb->cellhd.format = fcb->nbytes - 1; /* write header file */ G_put_cellhd(fcb->name, &fcb->cellhd); /* if map is floating point write the quant rules, otherwise remove f_quant */ if (fcb->map_type != CELL_TYPE) { /* DEFAULT RANGE QUANT G_get_fp_range_min_max(&fcb->fp_range, &dcell_min, &dcell_max); if(!G_is_d_null_value(&dcell_min) && !G_is_d_null_value(&dcell_max)) { G_get_range_min_max(&fcb->range, &cell_min, &cell_max); G_quant_add_rule(&fcb->quant, dcell_min, dcell_max, cell_min, cell_max); } */ G_quant_round(&fcb->quant); if (G_write_quant(fcb->name, fcb->mapset, &fcb->quant) < 0) G_warning(_("unable to write quant file!")); } else { /* remove cell_misc/name/f_quant */ G__file_name_misc(path, "cell_misc", QUANT_FILE, fcb->name, fcb->mapset); remove(path); } /* create empty cats file */ G_get_range_min_max(&fcb->range, &cell_min, &cell_max); if (G_is_c_null_value(&cell_max)) cell_max = 0; G_init_cats(cell_max, (char *)NULL, &cats); G_write_cats(fcb->name, &cats); G_free_cats(&cats); /* write the histogram */ /* only works for integer maps */ if ((fcb->map_type == CELL_TYPE) && (fcb->want_histogram)) { G_write_histogram_cs(fcb->name, &fcb->statf); G_free_cell_stats(&fcb->statf); } else { G_remove_histogram(fcb->name); } } /* OK */ G_free(fcb->name); G_free(fcb->mapset); for (i = 0; i < NULL_ROWS_INMEM; i++) G_free(fcb->NULL_ROWS[i]); G_free(fcb->null_work_buf); if (fcb->map_type != CELL_TYPE) G_quant_free(&fcb->quant); return stat; }
/* 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); }
void do_calculations_rast (Shypothesis **samples, char **groups, int norm, char* basename, char **outvals, char *hypspec, int quiet_flag, char *logfile, xmlDocPtr doc, Sfp_struct* file_pointers) { long y,x; int i, j, k, l, m; long ymax,xmax; double woc; struct Categories cats, icats; DCELL cmin, cmax; Sresult_struct *result_row; /* one result_struct for each DST value */ BOOL **garbage; int no_hyps; char* val_names[NUMVALS]={"bel","pl","doubt","common","bint","woc","maxbpa","minbpa", "maxsrc","minsrc"}; int error; char **outhyps; int no_sets; /* for keeping min and max statistics */ Uint nsets; double *min_backup, *max_backup; int *minev_backup, *maxev_backup; woc = 0; /* check for output options */ if ( G_legal_filename(basename) != 1 ) { G_fatal_error ("Please provide a legal filename as basename for output maps(s).\n"); } if ( hypspec != NULL ) { /* user specified hyps, let's see if they're valid */ /* create an outhyps array that has as each of its elements the name of one of the hypotheses specified on the command line */ outhyps = parse_hyps (hypspec, &no_hyps); check_hyps ( outhyps, no_hyps, doc ); } else { /* just process all hypotheses */ outhyps = get_hyp_names_XML ( &no_hyps, doc ); } if ( logfile != NULL ) { fprintf (lp,"Writing output RASTER maps for: \n"); } /* create raster rows to store results */ result_row = G_malloc ( NUMVALS * sizeof (Sresult_struct) ); for (i=0; i<NUMVALS; i++) { result_row[i].use = NO; strcpy (result_row[i].valname,val_names[i]); /* individual raster rows will be alloc'd later */ result_row[i].row = (DCELL **) G_malloc ( no_hyps * sizeof (DCELL*) ); result_row[i].crow = (CELL **) G_malloc ( no_hyps * sizeof (CELL*) ); result_row[i].filename = NULL; } j = 0; while ( outvals[j] != NULL ) { if ( !strcmp (outvals[j],"bel") ) { if ( logfile != NULL ) fprintf (lp,"\t'bel' (Believe) values\n"); make_result_row ( BEL, basename, outhyps, no_hyps, &result_row[BEL], doc ); } if ( !strcmp (outvals[j],"pl") ) { if ( logfile != NULL ) fprintf (lp,"\t'pl' (Plausibility) values\n"); make_result_row ( PL, basename, outhyps, no_hyps, &result_row[PL], doc ); } if ( !strcmp (outvals[j],"doubt") ) { if ( logfile != NULL ) fprintf (lp,"\t'doubt' (Doubt) values\n"); make_result_row ( DOUBT, basename, outhyps, no_hyps, &result_row[DOUBT], doc ); } if ( !strcmp (outvals[j],"common") ) { if ( logfile != NULL ) fprintf (lp,"\t'common' (Commonality) values\n"); make_result_row ( COMMON, basename, outhyps, no_hyps, &result_row[COMMON], doc ); } if ( !strcmp (outvals[j],"bint") ) { if ( logfile != NULL ) fprintf (lp,"\t'bint' (Believe interval) values\n"); make_result_row ( BINT, basename, outhyps, no_hyps, &result_row[BINT], doc ); } if ( !strcmp (outvals[j],"woc") ) { if ( logfile != NULL ) fprintf (lp,"\t'woc' (Weight of conflict) values\n"); make_result_row ( WOC, basename, outhyps, no_hyps,&result_row[WOC], doc ); } if ( !strcmp (outvals[j],"maxbpa") ) { if ( logfile != NULL ) fprintf (lp,"\t'maxbpa' (Maximum BPA) values\n"); make_result_row ( MAXBPA, basename, outhyps, no_hyps,&result_row[MAXBPA], doc ); } if ( !strcmp (outvals[j],"minbpa") ) { if ( logfile != NULL ) fprintf (lp,"\t'minbpa' (Minimum BPA) values\n"); make_result_row ( MINBPA, basename, outhyps, no_hyps,&result_row[MINBPA], doc ); } if ( !strcmp (outvals[j],"maxsrc") ) { if ( logfile != NULL ) fprintf (lp,"\t'maxsrc' (source of highest BPA) values\n"); make_result_row ( MAXSRC, basename, outhyps, no_hyps,&result_row[MAXSRC], doc ); } if ( !strcmp (outvals[j],"minsrc") ) { if ( logfile != NULL ) fprintf (lp,"\t'minsrc' (source of lowest BPA) values\n"); make_result_row ( MINSRC, basename, outhyps, no_hyps,&result_row[MINSRC], doc ); } j ++; } /* open output maps to store results */ if ( logfile != NULL ) fprintf (lp,"Opening output maps:\n"); for (i=0; i<NUMVALS;i++) { if (result_row[i].use == YES) { if ( i == WOC ) { if ( logfile != NULL ) fprintf (lp,"\t%s\n",result_row[i].filename[0]); result_row[i].fd[0] = G_open_raster_new (result_row[i].filename[0],DCELL_TYPE); } else { for (j=0; j < no_hyps; j++) { if ( logfile != NULL ) fprintf (lp,"\t%s\n",result_row[i].filename[j]); if ((i == MAXSRC) || (i == MINSRC)) { result_row[i].fd[j] = G_open_raster_new (result_row[i].filename[j],CELL_TYPE); } else { result_row[i].fd[j] = G_open_raster_new (result_row[i].filename[j],DCELL_TYPE); } /* check fd for errors */ if ( result_row[i].fd[j] < 0 ) { G_fatal_error ("Could not create output map for %s\n", result_row[i].filename[j]); } } } } } if ( logfile != NULL ) { fprintf (lp, "Evidence will be combined for these groups:\n"); for ( i=0; i < N; i++) { fprintf (lp,"\t%s\n",groups[i]); } fprintf (lp, "Output will be stored in mapset '%s'.\n", G_mapset()); fprintf (lp,"\nRead output below carefully to detect potential problems:\n"); } /* set start coordinates for reading from raster maps */ ReadX = 0; ReadY = 0; ymax = G_window_rows (); xmax = G_window_cols (); if ( !quiet_flag ) { fprintf (stdout,"Combining RAST evidence: \n"); fflush (stdout); } /* allocate all file pointers */ /* open raster maps for this group */ /* 0 is the NULL hypothesis, so we start at 1 */ no_sets = (Uint) pow((float) 2, (float) NO_SINGLETONS); for (l=0; l<N; l++) { for ( m = 1; m < no_sets; m ++ ) { file_pointers[l].fp[m] = G_open_cell_old ( file_pointers[l].filename[m], G_find_cell ( file_pointers[l].filename[m],"") ); if ( file_pointers[l].fp[m] < 0 ) { G_fatal_error ("Could not open raster map '%s' for reading.\n", file_pointers[l].filename[m] ); } } } for (y=0; y<ymax; y++) { for (x=0; x<xmax; x++) { garbage = garbage_init (); NULL_SIGNAL = 0; for (i=0; i<N; i++) { samples[i] = get_rast_samples_XML (groups[i],i, norm, &nsets, garbage, doc, file_pointers ); } /* get min and max values */ for (i=0; i<N; i++) { if (NULL_SIGNAL == 0) { for (k=0; k < nsets; k++) { samples[i][k].minbpn = samples[i][k].bpa; samples[i][k].maxbpn = samples[i][k].bpa; samples[i][k].minbpnev = i + 1; samples[i][k].maxbpnev = i + 1; } } } for (i=0; i<N; i++) { if (NULL_SIGNAL == 0) { for (j=0; j < N; j++) { for (k=0; k < nsets; k++) { if (samples[i][k].bpa < samples[j][k].minbpn) { samples[j][k].minbpn = samples[i][k].bpa; samples[j][k].minbpnev = i + 1; } if (samples[i][k].bpa > samples[j][k].maxbpn) { samples[j][k].maxbpn = samples[i][k].bpa; samples[j][k].maxbpnev = i + 1; } } } } } /* initialise: */ /* set belief and plausibility before first combination of evidence */ for(i=0;i<N;i++) { if ( NULL_SIGNAL == 0 ) { set_beliefs(samples[i]); set_plausibilities(samples[i]); } } /* combine evidence and set bel and pl again */ /* AFTER COMBINE_BPN(), VALUES IN SAMPLES[0] WILL ALL BE ALTERED */ /* so we must save min and max values for later use */ min_backup = G_malloc ((unsigned)(nsets * sizeof(double))); max_backup = G_malloc ((unsigned)(nsets * sizeof(double))); minev_backup = G_malloc ((unsigned)(nsets * sizeof(int))); maxev_backup = G_malloc ((unsigned)(nsets * sizeof(int))); for (k=0; k < nsets; k++) { min_backup[k] = samples[0][k].minbpn; max_backup[k] = samples[0][k].maxbpn; minev_backup[k] = samples[0][k].minbpnev; maxev_backup[k] = samples[0][k].maxbpnev; } /* now, do the combination! */ for(i=0;i<N-1;i++) { if ( NULL_SIGNAL == 0 ) { woc = combine_bpn(samples[0], samples[i+1], garbage, RAST_MODE ); set_beliefs(samples[0]); set_plausibilities(samples[0]); } } /* restore min and max values */ for (k=0; k < nsets; k++) { samples[0][k].minbpn = min_backup[k]; samples[0][k].maxbpn = max_backup[k]; samples[0][k].minbpnev = minev_backup[k]; samples[0][k].maxbpnev = maxev_backup[k]; } G_free (min_backup); G_free (max_backup); G_free (minev_backup); G_free (maxev_backup); /* all other metrics can be derived from bel and pl, no need */ /* to combine evidence again! */ if ( NULL_SIGNAL == 0 ) { set_commonalities(samples[0]); set_doubts(samples[0]); set_bint(samples[0]); } if ( NULL_SIGNAL == 1 ) { for (i=0; i<NUMVALS;i++) { if (result_row[i].use == YES) { if ( i == WOC) { write_row_null (result_row[i].row[0], ReadX); } else { if ((i == MAXSRC)||(i == MINSRC)) { for (j=0; j < no_hyps; j++) { write_crow_null (result_row[i].crow[j], ReadX); } } else { for (j=0; j < no_hyps; j++) { write_row_null (result_row[i].row[j], ReadX); } } } } } } else { for (i=0; i<NUMVALS;i++) { if (result_row[i].use == YES) { if ( i == WOC ) { write_row_val (result_row[i].row[0], ReadX, samples[0], result_row[i].hyp_idx[0], i, woc); } else { if (( i == MAXSRC ) || ( i == MINSRC )) { for (j=0; j < no_hyps; j++) { write_crow_val (result_row[i].crow[j], ReadX, samples[0], result_row[i].hyp_idx[j], i); } } else { for (j=0; j < no_hyps; j++) { write_row_val (result_row[i].row[j], ReadX, samples[0], result_row[i].hyp_idx[j], i, woc); } } } } } } ReadX ++; garbage_free ( garbage ); for (i=0; i<N; i++) { free_sample (samples[i]); } } ReadY ++; /* go to next row */ ReadX = 0; /* save this row to the result file */ for (i=0; i<NUMVALS;i++) { if (result_row[i].use == YES) { if ( i == WOC ) { write_row_file ( result_row[i].row[0],result_row[i].fd[0]); } else { if ( ( i == MAXSRC ) || ( i == MINSRC ) ) { for (j=0; j<no_hyps; j++) { write_crow_file ( result_row[i].crow[j],result_row[i].fd[j]); } } else { for (j=0; j<no_hyps; j++) { write_row_file ( result_row[i].row[j],result_row[i].fd[j]); } } } } } if ( !quiet_flag ) { G_percent (ReadY,ymax,1); fflush (stdout); } } if ( !quiet_flag ) { fprintf (stdout,"\n"); fflush (stdout); } for (i=0; i<NUMVALS;i++) { if (result_row[i].use == YES) { if ( i == WOC ) { G_close_cell (result_row[i].fd[0]); } else { for (j=0; j<no_hyps; j++) { G_close_cell (result_row[i].fd[j]); } } } } /* close raster maps */ /* 0 is the NULL hypothesis, so we start at 1 */ for (l=0; l<N; l++) { for ( m = 1; m < no_sets; m ++ ) { G_close_cell (file_pointers[l].fp[m]); } } /* create a categories structure for output maps */ /* DCELL maps */ G_init_cats (3, "Value ranges", &cats); cmin = 0; cmax = 0.333333; G_set_d_raster_cat (&cmin, &cmax, "low", &cats); cmin = 0.333334; cmax = 0.666666; G_set_d_raster_cat (&cmin, &cmax, "medium", &cats); cmin = 0.666667; cmax = 1; G_set_d_raster_cat (&cmin, &cmax, "high", &cats); /* CELL maps */ G_init_cats (N+1, "Source of evidence", &icats); G_set_cat (0,"no data",&icats); for (i=1; i<=N; i++) { G_set_cat (i,groups[i-1],&icats); } /* write all color tables, categories information and history metadata */ for (i=0; i<NUMVALS;i++) { if (result_row[i].use == YES) { if ( i == WOC ) { error = G_write_colors (result_row[i].filename[0], G_mapset(), result_row[i].colors[0]); if (error == -1) { G_warning ("Could not create color table for map '%s'.\n",result_row[i].filename[j]); } } else { if (( i == MAXSRC ) || ( i == MINSRC )) { for (j=0; j<no_hyps; j++) { G_write_cats (result_row[i].filename[j], &icats); } } else { for (j=0; j<no_hyps; j++) { error = G_write_colors (result_row[i].filename[j], G_mapset(), result_row[i].colors[j]); if (error == -1) { G_warning ("Could not create color table for map '%s'.\n",result_row[i].filename[j]); } G_write_raster_cats (result_row[i].filename[j], &cats); } } } } } G_free (samples); for ( i=0; i < no_hyps; i ++ ) { G_free ( outhyps[i]); } G_free (outhyps); }
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; }