From 997d2424cfad99eacda6611810713d412bace5fa Mon Sep 17 00:00:00 2001 From: Bborie Park Date: Fri, 30 Nov 2012 01:12:58 +0000 Subject: [PATCH] Added a special case to RASTER_GDALWarp() where if the input raster has no SRID (SRID_UNKNOWN) AND the operation does not involve a reprojection, then use a catchall/substitute SRID (in this case 4326). Ticket #2119 git-svn-id: http://svn.osgeo.org/postgis/trunk@10768 b70326c6-7e19-0410-871a-916f4a2858ee --- raster/rt_pg/rt_pg.c | 79 +++++++++++++++--------- raster/rt_pg/rtpostgis.sql.in.c | 2 - raster/test/regress/rt_gdalwarp.sql | 7 +++ raster/test/regress/rt_gdalwarp_expected | 1 + 4 files changed, 59 insertions(+), 30 deletions(-) diff --git a/raster/rt_pg/rt_pg.c b/raster/rt_pg/rt_pg.c index cf553e673..1c58ee43f 100644 --- a/raster/rt_pg/rt_pg.c +++ b/raster/rt_pg/rt_pg.c @@ -11193,6 +11193,7 @@ Datum RASTER_GDALWarp(PG_FUNCTION_ARGS) char *src_srs = NULL; int dst_srid = SRID_UNKNOWN; char *dst_srs = NULL; + int no_srid = 0; double scale[2] = {0}; double *scale_x = NULL; @@ -11240,20 +11241,14 @@ Datum RASTER_GDALWarp(PG_FUNCTION_ARGS) } POSTGIS_RT_DEBUGF(4, "max_err: %f", max_err); - /* source srid */ - src_srid = rt_raster_get_srid(raster); - if (clamp_srid(src_srid) == SRID_UNKNOWN) { - elog(ERROR, "RASTER_GDALWarp: Input raster has unknown (%d) SRID", src_srid); - rt_raster_destroy(raster); - PG_FREE_IF_COPY(pgraster, 0); - PG_RETURN_NULL(); - } - POSTGIS_RT_DEBUGF(4, "source srid: %d", src_srid); + /* source SRID */ + src_srid = clamp_srid(rt_raster_get_srid(raster)); + POSTGIS_RT_DEBUGF(4, "source SRID: %d", src_srid); - /* target srid */ + /* target SRID */ if (!PG_ARGISNULL(3)) { - dst_srid = PG_GETARG_INT32(3); - if (clamp_srid(dst_srid) == SRID_UNKNOWN) { + dst_srid = clamp_srid(PG_GETARG_INT32(3)); + if (dst_srid == SRID_UNKNOWN) { elog(ERROR, "RASTER_GDALWarp: %d is an invalid target SRID", dst_srid); rt_raster_destroy(raster); PG_FREE_IF_COPY(pgraster, 0); @@ -11262,7 +11257,21 @@ Datum RASTER_GDALWarp(PG_FUNCTION_ARGS) } else dst_srid = src_srid; - POSTGIS_RT_DEBUGF(4, "destination srid: %d", dst_srid); + POSTGIS_RT_DEBUGF(4, "destination SRID: %d", dst_srid); + + /* source SRID = SRID_UNKNOWN */ + if (src_srid == SRID_UNKNOWN) { + /* target SRID != src SRID, error */ + if (dst_srid != src_srid) { + elog(ERROR, "RASTER_GDALWarp: Input raster has unknown (%d) SRID", src_srid); + rt_raster_destroy(raster); + PG_FREE_IF_COPY(pgraster, 0); + PG_RETURN_NULL(); + } + + /* target SRID == src SRID, special */ + no_srid = 1; + } /* scale x */ if (!PG_ARGISNULL(4)) { @@ -11316,7 +11325,7 @@ Datum RASTER_GDALWarp(PG_FUNCTION_ARGS) /* check that at least something is to be done */ if ( - (clamp_srid(dst_srid) == SRID_UNKNOWN) && + (dst_srid == SRID_UNKNOWN) && (scale_x == NULL) && (scale_y == NULL) && (grid_xw == NULL) && @@ -11360,21 +11369,31 @@ Datum RASTER_GDALWarp(PG_FUNCTION_ARGS) /* get srses from srids */ /* source srs */ - src_srs = rtpg_getSR(src_srid); - if (NULL == src_srs) { - elog(ERROR, "RASTER_GDALWarp: Input raster has unknown SRID (%d)", src_srid); - rt_raster_destroy(raster); - PG_FREE_IF_COPY(pgraster, 0); - PG_RETURN_NULL(); + /* no SRID, use EPSG:4326 (WGS84) */ + if (no_srid) + src_srs = "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"; + /* legitimate SRID */ + else { + src_srs = rtpg_getSR(src_srid); + if (NULL == src_srs) { + elog(ERROR, "RASTER_GDALWarp: Input raster has unknown SRID (%d)", src_srid); + rt_raster_destroy(raster); + PG_FREE_IF_COPY(pgraster, 0); + PG_RETURN_NULL(); + } } POSTGIS_RT_DEBUGF(4, "src srs: %s", src_srs); /* target srs */ - if (clamp_srid(dst_srid) != SRID_UNKNOWN) { + /* no SRID, use src_srs */ + if (no_srid) + dst_srs = src_srs; + /* legitimate SRID */ + else if (dst_srid != SRID_UNKNOWN) { dst_srs = rtpg_getSR(dst_srid); if (NULL == dst_srs) { elog(ERROR, "RASTER_GDALWarp: Target SRID (%d) is unknown", dst_srid); - if (NULL != src_srs) pfree(src_srs); + if (!no_srid && NULL != src_srs) pfree(src_srs); rt_raster_destroy(raster); PG_FREE_IF_COPY(pgraster, 0); PG_RETURN_NULL(); @@ -11382,8 +11401,9 @@ Datum RASTER_GDALWarp(PG_FUNCTION_ARGS) POSTGIS_RT_DEBUGF(4, "dst srs: %s", dst_srs); } - rast = rt_raster_gdal_warp(raster, src_srs, - dst_srs, + rast = rt_raster_gdal_warp( + raster, + src_srs, dst_srs, scale_x, scale_y, dim_x, dim_y, NULL, NULL, @@ -11392,15 +11412,18 @@ Datum RASTER_GDALWarp(PG_FUNCTION_ARGS) alg, max_err); rt_raster_destroy(raster); PG_FREE_IF_COPY(pgraster, 0); - if (NULL != src_srs) pfree(src_srs); - if (NULL != dst_srs) pfree(dst_srs); + if (!no_srid) { + if (NULL != src_srs) pfree(src_srs); + if (NULL != dst_srs) pfree(dst_srs); + } if (!rast) { elog(ERROR, "RASTER_band: Could not create transformed raster"); PG_RETURN_NULL(); } - /* add target srid */ - rt_raster_set_srid(rast, dst_srid); + /* add target SRID but only if we're not using a no SRID */ + if (!no_srid) + rt_raster_set_srid(rast, dst_srid); pgrast = rt_raster_serialize(rast); rt_raster_destroy(rast); diff --git a/raster/rt_pg/rtpostgis.sql.in.c b/raster/rt_pg/rtpostgis.sql.in.c index 3eb8580ad..d2b86578d 100644 --- a/raster/rt_pg/rtpostgis.sql.in.c +++ b/raster/rt_pg/rtpostgis.sql.in.c @@ -1974,7 +1974,6 @@ CREATE OR REPLACE FUNCTION st_asraster( -- ST_GDALWarp -- has no public functions ----------------------------------------------------------------------- - -- cannot be strict as almost all parameters can be NULL CREATE OR REPLACE FUNCTION _st_gdalwarp( rast raster, @@ -1992,7 +1991,6 @@ CREATE OR REPLACE FUNCTION _st_gdalwarp( ----------------------------------------------------------------------- -- ST_Resample ----------------------------------------------------------------------- - CREATE OR REPLACE FUNCTION st_resample( rast raster, scalex double precision DEFAULT 0, scaley double precision DEFAULT 0, diff --git a/raster/test/regress/rt_gdalwarp.sql b/raster/test/regress/rt_gdalwarp.sql index a05f4a740..fb05c0244 100644 --- a/raster/test/regress/rt_gdalwarp.sql +++ b/raster/test/regress/rt_gdalwarp.sql @@ -242,6 +242,13 @@ INSERT INTO raster_gdalwarp_dst (rid, rast) VALUES ( 'NearestNeighbor', 0.125, 974269 ) FROM raster_gdalwarp_src) +), ( + 0.25, (SELECT _st_gdalwarp( + ST_SetSRID(rast, 0), + 'NearestNeighbor', 0.125, + NULL, + 500., 500. + ) FROM raster_gdalwarp_src) ); -- ST_Resample diff --git a/raster/test/regress/rt_gdalwarp_expected b/raster/test/regress/rt_gdalwarp_expected index b468b0ef2..ca376cc32 100644 --- a/raster/test/regress/rt_gdalwarp_expected +++ b/raster/test/regress/rt_gdalwarp_expected @@ -22,6 +22,7 @@ NOTICE: Values must be provided for both X and Y when specifying the scale. Re 0.22|993310|26|26|1|500.000|500.000|0.000|6.000|950452.000|1396632.000|t|t|t 0.23|984269|12|8|1|0.012|-0.012|0.000|0.000|-107.029|50.206|t|t|t 0.24|974269|12|8|1|0.012|-0.012|0.000|0.000|-107.029|50.206|t|t|t +0.25|0|20|20|1|500.000|500.000|0.000|0.000|-500000.000|590000.000|t|t|t 0.3|994269|12|8|1|0.012|-0.012|0.000|0.000|-107.029|50.206|t|t|t 0.4|993310|24|24|1|500.000|500.000|0.000|0.000|950732.188|1397281.783|t|t|t 0.5|992163|10|10|1|1000.000|-1000.000|0.000|0.000|-500000.000|600000.000|t|t|t -- 2.40.0