PostGIS  2.2.7dev-r@@SVN_REVISION@@
rt_errorstate rt_raster_get_envelope_geom ( rt_raster  raster,
LWGEOM **  env 
)

Get raster's envelope as a geometry.

Parameters
raster: the raster to get info from
**env: pointer to envelope
Returns
ES_NONE if success, ES_ERROR if error

Definition at line 689 of file rt_geometry.c.

References rt_context_t::err, 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(), rt_envelope::MaxX, rt_envelope::MaxY, rt_envelope::MinX, rt_envelope::MinY, ptarray_append_point(), ptarray_construct(), ptarray_construct_empty(), ptarray_set_point4d(), RASTER_DEBUGF, rt_raster_cell_to_geopoint(), rt_raster_get_envelope(), 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_envelope(), and test_raster_envelope_geom().

689  {
690  double gt[6] = {0.0};
691  int srid = SRID_UNKNOWN;
692 
693  POINTARRAY *pts = NULL;
694  POINT4D p4d;
695 
696  assert(env != NULL);
697  *env = NULL;
698 
699  /* raster is NULL, envelope is NULL */
700  if (raster == NULL)
701  return ES_NONE;
702 
703  /* raster metadata */
704  srid = rt_raster_get_srid(raster);
706 
708  3,
709  "rt_raster_get_envelope: raster is %dx%d",
710  raster->width,
711  raster->height
712  );
713 
714  /* return point or line since at least one of the two dimensions is 0 */
715  if ((!raster->width) || (!raster->height)) {
716  p4d.x = gt[0];
717  p4d.y = gt[3];
718 
719  /* return point */
720  if (!raster->width && !raster->height) {
721  LWPOINT *point = lwpoint_make2d(srid, p4d.x, p4d.y);
722  *env = lwpoint_as_lwgeom(point);
723  }
724  /* return linestring */
725  else {
726  LWLINE *line = NULL;
727  pts = ptarray_construct_empty(0, 0, 2);
728 
729  /* first point of line */
730  ptarray_append_point(pts, &p4d, LW_TRUE);
731 
732  /* second point of line */
734  raster,
735  rt_raster_get_width(raster), rt_raster_get_height(raster),
736  &p4d.x, &p4d.y,
737  gt
738  ) != ES_NONE) {
739  rterror("rt_raster_get_envelope: Could not get second point for linestring");
740  return ES_ERROR;
741  }
742  ptarray_append_point(pts, &p4d, LW_TRUE);
743  line = lwline_construct(srid, NULL, pts);
744 
745  *env = lwline_as_lwgeom(line);
746  }
747 
748  return ES_NONE;
749  }
750  else {
751  rt_envelope rtenv;
752  int err = ES_NONE;
753  POINTARRAY **rings = NULL;
754  LWPOLY* poly = NULL;
755 
756  /* only one ring */
757  rings = (POINTARRAY **) rtalloc(sizeof (POINTARRAY*));
758  if (!rings) {
759  rterror("rt_raster_get_envelope_geom: Could not allocate memory for polygon ring");
760  return ES_ERROR;
761  }
762  rings[0] = ptarray_construct(0, 0, 5);
763  if (!rings[0]) {
764  rterror("rt_raster_get_envelope_geom: Could not construct point array");
765  return ES_ERROR;
766  }
767  pts = rings[0];
768 
769  err = rt_raster_get_envelope(raster, &rtenv);
770  if (err != ES_NONE) {
771  rterror("rt_raster_get_envelope_geom: Could not get raster envelope");
772  return err;
773  }
774 
775  /* build ring */
776 
777  /* minx, maxy */
778  p4d.x = rtenv.MinX;
779  p4d.y = rtenv.MaxY;
780  ptarray_set_point4d(pts, 0, &p4d);
781  ptarray_set_point4d(pts, 4, &p4d);
782 
783  /* maxx, maxy */
784  p4d.x = rtenv.MaxX;
785  p4d.y = rtenv.MaxY;
786  ptarray_set_point4d(pts, 1, &p4d);
787 
788  /* maxx, miny */
789  p4d.x = rtenv.MaxX;
790  p4d.y = rtenv.MinY;
791  ptarray_set_point4d(pts, 2, &p4d);
792 
793  /* minx, miny */
794  p4d.x = rtenv.MinX;
795  p4d.y = rtenv.MinY;
796  ptarray_set_point4d(pts, 3, &p4d);
797 
798  poly = lwpoly_construct(srid, 0, 1, rings);
799  *env = lwpoly_as_lwgeom(poly);
800  }
801 
802  return ES_NONE;
803 }
void ptarray_set_point4d(POINTARRAY *pa, int n, const POINT4D *p4d)
Definition: lwgeom_api.c:536
double x
Definition: liblwgeom.h:336
double MinY
Definition: librtcore.h:179
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:62
double MaxY
Definition: librtcore.h:180
void rt_raster_get_geotransform_matrix(rt_raster raster, double *gt)
Get 6-element array of raster geotransform matrix.
Definition: rt_raster.c:706
double MaxX
Definition: librtcore.h:178
LWPOINT * lwpoint_make2d(int srid, double x, double y)
Definition: lwpoint.c:132
POINTARRAY * ptarray_construct_empty(char hasz, char hasm, uint32_t maxpoints)
Create a new POINTARRAY with no points.
Definition: ptarray.c:70
tuple gt
Definition: window.py:77
void rterror(const char *fmt,...)
Wrappers used for reporting errors and info.
Definition: rt_context.c:199
void * rtalloc(size_t size)
Wrappers used for managing memory.
Definition: rt_context.c:171
LWGEOM * lwpoly_as_lwgeom(const LWPOLY *obj)
Definition: lwgeom.c:239
uint16_t height
Definition: librtcore.h:2265
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:755
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:156
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:61
LWLINE * lwline_construct(int srid, GBOX *bbox, POINTARRAY *points)
Definition: lwline.c:29
uint16_t width
Definition: librtcore.h:2264
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:172
#define RASTER_DEBUGF(level, msg,...)
Definition: librtcore.h:311
int32_t rt_raster_get_srid(rt_raster raster)
Get raster's SRID.
Definition: rt_raster.c:356
uint16_t rt_raster_get_width(rt_raster raster)
Definition: rt_raster.c:121
double MinX
Definition: librtcore.h:177
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
Definition: lwgeom.c:254
uint16_t rt_raster_get_height(rt_raster raster)
Definition: rt_raster.c:129
double y
Definition: liblwgeom.h:336
rt_errorstate rt_raster_get_envelope(rt_raster raster, rt_envelope *env)
Get raster's envelope.
Definition: rt_raster.c:873

Here is the call graph for this function:

Here is the caller graph for this function: