int G_read_colors(const char *name, const char *mapset, struct Colors *colors) { int fp; char buf[GNAME_MAX]; char *err; char xname[GNAME_MAX]; struct Range range; struct FPRange drange; CELL min, max; DCELL dmin, dmax; fp = G_raster_map_is_fp(name, mapset); G_init_colors(colors); strcpy(xname, name); mapset = G_find_cell(xname, mapset); name = xname; if (fp) G_mark_colors_as_fp(colors); /* first look for secondary color table in current mapset */ sprintf(buf, "colr2/%s", mapset); if (read_colors(buf, name, G_mapset(), colors) >= 0) return 1; /* now look for the regular color table */ switch (read_colors("colr", name, mapset, colors)) { case -2: if (!fp) { if (G_read_range(name, mapset, &range) >= 0) { G_get_range_min_max(&range, &min, &max); if (!G_is_c_null_value(&min) && !G_is_c_null_value(&max)) G_make_rainbow_colors(colors, min, max); return 0; } } else { if (G_read_fp_range(name, mapset, &drange) >= 0) { G_get_fp_range_min_max(&drange, &dmin, &dmax); if (!G_is_d_null_value(&dmin) && !G_is_d_null_value(&dmax)) G_make_rainbow_fp_colors(colors, dmin, dmax); return 0; } } err = "missing"; break; case -1: err = "invalid"; break; default: return 1; } G_warning(_("color support for [%s] in mapset [%s] %s"), name, mapset, err); return -1; }
int G_update_cell_stats(const CELL * cell, int n, struct Cell_stats *s) { CELL cat; register int p, q; int idx, offset; int N; register NODE *node, *pnode; register NODE *new_node; if (n <= 0) return 1; node = s->node; /* first non-null node is special case */ if ((N = s->N) == 0) { cat = *cell++; while (G_is_c_null_value(&cat)) { s->null_data_count++; cat = *cell++; n--; } if (n > 0) { /* if there are some non-null cells */ N = 1; if (cat < 0) { idx = -((-cat) >> SHIFT) - 1; offset = cat + ((-idx) << SHIFT) - 1; }
int G_set_color(CELL cat, int r, int g, int b, struct Colors *colors) { CELL tmp = cat; if (G_is_c_null_value(&tmp)) return G_set_null_value_color(r, g, b, colors); return G_add_color_rule(cat, r, g, b, cat, r, g, b, colors); }
void new_stats(char *name, struct Reclass *reclass) { struct Histogram histo, histo2; struct Range range; CELL cat, cat2; int i; CELL min, max; min = reclass->min; max = reclass->max; /* read histogram for original file */ G_suppress_warnings(1); i = G_read_histogram(reclass->name, reclass->mapset, &histo); G_suppress_warnings(0); if (i <= 0) return; /* compute data rage for reclass */ G_init_range(&range); for (i = 0; i < histo.num; i++) { cat = histo.list[i].cat; if (cat < min || cat > max) continue; cat2 = reclass->table[cat - min]; G_update_range(cat2, &range); } G_write_range(name, &range); /* now generate a histogram from the original */ /* allocate histogram list */ histo2.num += range.max - range.min + 1; histo2.list = (LIST *) G_calloc(histo2.num, sizeof(LIST)); /* set all counts to 0 */ i = 0; for (cat = range.min; cat <= range.max; cat++) { histo2.list[i].cat = cat; histo2.list[i++].count = 0; } /* go thru original histogram and add into histo2 */ for (i = 0; i < histo.num; i++) { cat = histo.list[i].cat; if (cat < min || cat > max) G_set_c_null_value(&cat, 1); else cat2 = reclass->table[cat - min]; if (!G_is_c_null_value(&cat)) histo2.list[cat2 - range.min].count += histo.list[i].count; } G_write_histogram(name, &histo2); }
int QgsGrassGisLib::putRasterRow( int fd, const void *buf, RASTER_MAP_TYPE data_type ) { Raster rast = mRasters.value( fd ); if ( rast.row < 0 || rast.row >= mRows ) { QgsDebugMsg( QString( "row %1 out of range 0 - %2" ).arg( rast.row ).arg( mRows ) ); return -1; } QGis::DataType inputType = qgisRasterType( data_type ); //QgsDebugMsg( QString("data_type = %1").arg(data_type) ); //QgsDebugMsg( QString("inputType = %1").arg(inputType) ); //QgsDebugMsg( QString("provider->dataType = %1").arg( rast.provider->dataType( rast.band ) ) ); //double noDataValue = rast.provider->noDataValue( rast.band ); QgsRasterBlock block( inputType, mColumns, 1, rast.noDataValue ); memcpy( block.bits( 0 ), buf, QgsRasterBlock::typeSize( inputType )*mColumns ); block.convert( rast.provider->dataType( rast.band ) ); // Set no data after converting to output type for ( int i = 0; i < mColumns; i++ ) { bool isNoData = false; switch ( data_type ) { case CELL_TYPE: isNoData = G_is_c_null_value( &(( CELL * ) buf )[i] ) != 0; break; case FCELL_TYPE: isNoData = G_is_f_null_value( &(( FCELL * ) buf )[i] ) != 0; break; case DCELL_TYPE: isNoData = G_is_d_null_value( &(( DCELL * ) buf )[i] ) != 0; break; default: break; } if ( isNoData ) { block.setIsNoData( i ); } } if ( !rast.provider->write( block.bits( 0 ), rast.band, mColumns, 1, 0, rast.row ) ) { fatal( "Cannot write block" ); } mRasters[fd].row++; return 1; }
CPLErr GRASSRasterBand::IReadBlock( int /*nBlockXOff*/, int nBlockYOff, void *pImage ) { if ( ! this->valid ) return CE_Failure; // Reset window because IRasterIO could be previously called. if ( ResetReading ( &(((GRASSDataset *)poDS)->sCellInfo) ) != CE_None ) { return CE_Failure; } if ( eDataType == GDT_Byte || eDataType == GDT_UInt16 ) { CELL *cbuf = G_allocate_c_raster_buf(); G_get_c_raster_row ( hCell, cbuf, nBlockYOff ); /* Reset NULLs */ for ( int col = 0; col < nBlockXSize; col++ ) { if ( G_is_c_null_value(&(cbuf[col])) ) cbuf[col] = (CELL) dfNoData; } GDALCopyWords ( (void *) cbuf, GDT_Int32, sizeof(CELL), pImage, eDataType, GDALGetDataTypeSize(eDataType)/8, nBlockXSize ); G_free ( cbuf ); } else if ( eDataType == GDT_Int32 ) { G_get_c_raster_row ( hCell, (CELL *) pImage, nBlockYOff ); } else if ( eDataType == GDT_Float32 ) { G_get_f_raster_row ( hCell, (FCELL *) pImage, nBlockYOff ); } else if ( eDataType == GDT_Float64 ) { G_get_d_raster_row ( hCell, (DCELL *) pImage, nBlockYOff ); } return CE_None; }
void resolve(int fd, int nl, struct band3 *bnd) { CELL cvalue; int *active; int offset, isz, i, j, pass, activity, goagain, done; active = (int *)G_calloc(nl, sizeof(int)); isz = sizeof(CELL); /* select a direction when there are multiple non-flat links */ lseek(fd, bnd->sz, SEEK_SET); for (i = 1; i < nl - 1; i += 1) { read(fd, bnd->b[0], bnd->sz); for (j = 1; j < bnd->ns - 1; j += 1) { offset = j * isz; if (G_is_c_null_value((void *)(bnd->b[0] + offset))) continue; memcpy(&cvalue, bnd->b[0] + offset, isz); if (cvalue > 0) cvalue = select_dir(cvalue); memcpy(bnd->b[0] + offset, &cvalue, isz); } lseek(fd, -bnd->sz, SEEK_CUR); write(fd, bnd->b[0], bnd->sz); } pass = 0; for (i = 1; i < nl - 1; i += 1) active[i] = 1; /* select a direction when there are multiple flat links */ do { done = 1; pass += 1; activity = 0; lseek(fd, 0, SEEK_SET); advance_band3(fd, bnd); advance_band3(fd, bnd); for (i = 1; i < nl - 1; i++) { lseek(fd, (off_t) (i + 1) * bnd->sz, SEEK_SET); advance_band3(fd, bnd); if (!active[i]) continue; done = 0; active[i] = 0; do { goagain = 0; for (j = 1; j < bnd->ns - 1; j += 1) { flink(i, j, nl, bnd->ns, (void *)bnd->b[0], (void *)bnd->b[1], (void *)bnd->b[2], &active[i], &goagain); if (goagain) activity = 1; } } while (goagain); lseek(fd, (off_t) i * bnd->sz, SEEK_SET); write(fd, bnd->b[1], bnd->sz); } if (!activity) { done = 1; continue; } activity = 0; lseek(fd, (off_t) (nl - 1) * bnd->sz, SEEK_SET); retreat_band3(fd, bnd); retreat_band3(fd, bnd); for (i = nl - 2; i >= 1; i -= 1) { lseek(fd, (off_t) (i - 1) * bnd->sz, SEEK_SET); retreat_band3(fd, bnd); if (!active[i]) continue; done = 0; active[i] = 0; do { goagain = 0; for (j = 1; j < bnd->ns - 1; j++) { flink(i, j, nl, bnd->ns, (void *)bnd->b[0], (void *)bnd->b[1], (void *)bnd->b[2], &active[i], &goagain); if (goagain) activity = 1; } } while (goagain); lseek(fd, (off_t) i * bnd->sz, SEEK_SET); write(fd, bnd->b[1], bnd->sz); } if (!activity) { done = 1; } } while (!done); G_free(active); return; }
void flink(int i, int j, int nl, int ns, CELL * p1, CELL * p2, CELL * p3, int *active, int *goagain) { CELL bitmask[8] = { 1, 2, 4, 8, 16, 32, 64, 128 }; CELL outflow, cwork, c[8]; int k; cwork = p2[j]; if (G_is_c_null_value(p2 + j) || cwork >= 0 || cwork == -256) return; cwork = -cwork; for (k = 7; k >= 0; k--) { c[k] = 0; if (cwork >= bitmask[k]) { c[k] = 1; cwork -= bitmask[k]; } } outflow = 0; /* There's no need to resolve directions at cells adjacent to null cells, * as those directions will be resolved before we get here */ /* look one row back */ cwork = p1[j - 1]; if (cwork > 0 && cwork != 4 && c[6]) outflow += 64; cwork = p1[j]; if (cwork > 0 && cwork != 8 && c[7]) outflow += 128; cwork = p1[j + 1]; if (cwork > 0 && cwork != 16 && c[0]) outflow += 1; /* look at this row */ cwork = p2[j - 1]; if (cwork > 0 && cwork != 2 && c[5]) outflow += 32; cwork = p2[j + 1]; if (cwork > 0 && cwork != 32 && c[1]) outflow += 2; /* look one row forward */ cwork = p3[j - 1]; if (cwork > 0 && cwork != 1 && c[4]) outflow += 16; cwork = p3[j]; if (cwork > 0 && cwork != 128 && c[3]) outflow += 8; cwork = p3[j + 1]; if (cwork > 0 && cwork != 64 && c[2]) outflow += 4; if (outflow == 0) { *active = 1; } else { *goagain = 1; p2[j] = select_dir(outflow); } return; }
/* useful to create randomised samples for statistical tests */ void do_split_sample ( char *input, char *output, int in_types, double percentage, char *map, int all, int processing_mode, int quiet) { CELL *cellbuf; DCELL *dcellbuf; GT_Row_cache_t *cache; int fd; int i,j,k,l; int no_sites; int sites_tried = 0; struct Cell_head region; int error; char *mapset, errmsg [200]; unsigned int *taken; /* this is an array of 0/1 which signals, if a certain site has already been 'drawn' */ long row_idx, col_idx; struct Map_info in_vect_map; struct Map_info out_vect_map; struct line_pnts *vect_points; struct line_cats *vect_cats; double x,y,z; int n_points = 1; int cur_type; cellbuf = NULL; dcellbuf = NULL; cache = NULL; /* get current region */ G_get_window (®ion); /* attempt to create new file for output */ Vect_set_open_level (2); if (0 > Vect_open_new (&out_vect_map, output, 0) ) { G_fatal_error ("Could not open output vector map.\n"); } /* open input vector map */ if ((mapset = G_find_vector2 (input, "")) == NULL) { sprintf (errmsg, "Could not find input %s\n", input); G_fatal_error ("%s",errmsg); } if (1 > Vect_open_old (&in_vect_map, input, "")) { sprintf (errmsg, "Could not open input map %s.\n", input); G_fatal_error ("%s",errmsg); } vect_points = Vect_new_line_struct (); vect_cats = Vect_new_cats_struct (); /* set constraints specified */ if (in_types != 0) { Vect_set_constraint_type (&in_vect_map, in_types); } if (all != 1) { Vect_set_constraint_region (&in_vect_map, region.north, region.south, region.east, region.west, 0.0, 0.0); } /* get total number of objects with constraints */ i = 0; while ((cur_type = Vect_read_next_line (&in_vect_map, vect_points, vect_cats) > 0)) { i ++; } k = ( ((float) i/100)) * percentage; /* k now has the number of objects wanted */ if ( quiet != 1 ) { fprintf (stderr,"Creating randomised sample of size n = %i.\n",k); } /* now, we need to acquire exactly 'k' random objects that fall in NON-NULL */ /* coverage raster cells. */ taken = G_calloc (i, sizeof (unsigned int)); for ( l = 0; l < k; l ++ ) { taken[l] = 0; } no_sites = i; /* store this for later use */ /* does user want to filter objects through a raster map? */ if ( map != NULL) { /* open raster map */ fd = G_open_cell_old (map, G_find_cell (map, "")); if (fd < 0) { G_fatal_error ("Could not open raster map for reading!\n"); } /* allocate cache and buffer, according to type of coverage */ if ( processing_mode == CELL_TYPE) { /* INT coverage */ cache = (GT_Row_cache_t *) G_malloc (sizeof (GT_Row_cache_t)); /* TODO: check error value */ error = GT_RC_open (cache, cachesize, fd, CELL_TYPE); cellbuf = G_allocate_raster_buf (CELL_TYPE); } if ( (processing_mode == FCELL_TYPE) || (processing_mode == DCELL_TYPE) ) { /* FP coverage */ cache = (GT_Row_cache_t *) G_malloc (sizeof (GT_Row_cache_t)); /* TODO: check error value */ error = GT_RC_open (cache, cachesize, fd, DCELL_TYPE); dcellbuf = G_allocate_raster_buf (DCELL_TYPE); } } srand ( ((unsigned int) time (NULL)) + getpid()); /* set seed for random number generator from system time and process ID*/ i = 0; /* MAIN LOOP */ while ( i < k ) { /* get a random index, but one that was not taken already */ l = 0; while ( l == 0 ) { j = rand () % ( no_sites - 1 + 1) + 1; /* j now has the random position to try */ if ( taken[j-1] == 0 ) { l = 1; /* exit loop */ } } taken [j-1] = 1; /* mark this index as 'taken' */ sites_tried ++; /* keep track of this so we do not enter an infinite loop */ if ( sites_tried > no_sites ) { /* could not create a large enough sample */ G_fatal_error ("Could not find enough objects for split sampling.\nDecrease split sample size.\n"); } /* get next vector object */ cur_type = Vect_read_line (&in_vect_map, vect_points, vect_cats, j); if (cur_type < 0 ) { G_fatal_error ("Error reading vector map: premature EOF.\n"); } /* now, check if coverage under site is NON-NULL and within region */ /* convert site northing to row! */ /* for this check, we use only the first pair of coordinates! */ Vect_copy_pnts_to_xyz (vect_points, &x, &y, &z, &n_points); row_idx = (long) G_northing_to_row (y, ®ion); col_idx = (long) G_easting_to_col (x, ®ion); /* do region check, first... OBSOLETE */ /* read row from cache and check for NULL */ /* if required */ if ( map != NULL ) { if ( processing_mode == CELL_TYPE ) { cellbuf = GT_RC_get (cache, row_idx); if (!G_is_c_null_value(&cellbuf[col_idx])) { i ++; Vect_write_line (&out_vect_map, cur_type, vect_points, vect_cats ); fflush (stdout); } } if ( (processing_mode == FCELL_TYPE) || (processing_mode == DCELL_TYPE) ) { dcellbuf = GT_RC_get (cache, row_idx); if (!G_is_d_null_value(&dcellbuf[col_idx])) { i ++; Vect_write_line (&out_vect_map, cur_type, vect_points, vect_cats ); fflush (stdout); } } } else { i ++; Vect_write_line (&out_vect_map, GV_POINT, vect_points, vect_cats ); fflush (stdout); } /* disregard region setting and map, if -a flag is given */ if ( all == 1 ) { i ++; Vect_write_line (&out_vect_map, cur_type, vect_points, vect_cats ); fflush (stdout); } if ( quiet != 1 ) { G_percent(i,k,1); } } /* END OF MAIN LOOP */ Vect_copy_head_data (&in_vect_map, &out_vect_map); fprintf (stdout, "Building topology information for output map.\n"); Vect_build (&out_vect_map); Vect_close (&in_vect_map); Vect_close (&out_vect_map); if ( map != NULL ) { /* close cache, free buffers! */ GT_RC_close (cache); if ( processing_mode == CELL_TYPE ) { G_free (cellbuf); } if ( (processing_mode == FCELL_TYPE) || (processing_mode == DCELL_TYPE) ) { G_free (dcellbuf); } G_free (cache); } }
int describe(char *name, char *mapset, int compact, char *no_data_str, int range, int windowed, int nsteps, int as_int, int skip_nulls) { int fd; struct Cell_stats statf; CELL *buf, *b; int nrows, ncols; int row, col; struct Cell_head window; CELL negmin = 0, negmax = 0, zero = 0, posmin = 0, posmax = 0; CELL null = 0; RASTER_MAP_TYPE map_type; struct Quant q; struct FPRange r; DCELL dmin, dmax; int (*get_row) (); if (windowed) { get_row = G_get_c_raster_row; } else { char msg[100]; if (G_get_cellhd(name, mapset, &window) < 0) { sprintf(msg, "can't get cell header for [%s] in [%s]", name, mapset); G_fatal_error(msg); } G_set_window(&window); get_row = G_get_c_raster_row_nomask; } fd = G_open_cell_old(name, mapset); if (fd < 0) return 0; map_type = G_get_raster_map_type(fd); if (as_int) map_type = CELL_TYPE; /* read as int */ /* allocate the cell buffer */ buf = G_allocate_cell_buf(); if (map_type != CELL_TYPE && range) /* this will make it report fp range */ { range = 0; nsteps = 1; } /* start the cell stats */ if (!range) { G_init_cell_stats(&statf); } else { zero = 0; negmin = 0; negmax = 0; posmin = 0; posmax = 0; null = 0; dmin = 0.0; dmax = 0.0; } /* set up quantization rules */ if (map_type != CELL_TYPE) { G_quant_init(&q); G_read_fp_range(name, mapset, &r); G_get_fp_range_min_max(&r, &dmin, &dmax); G_quant_add_rule(&q, dmin, dmax, 1, nsteps); G_set_quant_rules(fd, &q); } nrows = G_window_rows(); ncols = G_window_cols(); G_verbose_message("Reading [%s in %s] ...", name, mapset); for (row = 0; row < nrows; row++) { G_percent(row, nrows, 2); if ((*get_row) (fd, b = buf, row) < 0) break; if (range) { for (col = ncols; col-- > 0; b++) { if (G_is_c_null_value(b)) null = 1; else if (*b == 0) zero = 1; else if (*b < 0) { if (!negmin) negmin = negmax = *b; else if (*b > negmax) negmax = *b; else if (*b < negmin) negmin = *b; } else { if (!posmin) posmin = posmax = *b; else if (*b > posmax) posmax = *b; else if (*b < posmin) posmin = *b; } } } else G_update_cell_stats(buf, ncols, &statf); } G_percent(nrows, nrows, 2); G_close_cell(fd); G_free(buf); if (range) { if (compact) compact_range_list(negmin, negmax, zero, posmin, posmax, null, no_data_str, skip_nulls); else range_list(negmin, negmax, zero, posmin, posmax, null, no_data_str, skip_nulls); } else { G_rewind_cell_stats(&statf); if (compact) compact_list(&statf, dmin, dmax, no_data_str, skip_nulls, map_type, nsteps); else long_list(&statf, dmin, dmax, no_data_str, skip_nulls, map_type, nsteps); G_free_cell_stats(&statf); } return 1; }
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; }
/* create the actual report */ void do_report_CELL ( 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) { CELL *cellbuf; struct Cell_head region; GT_Row_cache_t *cache; unsigned long row_idx, col_idx; 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; /* 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, version %.2f\n",PROGNAME, 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, "")) { delete_tmpfile (map); sprintf (errmsg, "Could not open input map %s.\n", sites); 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, CELL_TYPE); cellbuf = G_allocate_raster_buf (CELL_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_c_counts (map,mapset, show_progress); if (cat_count == NULL) { delete_tmpfile (map); G_fatal_error ("Could not count categories in 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 sampling points */ 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); cellbuf = GT_RC_get (cache, (signed) row_idx); /* now read the raster value under the current site */ if (G_is_c_null_value (&cellbuf[col_idx]) == 0) { /* site on cell within category range [0..cats] ? */ if ( (cellbuf[col_idx] > -1) && (cellbuf[col_idx] <= cats) ) { share_smp [cellbuf[col_idx] ] ++; /* i keeps track of samples on non-null coverage only */ /* inside the current region */ i ++; } else { if ( uncat_flag ) { /* also keep count of sites on uncategorised cells? */ i ++; nocat_count++; } } } if (G_is_c_null_value (&cellbuf[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 (cellbuf); G_free (cache); }
void ppupdate(int fe, int fb, int nl, int nbasins, struct band3 *elev, struct band3 *basins) { int i, j, ii, n; CELL *here; CELL that_basin; void *barrier_height; void *this_elev; void *that_elev; struct links *list; list = G_malloc((nbasins + 1) * sizeof(struct links)); for (i = 1; i <= nbasins; i += 1) { list[i].next = -1; list[i].pp = G_malloc(bpe()); set_max(list[i].pp); list[i].next_alt = -1; list[i].pp_alt = G_malloc(bpe()); set_max(list[i].pp_alt); list[i].trace = 0; } lseek(fe, 0, SEEK_SET); lseek(fb, 0, SEEK_SET); advance_band3(fb, basins); advance_band3(fb, basins); advance_band3(fe, elev); advance_band3(fe, elev); for (i = 1; i < nl - 1; i += 1) { advance_band3(fb, basins); advance_band3(fe, elev); for (j = 1; j < basins->ns - 1; j += 1) { /* check to see if the cell is non-null and in a basin */ here = (CELL *) basins->b[1] + j; if (G_is_c_null_value(here) || *here < 0) continue; ii = *here; this_elev = elev->b[1] + j * bpe(); /* check each adjoining cell; see if we're on a boundary. */ for (n = 0; n < 8; n += 1) { switch (n) { case 0: that_basin = *((CELL *) basins->b[0] + j + 1); that_elev = elev->b[0] + (j + 1) * bpe(); break; case 1: that_basin = *((CELL *) basins->b[1] + j + 1); that_elev = elev->b[1] + (j + 1) * bpe(); break; case 2: that_basin = *((CELL *) basins->b[2] + j + 1); that_elev = elev->b[2] + (j + 1) * bpe(); break; case 3: that_basin = *((CELL *) basins->b[2] + j); that_elev = elev->b[2] + j * bpe(); break; case 4: that_basin = *((CELL *) basins->b[2] + j - 1); that_elev = elev->b[2] + (j - 1) * bpe(); break; case 5: that_basin = *((CELL *) basins->b[1] + j - 1); that_elev = elev->b[1] + (j - 1) * bpe(); break; case 6: that_basin = *((CELL *) basins->b[0] + j - 1); that_elev = elev->b[0] + (j - 1) * bpe(); break; case 7: that_basin = *((CELL *) basins->b[0] + j); that_elev = elev->b[0] + j * bpe(); } /* end switch */ /* see if we're on a boundary */ if (that_basin != ii) { /* what is that_basin if that_elev is null ? */ if (is_null(that_elev)) { barrier_height = this_elev; } else { barrier_height = get_max(that_elev, this_elev); } if (get_min(barrier_height, list[ii].pp) == barrier_height) { /* save the old list entry in case we need it to fix a loop */ if (list[ii].next != that_basin) { memcpy(list[ii].pp_alt, list[ii].pp, bpe()); list[ii].next_alt = list[ii].next; } /* create the new list entry */ memcpy(list[ii].pp, barrier_height, bpe()); list[ii].next = that_basin; } else if (get_min(barrier_height, list[ii].pp_alt) == barrier_height) { if (list[ii].next == that_basin) continue; memcpy(list[ii].pp_alt, barrier_height, bpe()); list[ii].next_alt = that_basin; } } /* end if */ } /* end neighbor cells */ } /* end cell */ } /* end row */ /* Look for pairs of basins that drain to each other */ for (i = 1; i <= nbasins; i += 1) { if (list[i].next <= 0) continue; n = list[i].next; if (list[n].next == i) { /* we have a pair */ /* find out how large the elevation difference would be for a change in * each basin */ memcpy(that_elev, list[n].pp_alt, bpe()); diff(that_elev, list[n].pp); memcpy(this_elev, list[i].pp_alt, bpe()); diff(this_elev, list[i].pp); /* switch pour points in the basin where it makes the smallest change */ if (get_min(this_elev, that_elev) == this_elev) { list[i].next = list[i].next_alt; list[i].next_alt = n; this_elev = list[i].pp; list[i].pp = list[i].pp_alt; list[i].pp_alt = this_elev; } else { ii = list[n].next; list[n].next = list[n].next_alt; list[n].next_alt = ii; this_elev = list[n].pp; list[n].pp = list[n].pp_alt; list[n].pp_alt = this_elev; } /* end fix */ } /* end problem */ } /* end loop */ /* backtrace drainages from the bottom and adjust pour points */ for (i = 1; i <= nbasins; i += 1) { if (list[i].next == -1) { list[i].trace = i; backtrace(i, nbasins, list); } } /* fill all basins up to the elevation of their lowest bounding elevation */ lseek(fe, 0, SEEK_SET); lseek(fb, 0, SEEK_SET); for (i = 0; i < nl; i += 1) { read(fe, elev->b[1], elev->sz); read(fb, basins->b[1], basins->sz); for (j = 0; j < basins->ns; j += 1) { ii = *((CELL *) basins->b[1] + j); if (ii <= 0) continue; this_elev = elev->b[1] + j * bpe(); memcpy(this_elev, get_max(this_elev, list[ii].pp), bpe()); } lseek(fe, -elev->sz, SEEK_CUR); write(fe, elev->b[1], elev->sz); } G_free(list); }
/*! \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); }
/* THIS IS CALLED IF ONLY THE TARGET POINT UNDER CURRENT CONSIDERATION IS TO BE RAISED BY 'OFFSETB' AMOUNT */ double find_inclination2(int x, int y, double viewpt_elev, SEGMENT *seg_in_p, int row_viewpt, int col_viewpt, RASTER_MAP_TYPE data_type) { double del_x, del_y,dist,atan(),sqrt(); int abs(); double dest_elev = 0.0; extern struct Cell_head window; void *value = NULL; /* these vars can store one data value from the elevation input map, */ /* which could be CELL, DCELL or FCELL type. */ CELL c_value; FCELL f_value; DCELL d_value; del_x = abs(x) ; del_y = abs(y) ; dist=sqrt(del_x * del_x + del_y * del_y)*window.ns_res; /* this takes care, that target elevation has the right format, depending on type of input DEM */ if (data_type == CELL_TYPE) { value = (CELL*) &c_value; } if (data_type == FCELL_TYPE) { value = (FCELL*) &f_value; } if (data_type == DCELL_TYPE) { value = (DCELL*) &d_value; } /* read value from elevation input map, convert to appropriate */ /* map type */ segment_get(seg_in_p,value,row_viewpt-y,x+col_viewpt); if (data_type == CELL_TYPE) { if ( G_is_c_null_value (&c_value) ) { return (NULLPT); } else { dest_elev = c_value + OFFSETB; } } if (data_type == FCELL_TYPE) { if ( G_is_f_null_value (&f_value) ) { return (NULLPT); } else { dest_elev = f_value + OFFSETB; } } if (data_type == DCELL_TYPE) { if ( G_is_d_null_value (&d_value) ) { return (NULLPT); } else { dest_elev = d_value + OFFSETB; } } /* CURVATURE CORRECTION */ /* decrease height of target point */ if (DIST_CC > 0.0) { if (dist >= DIST_CC) { dest_elev = dest_elev - ((dist*dist) / (2 * Re)); } } return(atan((dest_elev - viewpt_elev) / dist)); }
CPLErr GRASSRasterBand::IRasterIO ( GDALRWFlag eRWFlag, int nXOff, int nYOff, int nXSize, int nYSize, void * pData, int nBufXSize, int nBufYSize, GDALDataType eBufType, int nPixelSpace, int nLineSpace ) { /* GRASS library does that, we have only calculate and reset the region in map units * and if the region has changed, reopen the raster */ /* Calculate the region */ struct Cell_head sWindow; struct Cell_head *psDsWindow; if ( ! this->valid ) return CE_Failure; psDsWindow = &(((GRASSDataset *)poDS)->sCellInfo); sWindow.north = psDsWindow->north - nYOff * psDsWindow->ns_res; sWindow.south = sWindow.north - nYSize * psDsWindow->ns_res; sWindow.west = psDsWindow->west + nXOff * psDsWindow->ew_res; sWindow.east = sWindow.west + nXSize * psDsWindow->ew_res; sWindow.proj = psDsWindow->proj; sWindow.zone = psDsWindow->zone; sWindow.cols = nBufXSize; sWindow.rows = nBufYSize; /* Reset resolution */ G_adjust_Cell_head ( &sWindow, 1, 1); if ( ResetReading ( &sWindow ) != CE_None ) { return CE_Failure; } /* Read Data */ CELL *cbuf = NULL; FCELL *fbuf = NULL; DCELL *dbuf = NULL; bool direct = false; /* Reset space if default (0) */ if ( nPixelSpace == 0 ) nPixelSpace = GDALGetDataTypeSize ( eBufType ) / 8; if ( nLineSpace == 0 ) nLineSpace = nBufXSize * nPixelSpace; if ( nGRSType == CELL_TYPE && ( !nativeNulls || eBufType != GDT_Int32 || sizeof(CELL) != 4 || nPixelSpace != sizeof(CELL) ) ) { cbuf = G_allocate_c_raster_buf(); } else if( nGRSType == FCELL_TYPE && ( eBufType != GDT_Float32 || nPixelSpace != sizeof(FCELL) ) ) { fbuf = G_allocate_f_raster_buf(); } else if( nGRSType == DCELL_TYPE && ( eBufType != GDT_Float64 || nPixelSpace != sizeof(DCELL) ) ) { dbuf = G_allocate_d_raster_buf(); } else { direct = true; } for ( int row = 0; row < nBufYSize; row++ ) { char *pnt = (char *)pData + row * nLineSpace; if ( nGRSType == CELL_TYPE ) { if ( direct ) { G_get_c_raster_row ( hCell, (CELL *) pnt, row ); } else { G_get_c_raster_row ( hCell, cbuf, row ); /* Reset NULLs */ for ( int col = 0; col < nBufXSize; col++ ) { if ( G_is_c_null_value(&(cbuf[col])) ) cbuf[col] = (CELL) dfNoData; } GDALCopyWords ( (void *) cbuf, GDT_Int32, sizeof(CELL), (void *) pnt, eBufType, nPixelSpace, nBufXSize ); } } else if( nGRSType == FCELL_TYPE ) { if ( direct ) { G_get_f_raster_row ( hCell, (FCELL *) pnt, row ); } else { G_get_f_raster_row ( hCell, fbuf, row ); GDALCopyWords ( (void *) fbuf, GDT_Float32, sizeof(FCELL), (void *) pnt, eBufType, nPixelSpace, nBufXSize ); } } else if( nGRSType == DCELL_TYPE ) { if ( direct ) { G_get_d_raster_row ( hCell, (DCELL *) pnt, row ); } else { G_get_d_raster_row ( hCell, dbuf, row ); GDALCopyWords ( (void *) dbuf, GDT_Float64, sizeof(DCELL), (void *) pnt, eBufType, nPixelSpace, nBufXSize ); } } } if ( cbuf ) G_free ( cbuf ); if ( fbuf ) G_free ( fbuf ); if ( dbuf ) G_free ( dbuf ); return CE_None; }
/* NULL cell handling added by Benjamin Ducke, May 2004 */ void Close_segmented_outfile (char* map_name, int map_fd, char* seg_name, int seg_fd, SEGMENT* seg, CELL* cell_buf, int terse, SEGMENT* elevation_seg, RASTER_MAP_TYPE data_type, int make_nulls) { unsigned long row, nrows, col, ncols; void *value = NULL; char *null_flags; /* the following are used to store different raster map types */ CELL c_value; FCELL f_value; DCELL d_value; /* Find number of rows and columns in elevation map */ nrows = G_window_rows(); ncols = G_window_cols(); /* allocate memory for a NULL data row */ null_flags = G_calloc ((unsigned) ncols, sizeof (char)); /* Write pending updates by segment_put() to output map */ segment_flush(seg); if (!terse) { fprintf (stdout, "\nWriting raster map '%s': \n", map_name); } /* Convert output submatrices to full cell overlay */ for(row=0; row< nrows; row++) { segment_get_row(seg, cell_buf, (signed) row); /* check for NULL values in the corresponding row of the */ /* elevation input file. If there are any, set the CELLs */ /* in the output file to NULL, as well */ /* initialize null data row */ for (col=0; col<ncols; col++) { null_flags[col] = 0; } /* convert all -1 cells to NULL, if user wants it so */ if ( make_nulls == 1 ) { for (col=0; col<ncols; col++) { if ( cell_buf[col] == -1 ) { null_flags[col] = 1; } } } /* update NULL flags in row */ for (col=0; col<ncols; col++) { if ( data_type == CELL_TYPE ) { value = (CELL*) &c_value; } if ( data_type == FCELL_TYPE ) { value = (FCELL*) &f_value; } if ( data_type == DCELL_TYPE ) { value = (DCELL*) &d_value; } segment_get (elevation_seg, value, (signed) row, (signed) col); /* check for NULL value and record in null_flags row */ if ( data_type == CELL_TYPE ) { if (G_is_c_null_value (&c_value) ) { null_flags[col] = 1; /* signal NULL value in null_flags row */ } } if ( data_type == FCELL_TYPE ) { if (G_is_f_null_value (&f_value) ) { null_flags[col] = 1; /* signal NULL value in null_flags row */ } } if ( data_type == DCELL_TYPE ) { if (G_is_d_null_value (&d_value) ) { null_flags[col] = 1; /* signal NULL value in null_flags row */ } } } /* set all NULL cells according to null_flags row */ G_insert_c_null_values (cell_buf, null_flags, (signed) ncols); /* now, write this row to disk */ if(G_put_raster_row (map_fd, cell_buf, CELL_TYPE) < 0) { G_fatal_error ("Failed to convert output submatrices "); } /* progress display */ if (! terse) { G_percent ((signed) row, (signed) nrows-1,2); } } G_free (null_flags); /* Close files */ segment_release (seg); close (seg_fd); unlink (seg_name); G_close_cell (map_fd); }