From 3b70874da9ee8e4a7197e9f52235c92d855675fd Mon Sep 17 00:00:00 2001 From: Bborie Park Date: Mon, 26 Nov 2012 21:09:02 +0000 Subject: [PATCH] Added ST_Transform(raster) variant that allows of aligning output rasters to a reference raster. Ticket #2105 git-svn-id: http://svn.osgeo.org/postgis/trunk@10741 b70326c6-7e19-0410-871a-916f4a2858ee --- NEWS | 2 + doc/reference_raster.xml | 64 ++++++++++++++++++++++-- raster/rt_pg/rtpostgis.sql.in.c | 9 ++++ raster/test/regress/rt_resample.sql | 37 +++++++++++++- raster/test/regress/rt_resample_expected | 1 + 5 files changed, 108 insertions(+), 5 deletions(-) diff --git a/NEWS b/NEWS index 87d0cf164..dd0f3ddc9 100644 --- a/NEWS +++ b/NEWS @@ -86,6 +86,8 @@ PostGIS 2.1.0 - #2078, New variants of ST_Slope, ST_Aspect and ST_HillShade to provide solution to handling tiles in a coverage - #2097, Added RANGE uniontype option for ST_Union(raster) + - #2105, Added ST_Transform(raster) variant for aligning output to + reference raster * Fixes * diff --git a/doc/reference_raster.xml b/doc/reference_raster.xml index 8263044f8..b1429cdda 100644 --- a/doc/reference_raster.xml +++ b/doc/reference_raster.xml @@ -5755,6 +5755,16 @@ SELECT ST_UpperLeftX(ST_SnapToGrid(ST_AddBand(ST_MakeEmptyRaster(10, 10, 0, 0, 0 + + raster ST_Transform + raster rast + integer srid + text algorithm=NearestNeighbor + double precision maxerr=0.125 + double precision scalex + double precision scaley + + raster ST_Transform raster rast @@ -5768,11 +5778,9 @@ SELECT ST_UpperLeftX(ST_SnapToGrid(ST_AddBand(ST_MakeEmptyRaster(10, 10, 0, 0, 0 raster ST_Transform raster rast - integer srid + raster alignto text algorithm=NearestNeighbor double precision maxerr=0.125 - double precision scalex - double precision scaley @@ -5783,9 +5791,16 @@ SELECT ST_UpperLeftX(ST_SnapToGrid(ST_AddBand(ST_MakeEmptyRaster(10, 10, 0, 0, 0 Reprojects a raster in a known spatial reference system to another known spatial reference system using specified pixel warping algorithm. Uses 'NearestNeighbor' if no algorithm is specified and maxerror percent of 0.125 if no maxerr is specified. Algorithm options are: 'NearestNeighbor', 'Bilinear', 'Cubic', 'CubicSpline', and 'Lanczos'. Refer to: GDAL Warp resampling methods for more details. + + + Unlike the other variants, Variant 3 requires a reference raster as alignto. The transformed raster will be transformed to the spatial reference system (SRID) of the reference raster and be aligned (ST_SameAlignment = TRUE) to the reference raster. + + Availability: 2.0.0 Requires GDAL 1.6.1+ If you find your transformation support is not working right, you may need to set the environment variable PROJSO to the .so or .dll projection library your PostGIS is using. This just needs to have the name of the file. So for example on windows, you would in Control Panel -> System -> Environment Variables add a system variable called PROJSO and set it to libproj.dll (if you are using proj 4.6.1). You'll have to restart your PostgreSQL service/daemon after this change. + + Enhanced: 2.1.0 Addition of ST_Transform(rast, alignto) variant @@ -5842,6 +5857,49 @@ SELECT ST_UpperLeftX(ST_SnapToGrid(ST_AddBand(ST_MakeEmptyRaster(10, 10, 0, 0, 0 + + Examples: Variant 3 + + The following shows the difference between using ST_Transform(raster, srid) and ST_Transform(raster, alignto) + +WITH foo AS ( + SELECT 0 AS rid, ST_AddBand(ST_MakeEmptyRaster(2, 2, -500000, 600000, 100, -100, 0, 0, 2163), 1, '16BUI', 1, 0) AS rast UNION ALL + SELECT 1, ST_AddBand(ST_MakeEmptyRaster(2, 2, -499800, 600000, 100, -100, 0, 0, 2163), 1, '16BUI', 2, 0) AS rast UNION ALL + SELECT 2, ST_AddBand(ST_MakeEmptyRaster(2, 2, -499600, 600000, 100, -100, 0, 0, 2163), 1, '16BUI', 3, 0) AS rast UNION ALL + + SELECT 3, ST_AddBand(ST_MakeEmptyRaster(2, 2, -500000, 599800, 100, -100, 0, 0, 2163), 1, '16BUI', 10, 0) AS rast UNION ALL + SELECT 4, ST_AddBand(ST_MakeEmptyRaster(2, 2, -499800, 599800, 100, -100, 0, 0, 2163), 1, '16BUI', 20, 0) AS rast UNION ALL + SELECT 5, ST_AddBand(ST_MakeEmptyRaster(2, 2, -499600, 599800, 100, -100, 0, 0, 2163), 1, '16BUI', 30, 0) AS rast UNION ALL + + SELECT 6, ST_AddBand(ST_MakeEmptyRaster(2, 2, -500000, 599600, 100, -100, 0, 0, 2163), 1, '16BUI', 100, 0) AS rast UNION ALL + SELECT 7, ST_AddBand(ST_MakeEmptyRaster(2, 2, -499800, 599600, 100, -100, 0, 0, 2163), 1, '16BUI', 200, 0) AS rast UNION ALL + SELECT 8, ST_AddBand(ST_MakeEmptyRaster(2, 2, -499600, 599600, 100, -100, 0, 0, 2163), 1, '16BUI', 300, 0) AS rast +), bar AS ( + SELECT + ST_Transform(rast, 4269) AS alignto + FROM foo + LIMIT 1 +), baz AS ( + SELECT + rid, + rast, + ST_Transform(rast, 4269) AS not_aligned, + ST_Transform(rast, alignto) AS aligned + FROM foo + CROSS JOIN bar +) +SELECT + ST_SameAlignment(rast) AS rast, + ST_SameAlignment(not_aligned) AS not_aligned, + ST_SameAlignment(aligned) AS aligned +FROM baz + + rast | not_aligned | aligned +------+-------------+--------- + t | f | t + + + See Also , diff --git a/raster/rt_pg/rtpostgis.sql.in.c b/raster/rt_pg/rtpostgis.sql.in.c index c98f9635f..4256fe688 100644 --- a/raster/rt_pg/rtpostgis.sql.in.c +++ b/raster/rt_pg/rtpostgis.sql.in.c @@ -2074,6 +2074,15 @@ CREATE OR REPLACE FUNCTION st_transform(rast raster, srid integer, scalexy doubl AS $$ SELECT _st_resample($1, $4, $5, $2, $3, $3) $$ LANGUAGE 'sql' STABLE STRICT; +CREATE OR REPLACE FUNCTION st_transform( + rast raster, + alignto raster, + algorithm text DEFAULT 'NearestNeighbour', maxerr double precision DEFAULT 0.125 +) + RETURNS raster + AS $$ SELECT st_resample($1, $2, $3, $4, TRUE) $$ + LANGUAGE 'sql' STABLE STRICT; + ----------------------------------------------------------------------- -- ST_Rescale ----------------------------------------------------------------------- diff --git a/raster/test/regress/rt_resample.sql b/raster/test/regress/rt_resample.sql index 7394e553c..4322cd68d 100644 --- a/raster/test/regress/rt_resample.sql +++ b/raster/test/regress/rt_resample.sql @@ -590,6 +590,41 @@ FROM ( ORDER BY rid ) foo; +DROP TABLE raster_resample_src; +DROP TABLE raster_resample_dst; + +WITH foo AS ( + SELECT 0 AS rid, ST_AddBand(ST_MakeEmptyRaster(2, 2, -500000, 600000, 100, -100, 0, 0, 992163), 1, '16BUI', 1, 0) AS rast UNION ALL + SELECT 1, ST_AddBand(ST_MakeEmptyRaster(2, 2, -499800, 600000, 100, -100, 0, 0, 992163), 1, '16BUI', 2, 0) AS rast UNION ALL + SELECT 2, ST_AddBand(ST_MakeEmptyRaster(2, 2, -499600, 600000, 100, -100, 0, 0, 992163), 1, '16BUI', 3, 0) AS rast UNION ALL + + SELECT 3, ST_AddBand(ST_MakeEmptyRaster(2, 2, -500000, 599800, 100, -100, 0, 0, 992163), 1, '16BUI', 10, 0) AS rast UNION ALL + SELECT 4, ST_AddBand(ST_MakeEmptyRaster(2, 2, -499800, 599800, 100, -100, 0, 0, 992163), 1, '16BUI', 20, 0) AS rast UNION ALL + SELECT 5, ST_AddBand(ST_MakeEmptyRaster(2, 2, -499600, 599800, 100, -100, 0, 0, 992163), 1, '16BUI', 30, 0) AS rast UNION ALL + + SELECT 6, ST_AddBand(ST_MakeEmptyRaster(2, 2, -500000, 599600, 100, -100, 0, 0, 992163), 1, '16BUI', 100, 0) AS rast UNION ALL + SELECT 7, ST_AddBand(ST_MakeEmptyRaster(2, 2, -499800, 599600, 100, -100, 0, 0, 992163), 1, '16BUI', 200, 0) AS rast UNION ALL + SELECT 8, ST_AddBand(ST_MakeEmptyRaster(2, 2, -499600, 599600, 100, -100, 0, 0, 992163), 1, '16BUI', 300, 0) AS rast +), bar AS ( + SELECT + ST_Transform(rast, 994269) AS alignto + FROM foo + LIMIT 1 +), baz AS ( + SELECT + rid, + rast, + ST_Transform(rast, 994269) AS not_aligned, + ST_Transform(rast, alignto) AS aligned + FROM foo + CROSS JOIN bar +) +SELECT + ST_SameAlignment(rast) AS rast, + ST_SameAlignment(not_aligned) AS not_aligned, + ST_SameAlignment(aligned) AS aligned +FROM baz; + DELETE FROM "spatial_ref_sys" WHERE srid = 992163; DELETE FROM "spatial_ref_sys" WHERE srid = 993309; DELETE FROM "spatial_ref_sys" WHERE srid = 993310; @@ -597,5 +632,3 @@ DELETE FROM "spatial_ref_sys" WHERE srid = 994269; DELETE FROM "spatial_ref_sys" WHERE srid = 984269; DELETE FROM "spatial_ref_sys" WHERE srid = 974269; -DROP TABLE raster_resample_src; -DROP TABLE raster_resample_dst; diff --git a/raster/test/regress/rt_resample_expected b/raster/test/regress/rt_resample_expected index 826a58ae4..aef8d650a 100644 --- a/raster/test/regress/rt_resample_expected +++ b/raster/test/regress/rt_resample_expected @@ -88,3 +88,4 @@ NOTICE: Values must be provided for both X and Y when specifying the scale. Re 5.7|992163|10|11|1|1000.000|-1000.000|0.000|0.000|-500000.000|600991.000|t|t|t 5.8|992163|10|11|1|1000.000|-1000.000|0.000|0.000|-500000.000|600001.000|t|t|t 5.9|992163|10|11|1|1000.000|-1000.000|0.000|0.000|-500000.000|600009.000|t|t|t +t|f|t -- 2.40.0