Return a raster of the provided geometry.
2529 double _scale[2] = {0};
2530 double _skew[2] = {0};
2533 OGRGeometryH src_geom;
2534 OGREnvelope src_env;
2536 OGRwkbGeometryType wkbtype = wkbUnknown;
2541 double _gt[6] = {0};
2542 GDALDriverH _drv = NULL;
2544 GDALDatasetH _ds = NULL;
2545 GDALRasterBandH _band = NULL;
2547 uint16_t _width = 0;
2548 uint16_t _height = 0;
2552 assert(NULL != wkb);
2553 assert(0 != wkb_len);
2558 rterror(
"rt_raster_gdal_rasterize: Could not initialize internal variables");
2563 if (num_bands < 1) {
2594 if (NULL != srs && strlen(srs)) {
2595 arg->
src_sr = OSRNewSpatialReference(NULL);
2596 if (OSRSetFromUserInput(arg->
src_sr, srs) != OGRERR_NONE) {
2597 rterror(
"rt_raster_gdal_rasterize: Could not create OSR spatial reference using the provided srs: %s", srs);
2604 ogrerr = OGR_G_CreateFromWkb((
unsigned char *) wkb, arg->
src_sr, &src_geom, wkb_len);
2605 if (ogrerr != OGRERR_NONE) {
2606 rterror(
"rt_raster_gdal_rasterize: Could not create OGR Geometry from WKB");
2615 if (OGR_G_IsEmpty(src_geom)) {
2616 rtinfo(
"Geometry provided is empty. Returning empty raster");
2618 OGR_G_DestroyGeometry(src_geom);
2626 OGR_G_GetEnvelope(src_geom, &src_env);
2629 RASTER_DEBUGF(3,
"Suggested raster envelope: %f, %f, %f, %f",
2634 (NULL != scale_x) &&
2635 (NULL != scale_y) &&
2640 _scale[0] = fabs(*scale_x);
2641 _scale[1] = fabs(*scale_y);
2650 _dim[0] = abs(*width);
2651 _dim[1] = abs(*height);
2654 _scale[0] = fabs((extent.
MaxX - extent.
MinX) / _dim[0]);
2659 _scale[1] = fabs((extent.
MaxY - extent.
MinY) / _dim[1]);
2664 rterror(
"rt_raster_gdal_rasterize: Values must be provided for width and height or X and Y of scale");
2666 OGR_G_DestroyGeometry(src_geom);
2672 RASTER_DEBUGF(3,
"scale (x, y) = %f, %f", _scale[0], -1 * _scale[1]);
2676 if (NULL != skew_x) {
2690 if (NULL != skew_y) {
2712 wkbtype = wkbFlatten(OGR_G_GetGeometryType(src_geom));
2714 (wkbtype == wkbPoint) ||
2715 (wkbtype == wkbMultiPoint) ||
2716 (wkbtype == wkbLineString) ||
2717 (wkbtype == wkbMultiLineString)
2723 #if POSTGIS_GDAL_VERSION > 18
2725 RASTER_DEBUG(3,
"Adjusting extent for GDAL > 1.8 by half the scale on X-axis");
2726 extent.
MinX -= (_scale[0] / 2.);
2727 extent.
MaxX += (_scale[0] / 2.);
2729 RASTER_DEBUG(3,
"Adjusting extent for GDAL > 1.8 by half the scale on Y-axis");
2730 extent.
MinY -= (_scale[1] / 2.);
2731 extent.
MaxY += (_scale[1] / 2.);
2735 RASTER_DEBUG(3,
"Adjusting extent for GDAL <= 1.8 by the scale on X-axis");
2736 extent.
MinX -= _scale[0];
2737 extent.
MaxX += _scale[0];
2739 RASTER_DEBUG(3,
"Adjusting extent for GDAL <= 1.8 by the scale on Y-axis");
2740 extent.
MinY -= _scale[1];
2741 extent.
MaxY += _scale[1];
2768 if (skewedrast == NULL) {
2769 rterror(
"rt_raster_gdal_rasterize: Could not compute skewed raster");
2771 OGR_G_DestroyGeometry(src_geom);
2778 _dim[0] = skewedrast->
width;
2779 _dim[1] = skewedrast->
height;
2789 _dim[0] = (int) fmax((fabs(extent.
MaxX - extent.
MinX) + (_scale[0] / 2.)) / _scale[0], 1);
2791 _dim[1] = (int) fmax((fabs(extent.
MaxY - extent.
MinY) + (_scale[1] / 2.)) / _scale[1], 1);
2796 rterror(
"rt_raster_gdal_rasterize: Out of memory allocating temporary raster");
2798 OGR_G_DestroyGeometry(src_geom);
2811 RASTER_DEBUGF(3,
"Temp raster's geotransform: %f, %f, %f, %f, %f, %f",
2812 _gt[0], _gt[1], _gt[2], _gt[3], _gt[4], _gt[5]);
2813 RASTER_DEBUGF(3,
"Temp raster's dimensions (width x height): %d x %d",
2823 RASTER_DEBUGF(4,
"Using user-specified upper-left corner: %f, %f", *ul_xw, *ul_yw);
2831 ((NULL != ul_xw) && (NULL == ul_yw)) ||
2832 ((NULL == ul_xw) && (NULL != ul_yw))
2834 rterror(
"rt_raster_gdal_rasterize: Both X and Y upper-left corner values must be provided");
2837 OGR_G_DestroyGeometry(src_geom);
2847 (NULL != grid_xw) || (NULL != grid_yw)
2852 ((NULL != grid_xw) && (NULL == grid_yw)) ||
2853 ((NULL == grid_xw) && (NULL != grid_yw))
2855 rterror(
"rt_raster_gdal_rasterize: Both X and Y alignment values must be provided");
2858 OGR_G_DestroyGeometry(src_geom);
2865 RASTER_DEBUGF(4,
"Aligning extent to user-specified grid: %f, %f", *grid_xw, *grid_yw);
2873 RASTER_DEBUG(3,
"Skipping raster alignment as it is already aligned to grid");
2888 rterror(
"rt_raster_gdal_rasterize: Could not compute raster pixel for spatial coordinates");
2891 OGR_G_DestroyGeometry(src_geom);
2904 rterror(
"rt_raster_gdal_rasterize: Could not compute spatial coordinates for raster pixel");
2907 OGR_G_DestroyGeometry(src_geom);
2918 else if (NULL == scale_x) {
2930 rterror(
"rt_raster_gdal_rasterize: Could not compute spatial coordinates for raster pixel");
2933 OGR_G_DestroyGeometry(src_geom);
2940 rast->scaleX = fabs((_c[0] - _w[0]) / ((
double)
rast->width));
2946 else if (NULL == scale_y) {
2958 rterror(
"rt_raster_gdal_rasterize: Could not compute spatial coordinates for raster pixel");
2961 OGR_G_DestroyGeometry(src_geom);
2968 rast->scaleY = -1 * fabs((_c[1] - _w[1]) / ((
double)
rast->height));
2982 _dim[0] =
rast->width;
2983 _dim[1] =
rast->height;
2988 (NULL != scale_x) && (*scale_x < 0.)
2990 (NULL != scale_y) && (*scale_y > 0)
2996 (NULL != scale_x) &&
3007 rterror(
"rt_raster_gdal_rasterize: Could not compute spatial coordinates for raster pixel");
3010 OGR_G_DestroyGeometry(src_geom);
3021 if (NULL != skew_x &&
FLT_NEQ(*skew_x, 0))
3026 (NULL != scale_y) &&
3037 rterror(
"rt_raster_gdal_rasterize: Could not compute spatial coordinates for raster pixel");
3040 OGR_G_DestroyGeometry(src_geom);
3051 if (NULL != skew_y &&
FLT_NEQ(*skew_y, 0))
3059 RASTER_DEBUGF(3,
"Applied geotransform: %f, %f, %f, %f, %f, %f",
3060 _gt[0], _gt[1], _gt[2], _gt[3], _gt[4], _gt[5]);
3061 RASTER_DEBUGF(3,
"Raster dimensions (width x height): %d x %d",
3070 _drv = GDALGetDriverByName(
"MEM");
3072 rterror(
"rt_raster_gdal_rasterize: Could not load the MEM GDAL driver");
3074 OGR_G_DestroyGeometry(src_geom);
3084 GDALDeregisterDriver(_drv);
3087 _ds = GDALCreate(_drv,
"", _dim[0], _dim[1], 0, GDT_Byte, NULL);
3089 rterror(
"rt_raster_gdal_rasterize: Could not create a GDALDataset to rasterize the geometry into");
3091 OGR_G_DestroyGeometry(src_geom);
3094 if (unload_drv) GDALDestroyDriver(_drv);
3100 cplerr = GDALSetGeoTransform(_ds, _gt);
3101 if (cplerr != CE_None) {
3102 rterror(
"rt_raster_gdal_rasterize: Could not set geotransform on GDALDataset");
3104 OGR_G_DestroyGeometry(src_geom);
3109 if (unload_drv) GDALDestroyDriver(_drv);
3115 if (NULL != arg->
src_sr) {
3117 OSRExportToWkt(arg->
src_sr, &_srs);
3119 cplerr = GDALSetProjection(_ds, _srs);
3121 if (cplerr != CE_None) {
3122 rterror(
"rt_raster_gdal_rasterize: Could not set projection on GDALDataset");
3124 OGR_G_DestroyGeometry(src_geom);
3129 if (unload_drv) GDALDestroyDriver(_drv);
3136 for (i = 0; i < arg->
numbands; i++) {
3142 if (cplerr != CE_None) {
3143 rterror(
"rt_raster_gdal_rasterize: Could not add band to GDALDataset");
3148 _band = GDALGetRasterBand(_ds, i + 1);
3149 if (NULL == _band) {
3150 rterror(
"rt_raster_gdal_rasterize: Could not get band %d from GDALDataset", i + 1);
3158 cplerr = GDALSetRasterNoDataValue(_band, arg->
nodata[i]);
3159 if (cplerr != CE_None) {
3160 rterror(
"rt_raster_gdal_rasterize: Could not set nodata value");
3164 RASTER_DEBUGF(4,
"NODATA value set to %f", GDALGetRasterNoDataValue(_band, NULL));
3168 cplerr = GDALFillRaster(_band, arg->
init[i], 0);
3169 if (cplerr != CE_None) {
3170 rterror(
"rt_raster_gdal_rasterize: Could not set initial value");
3179 OGR_G_DestroyGeometry(src_geom);
3185 if (unload_drv) GDALDestroyDriver(_drv);
3195 cplerr = GDALRasterizeGeometries(
3204 if (cplerr != CE_None) {
3205 rterror(
"rt_raster_gdal_rasterize: Could not rasterize geometry");
3207 OGR_G_DestroyGeometry(src_geom);
3212 if (unload_drv) GDALDestroyDriver(_drv);
3218 GDALFlushCache(_ds);
3222 OGR_G_DestroyGeometry(src_geom);
3226 if (unload_drv) GDALDestroyDriver(_drv);
3229 rterror(
"rt_raster_gdal_rasterize: Could not rasterize geometry");
3238 for (i = 0; i < arg->
numbands; i++) {
3246 double nodataval = 0;
3251 if (oldband == NULL) {
3252 rterror(
"rt_raster_gdal_rasterize: Could not get band %d of output raster", i);
3270 rterror(
"rt_raster_gdal_rasterize: Could not allocate memory for band data");
3281 hasnodata, nodataval,
3285 rterror(
"rt_raster_gdal_rasterize: Could not create band");
3296 for (
x = 0;
x < _width;
x++) {
3297 for (
y = 0;
y < _height;
y++) {
3300 rterror(
"rt_raster_gdal_rasterize: Could not get pixel value");
3312 rterror(
"rt_raster_gdal_rasterize: Could not set pixel value");
3323 if (oldband == NULL) {
3324 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