50 assert(
band->raster != NULL);
53 memset(
trim, 0,
sizeof(uint16_t) * 4);
59 for (
y = 0;
y < height;
y++) {
60 for (offset = 0; offset < 3; offset++) {
62 for (
x = offset;
x < width;
x += 3) {
64 rterror(
"_rti_raster_get_band_perimeter: Could not get band pixel");
68 RASTER_DEBUGF(4,
"top (x, y, value, nodata) = (%d, %d, %f, %d)",
x,
y,
value, nodata);
85 for (
x = width - 1;
x >= 0;
x--) {
86 for (offset = 0; offset < 3; offset++) {
88 for (
y = offset;
y < height;
y += 3) {
90 rterror(
"_rti_raster_get_band_perimeter: Could not get band pixel");
94 RASTER_DEBUGF(4,
"right (x, y, value, nodata) = (%d, %d, %f, %d)",
x,
y,
value, nodata);
96 trim[1] = width - (
x + 1);
111 for (
y = height - 1;
y >= 0;
y--) {
112 for (offset = 0; offset < 3; offset++) {
114 for (
x = offset;
x < width;
x += 3) {
116 rterror(
"_rti_raster_get_band_perimeter: Could not get band pixel");
120 RASTER_DEBUGF(4,
"bottom (x, y, value, nodata) = (%d, %d, %f, %d)",
x,
y,
value, nodata);
122 trim[2] = height - (
y + 1);
137 for (
x = 0;
x < width;
x++) {
138 for (offset = 0; offset < 3; offset++) {
140 for (
y = offset;
y < height;
y += 3) {
142 rterror(
"_rti_raster_get_band_perimeter: Could not get band pixel");
146 RASTER_DEBUGF(4,
"left (x, , value, nodata) = (%d, %d, %f, %d)",
x,
y,
value, nodata);
188 uint16_t *_nband = NULL;
191 uint16_t _trim[4] = {0};
192 uint16_t
trim[4] = {0};
194 double gt[6] = {0.0};
202 assert(perimeter != NULL);
219 if (
nband >= numband) {
220 rterror(
"rt_raster_get_boundary: Band %d not found for raster",
nband);
231 _nband =
rtalloc(
sizeof(uint16_t) * numband);
232 if (_nband == NULL) {
233 rterror(
"rt_raster_get_boundary: Could not allocate memory for band indices");
238 for (i = 0; i < numband; i++)
244 for (i = 0; i < numband; i++) {
247 rterror(
"rt_raster_get_boundary: Could not get band at index %d", _nband[i]);
257 rterror(
"rt_raster_get_boundary: Could not get band perimeter");
262 for (j = 0; j < 4; j++) {
263 if (!isset[j] ||
trim[j] < _trim[j]) {
285 rterror(
"rt_raster_get_perimeter: Could not allocate memory for polygon ring");
290 rterror(
"rt_raster_get_perimeter: Could not construct point array");
308 raster->width - _trim[1], _trim[0],
326 _trim[3],
raster->height - _trim[2],
362 GEOSGeometry *gc = NULL;
363 GEOSGeometry *gunion = NULL;
364 GEOSGeometry **geoms = NULL;
368 assert(surface != NULL);
386 rterror(
"rt_raster_surface: Could not get convex hull of raster");
399 rterror(
"rt_raster_surface: The band index %d is invalid",
nband);
406 rterror(
"rt_raster_surface: Error getting band %d from raster",
nband);
419 rterror(
"rt_raster_surface: Could not get convex hull of raster");
440 RASTER_DEBUG(3,
"All pixels of band are NODATA. Returning NULL");
445 else if (gvcount > 1) {
447 geomscount = gvcount;
448 geoms =
rtalloc(
sizeof(GEOSGeometry *) * geomscount);
450 rterror(
"rt_raster_surface: Could not allocate memory for pixel polygons as GEOSGeometry");
451 for (i = 0; i < gvcount; i++)
lwpoly_free(gv[i].geom);
455 for (i = 0; i < gvcount; i++) {
456 #if POSTGIS_DEBUG_LEVEL > 3
470 gc = GEOSGeom_createCollection(GEOS_GEOMETRYCOLLECTION, geoms, geomscount);
473 rterror(
"rt_raster_surface: Could not create GEOS GEOMETRYCOLLECTION from set of pixel polygons");
475 for (i = 0; i < geomscount; i++)
476 GEOSGeom_destroy(geoms[i]);
482 gunion = GEOSUnaryUnion(gc);
484 GEOSGeom_destroy(gc);
487 if (gunion == NULL) {
488 rterror(
"rt_raster_surface: Could not union the pixel polygons using GEOSUnaryUnion()");
500 LWGEOM *mpolyValid = NULL;
502 if (GEOSisValid(gunion))
507 if (mpolyValid == NULL) {
508 rtwarn(
"Cannot fix invalid geometry");
517 GEOSGeom_destroy(gunion);
523 #if POSTGIS_DEBUG_LEVEL > 3
540 #if POSTGIS_DEBUG_LEVEL > 3
564 #if POSTGIS_DEBUG_LEVEL > 3
573 #if POSTGIS_DEBUG_LEVEL > 3
609 double scale_x, scale_y;
610 double skew_x, skew_y;
617 assert(
rast != NULL);
630 p0.
x = scale_x *
x + skew_x *
y + ul_x;
631 p0.
y = scale_y *
y + skew_y *
x + ul_y;
634 p.
x = p0.
x + scale_x;
638 p.
x = p0.
x + scale_x + skew_x;
639 p.
y = p0.
y + scale_y + skew_y;
643 p.
y = p0.
y + scale_y;
670 double scale_x, scale_y;
671 double skew_x, skew_y;
674 double center_x, center_y;
685 center_x = scale_x *
x + skew_x *
y + ul_x + (scale_x + skew_x) * 0.5;
686 center_y = scale_y *
y + skew_y *
x + ul_y + (scale_y + skew_y) * 0.5;
706 double gt[6] = {0.0};
725 "rt_raster_get_envelope: raster is %dx%d",
755 rterror(
"rt_raster_get_envelope: Could not get second point for linestring");
775 rterror(
"rt_raster_get_envelope_geom: Could not allocate memory for polygon ring");
780 rterror(
"rt_raster_get_envelope_geom: Could not construct point array");
787 rterror(
"rt_raster_get_envelope_geom: Could not get raster envelope");
839 double gt[6] = {0.0};
845 assert(hull != NULL);
883 rterror(
"rt_raster_get_convex_hull: Could not get second point for linestring");
901 rterror(
"rt_raster_get_convex_hull: Could not allocate memory for polygon ring");
908 rterror(
"rt_raster_get_convex_hull: Could not construct point array");
977 int exclude_nodata_value,
980 CPLErr cplerr = CE_None;
983 OGRSFDriverH ogr_drv = NULL;
984 GDALDriverH gdal_drv = NULL;
985 int destroy_gdal_drv = 0;
986 GDALDatasetH memdataset = NULL;
987 GDALRasterBandH gdal_band = NULL;
988 OGRDataSourceH memdatasource = NULL;
990 OGRLayerH hLayer = NULL;
991 OGRFeatureH hFeature = NULL;
992 OGRGeometryH hGeom = NULL;
993 OGRFieldDefnH hFldDfn = NULL;
994 unsigned char *wkb = NULL;
997 int nFeatureCount = 0;
1000 double dValue = 0.0;
1001 int iBandHasNodataValue =
FALSE;
1002 double dBandNoData = 0.0;
1004 uint32_t bandNums[1] = {
nband};
1005 int excludeNodataValues[1] = {exclude_nodata_value};
1009 assert(NULL != pnElements);
1020 rterror(
"rt_raster_gdal_polygonize: Error getting band %d from raster",
nband);
1024 if (exclude_nodata_value) {
1034 if (iBandHasNodataValue)
1037 exclude_nodata_value =
FALSE;
1044 if (NULL == memdataset) {
1045 rterror(
"rt_raster_gdal_polygonize: Couldn't convert raster to GDAL MEM dataset");
1052 #ifdef GDAL_DCAP_RASTER
1064 ogr_drv = OGRGetDriverByName(
"Memory");
1065 memdatasource = OGR_Dr_CreateDataSource(ogr_drv,
"", NULL);
1066 if (NULL == memdatasource) {
1067 rterror(
"rt_raster_gdal_polygonize: Couldn't create a OGR Datasource to store pols");
1068 GDALClose(memdataset);
1069 if (destroy_gdal_drv) GDALDestroyDriver(gdal_drv);
1074 if (!OGR_DS_TestCapability(memdatasource, ODsCCreateLayer)) {
1075 rterror(
"rt_raster_gdal_polygonize: MEM driver can't create new layers, aborting");
1078 GDALClose(memdataset);
1079 if (destroy_gdal_drv) GDALDestroyDriver(gdal_drv);
1080 OGRReleaseDataSource(memdatasource);
1096 hLayer = OGR_DS_CreateLayer(memdatasource,
"PolygonizedLayer", NULL, wkbPolygon, NULL);
1098 if (NULL == hLayer) {
1099 rterror(
"rt_raster_gdal_polygonize: Couldn't create layer to store polygons");
1101 GDALClose(memdataset);
1102 if (destroy_gdal_drv) GDALDestroyDriver(gdal_drv);
1103 OGRReleaseDataSource(memdatasource);
1113 hFldDfn = OGR_Fld_Create(
"PixelValue", OFTReal);
1116 if (OGR_L_CreateField(hLayer, hFldDfn,
TRUE) != OGRERR_NONE) {
1117 rtwarn(
"Couldn't create a field in OGR Layer. The polygons generated won't be able to store the pixel value");
1126 gdal_band = GDALGetRasterBand(memdataset, 1);
1127 if (NULL == gdal_band) {
1128 rterror(
"rt_raster_gdal_polygonize: Couldn't get GDAL band to polygonize");
1130 GDALClose(memdataset);
1131 if (destroy_gdal_drv) GDALDestroyDriver(gdal_drv);
1132 OGR_Fld_Destroy(hFldDfn);
1133 OGR_DS_DeleteLayer(memdatasource, 0);
1134 OGRReleaseDataSource(memdatasource);
1140 cplerr = GDALFPolygonize(gdal_band, NULL, hLayer, iPixVal, NULL, NULL, NULL);
1142 if (cplerr != CE_None) {
1143 rterror(
"rt_raster_gdal_polygonize: Could not polygonize GDAL band");
1145 GDALClose(memdataset);
1146 if (destroy_gdal_drv) GDALDestroyDriver(gdal_drv);
1147 OGR_Fld_Destroy(hFldDfn);
1148 OGR_DS_DeleteLayer(memdatasource, 0);
1149 OGRReleaseDataSource(memdatasource);
1160 if (iBandHasNodataValue) {
1161 size_t sz = 50 *
sizeof (char);
1162 pszQuery = (
char *)
rtalloc(sz);
1163 snprintf(pszQuery, sz,
"PixelValue != %f", dBandNoData );
1164 OGRErr e = OGR_L_SetAttributeFilter(hLayer, pszQuery);
1165 if (e != OGRERR_NONE) {
1166 rtwarn(
"Error filtering NODATA values for band. All values will be treated as data values");
1179 nFeatureCount = OGR_L_GetFeatureCount(hLayer,
TRUE);
1185 rterror(
"rt_raster_gdal_polygonize: Could not allocate memory for geomval set");
1187 GDALClose(memdataset);
1188 if (destroy_gdal_drv) GDALDestroyDriver(gdal_drv);
1189 OGR_Fld_Destroy(hFldDfn);
1190 OGR_DS_DeleteLayer(memdatasource, 0);
1191 if (NULL != pszQuery)
1193 OGRReleaseDataSource(memdatasource);
1204 OGR_L_ResetReading(hLayer);
1206 for (j = 0; j < nFeatureCount; j++) {
1207 hFeature = OGR_L_GetNextFeature(hLayer);
1208 dValue = OGR_F_GetFieldAsDouble(hFeature, iPixVal);
1210 hGeom = OGR_F_GetGeometryRef(hFeature);
1211 wkbsize = OGR_G_WkbSize(hGeom);
1214 wkb =
rtalloc(
sizeof(
unsigned char) * wkbsize);
1216 rterror(
"rt_raster_gdal_polygonize: Could not allocate memory for WKB buffer");
1218 OGR_F_Destroy(hFeature);
1219 GDALClose(memdataset);
1220 if (destroy_gdal_drv) GDALDestroyDriver(gdal_drv);
1221 OGR_Fld_Destroy(hFldDfn);
1222 OGR_DS_DeleteLayer(memdatasource, 0);
1223 if (NULL != pszQuery)
1225 OGRReleaseDataSource(memdatasource);
1231 OGR_G_ExportToWkb(hGeom, wkbNDR, wkb);
1236 #if POSTGIS_DEBUG_LEVEL > 3
1239 OGR_G_ExportToWkt(hGeom, &wkt);
1243 d_print_binary_hex(
"GDAL wkb", wkb, wkbsize);
1252 OGR_F_Destroy(hFeature);
1260 #if POSTGIS_DEBUG_LEVEL > 3
1269 d_print_binary_hex(
"LWGEOM wkb", (
const uint8_t *)lwwkb->
data, lwwkbsize);
1276 pols[j].
val = dValue;
1279 *pnElements = nFeatureCount;
1282 GDALClose(memdataset);
1283 if (destroy_gdal_drv) GDALDestroyDriver(gdal_drv);
1286 OGR_Fld_Destroy(hFldDfn);
1287 OGR_DS_DeleteLayer(memdatasource, 0);
1288 if (NULL != pszQuery)
rtdealloc(pszQuery);
1289 OGRReleaseDataSource(memdatasource);
GEOSGeometry * LWGEOM2GEOS(const LWGEOM *lwgeom, uint8_t autofix)
LWGEOM * GEOS2LWGEOM(const GEOSGeometry *geom, uint8_t want3d)
void lwgeom_geos_error(const char *fmt,...)
LWGEOM * lwline_as_lwgeom(const LWLINE *obj)
LWPOINT * lwpoint_make2d(int32_t srid, double x, double y)
void lwgeom_set_srid(LWGEOM *geom, int32_t srid)
Set the SRID on an LWGEOM For collections, only the parent gets an SRID, all the children get SRID_UN...
void lwgeom_free(LWGEOM *geom)
#define LW_PARSER_CHECK_NONE
LWGEOM * lwgeom_as_multi(const LWGEOM *lwgeom)
Create a new LWGEOM of the appropriate MULTI* type.
#define LWSIZE_GET(varsize)
Macro for reading the size from the GSERIALIZED size attribute.
LWGEOM * lwpoly_as_lwgeom(const LWPOLY *obj)
LWGEOM * lwgeom_clone_deep(const LWGEOM *lwgeom)
Deep clone an LWGEOM, everything is copied.
LWMPOLY * lwgeom_as_lwmpoly(const LWGEOM *lwgeom)
POINTARRAY * ptarray_construct(char hasz, char hasm, uint32_t npoints)
Construct an empty pointarray, allocating storage and setting the npoints, but not filling in any inf...
LWLINE * lwline_construct(int32_t srid, GBOX *bbox, POINTARRAY *points)
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
int lwgeom_is_collection(const LWGEOM *lwgeom)
Determine whether a LWGEOM can contain sub-geometries or not.
POINTARRAY * ptarray_construct_empty(char hasz, char hasm, uint32_t maxpoints)
Create a new POINTARRAY with no points.
char * lwgeom_to_wkt(const LWGEOM *geom, uint8_t variant, int precision, size_t *size_out)
WKT emitter function.
int ptarray_append_point(POINTARRAY *pa, const POINT4D *pt, int allow_duplicates)
Append a point to the end of an existing POINTARRAY If allow_duplicate is LW_FALSE,...
void lwpoly_free(LWPOLY *poly)
#define LW_TRUE
Return types for functions with status returns.
LWPOLY * lwgeom_as_lwpoly(const LWGEOM *lwgeom)
#define SRID_UNKNOWN
Unknown SRID value.
LWGEOM * lwgeom_from_wkb(const uint8_t *wkb, const size_t wkb_size, const char check)
WKB inputs must have a declared size, to prevent malformed WKB from reading off the end of the memory...
lwvarlena_t * lwgeom_to_wkb_varlena(const LWGEOM *geom, uint8_t variant)
LWPOLY * lwpoly_construct(int32_t srid, GBOX *bbox, uint32_t nrings, POINTARRAY **points)
void ptarray_set_point4d(POINTARRAY *pa, uint32_t n, const POINT4D *p4d)
LWGEOM * lwgeom_make_valid(LWGEOM *geom)
Attempts to make an invalid geometries valid w/out losing points.
void rterror(const char *fmt,...)
Wrappers used for reporting errors and info.
void * rtalloc(size_t size)
Wrappers used for managing memory.
uint16_t rt_band_get_width(rt_band band)
Return width of this band.
#define RASTER_DEBUG(level, msg)
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.
int32_t rt_raster_get_srid(rt_raster raster)
Get raster's SRID.
int rt_util_gdal_register_all(int force_register_all)
#define RASTER_DEBUGF(level, msg,...)
double rt_raster_get_x_skew(rt_raster raster)
Get skew about the X axis.
double rt_raster_get_x_offset(rt_raster raster)
Get raster x offset, in projection units.
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.
int rt_band_get_isnodata_flag(rt_band band)
Get isnodata flag value.
double rt_raster_get_x_scale(rt_raster raster)
Get scale X in projection units.
void rtwarn(const char *fmt,...)
rt_errorstate
Enum definitions.
uint16_t rt_raster_get_num_bands(rt_raster raster)
uint16_t rt_raster_get_height(rt_raster raster)
struct rt_geomval_t * rt_geomval
rt_errorstate rt_band_get_nodata(rt_band band, double *nodata)
Get NODATA value.
uint16_t rt_raster_get_width(rt_raster raster)
void rtdealloc(void *mem)
GDALDatasetH rt_raster_to_gdal_mem(rt_raster raster, const char *srs, uint32_t *bandNums, int *excludeNodataValues, int count, GDALDriverH *rtn_drv, int *destroy_rtn_drv)
Return GDAL dataset using GDAL MEM driver from raster.
double rt_raster_get_y_scale(rt_raster raster)
Get scale Y in projection units.
rt_errorstate rt_raster_get_envelope(rt_raster raster, rt_envelope *env)
Get raster's envelope.
double rt_raster_get_y_skew(rt_raster raster)
Get skew about the Y axis.
void rt_raster_get_geotransform_matrix(rt_raster raster, double *gt)
Get 6-element array of raster geotransform matrix.
uint16_t rt_band_get_height(rt_band band)
Return height of this band.
double rt_raster_get_y_offset(rt_raster raster)
Get raster y offset, in projection units.
int rt_raster_is_empty(rt_raster raster)
Return TRUE if the raster is empty.
rt_band rt_raster_get_band(rt_raster raster, int bandNum)
Return Nth band, or NULL if unavailable.
This library is the generic raster handling section of PostGIS.
raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
static char * trim(const char *input)
LWPOINT * rt_raster_pixel_as_centroid_point(rt_raster rast, int x, int y)
Get a raster pixel centroid point.
rt_errorstate rt_raster_surface(rt_raster raster, int nband, LWMPOLY **surface)
Get a raster as a surface (multipolygon).
rt_geomval rt_raster_gdal_polygonize(rt_raster raster, int nband, int exclude_nodata_value, int *pnElements)
Returns a set of "geomval" value, one for each group of pixel sharing the same value for the provided...
LWPOLY * rt_raster_pixel_as_polygon(rt_raster rast, int x, int y)
Get a raster pixel as a polygon.
rt_errorstate rt_raster_get_convex_hull(rt_raster raster, LWGEOM **hull)
Get raster's convex hull.
static rt_errorstate _rti_raster_get_band_perimeter(rt_band band, uint16_t *trim)
rt_errorstate rt_raster_get_envelope_geom(rt_raster raster, LWGEOM **env)
Get raster's envelope as a geometry.
rt_errorstate rt_raster_get_perimeter(rt_raster raster, int nband, LWGEOM **perimeter)
Get raster perimeter.