From: Bborie Park Date: Mon, 29 Oct 2012 18:56:41 +0000 (+0000) Subject: Added UpdateRasterSRID() as per ticket #739 X-Git-Tag: 2.1.0beta2~445 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=19dc276d02ce187babf1b96f7e5013a7d5b2d815;p=postgis Added UpdateRasterSRID() as per ticket #739 git-svn-id: http://svn.osgeo.org/postgis/trunk@10581 b70326c6-7e19-0410-871a-916f4a2858ee --- diff --git a/NEWS b/NEWS index 80213263a..4b9c59a68 100644 --- a/NEWS +++ b/NEWS @@ -42,6 +42,7 @@ PostGIS 2.1.0 a band using a set of geometries and corresponding values in one call (Bborie Park / UC Davis) - ST_Tile(raster) to break up a raster into tiles (Bborie Park / UC Davis) + - #739, UpdateRasterSRID() * Enhancements * - #823, tiger geocoder: Make loader_generate_script download portion diff --git a/doc/reference_raster.xml b/doc/reference_raster.xml index fae54f717..a4866b00c 100644 --- a/doc/reference_raster.xml +++ b/doc/reference_raster.xml @@ -976,6 +976,51 @@ WHERE short_name = 'GTiff') As g; + + + UpdateRasterSRID + + Change the SRID of all rasters in the user-specified column and table. + + + + + + + + raster UpdateRasterSRID + name schema_name + name table_name + name column_name + integer new_srid + + + + raster UpdateRasterSRID + name table_name + name column_name + integer new_srid + + + + + + + Description + + + Change the SRID of all rasters in the user-specified column and table. The function will drop all appropriate column constraints (extent, alignment and SRID) before changing the SRID of the specified column's rasters. + + + + + The data (band pixel values) of the rasters are not touched by this function. Only the raster's metadata is changed. + + + + Availability: 2.1.0 + + diff --git a/raster/rt_pg/rtpostgis.sql.in.c b/raster/rt_pg/rtpostgis.sql.in.c index 68442d781..2c072142d 100644 --- a/raster/rt_pg/rtpostgis.sql.in.c +++ b/raster/rt_pg/rtpostgis.sql.in.c @@ -7120,6 +7120,101 @@ CREATE OR REPLACE FUNCTION DropOverviewConstraints ( LANGUAGE 'sql' VOLATILE STRICT COST 100; +------------------------------------------------------------------------------ +-- UpdateRasterSRID +------------------------------------------------------------------------------ + +CREATE OR REPLACE FUNCTION _UpdateRasterSRID( + schema_name name, table_name name, column_name name, + new_srid integer +) + RETURNS boolean + AS $$ + DECLARE + fqtn text; + schema name; + sql text; + srid integer; + BEGIN + -- validate schema + schema := NULL; + IF length($1) > 0 THEN + sql := 'SELECT nspname FROM pg_namespace ' + || 'WHERE nspname = ' || quote_literal($1) + || 'LIMIT 1'; + EXECUTE sql INTO schema; + + IF schema IS NULL THEN + RAISE EXCEPTION 'The value provided for schema is invalid'; + RETURN FALSE; + END IF; + END IF; + + IF schema IS NULL THEN + sql := 'SELECT n.nspname AS schemaname ' + || 'FROM pg_catalog.pg_class c ' + || 'JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace ' + || 'WHERE c.relkind = ' || quote_literal('r') + || ' AND n.nspname NOT IN (' || quote_literal('pg_catalog') + || ', ' || quote_literal('pg_toast') + || ') AND pg_catalog.pg_table_is_visible(c.oid)' + || ' AND c.relname = ' || quote_literal($2); + EXECUTE sql INTO schema; + + IF schema IS NULL THEN + RAISE EXCEPTION 'The table % does not occur in the search_path', quote_literal($2); + RETURN FALSE; + END IF; + END IF; + + -- clamp SRID + IF new_srid < 0 THEN + srid := ST_SRID('POINT EMPTY'::geometry); + RAISE NOTICE 'SRID % converted to the officially unknown SRID %', new_srid, srid; + ELSE + srid := new_srid; + END IF; + + -- drop SRID, extent, alignment constraints + PERFORM DropRasterConstraints(schema, $2, $3, 'extent', 'alignment', 'srid'); + + fqtn := ''; + IF length($1) > 0 THEN + fqtn := quote_ident($1) || '.'; + END IF; + fqtn := fqtn || quote_ident($2); + + -- update SRID + sql := 'UPDATE ' || fqtn || + ' SET ' || quote_ident($3) || + ' = ST_SetSRID(' || quote_ident($3) || + '::raster, ' || srid || ')'; + RAISE NOTICE 'sql = %', sql; + EXECUTE sql; + + -- add SRID constraint + PERFORM AddRasterConstraints(schema, $2, $3, 'srid', 'extent', 'alignment'); + + RETURN TRUE; + END; + $$ LANGUAGE 'plpgsql' VOLATILE; + +CREATE OR REPLACE FUNCTION UpdateRasterSRID( + schema_name name, table_name name, column_name name, + new_srid integer +) + RETURNS boolean + AS $$ SELECT _UpdateRasterSRID($1, $2, $3, $4) $$ + LANGUAGE 'sql' VOLATILE STRICT; + +CREATE OR REPLACE FUNCTION UpdateRasterSRID( + table_name name, column_name name, + new_srid integer +) + RETURNS boolean + AS $$ SELECT _UpdateRasterSRID('', $1, $2, $3) $$ + LANGUAGE 'sql' VOLATILE STRICT; + ------------------------------------------------------------------- -- END -------------------------------------------------------------------