PostGIS  3.4.0dev-r@@SVN_REVISION@@

◆ rt_raster_get_convex_hull()

rt_errorstate rt_raster_get_convex_hull ( rt_raster  raster,
LWGEOM **  hull 
)

Get raster's convex hull.

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 838 of file rt_geometry.c.

838  {
839  double gt[6] = {0.0};
840  int32_t srid = SRID_UNKNOWN;
841 
842  POINTARRAY *pts = NULL;
843  POINT4D p4d;
844 
845  assert(hull != NULL);
846  *hull = NULL;
847 
848  /* raster is NULL, convex hull is NULL */
849  if (raster == NULL)
850  return ES_NONE;
851 
852  /* raster metadata */
853  srid = rt_raster_get_srid(raster);
855 
856  RASTER_DEBUGF(3, "rt_raster_get_convex_hull: raster is %dx%d", raster->width, raster->height);
857 
858  /* return point or line since at least one of the two dimensions is 0 */
859  if ((!raster->width) || (!raster->height)) {
860  p4d.x = gt[0];
861  p4d.y = gt[3];
862 
863  /* return point */
864  if (!raster->width && !raster->height) {
865  LWPOINT *point = lwpoint_make2d(srid, p4d.x, p4d.y);
866  *hull = lwpoint_as_lwgeom(point);
867  }
868  /* return linestring */
869  else {
870  LWLINE *line = NULL;
871  pts = ptarray_construct_empty(0, 0, 2);
872 
873  /* first point of line */
874  ptarray_append_point(pts, &p4d, LW_TRUE);
875 
876  /* second point of line */
878  raster,
880  &p4d.x, &p4d.y,
881  gt
882  ) != ES_NONE) {
883  rterror("rt_raster_get_convex_hull: Could not get second point for linestring");
884  return ES_ERROR;
885  }
886  ptarray_append_point(pts, &p4d, LW_TRUE);
887  line = lwline_construct(srid, NULL, pts);
888 
889  *hull = lwline_as_lwgeom(line);
890  }
891 
892  return ES_NONE;
893  }
894  else {
895  POINTARRAY **rings = NULL;
896  LWPOLY* poly = NULL;
897 
898  /* only one ring */
899  rings = (POINTARRAY **) rtalloc(sizeof (POINTARRAY*));
900  if (!rings) {
901  rterror("rt_raster_get_convex_hull: Could not allocate memory for polygon ring");
902  return ES_ERROR;
903  }
904  rings[0] = ptarray_construct(0, 0, 5);
905  /* TODO: handle error on ptarray construction */
906  /* XXX jorgearevalo: the error conditions aren't managed in ptarray_construct */
907  if (!rings[0]) {
908  rterror("rt_raster_get_convex_hull: Could not construct point array");
909  return ES_ERROR;
910  }
911  pts = rings[0];
912 
913  /* Upper-left corner (first and last points) */
914  p4d.x = gt[0];
915  p4d.y = gt[3];
916  ptarray_set_point4d(pts, 0, &p4d);
917  ptarray_set_point4d(pts, 4, &p4d);
918 
919  /* Upper-right corner (we go clockwise) */
921  raster,
922  raster->width, 0,
923  &p4d.x, &p4d.y,
924  gt
925  );
926  ptarray_set_point4d(pts, 1, &p4d);
927 
928  /* Lower-right corner */
930  raster,
931  raster->width, raster->height,
932  &p4d.x, &p4d.y,
933  gt
934  );
935  ptarray_set_point4d(pts, 2, &p4d);
936 
937  /* Lower-left corner */
939  raster,
940  0, raster->height,
941  &p4d.x, &p4d.y,
942  gt
943  );
944  ptarray_set_point4d(pts, 3, &p4d);
945 
946  poly = lwpoly_construct(srid, 0, 1, rings);
947  *hull = lwpoly_as_lwgeom(poly);
948  }
949 
950  return ES_NONE;
951 }
LWGEOM * lwline_as_lwgeom(const LWLINE *obj)
Definition: lwgeom.c:339
LWPOINT * lwpoint_make2d(int32_t srid, double x, double y)
Definition: lwpoint.c:163
LWGEOM * lwpoly_as_lwgeom(const LWPOLY *obj)
Definition: lwgeom.c:329
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
LWLINE * lwline_construct(int32_t srid, GBOX *bbox, POINTARRAY *points)
Definition: lwline.c:42
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
Definition: lwgeom.c:344
POINTARRAY * ptarray_construct_empty(char hasz, char hasm, uint32_t maxpoints)
Create a new POINTARRAY with no points.
Definition: ptarray.c:59
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,...
Definition: ptarray.c:147
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:93
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:215
LWPOLY * lwpoly_construct(int32_t srid, GBOX *bbox, uint32_t nrings, POINTARRAY **points)
Definition: lwpoly.c:43
void ptarray_set_point4d(POINTARRAY *pa, uint32_t n, const POINT4D *p4d)
Definition: lwgeom_api.c:369
void rterror(const char *fmt,...)
Wrappers used for reporting errors and info.
Definition: rt_context.c:219
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:759
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:306
@ ES_NONE
Definition: librtcore.h:182
@ ES_ERROR
Definition: librtcore.h:183
uint16_t rt_raster_get_height(rt_raster raster)
Definition: rt_raster.c:133
uint16_t rt_raster_get_width(rt_raster raster)
Definition: rt_raster.c:125
void rt_raster_get_geotransform_matrix(rt_raster raster, double *gt)
Get 6-element array of raster geotransform matrix.
Definition: rt_raster.c:710
raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
Definition: rtrowdump.py:121
gt
Definition: window.py:78
double x
Definition: liblwgeom.h:414
double y
Definition: liblwgeom.h:414

References ES_ERROR, ES_NONE, window::gt, 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(), rtrowdump::raster, 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, 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().

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