Return a raster of the provided geometry.
2511 double _scale[2] = {0};
2512 double _skew[2] = {0};
2515 OGRGeometryH src_geom;
2516 OGREnvelope src_env;
2518 OGRwkbGeometryType wkbtype = wkbUnknown;
2523 double _gt[6] = {0};
2524 GDALDriverH _drv = NULL;
2526 GDALDatasetH _ds = NULL;
2527 GDALRasterBandH _band = NULL;
2529 uint16_t _width = 0;
2530 uint16_t _height = 0;
2534 assert(NULL != wkb);
2535 assert(0 != wkb_len);
2540 rterror(
"rt_raster_gdal_rasterize: Could not initialize internal variables");
2545 if (num_bands < 1) {
2576 if (NULL != srs && strlen(srs)) {
2577 arg->
src_sr = OSRNewSpatialReference(NULL);
2578 if (OSRSetFromUserInput(arg->
src_sr, srs) != OGRERR_NONE) {
2579 rterror(
"rt_raster_gdal_rasterize: Could not create OSR spatial reference using the provided srs: %s", srs);
2586 ogrerr = OGR_G_CreateFromWkb((
unsigned char *) wkb, arg->
src_sr, &src_geom, wkb_len);
2587 if (ogrerr != OGRERR_NONE) {
2588 rterror(
"rt_raster_gdal_rasterize: Could not create OGR Geometry from WKB");
2597 if (OGR_G_IsEmpty(src_geom)) {
2598 rtinfo(
"Geometry provided is empty. Returning empty raster");
2600 OGR_G_DestroyGeometry(src_geom);
2608 OGR_G_GetEnvelope(src_geom, &src_env);
2611 RASTER_DEBUGF(3,
"Suggested raster envelope: %f, %f, %f, %f",
2616 (NULL != scale_x) &&
2617 (NULL != scale_y) &&
2622 _scale[0] = fabs(*scale_x);
2623 _scale[1] = fabs(*scale_y);
2626 else if ((NULL != width) && (NULL != height) && (*width != 0) && (*height != 0))
2628 _dim[0] = abs(*width);
2629 _dim[1] = abs(*height);
2632 _scale[0] = fabs((extent.
MaxX - extent.
MinX) / _dim[0]);
2637 _scale[1] = fabs((extent.
MaxY - extent.
MinY) / _dim[1]);
2642 rterror(
"rt_raster_gdal_rasterize: Values must be provided for width and height or X and Y of scale");
2644 OGR_G_DestroyGeometry(src_geom);
2650 RASTER_DEBUGF(3,
"scale (x, y) = %f, %f", _scale[0], -1 * _scale[1]);
2654 if (NULL != skew_x) {
2668 if (NULL != skew_y) {
2690 wkbtype = wkbFlatten(OGR_G_GetGeometryType(src_geom));
2692 (wkbtype == wkbPoint) ||
2693 (wkbtype == wkbMultiPoint) ||
2694 (wkbtype == wkbLineString) ||
2695 (wkbtype == wkbMultiLineString)
2701 #if POSTGIS_GDAL_VERSION > 18
2703 RASTER_DEBUG(3,
"Adjusting extent for GDAL > 1.8 by half the scale on X-axis");
2704 extent.
MinX -= (_scale[0] / 2.);
2705 extent.
MaxX += (_scale[0] / 2.);
2707 RASTER_DEBUG(3,
"Adjusting extent for GDAL > 1.8 by half the scale on Y-axis");
2708 extent.
MinY -= (_scale[1] / 2.);
2709 extent.
MaxY += (_scale[1] / 2.);
2713 RASTER_DEBUG(3,
"Adjusting extent for GDAL <= 1.8 by the scale on X-axis");
2714 extent.
MinX -= _scale[0];
2715 extent.
MaxX += _scale[0];
2717 RASTER_DEBUG(3,
"Adjusting extent for GDAL <= 1.8 by the scale on Y-axis");
2718 extent.
MinY -= _scale[1];
2719 extent.
MaxY += _scale[1];
2744 if (skewedrast == NULL) {
2745 rterror(
"rt_raster_gdal_rasterize: Could not compute skewed raster");
2747 OGR_G_DestroyGeometry(src_geom);
2754 _dim[0] = skewedrast->
width;
2755 _dim[1] = skewedrast->
height;
2765 _dim[0] = (int) fmax((fabs(extent.
MaxX - extent.
MinX) + (_scale[0] / 2.)) / _scale[0], 1);
2767 _dim[1] = (int) fmax((fabs(extent.
MaxY - extent.
MinY) + (_scale[1] / 2.)) / _scale[1], 1);
2772 rterror(
"rt_raster_gdal_rasterize: Out of memory allocating temporary raster");
2774 OGR_G_DestroyGeometry(src_geom);
2787 RASTER_DEBUGF(3,
"Temp raster's geotransform: %f, %f, %f, %f, %f, %f",
2788 _gt[0], _gt[1], _gt[2], _gt[3], _gt[4], _gt[5]);
2789 RASTER_DEBUGF(3,
"Temp raster's dimensions (width x height): %d x %d",
2799 RASTER_DEBUGF(4,
"Using user-specified upper-left corner: %f, %f", *ul_xw, *ul_yw);
2807 ((NULL != ul_xw) && (NULL == ul_yw)) ||
2808 ((NULL == ul_xw) && (NULL != ul_yw))
2810 rterror(
"rt_raster_gdal_rasterize: Both X and Y upper-left corner values must be provided");
2813 OGR_G_DestroyGeometry(src_geom);
2823 (NULL != grid_xw) || (NULL != grid_yw)
2828 ((NULL != grid_xw) && (NULL == grid_yw)) ||
2829 ((NULL == grid_xw) && (NULL != grid_yw))
2831 rterror(
"rt_raster_gdal_rasterize: Both X and Y alignment values must be provided");
2834 OGR_G_DestroyGeometry(src_geom);
2841 RASTER_DEBUGF(4,
"Aligning extent to user-specified grid: %f, %f", *grid_xw, *grid_yw);
2849 RASTER_DEBUG(3,
"Skipping raster alignment as it is already aligned to grid");
2864 rterror(
"rt_raster_gdal_rasterize: Could not compute raster pixel for spatial coordinates");
2867 OGR_G_DestroyGeometry(src_geom);
2880 rterror(
"rt_raster_gdal_rasterize: Could not compute spatial coordinates for raster pixel");
2883 OGR_G_DestroyGeometry(src_geom);
2894 else if (NULL == scale_x) {
2906 rterror(
"rt_raster_gdal_rasterize: Could not compute spatial coordinates for raster pixel");
2909 OGR_G_DestroyGeometry(src_geom);
2916 rast->scaleX = fabs((_c[0] - _w[0]) / ((
double)
rast->width));
2922 else if (NULL == scale_y) {
2934 rterror(
"rt_raster_gdal_rasterize: Could not compute spatial coordinates for raster pixel");
2937 OGR_G_DestroyGeometry(src_geom);
2944 rast->scaleY = -1 * fabs((_c[1] - _w[1]) / ((
double)
rast->height));
2958 _dim[0] =
rast->width;
2959 _dim[1] =
rast->height;
2964 (NULL != scale_x) && (*scale_x < 0.)
2966 (NULL != scale_y) && (*scale_y > 0)
2972 (NULL != scale_x) &&
2983 rterror(
"rt_raster_gdal_rasterize: Could not compute spatial coordinates for raster pixel");
2986 OGR_G_DestroyGeometry(src_geom);
2997 if (NULL != skew_x &&
FLT_NEQ(*skew_x, 0.0))
3002 (NULL != scale_y) &&
3013 rterror(
"rt_raster_gdal_rasterize: Could not compute spatial coordinates for raster pixel");
3016 OGR_G_DestroyGeometry(src_geom);
3027 if (NULL != skew_y &&
FLT_NEQ(*skew_y, 0.0))
3035 RASTER_DEBUGF(3,
"Applied geotransform: %f, %f, %f, %f, %f, %f",
3036 _gt[0], _gt[1], _gt[2], _gt[3], _gt[4], _gt[5]);
3037 RASTER_DEBUGF(3,
"Raster dimensions (width x height): %d x %d",
3046 _drv = GDALGetDriverByName(
"MEM");
3048 rterror(
"rt_raster_gdal_rasterize: Could not load the MEM GDAL driver");
3050 OGR_G_DestroyGeometry(src_geom);
3060 GDALDeregisterDriver(_drv);
3063 _ds = GDALCreate(_drv,
"", _dim[0], _dim[1], 0, GDT_Byte, NULL);
3065 rterror(
"rt_raster_gdal_rasterize: Could not create a GDALDataset to rasterize the geometry into");
3067 OGR_G_DestroyGeometry(src_geom);
3070 if (unload_drv) GDALDestroyDriver(_drv);
3076 cplerr = GDALSetGeoTransform(_ds, _gt);
3077 if (cplerr != CE_None) {
3078 rterror(
"rt_raster_gdal_rasterize: Could not set geotransform on GDALDataset");
3080 OGR_G_DestroyGeometry(src_geom);
3085 if (unload_drv) GDALDestroyDriver(_drv);
3091 if (NULL != arg->
src_sr) {
3093 OSRExportToWkt(arg->
src_sr, &_srs);
3095 cplerr = GDALSetProjection(_ds, _srs);
3097 if (cplerr != CE_None) {
3098 rterror(
"rt_raster_gdal_rasterize: Could not set projection on GDALDataset");
3100 OGR_G_DestroyGeometry(src_geom);
3105 if (unload_drv) GDALDestroyDriver(_drv);
3112 for (i = 0; i < arg->
numbands; i++) {
3118 if (cplerr != CE_None) {
3119 rterror(
"rt_raster_gdal_rasterize: Could not add band to GDALDataset");
3124 _band = GDALGetRasterBand(_ds, i + 1);
3125 if (NULL == _band) {
3126 rterror(
"rt_raster_gdal_rasterize: Could not get band %d from GDALDataset", i + 1);
3134 cplerr = GDALSetRasterNoDataValue(_band, arg->
nodata[i]);
3135 if (cplerr != CE_None) {
3136 rterror(
"rt_raster_gdal_rasterize: Could not set nodata value");
3140 RASTER_DEBUGF(4,
"NODATA value set to %f", GDALGetRasterNoDataValue(_band, NULL));
3144 cplerr = GDALFillRaster(_band, arg->
init[i], 0);
3145 if (cplerr != CE_None) {
3146 rterror(
"rt_raster_gdal_rasterize: Could not set initial value");
3155 OGR_G_DestroyGeometry(src_geom);
3161 if (unload_drv) GDALDestroyDriver(_drv);
3171 cplerr = GDALRasterizeGeometries(
3180 if (cplerr != CE_None) {
3181 rterror(
"rt_raster_gdal_rasterize: Could not rasterize geometry");
3183 OGR_G_DestroyGeometry(src_geom);
3188 if (unload_drv) GDALDestroyDriver(_drv);
3194 GDALFlushCache(_ds);
3198 OGR_G_DestroyGeometry(src_geom);
3202 if (unload_drv) GDALDestroyDriver(_drv);
3205 rterror(
"rt_raster_gdal_rasterize: Could not rasterize geometry");
3214 for (i = 0; i < arg->
numbands; i++) {
3215 uint8_t *
data = NULL;
3222 double nodataval = 0;
3227 if (oldband == NULL) {
3228 rterror(
"rt_raster_gdal_rasterize: Could not get band %d of output raster", i);
3246 rterror(
"rt_raster_gdal_rasterize: Could not allocate memory for band data");
3257 hasnodata, nodataval,
3261 rterror(
"rt_raster_gdal_rasterize: Could not create band");
3272 for (
x = 0;
x < _width;
x++) {
3273 for (
y = 0;
y < _height;
y++) {
3276 rterror(
"rt_raster_gdal_rasterize: Could not get pixel value");
3288 rterror(
"rt_raster_gdal_rasterize: Could not set pixel value");
3299 if (oldband == NULL) {
3300 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