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);
163 trim[0], trim[1], trim[2], trim[3]);
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);
229 RASTER_DEBUGF(3,
"rt_raster_get_perimeter: nband, numband = %d, %d", nband, numband);
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]) {
280 trim[0], trim[1], trim[2], trim[3]);
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],
317 raster->
width - _trim[1], raster->
height - _trim[2],
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");
443 RASTER_DEBUG(3,
"All pixels of band are NODATA. Returning NULL");
448 else if (gvcount > 1) {
450 geomscount = gvcount;
451 geoms =
rtalloc(
sizeof(GEOSGeometry *) * geomscount);
453 rterror(
"rt_raster_surface: Could not allocate memory for pixel polygons as GEOSGeometry");
454 for (i = 0; i < gvcount; i++)
lwpoly_free(gv[i].geom);
458 for (i = 0; i < gvcount; i++) {
459 #if POSTGIS_DEBUG_LEVEL > 3 473 gc = GEOSGeom_createCollection(GEOS_GEOMETRYCOLLECTION, geoms, geomscount);
476 rterror(
"rt_raster_surface: Could not create GEOS GEOMETRYCOLLECTION from set of pixel polygons");
478 for (i = 0; i < geomscount; i++)
479 GEOSGeom_destroy(geoms[i]);
485 gunion = GEOSUnaryUnion(gc);
487 GEOSGeom_destroy(gc);
490 if (gunion == NULL) {
491 rterror(
"rt_raster_surface: Could not union the pixel polygons using GEOSUnaryUnion()");
503 LWGEOM *mpolyValid = NULL;
505 if (GEOSisValid(gunion))
510 if (mpolyValid == NULL) {
511 rtwarn(
"Cannot fix invalid geometry");
520 GEOSGeom_destroy(gunion);
526 #if POSTGIS_DEBUG_LEVEL > 3 543 #if POSTGIS_DEBUG_LEVEL > 3 567 #if POSTGIS_DEBUG_LEVEL > 3 576 #if POSTGIS_DEBUG_LEVEL > 3 612 double scale_x, scale_y;
613 double skew_x, skew_y;
620 assert(rast != NULL);
633 p0.
x = scale_x * x + skew_x * y + ul_x;
634 p0.
y = scale_y * y + skew_y * x + ul_y;
637 p.
x = p0.
x + scale_x;
641 p.
x = p0.
x + scale_x + skew_x;
642 p.
y = p0.
y + scale_y + skew_y;
646 p.
y = p0.
y + scale_y;
671 double gt[6] = {0.0};
690 "rt_raster_get_envelope: raster is %dx%d",
720 rterror(
"rt_raster_get_envelope: Could not get second point for linestring");
740 rterror(
"rt_raster_get_envelope_geom: Could not allocate memory for polygon ring");
745 rterror(
"rt_raster_get_envelope_geom: Could not construct point array");
752 rterror(
"rt_raster_get_envelope_geom: Could not get raster envelope");
804 double gt[6] = {0.0};
810 assert(hull != NULL);
848 rterror(
"rt_raster_get_convex_hull: Could not get second point for linestring");
866 rterror(
"rt_raster_get_convex_hull: Could not allocate memory for polygon ring");
873 rterror(
"rt_raster_get_convex_hull: Could not construct point array");
942 int exclude_nodata_value,
945 CPLErr cplerr = CE_None;
948 OGRSFDriverH ogr_drv = NULL;
949 GDALDriverH gdal_drv = NULL;
950 int destroy_gdal_drv = 0;
951 GDALDatasetH memdataset = NULL;
952 GDALRasterBandH gdal_band = NULL;
953 OGRDataSourceH memdatasource = NULL;
955 OGRLayerH hLayer = NULL;
956 OGRFeatureH hFeature = NULL;
957 OGRGeometryH hGeom = NULL;
958 OGRFieldDefnH hFldDfn = NULL;
959 unsigned char *wkb = NULL;
962 int nFeatureCount = 0;
966 int iBandHasNodataValue =
FALSE;
967 double dBandNoData = 0.0;
970 GEOSGeometry *ggeom = NULL;
972 LWGEOM *lwgeomValid = NULL;
975 int excludeNodataValues[1] = {exclude_nodata_value};
978 assert(NULL != raster);
979 assert(NULL != pnElements);
990 rterror(
"rt_raster_gdal_polygonize: Error getting band %d from raster", nband);
994 if (exclude_nodata_value) {
1004 if (iBandHasNodataValue)
1007 exclude_nodata_value =
FALSE;
1013 memdataset =
rt_raster_to_gdal_mem(raster, NULL, bandNums, excludeNodataValues, 1, &gdal_drv, &destroy_gdal_drv);
1014 if (NULL == memdataset) {
1015 rterror(
"rt_raster_gdal_polygonize: Couldn't convert raster to GDAL MEM dataset");
1022 #ifdef GDAL_DCAP_RASTER 1034 ogr_drv = OGRGetDriverByName(
"Memory");
1035 memdatasource = OGR_Dr_CreateDataSource(ogr_drv,
"", NULL);
1036 if (NULL == memdatasource) {
1037 rterror(
"rt_raster_gdal_polygonize: Couldn't create a OGR Datasource to store pols");
1038 GDALClose(memdataset);
1039 if (destroy_gdal_drv) GDALDestroyDriver(gdal_drv);
1044 if (!OGR_DS_TestCapability(memdatasource, ODsCCreateLayer)) {
1045 rterror(
"rt_raster_gdal_polygonize: MEM driver can't create new layers, aborting");
1048 GDALClose(memdataset);
1049 if (destroy_gdal_drv) GDALDestroyDriver(gdal_drv);
1050 OGRReleaseDataSource(memdatasource);
1066 hLayer = OGR_DS_CreateLayer(memdatasource,
"PolygonizedLayer", NULL, wkbPolygon, NULL);
1068 if (NULL == hLayer) {
1069 rterror(
"rt_raster_gdal_polygonize: Couldn't create layer to store polygons");
1071 GDALClose(memdataset);
1072 if (destroy_gdal_drv) GDALDestroyDriver(gdal_drv);
1073 OGRReleaseDataSource(memdatasource);
1083 hFldDfn = OGR_Fld_Create(
"PixelValue", OFTReal);
1086 if (OGR_L_CreateField(hLayer, hFldDfn,
TRUE) != OGRERR_NONE) {
1087 rtwarn(
"Couldn't create a field in OGR Layer. The polygons generated won't be able to store the pixel value");
1096 gdal_band = GDALGetRasterBand(memdataset, 1);
1097 if (NULL == gdal_band) {
1098 rterror(
"rt_raster_gdal_polygonize: Couldn't get GDAL band to polygonize");
1100 GDALClose(memdataset);
1101 if (destroy_gdal_drv) GDALDestroyDriver(gdal_drv);
1102 OGR_Fld_Destroy(hFldDfn);
1103 OGR_DS_DeleteLayer(memdatasource, 0);
1104 OGRReleaseDataSource(memdatasource);
1110 cplerr = GDALFPolygonize(gdal_band, NULL, hLayer, iPixVal, NULL, NULL, NULL);
1112 if (cplerr != CE_None) {
1113 rterror(
"rt_raster_gdal_polygonize: Could not polygonize GDAL band");
1115 GDALClose(memdataset);
1116 if (destroy_gdal_drv) GDALDestroyDriver(gdal_drv);
1117 OGR_Fld_Destroy(hFldDfn);
1118 OGR_DS_DeleteLayer(memdatasource, 0);
1119 OGRReleaseDataSource(memdatasource);
1130 if (iBandHasNodataValue) {
1131 pszQuery = (
char *)
rtalloc(50 *
sizeof (
char));
1132 sprintf(pszQuery,
"PixelValue != %f", dBandNoData );
1133 OGRErr e = OGR_L_SetAttributeFilter(hLayer, pszQuery);
1134 if (e != OGRERR_NONE) {
1135 rtwarn(
"Error filtering NODATA values for band. All values will be treated as data values");
1148 nFeatureCount = OGR_L_GetFeatureCount(hLayer,
TRUE);
1154 rterror(
"rt_raster_gdal_polygonize: Could not allocate memory for geomval set");
1156 GDALClose(memdataset);
1157 if (destroy_gdal_drv) GDALDestroyDriver(gdal_drv);
1158 OGR_Fld_Destroy(hFldDfn);
1159 OGR_DS_DeleteLayer(memdatasource, 0);
1160 if (NULL != pszQuery)
1162 OGRReleaseDataSource(memdatasource);
1173 OGR_L_ResetReading(hLayer);
1175 for (j = 0; j < nFeatureCount; j++) {
1176 hFeature = OGR_L_GetNextFeature(hLayer);
1177 dValue = OGR_F_GetFieldAsDouble(hFeature, iPixVal);
1179 hGeom = OGR_F_GetGeometryRef(hFeature);
1180 wkbsize = OGR_G_WkbSize(hGeom);
1183 wkb =
rtalloc(
sizeof(
unsigned char) * wkbsize);
1185 rterror(
"rt_raster_gdal_polygonize: Could not allocate memory for WKB buffer");
1187 OGR_F_Destroy(hFeature);
1188 GDALClose(memdataset);
1189 if (destroy_gdal_drv) GDALDestroyDriver(gdal_drv);
1190 OGR_Fld_Destroy(hFldDfn);
1191 OGR_DS_DeleteLayer(memdatasource, 0);
1192 if (NULL != pszQuery)
1194 OGRReleaseDataSource(memdatasource);
1200 OGR_G_ExportToWkb(hGeom, wkbNDR, wkb);
1205 #if POSTGIS_DEBUG_LEVEL > 3 1208 OGR_G_ExportToWkt(hGeom, &wkt);
1212 d_print_binary_hex(
"GDAL wkb", wkb, wkbsize);
1221 OGR_F_Destroy(hFeature);
1232 if (ggeom == NULL) {
1233 rtwarn(
"Cannot test geometry for validity");
1237 isValid = GEOSisValid(ggeom);
1239 GEOSGeom_destroy(ggeom);
1250 if (lwgeomValid == NULL) {
1251 rtwarn(
"Cannot fix invalid geometry");
1256 lwgeom = lwgeomValid;
1263 #if POSTGIS_DEBUG_LEVEL > 3 1269 size_t lwwkbsize = 0;
1272 d_print_binary_hex(
"LWGEOM wkb", lwwkb, lwwkbsize);
1279 pols[j].
val = dValue;
1282 *pnElements = nFeatureCount;
1285 GDALClose(memdataset);
1286 if (destroy_gdal_drv) GDALDestroyDriver(gdal_drv);
1289 OGR_Fld_Destroy(hFldDfn);
1290 OGR_DS_DeleteLayer(memdatasource, 0);
1291 if (NULL != pszQuery)
rtdealloc(pszQuery);
1292 OGRReleaseDataSource(memdatasource);
void ptarray_set_point4d(POINTARRAY *pa, int n, const POINT4D *p4d)
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...
int rt_util_gdal_register_all(int force_register_all)
double rt_raster_get_x_offset(rt_raster raster)
Get raster x offset, in projection units.
int rt_raster_get_num_bands(rt_raster raster)
double rt_raster_get_y_skew(rt_raster raster)
Get skew about the Y axis.
int lwgeom_is_collection(const LWGEOM *lwgeom)
Determine whether a LWGEOM can contain sub-geometries or not.
char * lwgeom_to_wkt(const LWGEOM *geom, uint8_t variant, int precision, size_t *size_out)
WKT emitter function.
raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
void rt_raster_get_geotransform_matrix(rt_raster raster, double *gt)
Get 6-element array of raster geotransform matrix.
LWPOINT * lwpoint_make2d(int srid, double x, double y)
static rt_errorstate _rti_raster_get_band_perimeter(rt_band band, uint16_t *trim)
POINTARRAY * ptarray_construct_empty(char hasz, char hasm, uint32_t maxpoints)
Create a new POINTARRAY with no points.
void lwgeom_free(LWGEOM *geom)
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.
void rterror(const char *fmt,...)
Wrappers used for reporting errors and info.
void * rtalloc(size_t size)
Wrappers used for managing memory.
rt_errorstate
Enum definitions.
LWGEOM * lwgeom_clone_deep(const LWGEOM *lwgeom)
Deep clone an LWGEOM, everything is copied.
LWPOLY * lwgeom_as_lwpoly(const LWGEOM *lwgeom)
LWGEOM * lwgeom_as_multi(const LWGEOM *lwgeom)
Create a new LWGEOM of the appropriate MULTI* type.
LWGEOM * lwpoly_as_lwgeom(const LWPOLY *obj)
int rt_raster_is_empty(rt_raster raster)
Return TRUE if the raster is empty.
LWMPOLY * lwgeom_as_lwmpoly(const LWGEOM *lwgeom)
rt_errorstate rt_band_get_nodata(rt_band band, double *nodata)
Get NODATA value.
#define LW_PARSER_CHECK_NONE
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.
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.
uint8_t * lwgeom_to_wkb(const LWGEOM *geom, uint8_t variant, size_t *size_out)
Convert LWGEOM to a char* in WKB format.
LWGEOM * lwline_as_lwgeom(const LWLINE *obj)
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, then a duplicate point will not be added.
LWGEOM * lwgeom_make_valid(LWGEOM *geom)
Attempts to make an invalid geometries valid w/out losing points.
void lwgeom_geos_error(const char *fmt,...)
void rtwarn(const char *fmt,...)
LWPOLY * lwpoly_construct(int srid, GBOX *bbox, uint32_t nrings, POINTARRAY **points)
void lwpoly_free(LWPOLY *poly)
rt_errorstate rt_band_get_pixel(rt_band band, int x, int y, double *value, int *nodata)
Get pixel value.
#define LW_TRUE
Return types for functions with status returns.
LWLINE * lwline_construct(int srid, GBOX *bbox, POINTARRAY *points)
#define SRID_UNKNOWN
Unknown SRID value.
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...
rt_band rt_raster_get_band(rt_raster raster, int bandNum)
Return Nth band, or NULL if unavailable.
void rtinfo(const char *fmt,...)
int rt_band_get_hasnodata_flag(rt_band band)
Get hasnodata flag value.
double rt_raster_get_x_scale(rt_raster raster)
Get scale X in projection units.
uint16_t rt_band_get_width(rt_band band)
Return width of this band.
uint16_t rt_band_get_height(rt_band band)
Return height of this band.
#define RASTER_DEBUGF(level, msg,...)
int32_t rt_raster_get_srid(rt_raster raster)
Get raster's SRID.
GEOSGeometry * LWGEOM2GEOS(const LWGEOM *lwgeom, int autofix)
double rt_raster_get_y_scale(rt_raster raster)
Get scale Y in projection units.
double rt_raster_get_x_skew(rt_raster raster)
Get skew about the X axis.
rt_errorstate rt_raster_get_envelope_geom(rt_raster raster, LWGEOM **env)
Get raster's envelope as a geometry.
struct rt_geomval_t * rt_geomval
uint16_t rt_raster_get_width(rt_raster raster)
void rtdealloc(void *mem)
#define RASTER_DEBUG(level, msg)
This library is the generic raster handling section of PostGIS.
rt_errorstate rt_raster_get_perimeter(rt_raster raster, int nband, LWGEOM **perimeter)
Get raster perimeter.
LWGEOM * GEOS2LWGEOM(const GEOSGeometry *geom, char want3d)
static char * trim(const char *input)
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
int rt_band_get_isnodata_flag(rt_band band)
Get isnodata flag value.
void lwgeom_set_srid(LWGEOM *geom, int srid)
Set the SRID on an LWGEOM For collections, only the parent gets an SRID, all the children get SRID_UN...
uint16_t rt_raster_get_height(rt_raster raster)
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...
rt_errorstate rt_raster_surface(rt_raster raster, int nband, LWMPOLY **surface)
Get a raster as a surface (multipolygon).
rt_errorstate rt_raster_get_envelope(rt_raster raster, rt_envelope *env)
Get raster's envelope.
double rt_raster_get_y_offset(rt_raster raster)
Get raster y offset, in projection units.