PostGIS  3.3.9dev-r@@SVN_REVISION@@

◆ RASTER_InterpolateRaster()

Datum RASTER_InterpolateRaster ( PG_FUNCTION_ARGS  )

Definition at line 641 of file rtpg_gdal.c.

642 {
643  rt_pgraster *in_pgrast = NULL;
644  rt_pgraster *out_pgrast = NULL;
645  rt_raster in_rast = NULL;
646  rt_raster out_rast = NULL;
647  uint32_t out_rast_bands[1] = {0};
648  rt_band in_band = NULL;
649  rt_band out_band = NULL;
650  int band_number;
651  uint16_t in_band_width, in_band_height;
652  uint32_t npoints;
653  rt_pixtype in_band_pixtype;
654  GDALDataType in_band_gdaltype;
655  size_t in_band_gdaltype_size;
656 
657  rt_envelope env;
658 
659  GDALGridAlgorithm algorithm;
660  text *options_txt = NULL;
661  void *options_struct = NULL;
662  CPLErr err;
663  uint8_t *out_data;
664  rt_errorstate rterr;
665 
666  /* Input points */
667  LWPOINTITERATOR *iterator;
668  POINT4D pt;
669  size_t coord_count = 0;
670  LWGEOM *lwgeom;
671  double *xcoords, *ycoords, *zcoords;
672 
673  GSERIALIZED *gser = (GSERIALIZED*)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
674 
675  /* Z value is required to drive the grid heights */
676  if (!gserialized_has_z(gser))
677  elog(ERROR, "%s: input geometry does not have Z values", __func__);
678 
679  /* Cannot process empties */
680  if (gserialized_is_empty(gser))
681  PG_RETURN_NULL();
682 
683  in_pgrast = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(2));
684  in_rast = rt_raster_deserialize(in_pgrast, FALSE);
685  if (!in_rast)
686  elog(ERROR, "%s: Could not deserialize raster", __func__);
687 
688  /* GDAL cannot grid a skewed raster */
689  if (rt_raster_get_x_skew(in_rast) != 0.0 ||
690  rt_raster_get_y_skew(in_rast) != 0.0) {
691  elog(ERROR, "%s: Cannot generate a grid into a skewed raster",__func__);
692  }
693 
694  /* Flat JSON map of options from user */
695  options_txt = PG_GETARG_TEXT_P(1);
696  /* 1-base band number from user */
697  band_number = PG_GETARG_INT32(3);
698  if (band_number < 1)
699  elog(ERROR, "%s: Invalid band number %d", __func__, band_number);
700 
701  lwgeom = lwgeom_from_gserialized(gser);
702  npoints = lwgeom_count_vertices(lwgeom);
703  /* This shouldn't happen, but just in case... */
704  if (npoints < 1)
705  elog(ERROR, "%s: Geometry has no points", __func__);
706 
707  in_band = rt_raster_get_band(in_rast, band_number-1);
708  if (!in_band)
709  elog(ERROR, "%s: Cannot access raster band %d", __func__, band_number);
710 
711 
712  rterr = rt_raster_get_envelope(in_rast, &env);
713  if (rterr == ES_ERROR)
714  elog(ERROR, "%s: Unable to calculate envelope", __func__);
715 
716  /* Get geometry of input raster */
717  in_band_width = rt_band_get_width(in_band);
718  in_band_height = rt_band_get_height(in_band);
719  in_band_pixtype = rt_band_get_pixtype(in_band);
720  in_band_gdaltype = rt_util_pixtype_to_gdal_datatype(in_band_pixtype);
721  in_band_gdaltype_size = GDALGetDataTypeSize(in_band_gdaltype) / 8;
722 
723  /* Quickly copy options struct into local memory context, so we */
724  /* don't have malloc'ed memory lying around */
725  // if (err == CE_None && options_struct) {
726  // void *tmp = options_struct;
727  // switch (algorithm) {
728  // case GGA_InverseDistanceToAPower:
729  // options_struct = palloc(sizeof(GDALGridInverseDistanceToAPowerOptions));
730  // memcpy(options_struct, tmp, sizeof(GDALGridInverseDistanceToAPowerOptions));
731  // break;
732  // case GGA_InverseDistanceToAPowerNearestNeighbor:
733  // options_struct = palloc(sizeof(GDALGridInverseDistanceToAPowerNearestNeighborOptions));
734  // memcpy(options_struct, tmp, sizeof(GDALGridInverseDistanceToAPowerNearestNeighborOptions));
735  // break;
736  // case GGA_MovingAverage:
737  // options_struct = palloc(sizeof(GDALGridMovingAverageOptions));
738  // memcpy(options_struct, tmp, sizeof(GDALGridMovingAverageOptions));
739  // break;
740  // case GGA_NearestNeighbor:
741  // options_struct = palloc(sizeof(GDALGridNearestNeighborOptions));
742  // memcpy(options_struct, tmp, sizeof(GDALGridNearestNeighborOptions));
743  // break;
744  // case GGA_Linear:
745  // options_struct = palloc(sizeof(GDALGridLinearOptions));
746  // memcpy(options_struct, tmp, sizeof(GDALGridLinearOptions));
747  // break;
748  // default:
749  // elog(ERROR, "%s: Unsupported gridding algorithm %d", __func__, algorithm);
750  // }
751  // free(tmp);
752  // }
753 
754  /* Prepare destination grid buffer for output */
755  out_data = palloc(in_band_gdaltype_size * in_band_width * in_band_height);
756 
757  /* Prepare input points for processing */
758  xcoords = palloc(sizeof(double) * npoints);
759  ycoords = palloc(sizeof(double) * npoints);
760  zcoords = palloc(sizeof(double) * npoints);
761 
762  /* Populate input points */
763  iterator = lwpointiterator_create(lwgeom);
764  while(lwpointiterator_next(iterator, &pt) == LW_SUCCESS) {
765  if (coord_count >= npoints)
766  elog(ERROR, "%s: More points from iterator than expected", __func__);
767  xcoords[coord_count] = pt.x;
768  ycoords[coord_count] = pt.y;
769  zcoords[coord_count] = pt.z;
770  coord_count++;
771  }
772  lwpointiterator_destroy(iterator);
773 
774  /* Extract algorithm and options from options text */
775  /* This malloc's the options struct, so clean up right away */
776  err = ParseAlgorithmAndOptions(
777  text_to_cstring(options_txt),
778  &algorithm,
779  &options_struct);
780  if (err != CE_None) {
781  if (options_struct) free(options_struct);
782  elog(ERROR, "%s: Unable to parse options string: %s", __func__, CPLGetLastErrorMsg());
783  }
784 
785  /* Run the gridding algorithm */
786  err = GDALGridCreate(
787  algorithm, options_struct,
788  npoints, xcoords, ycoords, zcoords,
789  env.MinX, env.MaxX, env.MinY, env.MaxY,
790  in_band_width, in_band_height,
791  in_band_gdaltype, out_data,
792  NULL, /* GDALProgressFunc */
793  NULL /* ProgressArgs */
794  );
795 
796  /* Quickly clean up malloc'ed memory */
797  if (options_struct)
798  free(options_struct);
799 
800  if (err != CE_None) {
801  elog(ERROR, "%s: GDALGridCreate failed: %s", __func__, CPLGetLastErrorMsg());
802  }
803 
804  out_rast_bands[0] = band_number-1;
805  out_rast = rt_raster_from_band(in_rast, out_rast_bands, 1);
806  out_band = rt_raster_get_band(out_rast, 0);
807  if (!out_band)
808  elog(ERROR, "%s: Cannot access output raster band", __func__);
809 
810  /* Copy the data from the output buffer into the destination band */
811  for (uint32_t y = 0; y < in_band_height; y++) {
812  size_t offset = (in_band_height-y-1) * (in_band_gdaltype_size * in_band_width);
813  rterr = rt_band_set_pixel_line(out_band, 0, y, out_data + offset, in_band_width);
814  }
815 
816  out_pgrast = rt_raster_serialize(out_rast);
817  rt_raster_destroy(out_rast);
818  rt_raster_destroy(in_rast);
819 
820  if (NULL == out_pgrast) PG_RETURN_NULL();
821 
822  SET_VARSIZE(out_pgrast, out_pgrast->size);
823  PG_RETURN_POINTER(out_pgrast);
824 }
#define FALSE
Definition: dbfopen.c:72
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
Definition: gserialized.c:239
int gserialized_is_empty(const GSERIALIZED *g)
Check if a GSERIALIZED is empty without deserializing first.
Definition: gserialized.c:152
int gserialized_has_z(const GSERIALIZED *g)
Check if a GSERIALIZED has a Z ordinate.
Definition: gserialized.c:174
LWPOINTITERATOR * lwpointiterator_create(const LWGEOM *g)
Create a new LWPOINTITERATOR over supplied LWGEOM*.
Definition: lwiterator.c:242
int lwpointiterator_next(LWPOINTITERATOR *s, POINT4D *p)
Attempts to assign the next point in the iterator to p, and advances the iterator to the next point.
Definition: lwiterator.c:210
#define LW_SUCCESS
Definition: liblwgeom.h:112
void lwpointiterator_destroy(LWPOINTITERATOR *s)
Free all memory associated with the iterator.
Definition: lwiterator.c:267
uint32_t lwgeom_count_vertices(const LWGEOM *geom)
Count the total number of vertices in any LWGEOM.
Definition: lwgeom.c:1246
uint16_t rt_band_get_width(rt_band band)
Return width of this band.
Definition: rt_band.c:640
double rt_raster_get_x_skew(rt_raster raster)
Get skew about the X axis.
Definition: rt_raster.c:185
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
Definition: rt_raster.c:86
rt_pixtype
Definition: librtcore.h:187
GDALDataType rt_util_pixtype_to_gdal_datatype(rt_pixtype pt)
Convert rt_pixtype to GDALDataType.
Definition: rt_util.c:121
void * rt_raster_serialize(rt_raster raster)
Return this raster in serialized form.
Definition: rt_serialize.c:521
rt_errorstate
Enum definitions.
Definition: librtcore.h:181
@ ES_ERROR
Definition: librtcore.h:183
rt_errorstate rt_band_set_pixel_line(rt_band band, int x, int y, void *vals, uint32_t len)
Set values of multiple pixels.
Definition: rt_band.c:853
rt_raster rt_raster_from_band(rt_raster raster, uint32_t *bandNums, int count)
Construct a new rt_raster from an existing rt_raster and an array of band numbers.
Definition: rt_raster.c:1463
rt_pixtype rt_band_get_pixtype(rt_band band)
Return pixeltype of this band.
Definition: rt_band.c:631
rt_errorstate rt_raster_get_envelope(rt_raster raster, rt_envelope *env)
Get raster's envelope.
Definition: rt_raster.c:904
double rt_raster_get_y_skew(rt_raster raster)
Get skew about the Y axis.
Definition: rt_raster.c:194
uint16_t rt_band_get_height(rt_band band)
Return height of this band.
Definition: rt_band.c:649
rt_raster rt_raster_deserialize(void *serialized, int header_only)
Return a raster from a serialized form.
Definition: rt_serialize.c:725
rt_band rt_raster_get_band(rt_raster raster, int bandNum)
Return Nth band, or NULL if unavailable.
Definition: rt_raster.c:385
void free(void *)
double x
Definition: liblwgeom.h:429
double z
Definition: liblwgeom.h:429
double y
Definition: liblwgeom.h:429
double MinX
Definition: librtcore.h:167
double MaxX
Definition: librtcore.h:168
double MinY
Definition: librtcore.h:169
double MaxY
Definition: librtcore.h:170
Struct definitions.
Definition: librtcore.h:2396

References ES_ERROR, FALSE, free(), gserialized_has_z(), gserialized_is_empty(), LW_SUCCESS, lwgeom_count_vertices(), lwgeom_from_gserialized(), lwpointiterator_create(), lwpointiterator_destroy(), lwpointiterator_next(), rt_envelope::MaxX, rt_envelope::MaxY, rt_envelope::MinX, rt_envelope::MinY, rt_band_get_height(), rt_band_get_pixtype(), rt_band_get_width(), rt_band_set_pixel_line(), rt_raster_deserialize(), rt_raster_destroy(), rt_raster_from_band(), rt_raster_get_band(), rt_raster_get_envelope(), rt_raster_get_x_skew(), rt_raster_get_y_skew(), rt_raster_serialize(), rt_util_pixtype_to_gdal_datatype(), rt_raster_serialized_t::size, POINT4D::x, POINT4D::y, pixval::y, and POINT4D::z.

Here is the call graph for this function: