PostGIS 3.7.0dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches

◆ rt_raster_get_perimeter()

rt_errorstate rt_raster_get_perimeter ( rt_raster  raster,
int  nband,
LWGEOM **  perimeter 
)

Get raster perimeter.

The perimeter is a 4 vertices (5 to be closed) single ring polygon bearing the raster's rotation and using projection coordinates.

Parameters
raster: the raster to get info from
nband: the band for the perimeter. 0-based
**perimeter: pointer to perimeter
Returns
ES_NONE if success, ES_ERROR if error

The perimeter is a 4 vertices (5 to be closed) single ring polygon bearing the raster's rotation and using projection coordinates.

Parameters
raster: the raster to get info from
nband: the band for the perimeter. 0-based value less than zero means all bands
**perimeter: pointer to perimeter
Returns
ES_NONE if success, ES_ERROR if error

Definition at line 183 of file rt_geometry.c.

186 {
187 rt_band band = NULL;
188 int numband = 0;
189 uint16_t *_nband = NULL;
190 int i = 0;
191 int j = 0;
192 uint16_t _trim[4] = {0};
193 uint16_t trim[4] = {0}; /* top, right, bottom, left */
194 int isset[4] = {0};
195 double gt[6] = {0.0};
196 int32_t srid = SRID_UNKNOWN;
197
198 POINTARRAY *pts = NULL;
199 POINT4D p4d;
200 POINTARRAY **rings = NULL;
201 LWPOLY* poly = NULL;
202
203 assert(perimeter != NULL);
204
205 *perimeter = NULL;
206
207 /* empty raster, no perimeter */
208 if (rt_raster_is_empty(raster))
209 return ES_NONE;
210
211 /* raster metadata */
212 srid = rt_raster_get_srid(raster);
214 numband = rt_raster_get_num_bands(raster);
215
216 RASTER_DEBUGF(3, "rt_raster_get_perimeter: raster is %dx%d", raster->width, raster->height);
217
218 /* nband < 0 means all bands */
219 if (nband >= 0) {
220 if (nband >= numband) {
221 rterror("rt_raster_get_boundary: Band %d not found for raster", nband);
222 return ES_ERROR;
223 }
224
225 numband = 1;
226 }
227 else
228 nband = -1;
229
230 RASTER_DEBUGF(3, "rt_raster_get_perimeter: nband, numband = %d, %d", nband, numband);
231
232 _nband = rtalloc(sizeof(uint16_t) * numband);
233 if (_nband == NULL) {
234 rterror("rt_raster_get_boundary: Could not allocate memory for band indices");
235 return ES_ERROR;
236 }
237
238 if (nband < 0) {
239 for (i = 0; i < numband; i++)
240 _nband[i] = i;
241 }
242 else
243 _nband[0] = nband;
244
245 for (i = 0; i < numband; i++) {
246 band = rt_raster_get_band(raster, _nband[i]);
247 if (band == NULL) {
248 rterror("rt_raster_get_boundary: Could not get band at index %d", _nband[i]);
249 rtdealloc(_nband);
250 return ES_ERROR;
251 }
252
253 /* band is nodata */
254 if (rt_band_get_isnodata_flag(band) != 0)
255 continue;
256
258 rterror("rt_raster_get_boundary: Could not get band perimeter");
259 rtdealloc(_nband);
260 return ES_ERROR;
261 }
262
263 for (j = 0; j < 4; j++) {
264 if (!isset[j] || trim[j] < _trim[j]) {
265 _trim[j] = trim[j];
266 isset[j] = 1;
267 }
268 }
269 }
270
271 /* no longer needed */
272 rtdealloc(_nband);
273
274 /* check isset, just need to check one element */
275 if (!isset[0]) {
276 /* return NULL as bands are empty */
277 return ES_NONE;
278 }
279
280 RASTER_DEBUGF(4, "trim = (%d, %d, %d, %d)",
281 trim[0], trim[1], trim[2], trim[3]);
282
283 /* only one ring */
284 rings = (POINTARRAY **) rtalloc(sizeof (POINTARRAY*));
285 if (!rings) {
286 rterror("rt_raster_get_perimeter: Could not allocate memory for polygon ring");
287 return ES_ERROR;
288 }
289 rings[0] = ptarray_construct(0, 0, 5);
290 if (!rings[0]) {
291 rterror("rt_raster_get_perimeter: Could not construct point array");
292 return ES_ERROR;
293 }
294 pts = rings[0];
295
296 /* Upper-left corner (first and last points) */
298 raster,
299 _trim[3], _trim[0],
300 &p4d.x, &p4d.y,
301 gt
302 );
303 ptarray_set_point4d(pts, 0, &p4d);
304 ptarray_set_point4d(pts, 4, &p4d);
305
306 /* Upper-right corner (we go clockwise) */
308 raster,
309 raster->width - _trim[1], _trim[0],
310 &p4d.x, &p4d.y,
311 gt
312 );
313 ptarray_set_point4d(pts, 1, &p4d);
314
315 /* Lower-right corner */
317 raster,
318 raster->width - _trim[1], raster->height - _trim[2],
319 &p4d.x, &p4d.y,
320 gt
321 );
322 ptarray_set_point4d(pts, 2, &p4d);
323
324 /* Lower-left corner */
326 raster,
327 _trim[3], raster->height - _trim[2],
328 &p4d.x, &p4d.y,
329 gt
330 );
331 ptarray_set_point4d(pts, 3, &p4d);
332
333 poly = lwpoly_construct(srid, 0, 1, rings);
334 *perimeter = lwpoly_as_lwgeom(poly);
335
336 return ES_NONE;
337}
LWPOLY * lwpoly_construct(int32_t srid, GBOX *bbox, uint32_t nrings, POINTARRAY **points)
Definition lwpoly.c:43
#define SRID_UNKNOWN
Unknown SRID value.
Definition liblwgeom.h:215
void ptarray_set_point4d(POINTARRAY *pa, uint32_t n, const POINT4D *p4d)
Definition lwgeom_api.c:369
LWGEOM * lwpoly_as_lwgeom(const LWPOLY *obj)
Definition lwgeom.c:357
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...
Definition ptarray.c:51
void rterror(const char *fmt,...) __attribute__((format(printf
Wrappers used for reporting errors and info.
void * rtalloc(size_t size)
Wrappers used for managing memory.
Definition rt_context.c:191
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.
Definition rt_raster.c:637
int32_t rt_raster_get_srid(rt_raster raster)
Get raster's SRID.
Definition rt_raster.c:360
#define RASTER_DEBUGF(level, msg,...)
Definition librtcore.h:308
int rt_band_get_isnodata_flag(rt_band band)
Get isnodata flag value.
Definition rt_band.c:873
@ ES_NONE
Definition librtcore.h:182
@ ES_ERROR
Definition librtcore.h:183
uint16_t rt_raster_get_num_bands(rt_raster raster)
Definition rt_raster.c:376
void rtdealloc(void *mem)
Definition rt_context.c:206
void rt_raster_get_geotransform_matrix(rt_raster raster, double *gt)
Get 6-element array of raster geotransform matrix.
Definition rt_raster.c:588
int rt_raster_is_empty(rt_raster raster)
Return TRUE if the raster is empty.
Definition rt_raster.c:1240
rt_band rt_raster_get_band(rt_raster raster, int bandNum)
Return Nth band, or NULL if unavailable.
Definition rt_raster.c:385
nband
Definition pixval.py:56
raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
Definition rtrowdump.py:125
static char * trim(const char *input)
static rt_errorstate _rti_raster_get_band_perimeter(rt_band band, uint16_t *trim)
Definition rt_geometry.c:40
double x
Definition liblwgeom.h:414
double y
Definition liblwgeom.h:414

References _rti_raster_get_band_perimeter(), ES_ERROR, ES_NONE, lwpoly_as_lwgeom(), lwpoly_construct(), ptarray_construct(), ptarray_set_point4d(), RASTER_DEBUGF, rt_band_get_isnodata_flag(), rt_raster_cell_to_geopoint(), rt_raster_get_band(), rt_raster_get_geotransform_matrix(), rt_raster_get_num_bands(), rt_raster_get_srid(), rt_raster_is_empty(), rtalloc(), rtdealloc(), rterror(), SRID_UNKNOWN, trim(), POINT4D::x, and POINT4D::y.

Referenced by RASTER_convex_hull(), and test_raster_perimeter().

Here is the call graph for this function:
Here is the caller graph for this function: