PostGIS  2.1.10dev-r@@SVN_REVISION@@
rt_errorstate rt_raster_get_convex_hull ( rt_raster  raster,
LWGEOM **  hull 
)

Get raster's convex hull.

The convex hull 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
**hull: pointer to convex hull
Returns
ES_NONE if success, ES_ERROR if error

The convex hull is typically 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
**hull: pointer to convex hull
Returns
ES_NONE if success, ES_ERROR if error

Definition at line 6556 of file rt_api.c.

References ES_ERROR, ES_NONE, window::gt, rt_raster_t::height, LW_TRUE, lwline_as_lwgeom(), lwline_construct(), lwpoint_as_lwgeom(), lwpoint_make2d(), lwpoly_as_lwgeom(), lwpoly_construct(), ptarray_append_point(), ptarray_construct(), ptarray_construct_empty(), ptarray_set_point4d(), RASTER_DEBUGF, rt_raster_cell_to_geopoint(), rt_raster_get_geotransform_matrix(), rt_raster_get_height(), rt_raster_get_srid(), rt_raster_get_width(), rtalloc(), rterror(), SRID_UNKNOWN, rt_raster_t::width, POINT4D::x, and POINT4D::y.

Referenced by RASTER_clip(), RASTER_convex_hull(), rt_raster_compute_skewed_raster(), rt_raster_intersects(), rt_raster_surface(), and test_raster_convex_hull().

6556  {
6557  double gt[6] = {0.0};
6558  int srid = SRID_UNKNOWN;
6559 
6560  POINTARRAY *pts = NULL;
6561  POINT4D p4d;
6562 
6563  assert(hull != NULL);
6564  *hull = NULL;
6565 
6566  /* raster is NULL, convex hull is NULL */
6567  if (raster == NULL)
6568  return ES_NONE;
6569 
6570  /* raster metadata */
6571  srid = rt_raster_get_srid(raster);
6573 
6574  RASTER_DEBUGF(3, "rt_raster_get_convex_hull: raster is %dx%d", raster->width, raster->height);
6575 
6576  /* return point or line since at least one of the two dimensions is 0 */
6577  if ((!raster->width) || (!raster->height)) {
6578  p4d.x = gt[0];
6579  p4d.y = gt[3];
6580 
6581  /* return point */
6582  if (!raster->width && !raster->height) {
6583  LWPOINT *point = lwpoint_make2d(srid, p4d.x, p4d.y);
6584  *hull = lwpoint_as_lwgeom(point);
6585  }
6586  /* return linestring */
6587  else {
6588  LWLINE *line = NULL;
6589  pts = ptarray_construct_empty(0, 0, 2);
6590 
6591  /* first point of line */
6592  ptarray_append_point(pts, &p4d, LW_TRUE);
6593 
6594  /* second point of line */
6596  raster,
6597  rt_raster_get_width(raster), rt_raster_get_height(raster),
6598  &p4d.x, &p4d.y,
6599  gt
6600  ) != ES_NONE) {
6601  rterror("rt_raster_get_convex_hull: Could not get second point for linestring");
6602  return ES_ERROR;
6603  }
6604  ptarray_append_point(pts, &p4d, LW_TRUE);
6605  line = lwline_construct(srid, NULL, pts);
6606 
6607  *hull = lwline_as_lwgeom(line);
6608  }
6609 
6610  return ES_NONE;
6611  }
6612  else {
6613  POINTARRAY **rings = NULL;
6614  LWPOLY* poly = NULL;
6615 
6616  /* only one ring */
6617  rings = (POINTARRAY **) rtalloc(sizeof (POINTARRAY*));
6618  if (!rings) {
6619  rterror("rt_raster_get_convex_hull: Could not allocate memory for polygon ring");
6620  return ES_ERROR;
6621  }
6622  rings[0] = ptarray_construct(0, 0, 5);
6623  /* TODO: handle error on ptarray construction */
6624  /* XXX jorgearevalo: the error conditions aren't managed in ptarray_construct */
6625  if (!rings[0]) {
6626  rterror("rt_raster_get_convex_hull: Could not construct point array");
6627  return ES_ERROR;
6628  }
6629  pts = rings[0];
6630 
6631  /* Upper-left corner (first and last points) */
6632  p4d.x = gt[0];
6633  p4d.y = gt[3];
6634  ptarray_set_point4d(pts, 0, &p4d);
6635  ptarray_set_point4d(pts, 4, &p4d);
6636 
6637  /* Upper-right corner (we go clockwise) */
6639  raster,
6640  raster->width, 0,
6641  &p4d.x, &p4d.y,
6642  gt
6643  );
6644  ptarray_set_point4d(pts, 1, &p4d);
6645 
6646  /* Lower-right corner */
6648  raster,
6649  raster->width, raster->height,
6650  &p4d.x, &p4d.y,
6651  gt
6652  );
6653  ptarray_set_point4d(pts, 2, &p4d);
6654 
6655  /* Lower-left corner */
6657  raster,
6658  0, raster->height,
6659  &p4d.x, &p4d.y,
6660  gt
6661  );
6662  ptarray_set_point4d(pts, 3, &p4d);
6663 
6664  poly = lwpoly_construct(srid, 0, 1, rings);
6665  *hull = lwpoly_as_lwgeom(poly);
6666  }
6667 
6668  return ES_NONE;
6669 }
void ptarray_set_point4d(POINTARRAY *pa, int n, const POINT4D *p4d)
Definition: lwgeom_api.c:501
double x
Definition: liblwgeom.h:308
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:49
LWPOINT * lwpoint_make2d(int srid, double x, double y)
Definition: lwpoint.c:130
POINTARRAY * ptarray_construct_empty(char hasz, char hasm, uint32_t maxpoints)
Create a new POINTARRAY with no points.
Definition: ptarray.c:57
tuple gt
Definition: window.py:79
int32_t rt_raster_get_srid(rt_raster raster)
Get raster's SRID.
Definition: rt_api.c:5661
LWGEOM * lwpoly_as_lwgeom(const LWPOLY *obj)
Definition: lwgeom.c:239
uint16_t height
Definition: rt_api.h:2227
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_api.c:6054
void rt_raster_get_geotransform_matrix(rt_raster raster, double *gt)
Get 6-element array of raster geotransform matrix.
Definition: rt_api.c:6005
LWGEOM * lwline_as_lwgeom(const LWLINE *obj)
Definition: lwgeom.c:249
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_TRUE, then a duplicate point will not be added.
Definition: ptarray.c:141
#define RASTER_DEBUGF(level, msg,...)
Definition: rt_api.h:285
LWPOLY * lwpoly_construct(int srid, GBOX *bbox, uint32_t nrings, POINTARRAY **points)
Definition: lwpoly.c:29
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:51
LWLINE * lwline_construct(int srid, GBOX *bbox, POINTARRAY *points)
Definition: lwline.c:29
uint16_t width
Definition: rt_api.h:2226
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:154
uint16_t rt_raster_get_height(rt_raster raster)
Definition: rt_api.c:5434
void * rtalloc(size_t size)
Raster core memory management functions.
Definition: rt_api.c:867
void rterror(const char *fmt,...)
Raster core error and info handlers.
Definition: rt_api.c:895
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
Definition: lwgeom.c:254
double y
Definition: liblwgeom.h:308
uint16_t rt_raster_get_width(rt_raster raster)
Definition: rt_api.c:5426

Here is the call graph for this function:

Here is the caller graph for this function: