PostGIS  3.2.2dev-r@@SVN_REVISION@@

◆ RASTER_InterpolateRaster()

Datum RASTER_InterpolateRaster ( PG_FUNCTION_ARGS  )

Definition at line 643 of file rtpg_gdal.c.

644 {
645  rt_pgraster *in_pgrast = NULL;
646  rt_pgraster *out_pgrast = NULL;
647  rt_raster in_rast = NULL;
648  rt_raster out_rast = NULL;
649  uint32_t out_rast_bands[1] = {0};
650  rt_band in_band = NULL;
651  rt_band out_band = NULL;
652  int band_number;
653  uint16_t in_band_width, in_band_height;
654  uint32_t npoints;
655  rt_pixtype in_band_pixtype;
656  GDALDataType in_band_gdaltype;
657  size_t in_band_gdaltype_size;
658 
659  rt_envelope env;
660 
661  GDALGridAlgorithm algorithm;
662  text *options_txt = NULL;
663  void *options_struct = NULL;
664  CPLErr err;
665  uint8_t *out_data;
666  rt_errorstate rterr;
667 
668  /* Input points */
669  LWPOINTITERATOR *iterator;
670  POINT4D pt;
671  size_t coord_count = 0;
672  LWGEOM *lwgeom;
673  double *xcoords, *ycoords, *zcoords;
674 
675  GSERIALIZED *gser = (GSERIALIZED*)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
676 
677  /* Z value is required to drive the grid heights */
678  if (!gserialized_has_z(gser))
679  elog(ERROR, "%s: input geometry does not have Z values", __func__);
680 
681  /* Cannot process empties */
682  if (gserialized_is_empty(gser))
683  PG_RETURN_NULL();
684 
685  in_pgrast = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(2));
686  in_rast = rt_raster_deserialize(in_pgrast, FALSE);
687  if (!in_rast)
688  elog(ERROR, "%s: Could not deserialize raster", __func__);
689 
690  /* GDAL cannot grid a skewed raster */
691  if (rt_raster_get_x_skew(in_rast) != 0.0 ||
692  rt_raster_get_y_skew(in_rast) != 0.0) {
693  elog(ERROR, "%s: Cannot generate a grid into a skewed raster",__func__);
694  }
695 
696  /* Flat JSON map of options from user */
697  options_txt = PG_GETARG_TEXT_P(1);
698  /* 1-base band number from user */
699  band_number = PG_GETARG_INT32(3);
700  if (band_number < 1)
701  elog(ERROR, "%s: Invalid band number %d", __func__, band_number);
702 
703  lwgeom = lwgeom_from_gserialized(gser);
704  npoints = lwgeom_count_vertices(lwgeom);
705  /* This shouldn't happen, but just in case... */
706  if (npoints < 1)
707  elog(ERROR, "%s: Geometry has no points", __func__);
708 
709  in_band = rt_raster_get_band(in_rast, band_number-1);
710  if (!in_band)
711  elog(ERROR, "%s: Cannot access raster band %d", __func__, band_number);
712 
713 
714  rterr = rt_raster_get_envelope(in_rast, &env);
715  if (rterr == ES_ERROR)
716  elog(ERROR, "%s: Unable to calculate envelope", __func__);
717 
718  /* Get geometry of input raster */
719  in_band_width = rt_band_get_width(in_band);
720  in_band_height = rt_band_get_height(in_band);
721  in_band_pixtype = rt_band_get_pixtype(in_band);
722  in_band_gdaltype = rt_util_pixtype_to_gdal_datatype(in_band_pixtype);
723  in_band_gdaltype_size = GDALGetDataTypeSize(in_band_gdaltype) / 8;
724 
725  /* Quickly copy options struct into local memory context, so we */
726  /* don't have malloc'ed memory lying around */
727  // if (err == CE_None && options_struct) {
728  // void *tmp = options_struct;
729  // switch (algorithm) {
730  // case GGA_InverseDistanceToAPower:
731  // options_struct = palloc(sizeof(GDALGridInverseDistanceToAPowerOptions));
732  // memcpy(options_struct, tmp, sizeof(GDALGridInverseDistanceToAPowerOptions));
733  // break;
734  // case GGA_InverseDistanceToAPowerNearestNeighbor:
735  // options_struct = palloc(sizeof(GDALGridInverseDistanceToAPowerNearestNeighborOptions));
736  // memcpy(options_struct, tmp, sizeof(GDALGridInverseDistanceToAPowerNearestNeighborOptions));
737  // break;
738  // case GGA_MovingAverage:
739  // options_struct = palloc(sizeof(GDALGridMovingAverageOptions));
740  // memcpy(options_struct, tmp, sizeof(GDALGridMovingAverageOptions));
741  // break;
742  // case GGA_NearestNeighbor:
743  // options_struct = palloc(sizeof(GDALGridNearestNeighborOptions));
744  // memcpy(options_struct, tmp, sizeof(GDALGridNearestNeighborOptions));
745  // break;
746  // case GGA_Linear:
747  // options_struct = palloc(sizeof(GDALGridLinearOptions));
748  // memcpy(options_struct, tmp, sizeof(GDALGridLinearOptions));
749  // break;
750  // default:
751  // elog(ERROR, "%s: Unsupported gridding algorithm %d", __func__, algorithm);
752  // }
753  // free(tmp);
754  // }
755 
756  /* Prepare destination grid buffer for output */
757  out_data = palloc(in_band_gdaltype_size * in_band_width * in_band_height);
758 
759  /* Prepare input points for processing */
760  xcoords = palloc(sizeof(double) * npoints);
761  ycoords = palloc(sizeof(double) * npoints);
762  zcoords = palloc(sizeof(double) * npoints);
763 
764  /* Populate input points */
765  iterator = lwpointiterator_create(lwgeom);
766  while(lwpointiterator_next(iterator, &pt) == LW_SUCCESS) {
767  if (coord_count >= npoints)
768  elog(ERROR, "%s: More points from iterator than expected", __func__);
769  xcoords[coord_count] = pt.x;
770  ycoords[coord_count] = pt.y;
771  zcoords[coord_count] = pt.z;
772  coord_count++;
773  }
774  lwpointiterator_destroy(iterator);
775 
776  /* Extract algorithm and options from options text */
777  /* This malloc's the options struct, so clean up right away */
778  err = ParseAlgorithmAndOptions(
779  text_to_cstring(options_txt),
780  &algorithm,
781  &options_struct);
782  if (err != CE_None) {
783  if (options_struct) free(options_struct);
784  elog(ERROR, "%s: Unable to parse options string: %s", __func__, CPLGetLastErrorMsg());
785  }
786 
787  /* Run the gridding algorithm */
788  err = GDALGridCreate(
789  algorithm, options_struct,
790  npoints, xcoords, ycoords, zcoords,
791  env.MinX, env.MaxX, env.MinY, env.MaxY,
792  in_band_width, in_band_height,
793  in_band_gdaltype, out_data,
794  NULL, /* GDALProgressFunc */
795  NULL /* ProgressArgs */
796  );
797 
798  /* Quickly clean up malloc'ed memory */
799  if (options_struct)
800  free(options_struct);
801 
802  if (err != CE_None) {
803  elog(ERROR, "%s: GDALGridCreate failed: %s", __func__, CPLGetLastErrorMsg());
804  }
805 
806  out_rast_bands[0] = band_number-1;
807  out_rast = rt_raster_from_band(in_rast, out_rast_bands, 1);
808  out_band = rt_raster_get_band(out_rast, 0);
809  if (!out_band)
810  elog(ERROR, "%s: Cannot access output raster band", __func__);
811 
812  /* Copy the data from the output buffer into the destination band */
813  for (uint32_t y = 0; y < in_band_height; y++) {
814  size_t offset = (in_band_height-y-1) * (in_band_gdaltype_size * in_band_width);
815  rterr = rt_band_set_pixel_line(out_band, 0, y, out_data + offset, in_band_width);
816  }
817 
818  out_pgrast = rt_raster_serialize(out_rast);
819  rt_raster_destroy(out_rast);
820  rt_raster_destroy(in_rast);
821 
822  if (NULL == out_pgrast) PG_RETURN_NULL();
823 
824  SET_VARSIZE(out_pgrast, out_pgrast->size);
825  PG_RETURN_POINTER(out_pgrast);
826 }
#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:111
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:1229
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:428
double z
Definition: liblwgeom.h:428
double y
Definition: liblwgeom.h:428
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: