/*! * \brief Number of rows in active window. * * This routine returns the number of rows in the active module window. * Before raster files can be read or written, it is necessary to * known how many rows are in the active window. For example: \code int nrows, cols; int row, col; nrows = Rast_window_rows(); ncols = Rast_window_cols(); for (row = 0; row < nrows; row++) { // read row ... for (col = 0; col < ncols; col++) { // process col ... } } \endcode * * \return number of rows */ int Rast_window_rows(void) { Rast__init_window(); if (R__.split_window) G_fatal_error(_("Internal error: Rast_window_rows() called with split window." "Use Rast_input_window_rows() or Rast_output_window_rows() instead.")); return R__.wr_window.rows; }
void Rast_get_window(struct Cell_head *window) { Rast__init_window(); if (R__.split_window) G_fatal_error(_("Internal error: Rast_get_window() called with split window." "Use Rast_get_input_window() or Rast_get_output_window() instead.")); *window = R__.wr_window; }
static int init(void) { Rast__init_window(); /* no histograms */ R__.want_histogram = 0; /* set the write type for floating maps */ R__.fp_type = getenv("GRASS_FP_DOUBLE") ? DCELL_TYPE : FCELL_TYPE; /* Set masking flag unknown */ R__.auto_mask = -1; R__.mask_fd = -1; R__.nbytes = sizeof(CELL); R__.compression_type = getenv("GRASS_INT_ZLIB") ? 2 : 1; G_add_error_handler(Rast__error_handler, NULL); initialized = 1; return 0; }
/*! \brief Create GDAL settings for given raster map \param name map name \param map_type map type (CELL, FCELL, DCELL) \return pointer to allocated GDAL_link structure \return NULL on error */ struct GDAL_link *Rast_create_gdal_link(const char *name, RASTER_MAP_TYPE map_type) { #ifdef GDAL_LINK char path[GPATH_MAX]; GDALDriverH driver; double transform[6]; struct GDAL_link *gdal; FILE *fp; struct Key_Value *key_val; char buf[32]; Rast__init_window(); Rast_init_gdal(); if (!G_is_initialized(&st->initialized)) { read_gdal_options(); st->projinfo = G_get_projinfo(); st->projunits = G_get_projunits(); #if 0 /* We cannot use GPJ_grass_to_wkt() here because that would create a circular dependency between libgis and libgproj */ if (st->projinfo && st->projunits) st->srswkt = GPJ_grass_to_wkt(st->projinfo, st->projunits); #endif G_initialize_done(&st->initialized); } gdal = G_calloc(1, sizeof(struct GDAL_link)); sprintf(path, "%s/%s%s", st->opts.dir, name, st->opts.ext); gdal->filename = G_store(path); gdal->band_num = 1; gdal->hflip = 0; gdal->vflip = 0; switch (map_type) { case CELL_TYPE: switch (R__.nbytes) { case 1: gdal->type = GDT_Byte; gdal->null_val = (DCELL) 0xFF; break; case 2: gdal->type = GDT_UInt16; gdal->null_val = (DCELL) 0xFFFF; break; case 3: case 4: gdal->type = GDT_Int32; gdal->null_val = (DCELL) 0x80000000U; break; } break; case FCELL_TYPE: gdal->type = GDT_Float32; Rast_set_d_null_value(&gdal->null_val, 1); break; case DCELL_TYPE: gdal->type = GDT_Float64; Rast_set_d_null_value(&gdal->null_val, 1); break; default: G_fatal_error(_("Invalid map type <%d>"), map_type); break; } driver = (*pGDALGetDriverByName) (st->opts.format); if (!driver) G_fatal_error(_("Unable to get <%s> driver"), st->opts.format); /* Does driver support GDALCreate ? */ if ((*pGDALGetMetadataItem) (driver, GDAL_DCAP_CREATE, NULL)) { gdal->data = (*pGDALCreate)(driver, gdal->filename, R__.wr_window.cols, R__.wr_window.rows, 1, gdal->type, st->opts.options); if (!gdal->data) G_fatal_error(_("Unable to create <%s> dataset using <%s> driver"), name, st->opts.format); } /* If not - create MEM driver for intermediate dataset. * Check if raster can be created at all (with GDALCreateCopy) */ else if ((*pGDALGetMetadataItem) (driver, GDAL_DCAP_CREATECOPY, NULL)) { GDALDriverH mem_driver; G_message(_("Driver <%s> does not support direct writing. " "Using MEM driver for intermediate dataset."), st->opts.format); mem_driver = (*pGDALGetDriverByName) ("MEM"); if (!mem_driver) G_fatal_error(_("Unable to get in-memory raster driver")); gdal->data = (*pGDALCreate)(mem_driver, "", R__.wr_window.cols, R__.wr_window.rows, 1, gdal->type, st->opts.options); if (!gdal->data) G_fatal_error(_("Unable to create <%s> dataset using memory driver"), name); } else G_fatal_error(_("Driver <%s> does not support creating rasters"), st->opts.format); gdal->band = (*pGDALGetRasterBand) (gdal->data, gdal->band_num); (*pGDALSetRasterNoDataValue) (gdal->band, gdal->null_val); /* Set Geo Transform */ transform[0] = R__.wr_window.west; transform[1] = R__.wr_window.ew_res; transform[2] = 0.0; transform[3] = R__.wr_window.north; transform[4] = 0.0; transform[5] = -R__.wr_window.ns_res; if ((*pGDALSetGeoTransform) (gdal->data, transform) >= CE_Failure) G_warning(_("Unable to set geo transform")); if (st->srswkt) if ((*pGDALSetProjection) (gdal->data, st->srswkt) == CE_Failure) G_warning(_("Unable to set projection")); fp = G_fopen_new_misc("cell_misc", "gdal", name); if (!fp) G_fatal_error(_("Unable to create cell_misc/%s/gdal file"), name); key_val = G_create_key_value(); G_set_key_value("file", gdal->filename, key_val); sprintf(buf, "%d", gdal->band_num); G_set_key_value("band", buf, key_val); sprintf(buf, "%.22g", gdal->null_val); G_set_key_value("null", buf, key_val); sprintf(buf, "%d", gdal->type); G_set_key_value("type", buf, key_val); if (G_fwrite_key_value(fp, key_val) < 0) G_fatal_error(_("Error writing cell_misc/%s/gdal file"), name); G_free_key_value(key_val); fclose(fp); return gdal; #else return NULL; #endif }
void Rast_get_output_window(struct Cell_head *window) { Rast__init_window(); *window = R__.wr_window; }
void Rast_get_input_window(struct Cell_head *window) { Rast__init_window(); *window = R__.rd_window; }
/*! * \brief Number of columns in active output window. * * This routine returns the number of columns in the active output window. * * \return number of columns */ int Rast_output_window_cols(void) { Rast__init_window(); return R__.wr_window.cols; }
/*! * \brief Number of rows in active output window. * * This routine returns the number of rows in the active output window. * * \return number of rows */ int Rast_output_window_rows(void) { Rast__init_window(); return R__.wr_window.rows; }
/*! * \brief Number of columns in active input window. * * This routine returns the number of columns in the active input window. * * \return number of columns */ int Rast_input_window_cols(void) { Rast__init_window(); return R__.rd_window.cols; }
/*! * \brief Number of rows in active input window. * * This routine returns the number of rows in the active input window. * * \return number of rows */ int Rast_input_window_rows(void) { Rast__init_window(); return R__.rd_window.rows; }