848{
853
854 text *algtext = NULL;
855 char *algchar = NULL;
856 GDALResampleAlg alg = GRA_NearestNeighbour;
857 double max_err = 0.125;
858
860 char *src_srs = NULL;
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
882
883
884 if (PG_ARGISNULL(0))
885 PG_RETURN_NULL();
886 pgraster = (
rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
887
888
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
897 if (!PG_ARGISNULL(1)) {
898 algtext = PG_GETARG_TEXT_P(1);
901 }
903
904
905 if (!PG_ARGISNULL(2)) {
906 max_err = PG_GETARG_FLOAT8(2);
907 if (max_err < 0.) max_err = 0.;
908 }
910
911
914
915
916 if (!PG_ARGISNULL(3)) {
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;
928
929
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
937 else if (dst_srid == src_srid) {
938 no_srid = 1;
939 }
940
941
942 if (!PG_ARGISNULL(4)) {
943 scale[0] = PG_GETARG_FLOAT8(4);
945 scale_x = &scale[0];
946 }
947
948
949 if (!PG_ARGISNULL(5)) {
950 scale[1] = PG_GETARG_FLOAT8(5);
952 scale_y = &scale[1];
953 }
954
955
956 if (!PG_ARGISNULL(6)) {
957 gridw[0] = PG_GETARG_FLOAT8(6);
958 grid_xw = &gridw[0];
959 }
960
961
962 if (!PG_ARGISNULL(7)) {
963 gridw[1] = PG_GETARG_FLOAT8(7);
964 grid_yw = &gridw[1];
965 }
966
967
968 if (!PG_ARGISNULL(8)) {
969 skew[0] = PG_GETARG_FLOAT8(8);
971 skew_x = &skew[0];
972 }
973
974
975 if (!PG_ARGISNULL(9)) {
976 skew[1] = PG_GETARG_FLOAT8(9);
978 skew_y = &skew[1];
979 }
980
981
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
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
996 if (
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
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
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
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
1036 if (!no_srid) {
1037
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 }
1046
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 }
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
1080
1083
1084 if (NULL == pgrast) PG_RETURN_NULL();
1085
1087
1088 SET_VARSIZE(pgrast, pgrast->
size);
1089 PG_RETURN_POINTER(pgrast);
1090}
#define SRID_UNKNOWN
Unknown SRID value.
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...
int32_t rt_raster_get_srid(rt_raster raster)
Get raster's SRID.
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
GDALResampleAlg rt_util_gdal_resample_alg(const char *algname)
Convert cstring name to GDAL Resample Algorithm.
void rt_raster_set_srid(rt_raster raster, int32_t srid)
Set raster's SRID.
void * rt_raster_serialize(rt_raster raster)
Return this raster in serialized form.
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.
rt_raster rt_raster_deserialize(void *serialized, int header_only)
Return a raster from a serialized form.
raster
Be careful!! Zeros function's input parameter can be a (height x width) array, not (width x height): ...
char * rtpg_getSR(int32_t srid)
char * rtpg_trim(const char *input)
char * rtpg_strtoupper(char *str)
#define POSTGIS_RT_DEBUG(level, msg)
#define POSTGIS_RT_DEBUGF(level, msg,...)