PostGIS  3.4.0dev-r@@SVN_REVISION@@

◆ RASTER_GDALWarp()

Datum RASTER_GDALWarp ( PG_FUNCTION_ARGS  )

Definition at line 833 of file rtpg_gdal.c.

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

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: