PostGIS  3.7.0dev-r@@SVN_REVISION@@

◆ RASTER_GDALWarp()

Datum RASTER_GDALWarp ( PG_FUNCTION_ARGS  )

Definition at line 847 of file rtpg_gdal.c.

848 {
849  rt_pgraster *pgraster = NULL;
850  rt_pgraster *pgrast = NULL;
851  rt_raster raster = NULL;
852  rt_raster rast = NULL;
853 
854  text *algtext = NULL;
855  char *algchar = NULL;
856  GDALResampleAlg alg = GRA_NearestNeighbour;
857  double max_err = 0.125;
858 
859  int src_srid = SRID_UNKNOWN;
860  char *src_srs = NULL;
861  int dst_srid = SRID_UNKNOWN;
862  char *dst_srs = NULL;
863  int no_srid = 0;
864 
865  double scale[2] = {0};
866  double *scale_x = NULL;
867  double *scale_y = NULL;
868 
869  double gridw[2] = {0};
870  double *grid_xw = NULL;
871  double *grid_yw = NULL;
872 
873  double skew[2] = {0};
874  double *skew_x = NULL;
875  double *skew_y = NULL;
876 
877  int dim[2] = {0};
878  int *dim_x = NULL;
879  int *dim_y = NULL;
880 
881  POSTGIS_RT_DEBUG(3, "RASTER_GDALWarp: Starting");
882 
883  /* pgraster is null, return null */
884  if (PG_ARGISNULL(0))
885  PG_RETURN_NULL();
886  pgraster = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
887 
888  /* raster */
889  raster = rt_raster_deserialize(pgraster, FALSE);
890  if (!raster) {
891  PG_FREE_IF_COPY(pgraster, 0);
892  elog(ERROR, "RASTER_GDALWarp: Could not deserialize raster");
893  PG_RETURN_NULL();
894  }
895 
896  /* resampling algorithm */
897  if (!PG_ARGISNULL(1)) {
898  algtext = PG_GETARG_TEXT_P(1);
899  algchar = rtpg_trim(rtpg_strtoupper(text_to_cstring(algtext)));
900  alg = rt_util_gdal_resample_alg(algchar);
901  }
902  POSTGIS_RT_DEBUGF(4, "Resampling algorithm: %d", alg);
903 
904  /* max error */
905  if (!PG_ARGISNULL(2)) {
906  max_err = PG_GETARG_FLOAT8(2);
907  if (max_err < 0.) max_err = 0.;
908  }
909  POSTGIS_RT_DEBUGF(4, "max_err: %f", max_err);
910 
911  /* source SRID */
912  src_srid = clamp_srid(rt_raster_get_srid(raster));
913  POSTGIS_RT_DEBUGF(4, "source SRID: %d", src_srid);
914 
915  /* target SRID */
916  if (!PG_ARGISNULL(3)) {
917  dst_srid = clamp_srid(PG_GETARG_INT32(3));
918  if (dst_srid == SRID_UNKNOWN) {
920  PG_FREE_IF_COPY(pgraster, 0);
921  elog(ERROR, "RASTER_GDALWarp: %d is an invalid target SRID", dst_srid);
922  PG_RETURN_NULL();
923  }
924  }
925  else
926  dst_srid = src_srid;
927  POSTGIS_RT_DEBUGF(4, "destination SRID: %d", dst_srid);
928 
929  /* target SRID != src SRID, error */
930  if (src_srid == SRID_UNKNOWN && dst_srid != src_srid) {
932  PG_FREE_IF_COPY(pgraster, 0);
933  elog(ERROR, "RASTER_GDALWarp: Input raster has unknown (%d) SRID", src_srid);
934  PG_RETURN_NULL();
935  }
936  /* target SRID == src SRID, no reprojection */
937  else if (dst_srid == src_srid) {
938  no_srid = 1;
939  }
940 
941  /* scale x */
942  if (!PG_ARGISNULL(4)) {
943  scale[0] = PG_GETARG_FLOAT8(4);
944  if (FLT_NEQ(scale[0], 0.0))
945  scale_x = &scale[0];
946  }
947 
948  /* scale y */
949  if (!PG_ARGISNULL(5)) {
950  scale[1] = PG_GETARG_FLOAT8(5);
951  if (FLT_NEQ(scale[1], 0.0))
952  scale_y = &scale[1];
953  }
954 
955  /* grid alignment x */
956  if (!PG_ARGISNULL(6)) {
957  gridw[0] = PG_GETARG_FLOAT8(6);
958  grid_xw = &gridw[0];
959  }
960 
961  /* grid alignment y */
962  if (!PG_ARGISNULL(7)) {
963  gridw[1] = PG_GETARG_FLOAT8(7);
964  grid_yw = &gridw[1];
965  }
966 
967  /* skew x */
968  if (!PG_ARGISNULL(8)) {
969  skew[0] = PG_GETARG_FLOAT8(8);
970  if (FLT_NEQ(skew[0], 0.0))
971  skew_x = &skew[0];
972  }
973 
974  /* skew y */
975  if (!PG_ARGISNULL(9)) {
976  skew[1] = PG_GETARG_FLOAT8(9);
977  if (FLT_NEQ(skew[1], 0.0))
978  skew_y = &skew[1];
979  }
980 
981  /* width */
982  if (!PG_ARGISNULL(10)) {
983  dim[0] = PG_GETARG_INT32(10);
984  if (dim[0] < 0) dim[0] = 0;
985  if (dim[0] > 0) dim_x = &dim[0];
986  }
987 
988  /* height */
989  if (!PG_ARGISNULL(11)) {
990  dim[1] = PG_GETARG_INT32(11);
991  if (dim[1] < 0) dim[1] = 0;
992  if (dim[1] > 0) dim_y = &dim[1];
993  }
994 
995  /* check that at least something is to be done */
996  if (
997  (dst_srid == SRID_UNKNOWN) &&
998  (scale_x == NULL) && (scale_y == NULL) &&
999  (grid_xw == NULL) && (grid_yw == NULL) &&
1000  (skew_x == NULL) && (skew_y == NULL) &&
1001  (dim_x == NULL) && (dim_y == NULL)
1002  ) {
1003  elog(NOTICE, "No resampling parameters provided. Returning original raster");
1005  PG_RETURN_POINTER(pgraster);
1006  }
1007  /* both values of alignment must be provided if any one is provided */
1008  else if (
1009  (grid_xw != NULL && grid_yw == NULL) ||
1010  (grid_xw == NULL && grid_yw != NULL)
1011  ) {
1012  elog(NOTICE, "Values must be provided for both X and Y when specifying the alignment. Returning original raster");
1014  PG_RETURN_POINTER(pgraster);
1015  }
1016  /* both values of scale must be provided if any one is provided */
1017  else if (
1018  (scale_x != NULL && scale_y == NULL) ||
1019  (scale_x == NULL && scale_y != NULL)
1020  ) {
1021  elog(NOTICE, "Values must be provided for both X and Y when specifying the scale. Returning original raster");
1023  PG_RETURN_POINTER(pgraster);
1024  }
1025  /* scale and width/height provided */
1026  else if (
1027  (scale_x != NULL || scale_y != NULL) &&
1028  (dim_x != NULL || dim_y != NULL)
1029  ) {
1030  elog(NOTICE, "Scale X/Y and width/height are mutually exclusive. Only provide one. Returning original raster");
1032  PG_RETURN_POINTER(pgraster);
1033  }
1034 
1035  /* get srses from srids */
1036  if (!no_srid) {
1037  /* source srs */
1038  src_srs = rtpg_getSR(src_srid);
1039  if (NULL == src_srs) {
1041  PG_FREE_IF_COPY(pgraster, 0);
1042  elog(ERROR, "RASTER_GDALWarp: Input raster has unknown SRID (%d)", src_srid);
1043  PG_RETURN_NULL();
1044  }
1045  POSTGIS_RT_DEBUGF(4, "src srs: %s", src_srs);
1046 
1047  dst_srs = rtpg_getSR(dst_srid);
1048  if (NULL == dst_srs) {
1049  pfree(src_srs);
1051  PG_FREE_IF_COPY(pgraster, 0);
1052  elog(ERROR, "RASTER_GDALWarp: Target SRID (%d) is unknown", dst_srid);
1053  PG_RETURN_NULL();
1054  }
1055  POSTGIS_RT_DEBUGF(4, "dst srs: %s", dst_srs);
1056  }
1057 
1059  raster,
1060  src_srs, dst_srs,
1061  scale_x, scale_y,
1062  dim_x, dim_y,
1063  NULL, NULL,
1064  grid_xw, grid_yw,
1065  skew_x, skew_y,
1066  alg, max_err);
1068  PG_FREE_IF_COPY(pgraster, 0);
1069  if (!no_srid) {
1070  pfree(src_srs);
1071  pfree(dst_srs);
1072  }
1073  if (!rast) {
1074  elog(ERROR, "RASTER_band: Could not create transformed raster");
1075  PG_RETURN_NULL();
1076  }
1077 
1078  /* add target SRID */
1079  rt_raster_set_srid(rast, dst_srid);
1080 
1081  pgrast = rt_raster_serialize(rast);
1083 
1084  if (NULL == pgrast) PG_RETURN_NULL();
1085 
1086  POSTGIS_RT_DEBUG(3, "RASTER_GDALWarp: done");
1087 
1088  SET_VARSIZE(pgrast, pgrast->size);
1089  PG_RETURN_POINTER(pgrast);
1090 }
#define FALSE
Definition: dbfopen.c:72
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:215
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:339
#define FLT_NEQ(x, y)
Definition: librtcore.h:2423
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:177
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:65
#define POSTGIS_RT_DEBUGF(level, msg,...)
Definition: rtpostgis.h:69
Struct definitions.
Definition: librtcore.h:2440

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: