Return a raster of the provided geometry.
2524 double _scale[2] = {0};
2525 double _skew[2] = {0};
2528 OGRGeometryH src_geom;
2529 OGREnvelope src_env;
2531 OGRwkbGeometryType wkbtype = wkbUnknown;
2536 double _gt[6] = {0};
2537 GDALDriverH _drv = NULL;
2539 GDALDatasetH _ds = NULL;
2540 GDALRasterBandH _band = NULL;
2542 uint16_t _width = 0;
2543 uint16_t _height = 0;
2547 assert(NULL != wkb);
2548 assert(0 != wkb_len);
2553 rterror(
"rt_raster_gdal_rasterize: Could not initialize internal variables");
2558 if (num_bands < 1) {
2589 if (NULL != srs && strlen(srs)) {
2590 arg->
src_sr = OSRNewSpatialReference(NULL);
2591 if (OSRSetFromUserInput(arg->
src_sr, srs) != OGRERR_NONE) {
2592 rterror(
"rt_raster_gdal_rasterize: Could not create OSR spatial reference using the provided srs: %s", srs);
2599 ogrerr = OGR_G_CreateFromWkb((
unsigned char *) wkb, arg->
src_sr, &src_geom, wkb_len);
2600 if (ogrerr != OGRERR_NONE) {
2601 rterror(
"rt_raster_gdal_rasterize: Could not create OGR Geometry from WKB");
2610 if (OGR_G_IsEmpty(src_geom)) {
2611 rtinfo(
"Geometry provided is empty. Returning empty raster");
2613 OGR_G_DestroyGeometry(src_geom);
2621 OGR_G_GetEnvelope(src_geom, &src_env);
2624 RASTER_DEBUGF(3,
"Suggested raster envelope: %f, %f, %f, %f",
2629 (NULL != scale_x) &&
2630 (NULL != scale_y) &&
2635 _scale[0] = fabs(*scale_x);
2636 _scale[1] = fabs(*scale_y);
2639 else if ((NULL != width) && (NULL != height) && (*width != 0) && (*height != 0))
2641 _dim[0] = abs(*width);
2642 _dim[1] = abs(*height);
2645 _scale[0] = fabs((extent.
MaxX - extent.
MinX) / _dim[0]);
2650 _scale[1] = fabs((extent.
MaxY - extent.
MinY) / _dim[1]);
2655 rterror(
"rt_raster_gdal_rasterize: Values must be provided for width and height or X and Y of scale");
2657 OGR_G_DestroyGeometry(src_geom);
2663 RASTER_DEBUGF(3,
"scale (x, y) = %f, %f", _scale[0], -1 * _scale[1]);
2667 if (NULL != skew_x) {
2681 if (NULL != skew_y) {
2703 wkbtype = wkbFlatten(OGR_G_GetGeometryType(src_geom));
2705 (wkbtype == wkbPoint) ||
2706 (wkbtype == wkbMultiPoint) ||
2707 (wkbtype == wkbLineString) ||
2708 (wkbtype == wkbMultiLineString)
2714 #if POSTGIS_GDAL_VERSION > 18
2716 RASTER_DEBUG(3,
"Adjusting extent for GDAL > 1.8 by half the scale on X-axis");
2717 extent.
MinX -= (_scale[0] / 2.);
2718 extent.
MaxX += (_scale[0] / 2.);
2720 RASTER_DEBUG(3,
"Adjusting extent for GDAL > 1.8 by half the scale on Y-axis");
2721 extent.
MinY -= (_scale[1] / 2.);
2722 extent.
MaxY += (_scale[1] / 2.);
2726 RASTER_DEBUG(3,
"Adjusting extent for GDAL <= 1.8 by the scale on X-axis");
2727 extent.
MinX -= _scale[0];
2728 extent.
MaxX += _scale[0];
2730 RASTER_DEBUG(3,
"Adjusting extent for GDAL <= 1.8 by the scale on Y-axis");
2731 extent.
MinY -= _scale[1];
2732 extent.
MaxY += _scale[1];
2757 if (skewedrast == NULL) {
2758 rterror(
"rt_raster_gdal_rasterize: Could not compute skewed raster");
2760 OGR_G_DestroyGeometry(src_geom);
2767 _dim[0] = skewedrast->
width;
2768 _dim[1] = skewedrast->
height;
2778 _dim[0] = (int) fmax((fabs(extent.
MaxX - extent.
MinX) + (_scale[0] / 2.)) / _scale[0], 1);
2780 _dim[1] = (int) fmax((fabs(extent.
MaxY - extent.
MinY) + (_scale[1] / 2.)) / _scale[1], 1);
2785 rterror(
"rt_raster_gdal_rasterize: Out of memory allocating temporary raster");
2787 OGR_G_DestroyGeometry(src_geom);
2800 RASTER_DEBUGF(3,
"Temp raster's geotransform: %f, %f, %f, %f, %f, %f",
2801 _gt[0], _gt[1], _gt[2], _gt[3], _gt[4], _gt[5]);
2802 RASTER_DEBUGF(3,
"Temp raster's dimensions (width x height): %d x %d",
2812 RASTER_DEBUGF(4,
"Using user-specified upper-left corner: %f, %f", *ul_xw, *ul_yw);
2820 ((NULL != ul_xw) && (NULL == ul_yw)) ||
2821 ((NULL == ul_xw) && (NULL != ul_yw))
2823 rterror(
"rt_raster_gdal_rasterize: Both X and Y upper-left corner values must be provided");
2826 OGR_G_DestroyGeometry(src_geom);
2836 (NULL != grid_xw) || (NULL != grid_yw)
2841 ((NULL != grid_xw) && (NULL == grid_yw)) ||
2842 ((NULL == grid_xw) && (NULL != grid_yw))
2844 rterror(
"rt_raster_gdal_rasterize: Both X and Y alignment values must be provided");
2847 OGR_G_DestroyGeometry(src_geom);
2854 RASTER_DEBUGF(4,
"Aligning extent to user-specified grid: %f, %f", *grid_xw, *grid_yw);
2862 RASTER_DEBUG(3,
"Skipping raster alignment as it is already aligned to grid");
2877 rterror(
"rt_raster_gdal_rasterize: Could not compute raster pixel for spatial coordinates");
2880 OGR_G_DestroyGeometry(src_geom);
2893 rterror(
"rt_raster_gdal_rasterize: Could not compute spatial coordinates for raster pixel");
2896 OGR_G_DestroyGeometry(src_geom);
2907 else if (NULL == scale_x) {
2919 rterror(
"rt_raster_gdal_rasterize: Could not compute spatial coordinates for raster pixel");
2922 OGR_G_DestroyGeometry(src_geom);
2929 rast->scaleX = fabs((_c[0] - _w[0]) / ((
double)
rast->width));
2935 else if (NULL == scale_y) {
2947 rterror(
"rt_raster_gdal_rasterize: Could not compute spatial coordinates for raster pixel");
2950 OGR_G_DestroyGeometry(src_geom);
2957 rast->scaleY = -1 * fabs((_c[1] - _w[1]) / ((
double)
rast->height));
2971 _dim[0] =
rast->width;
2972 _dim[1] =
rast->height;
2977 (NULL != scale_x) && (*scale_x < 0.)
2979 (NULL != scale_y) && (*scale_y > 0)
2985 (NULL != scale_x) &&
2996 rterror(
"rt_raster_gdal_rasterize: Could not compute spatial coordinates for raster pixel");
2999 OGR_G_DestroyGeometry(src_geom);
3010 if (NULL != skew_x &&
FLT_NEQ(*skew_x, 0.0))
3015 (NULL != scale_y) &&
3026 rterror(
"rt_raster_gdal_rasterize: Could not compute spatial coordinates for raster pixel");
3029 OGR_G_DestroyGeometry(src_geom);
3040 if (NULL != skew_y &&
FLT_NEQ(*skew_y, 0.0))
3048 RASTER_DEBUGF(3,
"Applied geotransform: %f, %f, %f, %f, %f, %f",
3049 _gt[0], _gt[1], _gt[2], _gt[3], _gt[4], _gt[5]);
3050 RASTER_DEBUGF(3,
"Raster dimensions (width x height): %d x %d",
3059 _drv = GDALGetDriverByName(
"MEM");
3061 rterror(
"rt_raster_gdal_rasterize: Could not load the MEM GDAL driver");
3063 OGR_G_DestroyGeometry(src_geom);
3073 GDALDeregisterDriver(_drv);
3076 _ds = GDALCreate(_drv,
"", _dim[0], _dim[1], 0, GDT_Byte, NULL);
3078 rterror(
"rt_raster_gdal_rasterize: Could not create a GDALDataset to rasterize the geometry into");
3080 OGR_G_DestroyGeometry(src_geom);
3083 if (unload_drv) GDALDestroyDriver(_drv);
3089 cplerr = GDALSetGeoTransform(_ds, _gt);
3090 if (cplerr != CE_None) {
3091 rterror(
"rt_raster_gdal_rasterize: Could not set geotransform on GDALDataset");
3093 OGR_G_DestroyGeometry(src_geom);
3098 if (unload_drv) GDALDestroyDriver(_drv);
3104 if (NULL != arg->
src_sr) {
3106 OSRExportToWkt(arg->
src_sr, &_srs);
3108 cplerr = GDALSetProjection(_ds, _srs);
3110 if (cplerr != CE_None) {
3111 rterror(
"rt_raster_gdal_rasterize: Could not set projection on GDALDataset");
3113 OGR_G_DestroyGeometry(src_geom);
3118 if (unload_drv) GDALDestroyDriver(_drv);
3125 for (i = 0; i < arg->
numbands; i++) {
3131 if (cplerr != CE_None) {
3132 rterror(
"rt_raster_gdal_rasterize: Could not add band to GDALDataset");
3137 _band = GDALGetRasterBand(_ds, i + 1);
3138 if (NULL == _band) {
3139 rterror(
"rt_raster_gdal_rasterize: Could not get band %d from GDALDataset", i + 1);
3147 cplerr = GDALSetRasterNoDataValue(_band, arg->
nodata[i]);
3148 if (cplerr != CE_None) {
3149 rterror(
"rt_raster_gdal_rasterize: Could not set nodata value");
3153 RASTER_DEBUGF(4,
"NODATA value set to %f", GDALGetRasterNoDataValue(_band, NULL));
3157 cplerr = GDALFillRaster(_band, arg->
init[i], 0);
3158 if (cplerr != CE_None) {
3159 rterror(
"rt_raster_gdal_rasterize: Could not set initial value");
3168 OGR_G_DestroyGeometry(src_geom);
3174 if (unload_drv) GDALDestroyDriver(_drv);
3184 cplerr = GDALRasterizeGeometries(
3193 if (cplerr != CE_None) {
3194 rterror(
"rt_raster_gdal_rasterize: Could not rasterize geometry");
3196 OGR_G_DestroyGeometry(src_geom);
3201 if (unload_drv) GDALDestroyDriver(_drv);
3207 GDALFlushCache(_ds);
3211 OGR_G_DestroyGeometry(src_geom);
3215 if (unload_drv) GDALDestroyDriver(_drv);
3218 rterror(
"rt_raster_gdal_rasterize: Could not rasterize geometry");
3227 for (i = 0; i < arg->
numbands; i++) {
3228 uint8_t *
data = NULL;
3235 double nodataval = 0;
3240 if (oldband == NULL) {
3241 rterror(
"rt_raster_gdal_rasterize: Could not get band %d of output raster", i);
3259 rterror(
"rt_raster_gdal_rasterize: Could not allocate memory for band data");
3270 hasnodata, nodataval,
3274 rterror(
"rt_raster_gdal_rasterize: Could not create band");
3285 for (
x = 0;
x < _width;
x++) {
3286 for (
y = 0;
y < _height;
y++) {
3289 rterror(
"rt_raster_gdal_rasterize: Could not get pixel value");
3301 rterror(
"rt_raster_gdal_rasterize: Could not set pixel value");
3312 if (oldband == NULL) {
3313 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,...)
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 rtinfo(const char *fmt,...)
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 raster point.
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