Return a raster of the provided geometry.
2522 double _scale[2] = {0};
2523 double _skew[2] = {0};
2526 OGRGeometryH src_geom;
2527 OGREnvelope src_env;
2529 OGRwkbGeometryType wkbtype = wkbUnknown;
2534 double _gt[6] = {0};
2535 GDALDriverH _drv = NULL;
2537 GDALDatasetH _ds = NULL;
2538 GDALRasterBandH _band = NULL;
2540 uint16_t _width = 0;
2541 uint16_t _height = 0;
2545 assert(NULL != wkb);
2546 assert(0 != wkb_len);
2551 rterror(
"rt_raster_gdal_rasterize: Could not initialize internal variables");
2556 if (num_bands < 1) {
2587 if (NULL != srs && strlen(srs)) {
2588 arg->
src_sr = OSRNewSpatialReference(NULL);
2589 if (OSRSetFromUserInput(arg->
src_sr, srs) != OGRERR_NONE) {
2590 rterror(
"rt_raster_gdal_rasterize: Could not create OSR spatial reference using the provided srs: %s", srs);
2597 ogrerr = OGR_G_CreateFromWkb((
unsigned char *) wkb, arg->
src_sr, &src_geom, wkb_len);
2598 if (ogrerr != OGRERR_NONE) {
2599 rterror(
"rt_raster_gdal_rasterize: Could not create OGR Geometry from WKB");
2608 if (OGR_G_IsEmpty(src_geom)) {
2609 rtinfo(
"Geometry provided is empty. Returning empty raster");
2611 OGR_G_DestroyGeometry(src_geom);
2619 OGR_G_GetEnvelope(src_geom, &src_env);
2622 RASTER_DEBUGF(3,
"Suggested raster envelope: %f, %f, %f, %f",
2627 (NULL != scale_x) &&
2628 (NULL != scale_y) &&
2633 _scale[0] = fabs(*scale_x);
2634 _scale[1] = fabs(*scale_y);
2637 else if ((NULL != width) && (NULL != height) && (*width != 0) && (*height != 0))
2639 _dim[0] = abs(*width);
2640 _dim[1] = abs(*height);
2643 _scale[0] = fabs((extent.
MaxX - extent.
MinX) / _dim[0]);
2648 _scale[1] = fabs((extent.
MaxY - extent.
MinY) / _dim[1]);
2653 rterror(
"rt_raster_gdal_rasterize: Values must be provided for width and height or X and Y of scale");
2655 OGR_G_DestroyGeometry(src_geom);
2661 RASTER_DEBUGF(3,
"scale (x, y) = %f, %f", _scale[0], -1 * _scale[1]);
2665 if (NULL != skew_x) {
2679 if (NULL != skew_y) {
2701 wkbtype = wkbFlatten(OGR_G_GetGeometryType(src_geom));
2703 (wkbtype == wkbPoint) ||
2704 (wkbtype == wkbMultiPoint) ||
2705 (wkbtype == wkbLineString) ||
2706 (wkbtype == wkbMultiLineString)
2712 #if POSTGIS_GDAL_VERSION > 10800
2714 RASTER_DEBUG(3,
"Adjusting extent for GDAL > 1.8 by half the scale on X-axis");
2715 extent.
MinX -= (_scale[0] / 2.);
2716 extent.
MaxX += (_scale[0] / 2.);
2718 RASTER_DEBUG(3,
"Adjusting extent for GDAL > 1.8 by half the scale on Y-axis");
2719 extent.
MinY -= (_scale[1] / 2.);
2720 extent.
MaxY += (_scale[1] / 2.);
2724 RASTER_DEBUG(3,
"Adjusting extent for GDAL <= 1.8 by the scale on X-axis");
2725 extent.
MinX -= _scale[0];
2726 extent.
MaxX += _scale[0];
2728 RASTER_DEBUG(3,
"Adjusting extent for GDAL <= 1.8 by the scale on Y-axis");
2729 extent.
MinY -= _scale[1];
2730 extent.
MaxY += _scale[1];
2755 if (skewedrast == NULL) {
2756 rterror(
"rt_raster_gdal_rasterize: Could not compute skewed raster");
2758 OGR_G_DestroyGeometry(src_geom);
2765 _dim[0] = skewedrast->
width;
2766 _dim[1] = skewedrast->
height;
2776 _dim[0] = (int) fmax((fabs(extent.
MaxX - extent.
MinX) + (_scale[0] / 2.)) / _scale[0], 1);
2778 _dim[1] = (int) fmax((fabs(extent.
MaxY - extent.
MinY) + (_scale[1] / 2.)) / _scale[1], 1);
2783 rterror(
"rt_raster_gdal_rasterize: Out of memory allocating temporary raster");
2785 OGR_G_DestroyGeometry(src_geom);
2798 RASTER_DEBUGF(3,
"Temp raster's geotransform: %f, %f, %f, %f, %f, %f",
2799 _gt[0], _gt[1], _gt[2], _gt[3], _gt[4], _gt[5]);
2800 RASTER_DEBUGF(3,
"Temp raster's dimensions (width x height): %d x %d",
2810 RASTER_DEBUGF(4,
"Using user-specified upper-left corner: %f, %f", *ul_xw, *ul_yw);
2818 ((NULL != ul_xw) && (NULL == ul_yw)) ||
2819 ((NULL == ul_xw) && (NULL != ul_yw))
2821 rterror(
"rt_raster_gdal_rasterize: Both X and Y upper-left corner values must be provided");
2824 OGR_G_DestroyGeometry(src_geom);
2834 (NULL != grid_xw) || (NULL != grid_yw)
2839 ((NULL != grid_xw) && (NULL == grid_yw)) ||
2840 ((NULL == grid_xw) && (NULL != grid_yw))
2842 rterror(
"rt_raster_gdal_rasterize: Both X and Y alignment values must be provided");
2845 OGR_G_DestroyGeometry(src_geom);
2852 RASTER_DEBUGF(4,
"Aligning extent to user-specified grid: %f, %f", *grid_xw, *grid_yw);
2860 RASTER_DEBUG(3,
"Skipping raster alignment as it is already aligned to grid");
2875 rterror(
"rt_raster_gdal_rasterize: Could not compute raster pixel for spatial coordinates");
2878 OGR_G_DestroyGeometry(src_geom);
2891 rterror(
"rt_raster_gdal_rasterize: Could not compute spatial coordinates for raster pixel");
2894 OGR_G_DestroyGeometry(src_geom);
2905 else if (NULL == scale_x) {
2917 rterror(
"rt_raster_gdal_rasterize: Could not compute spatial coordinates for raster pixel");
2920 OGR_G_DestroyGeometry(src_geom);
2927 rast->scaleX = fabs((_c[0] - _w[0]) / ((
double)
rast->width));
2933 else if (NULL == scale_y) {
2945 rterror(
"rt_raster_gdal_rasterize: Could not compute spatial coordinates for raster pixel");
2948 OGR_G_DestroyGeometry(src_geom);
2955 rast->scaleY = -1 * fabs((_c[1] - _w[1]) / ((
double)
rast->height));
2969 _dim[0] =
rast->width;
2970 _dim[1] =
rast->height;
2975 (NULL != scale_x) && (*scale_x < 0.)
2977 (NULL != scale_y) && (*scale_y > 0)
2983 (NULL != scale_x) &&
2994 rterror(
"rt_raster_gdal_rasterize: Could not compute spatial coordinates for raster pixel");
2997 OGR_G_DestroyGeometry(src_geom);
3008 if (NULL != skew_x &&
FLT_NEQ(*skew_x, 0.0))
3013 (NULL != scale_y) &&
3024 rterror(
"rt_raster_gdal_rasterize: Could not compute spatial coordinates for raster pixel");
3027 OGR_G_DestroyGeometry(src_geom);
3038 if (NULL != skew_y &&
FLT_NEQ(*skew_y, 0.0))
3046 RASTER_DEBUGF(3,
"Applied geotransform: %f, %f, %f, %f, %f, %f",
3047 _gt[0], _gt[1], _gt[2], _gt[3], _gt[4], _gt[5]);
3048 RASTER_DEBUGF(3,
"Raster dimensions (width x height): %d x %d",
3057 _drv = GDALGetDriverByName(
"MEM");
3059 rterror(
"rt_raster_gdal_rasterize: Could not load the MEM GDAL driver");
3061 OGR_G_DestroyGeometry(src_geom);
3071 GDALDeregisterDriver(_drv);
3074 _ds = GDALCreate(_drv,
"", _dim[0], _dim[1], 0, GDT_Byte, NULL);
3076 rterror(
"rt_raster_gdal_rasterize: Could not create a GDALDataset to rasterize the geometry into");
3078 OGR_G_DestroyGeometry(src_geom);
3081 if (unload_drv) GDALDestroyDriver(_drv);
3087 cplerr = GDALSetGeoTransform(_ds, _gt);
3088 if (cplerr != CE_None) {
3089 rterror(
"rt_raster_gdal_rasterize: Could not set geotransform on GDALDataset");
3091 OGR_G_DestroyGeometry(src_geom);
3096 if (unload_drv) GDALDestroyDriver(_drv);
3102 if (NULL != arg->
src_sr) {
3104 OSRExportToWkt(arg->
src_sr, &_srs);
3106 cplerr = GDALSetProjection(_ds, _srs);
3108 if (cplerr != CE_None) {
3109 rterror(
"rt_raster_gdal_rasterize: Could not set projection on GDALDataset");
3111 OGR_G_DestroyGeometry(src_geom);
3116 if (unload_drv) GDALDestroyDriver(_drv);
3123 for (i = 0; i < arg->
numbands; i++) {
3129 if (cplerr != CE_None) {
3130 rterror(
"rt_raster_gdal_rasterize: Could not add band to GDALDataset");
3135 _band = GDALGetRasterBand(_ds, i + 1);
3136 if (NULL == _band) {
3137 rterror(
"rt_raster_gdal_rasterize: Could not get band %d from GDALDataset", i + 1);
3145 cplerr = GDALSetRasterNoDataValue(_band, arg->
nodata[i]);
3146 if (cplerr != CE_None) {
3147 rterror(
"rt_raster_gdal_rasterize: Could not set nodata value");
3151 RASTER_DEBUGF(4,
"NODATA value set to %f", GDALGetRasterNoDataValue(_band, NULL));
3155 cplerr = GDALFillRaster(_band, arg->
init[i], 0);
3156 if (cplerr != CE_None) {
3157 rterror(
"rt_raster_gdal_rasterize: Could not set initial value");
3166 OGR_G_DestroyGeometry(src_geom);
3172 if (unload_drv) GDALDestroyDriver(_drv);
3182 cplerr = GDALRasterizeGeometries(
3191 if (cplerr != CE_None) {
3192 rterror(
"rt_raster_gdal_rasterize: Could not rasterize geometry");
3194 OGR_G_DestroyGeometry(src_geom);
3199 if (unload_drv) GDALDestroyDriver(_drv);
3205 GDALFlushCache(_ds);
3209 OGR_G_DestroyGeometry(src_geom);
3213 if (unload_drv) GDALDestroyDriver(_drv);
3216 rterror(
"rt_raster_gdal_rasterize: Could not rasterize geometry");
3225 for (i = 0; i < arg->
numbands; i++) {
3226 uint8_t *
data = NULL;
3233 double nodataval = 0;
3238 if (oldband == NULL) {
3239 rterror(
"rt_raster_gdal_rasterize: Could not get band %d of output raster", i);
3257 rterror(
"rt_raster_gdal_rasterize: Could not allocate memory for band data");
3268 hasnodata, nodataval,
3272 rterror(
"rt_raster_gdal_rasterize: Could not create band");
3283 for (
x = 0;
x < _width;
x++) {
3284 for (
y = 0;
y < _height;
y++) {
3287 rterror(
"rt_raster_gdal_rasterize: Could not get pixel value");
3299 rterror(
"rt_raster_gdal_rasterize: Could not set pixel value");
3310 if (oldband == NULL) {
3311 rterror(
"rt_raster_gdal_rasterize: Could not replace band %d of output raster", i);
rt_band rt_band_new_inline(uint16_t width, uint16_t height, rt_pixtype pixtype, uint32_t hasnodata, double nodataval, uint8_t *data)
Create an in-db rt_band with no data.
void rt_band_set_ownsdata_flag(rt_band band, int flag)
void rterror(const char *fmt,...) __attribute__((format(printf
Wrappers used for reporting errors and info.
void * rtalloc(size_t size)
Wrappers used for managing memory.
#define RASTER_DEBUG(level, msg)
#define RASTER_DEBUGF(level, msg,...)
void void rtinfo(const char *fmt,...) __attribute__((format(printf
int rt_band_get_hasnodata_flag(rt_band band)
Get hasnodata flag value.
rt_errorstate rt_band_get_pixel(rt_band band, int x, int y, double *value, int *nodata)
Get pixel value.
GDALDataType rt_util_pixtype_to_gdal_datatype(rt_pixtype pt)
Convert rt_pixtype to GDALDataType.
rt_errorstate rt_band_set_pixel(rt_band band, int x, int y, double val, int *converted)
Set single pixel's value.
void rt_band_destroy(rt_band band)
Destroy a raster band.
int rt_util_gdal_driver_registered(const char *drv)
rt_errorstate rt_band_get_nodata(rt_band band, double *nodata)
Get NODATA value.
rt_pixtype rt_band_get_pixtype(rt_band band)
Return pixeltype of this band.
void rtdealloc(void *mem)
void rt_util_from_ogr_envelope(OGREnvelope env, rt_envelope *ext)
int rt_pixtype_size(rt_pixtype pixtype)
Return size in bytes of a value in the given pixtype.
rt_errorstate rt_raster_cell_to_geopoint(rt_raster raster, double xr, double yr, double *xw, double *yw, double *gt)
Convert an xr, yr raster point to an xw, yw point on map.
void rt_raster_set_scale(rt_raster raster, double scaleX, double scaleY)
Set scale in projection units.
rt_errorstate rt_raster_geopoint_to_cell(rt_raster raster, double xw, double yw, double *xr, double *yr, double *igt)
Convert an xw,yw map point to a xr,yr cell coordinate.
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
void rt_raster_set_skews(rt_raster raster, double skewX, double skewY)
Set skews about the X and Y axis.
rt_raster rt_raster_new(uint32_t width, uint32_t height)
Construct a raster with given dimensions.
rt_raster rt_raster_compute_skewed_raster(rt_envelope extent, double *skew, double *scale, double tolerance)
rt_raster rt_raster_from_gdal_dataset(GDALDatasetH ds)
Return a raster from a GDAL dataset.
static void _rti_rasterize_arg_destroy(_rti_rasterize_arg arg)
rt_band rt_raster_replace_band(rt_raster raster, rt_band band, int index)
Replace band at provided index with new band.
uint16_t rt_raster_get_height(rt_raster raster)
rt_band rt_raster_get_band(rt_raster raster, int n)
Return Nth band, or NULL if unavailable.
uint16_t rt_raster_get_width(rt_raster raster)
static _rti_rasterize_arg _rti_rasterize_arg_init()
void rt_raster_get_geotransform_matrix(rt_raster raster, double *gt)
Get 6-element array of raster geotransform matrix.
void rt_raster_set_offsets(rt_raster raster, double x, double y)
Set insertion points in projection units.
OGRSpatialReferenceH src_sr