PostGIS  2.5.0dev-r@@SVN_REVISION@@
Datum RASTER_getPolygon ( PG_FUNCTION_ARGS  )

Definition at line 658 of file rtpg_geometry.c.

References ES_NONE, FALSE, geometry_serialize(), lwmpoly_as_lwgeom(), lwmpoly_free(), pixval::nband, rtrowdump::raster, rt_raster_deserialize(), rt_raster_destroy(), rt_raster_get_num_bands(), and rt_raster_surface().

659 {
660  rt_pgraster *pgraster = NULL;
661  rt_raster raster = NULL;
662  int num_bands = 0;
663  int nband = 1;
664  int err;
665  LWMPOLY *surface = NULL;
666  GSERIALIZED *rtn = NULL;
667 
668  /* raster */
669  if (PG_ARGISNULL(0))
670  PG_RETURN_NULL();
671  pgraster = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
672 
673  raster = rt_raster_deserialize(pgraster, FALSE);
674  if (!raster) {
675  PG_FREE_IF_COPY(pgraster, 0);
676  elog(ERROR, "RASTER_getPolygon: Could not deserialize raster");
677  PG_RETURN_NULL();
678  }
679 
680  /* num_bands */
681  num_bands = rt_raster_get_num_bands(raster);
682  if (num_bands < 1) {
683  elog(NOTICE, "Raster provided has no bands");
684  rt_raster_destroy(raster);
685  PG_FREE_IF_COPY(pgraster, 0);
686  PG_RETURN_NULL();
687  }
688 
689  /* band index is 1-based */
690  if (!PG_ARGISNULL(1))
691  nband = PG_GETARG_INT32(1);
692  if (nband < 1 || nband > num_bands) {
693  elog(NOTICE, "Invalid band index (must use 1-based). Returning NULL");
694  rt_raster_destroy(raster);
695  PG_FREE_IF_COPY(pgraster, 0);
696  PG_RETURN_NULL();
697  }
698 
699  /* get band surface */
700  err = rt_raster_surface(raster, nband - 1, &surface);
701  rt_raster_destroy(raster);
702  PG_FREE_IF_COPY(pgraster, 0);
703 
704  if (err != ES_NONE) {
705  elog(ERROR, "RASTER_getPolygon: Could not get raster band's surface");
706  PG_RETURN_NULL();
707  }
708  else if (surface == NULL) {
709  elog(NOTICE, "Raster is empty or all pixels of band are NODATA. Returning NULL");
710  PG_RETURN_NULL();
711  }
712 
713  rtn = geometry_serialize(lwmpoly_as_lwgeom(surface));
714  lwmpoly_free(surface);
715 
716  PG_RETURN_POINTER(rtn);
717 }
uint16_t rt_raster_get_num_bands(rt_raster raster)
Definition: rt_raster.c:372
tuple raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
Definition: rtrowdump.py:121
rt_errorstate rt_raster_surface(rt_raster raster, int nband, LWMPOLY **surface)
Get a raster as a surface (multipolygon).
Definition: rt_geometry.c:355
void lwmpoly_free(LWMPOLY *mpoly)
Definition: lwmpoly.c:53
tuple nband
Definition: pixval.py:52
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
Definition: rt_raster.c:82
#define FALSE
Definition: dbfopen.c:168
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
Struct definitions.
Definition: librtcore.h:2201
rt_raster rt_raster_deserialize(void *serialized, int header_only)
Return a raster from a serialized form.
Definition: rt_serialize.c:717
LWGEOM * lwmpoly_as_lwgeom(const LWMPOLY *obj)
Definition: lwgeom.c:284

Here is the call graph for this function: