]> granicus.if.org Git - postgis/commitdiff
Added UpdateRasterSRID() as per ticket #739
authorBborie Park <bkpark at ucdavis.edu>
Mon, 29 Oct 2012 18:56:41 +0000 (18:56 +0000)
committerBborie Park <bkpark at ucdavis.edu>
Mon, 29 Oct 2012 18:56:41 +0000 (18:56 +0000)
git-svn-id: http://svn.osgeo.org/postgis/trunk@10581 b70326c6-7e19-0410-871a-916f4a2858ee

NEWS
doc/reference_raster.xml
raster/rt_pg/rtpostgis.sql.in.c

diff --git a/NEWS b/NEWS
index 80213263a2d4b7fd7c801c1e980591d7c90f7600..4b9c59a688b1517fe285e914fc76c4e6dab2f7bd 100644 (file)
--- 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 
index fae54f717aef559e77429f5332422abdd3c8f611..a4866b00c5efa753793705fdad7e9dfd626ecb1f 100644 (file)
@@ -976,6 +976,51 @@ WHERE short_name = 'GTiff') As g;
          </refsection>
        </refentry>
 
+               <refentry id="RT_UpdateRasterSRID">
+                       <refnamediv>
+                               <refname>UpdateRasterSRID</refname>
+                               <refpurpose>
+                                       Change the SRID of all rasters in the user-specified column and table.
+                               </refpurpose>
+                       </refnamediv>
+
+                       <refsynopsisdiv>
+                               <funcsynopsis>
+
+                                       <funcprototype>
+                                               <funcdef>raster <function>UpdateRasterSRID</function></funcdef>
+                                               <paramdef><type>name </type> <parameter>schema_name</parameter></paramdef>
+                                               <paramdef><type>name </type> <parameter>table_name</parameter></paramdef>
+                                               <paramdef><type>name </type> <parameter>column_name</parameter></paramdef>
+                                               <paramdef><type>integer </type> <parameter>new_srid</parameter></paramdef>
+                                       </funcprototype>
+
+                                       <funcprototype>
+                                               <funcdef>raster <function>UpdateRasterSRID</function></funcdef>
+                                               <paramdef><type>name </type> <parameter>table_name</parameter></paramdef>
+                                               <paramdef><type>name </type> <parameter>column_name</parameter></paramdef>
+                                               <paramdef><type>integer </type> <parameter>new_srid</parameter></paramdef>
+                                       </funcprototype>
+
+                               </funcsynopsis>
+                       </refsynopsisdiv>
+
+                       <refsection>
+                               <title>Description</title> 
+
+                               <para>
+                                       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.
+                               </para>
+
+                               <note>
+                                       <para>
+                                               The data (band pixel values) of the rasters are not touched by this function.  Only the raster's metadata is changed.
+                                       </para>
+                               </note>
+
+                               <para>Availability: 2.1.0</para>
+                       </refsection> 
+               </refentry>
   </sect1>
   
        <sect1 id="Raster_Constructors">
index 68442d78163354e52e158dfe4bf0ee4144204226..2c072142d1caac041b7aaf36f44a8d4687915125 100644 (file)
@@ -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
 -------------------------------------------------------------------