PostGIS  3.3.9dev-r@@SVN_REVISION@@

◆ RASTER_GDALWarp()

Datum RASTER_GDALWarp ( PG_FUNCTION_ARGS  )

Definition at line 832 of file rtpg_gdal.c.

833 {
834  rt_pgraster *pgraster = NULL;
835  rt_pgraster *pgrast = NULL;
836  rt_raster raster = NULL;
837  rt_raster rast = NULL;
838 
839  text *algtext = NULL;
840  char *algchar = NULL;
841  GDALResampleAlg alg = GRA_NearestNeighbour;
842  double max_err = 0.125;
843 
844  int src_srid = SRID_UNKNOWN;
845  char *src_srs = NULL;
846  int dst_srid = SRID_UNKNOWN;
847  char *dst_srs = NULL;
848  int no_srid = 0;
849 
850  double scale[2] = {0};
851  double *scale_x = NULL;
852  double *scale_y = NULL;
853 
854  double gridw[2] = {0};
855  double *grid_xw = NULL;
856  double *grid_yw = NULL;
857 
858  double skew[2] = {0};
859  double *skew_x = NULL;
860  double *skew_y = NULL;
861 
862  int dim[2] = {0};
863  int *dim_x = NULL;
864  int *dim_y = NULL;
865 
866  POSTGIS_RT_DEBUG(3, "RASTER_GDALWarp: Starting");
867 
868  /* pgraster is null, return null */
869  if (PG_ARGISNULL(0))
870  PG_RETURN_NULL();
871  pgraster = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
872 
873  /* raster */
874  raster = rt_raster_deserialize(pgraster, FALSE);
875  if (!raster) {
876  PG_FREE_IF_COPY(pgraster, 0);
877  elog(ERROR, "RASTER_GDALWarp: Could not deserialize raster");
878  PG_RETURN_NULL();
879  }
880 
881  /* resampling algorithm */
882  if (!PG_ARGISNULL(1)) {
883  algtext = PG_GETARG_TEXT_P(1);
884  algchar = rtpg_trim(rtpg_strtoupper(text_to_cstring(algtext)));
885  alg = rt_util_gdal_resample_alg(algchar);
886  }
887  POSTGIS_RT_DEBUGF(4, "Resampling algorithm: %d", alg);
888 
889  /* max error */
890  if (!PG_ARGISNULL(2)) {
891  max_err = PG_GETARG_FLOAT8(2);
892  if (max_err < 0.) max_err = 0.;
893  }
894  POSTGIS_RT_DEBUGF(4, "max_err: %f", max_err);
895 
896  /* source SRID */
897  src_srid = clamp_srid(rt_raster_get_srid(raster));
898  POSTGIS_RT_DEBUGF(4, "source SRID: %d", src_srid);
899 
900  /* target SRID */
901  if (!PG_ARGISNULL(3)) {
902  dst_srid = clamp_srid(PG_GETARG_INT32(3));
903  if (dst_srid == SRID_UNKNOWN) {
905  PG_FREE_IF_COPY(pgraster, 0);
906  elog(ERROR, "RASTER_GDALWarp: %d is an invalid target SRID", dst_srid);
907  PG_RETURN_NULL();
908  }
909  }
910  else
911  dst_srid = src_srid;
912  POSTGIS_RT_DEBUGF(4, "destination SRID: %d", dst_srid);
913 
914  /* target SRID != src SRID, error */
915  if (src_srid == SRID_UNKNOWN && dst_srid != src_srid) {
917  PG_FREE_IF_COPY(pgraster, 0);
918  elog(ERROR, "RASTER_GDALWarp: Input raster has unknown (%d) SRID", src_srid);
919  PG_RETURN_NULL();
920  }
921  /* target SRID == src SRID, no reprojection */
922  else if (dst_srid == src_srid) {
923  no_srid = 1;
924  }
925 
926  /* scale x */
927  if (!PG_ARGISNULL(4)) {
928  scale[0] = PG_GETARG_FLOAT8(4);
929  if (FLT_NEQ(scale[0], 0.0))
930  scale_x = &scale[0];
931  }
932 
933  /* scale y */
934  if (!PG_ARGISNULL(5)) {
935  scale[1] = PG_GETARG_FLOAT8(5);
936  if (FLT_NEQ(scale[1], 0.0))
937  scale_y = &scale[1];
938  }
939 
940  /* grid alignment x */
941  if (!PG_ARGISNULL(6)) {
942  gridw[0] = PG_GETARG_FLOAT8(6);
943  grid_xw = &gridw[0];
944  }
945 
946  /* grid alignment y */
947  if (!PG_ARGISNULL(7)) {
948  gridw[1] = PG_GETARG_FLOAT8(7);
949  grid_yw = &gridw[1];
950  }
951 
952  /* skew x */
953  if (!PG_ARGISNULL(8)) {
954  skew[0] = PG_GETARG_FLOAT8(8);
955  if (FLT_NEQ(skew[0], 0.0))
956  skew_x = &skew[0];
957  }
958 
959  /* skew y */
960  if (!PG_ARGISNULL(9)) {
961  skew[1] = PG_GETARG_FLOAT8(9);
962  if (FLT_NEQ(skew[1], 0.0))
963  skew_y = &skew[1];
964  }
965 
966  /* width */
967  if (!PG_ARGISNULL(10)) {
968  dim[0] = PG_GETARG_INT32(10);
969  if (dim[0] < 0) dim[0] = 0;
970  if (dim[0] > 0) dim_x = &dim[0];
971  }
972 
973  /* height */
974  if (!PG_ARGISNULL(11)) {
975  dim[1] = PG_GETARG_INT32(11);
976  if (dim[1] < 0) dim[1] = 0;
977  if (dim[1] > 0) dim_y = &dim[1];
978  }
979 
980  /* check that at least something is to be done */
981  if (
982  (dst_srid == SRID_UNKNOWN) &&
983  (scale_x == NULL) && (scale_y == NULL) &&
984  (grid_xw == NULL) && (grid_yw == NULL) &&
985  (skew_x == NULL) && (skew_y == NULL) &&
986  (dim_x == NULL) && (dim_y == NULL)
987  ) {
988  elog(NOTICE, "No resampling parameters provided. Returning original raster");
990  PG_RETURN_POINTER(pgraster);
991  }
992  /* both values of alignment must be provided if any one is provided */
993  else if (
994  (grid_xw != NULL && grid_yw == NULL) ||
995  (grid_xw == NULL && grid_yw != NULL)
996  ) {
997  elog(NOTICE, "Values must be provided for both X and Y when specifying the alignment. Returning original raster");
999  PG_RETURN_POINTER(pgraster);
1000  }
1001  /* both values of scale must be provided if any one is provided */
1002  else if (
1003  (scale_x != NULL && scale_y == NULL) ||
1004  (scale_x == NULL && scale_y != NULL)
1005  ) {
1006  elog(NOTICE, "Values must be provided for both X and Y when specifying the scale. Returning original raster");
1008  PG_RETURN_POINTER(pgraster);
1009  }
1010  /* scale and width/height provided */
1011  else if (
1012  (scale_x != NULL || scale_y != NULL) &&
1013  (dim_x != NULL || dim_y != NULL)
1014  ) {
1015  elog(NOTICE, "Scale X/Y and width/height are mutually exclusive. Only provide one. Returning original raster");
1017  PG_RETURN_POINTER(pgraster);
1018  }
1019 
1020  /* get srses from srids */
1021  if (!no_srid) {
1022  /* source srs */
1023  src_srs = rtpg_getSR(src_srid);
1024  if (NULL == src_srs) {
1026  PG_FREE_IF_COPY(pgraster, 0);
1027  elog(ERROR, "RASTER_GDALWarp: Input raster has unknown SRID (%d)", src_srid);
1028  PG_RETURN_NULL();
1029  }
1030  POSTGIS_RT_DEBUGF(4, "src srs: %s", src_srs);
1031 
1032  dst_srs = rtpg_getSR(dst_srid);
1033  if (NULL == dst_srs) {
1034  pfree(src_srs);
1036  PG_FREE_IF_COPY(pgraster, 0);
1037  elog(ERROR, "RASTER_GDALWarp: Target SRID (%d) is unknown", dst_srid);
1038  PG_RETURN_NULL();
1039  }
1040  POSTGIS_RT_DEBUGF(4, "dst srs: %s", dst_srs);
1041  }
1042 
1044  raster,
1045  src_srs, dst_srs,
1046  scale_x, scale_y,
1047  dim_x, dim_y,
1048  NULL, NULL,
1049  grid_xw, grid_yw,
1050  skew_x, skew_y,
1051  alg, max_err);
1053  PG_FREE_IF_COPY(pgraster, 0);
1054  if (!no_srid) {
1055  pfree(src_srs);
1056  pfree(dst_srs);
1057  }
1058  if (!rast) {
1059  elog(ERROR, "RASTER_band: Could not create transformed raster");
1060  PG_RETURN_NULL();
1061  }
1062 
1063  /* add target SRID */
1064  rt_raster_set_srid(rast, dst_srid);
1065 
1066  pgrast = rt_raster_serialize(rast);
1068 
1069  if (NULL == pgrast) PG_RETURN_NULL();
1070 
1071  POSTGIS_RT_DEBUG(3, "RASTER_GDALWarp: done");
1072 
1073  SET_VARSIZE(pgrast, pgrast->size);
1074  PG_RETURN_POINTER(pgrast);
1075 }
#define FALSE
Definition: dbfopen.c:72
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:230
int32_t clamp_srid(int32_t srid)
Return a valid SRID from an arbitrary integer Raises a notice if what comes out is different from wha...
Definition: lwutil.c:333
#define FLT_NEQ(x, y)
Definition: librtcore.h:2379
int32_t rt_raster_get_srid(rt_raster raster)
Get raster's SRID.
Definition: rt_raster.c:360
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
Definition: rt_raster.c:86
void * rt_raster_serialize(rt_raster raster)
Return this raster in serialized form.
Definition: rt_serialize.c:521
GDALResampleAlg rt_util_gdal_resample_alg(const char *algname)
Convert cstring name to GDAL Resample Algorithm.
Definition: rt_util.c:94
void rt_raster_set_srid(rt_raster raster, int32_t srid)
Set raster's SRID.
Definition: rt_raster.c:367
rt_raster rt_raster_gdal_warp(rt_raster raster, const char *src_srs, const char *dst_srs, double *scale_x, double *scale_y, int *width, int *height, double *ul_xw, double *ul_yw, double *grid_xw, double *grid_yw, double *skew_x, double *skew_y, GDALResampleAlg resample_alg, double max_err)
Return a warped raster using GDAL Warp API.
Definition: rt_warp.c:178
rt_raster rt_raster_deserialize(void *serialized, int header_only)
Return a raster from a serialized form.
Definition: rt_serialize.c:725
raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
Definition: rtrowdump.py:121
char * rtpg_getSR(int32_t srid)
char * rtpg_strtoupper(char *str)
char * rtpg_trim(const char *input)
#define POSTGIS_RT_DEBUG(level, msg)
Definition: rtpostgis.h:69
#define POSTGIS_RT_DEBUGF(level, msg,...)
Definition: rtpostgis.h:73
Struct definitions.
Definition: librtcore.h:2396

References clamp_srid(), FALSE, FLT_NEQ, POSTGIS_RT_DEBUG, POSTGIS_RT_DEBUGF, rtpixdump::rast, rtrowdump::raster, rt_raster_deserialize(), rt_raster_destroy(), rt_raster_gdal_warp(), rt_raster_get_srid(), rt_raster_serialize(), rt_raster_set_srid(), rt_util_gdal_resample_alg(), rtpg_getSR(), rtpg_strtoupper(), rtpg_trim(), rt_raster_serialized_t::size, and SRID_UNKNOWN.

Here is the call graph for this function: