[postgis-commits] svn - r2674 - branches/gSoC2007_raster/pgraster
postgis-commits at postgis.refractions.net
postgis-commits at postgis.refractions.net
Thu Aug 2 01:07:29 PDT 2007
Author: xingkth
Date: 2007-08-02 01:07:27 -0700 (Thu, 02 Aug 2007)
New Revision: 2674
Added:
branches/gSoC2007_raster/pgraster/pgraster_tables.sql
Removed:
branches/gSoC2007_raster/pgraster/pgraster_tables.sql
Log:
PgRaster: Add Functions to SQL Script.(GoogleSoC2007)
Deleted: branches/gSoC2007_raster/pgraster/pgraster_tables.sql
===================================================================
--- branches/gSoC2007_raster/pgraster/pgraster_tables.sql 2007-08-01 22:17:04 UTC (rev 2673)
+++ branches/gSoC2007_raster/pgraster/pgraster_tables.sql 2007-08-02 08:07:27 UTC (rev 2674)
@@ -1,730 +0,0 @@
--- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
---
--- $Id: PGRASTER_TABLES.sql 2007-07-15 Xing Lin $
---
--- PostGIS - Spatial Types for PostgreSQL
--- http://postgis.refractions.net
--- Copyright 2001-2003 Refractions Research Inc.
---
--- This is free software; you can redistribute and/or modify it under
--- the terms of the GNU General Public Licence. See the COPYING file.
---
--- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
---
--- INFORMATION ABOUT PGRASTER:
---
--- PGRaster is a component of PostGIS that handels the management of
--- raster datasets in PostgreSQL/PostGIS spatial database. Currently,
--- this extension is still under designing and development. You can
--- download it from SVN site of PostGIS for a preview, or you can install
--- it with PostGIS after it is publicly released.
---
--- PGRaster is also serviced as one of Gooogle SoC 2007 projects.
---
--- Any advices or bugs reports, please write to Xing at solo.lin at gmail.com
---
--- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
---
--- WARNING: Any change in this file must be evaluated for compatibility.
--- Changes cleanly handled by lwpostgis_uptrade.sql are fine,
--- other changes will require a bump in Major version.
--- Currently only function replaceble by CREATE OR REPLACE
--- are cleanly handled.
---
--- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
--- ABOUT THIS FILE --
--- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
--- NAME: PGRASTER_TABLES.SQL
--- AUTHOR: XING LIN
--- CREATED_DATE: 2007-07-15
--- LAST_UPDATED: 2007-07-15
--- PURPORSE: This SQL file include the definition of the metadata table for
--- PGRaster extension. You can use this file as an template to
--- create your own metadata table for your raster database in
--- PostgreSQL/PostGIS.
--- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
--- CHANGE LOG --
--- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
--- 1) 2007-07-15 File created by Xing Lin. (Xing)
--- 2) 2007-07-22 Add "SCHEMA" column to PGRASTER_METADATA table. (Xing)
--- 3) 2007-07-30 Add several functions and triggers to help create/modify/
--- remove pgraster dataset in PGRASTER database. (Xing)
--- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
---DEFINITION OF TYPE INFORMATION
--- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
---BAND TYPE CODES & NAMES
-CREATE TABLE PGRASTER_TYPES_BANDTYPE
-(
- typeID SMALLINT,
- typeName TEXT,
- descriptioin TEXT,
- PRIMARY KEY (typeID)
-);
-INSERT INTO PGRASTER_TYPES_BANDTYPE VALUES(-1,'BT_UNKNOWN','Unknown Band Type');
-INSERT INTO PGRASTER_TYPES_BANDTYPE VALUES(0,'BT_SINGLE','Single Band Raster Dataset');
-INSERT INTO PGRASTER_TYPES_BANDTYPE VALUES(1,'BT_RGB','RGB Image Raster Dataset');
-INSERT INTO PGRASTER_TYPES_BANDTYPE VALUES(2,'BT_RGBA','RGBA Image Raster Dataset');
-INSERT INTO PGRASTER_TYPES_BANDTYPE VALUES(3,'BT_MULTI','Multi-bands Raster Dataset');
-INSERT INTO PGRASTER_TYPES_BANDTYPE VALUES(4,'BT_OTHERS','Other Image Raster Dataset');
-
---DATA TYPE CODES & NAMES
-CREATE TABLE PGRASTER_TYPES_DATATYPE
-(
- typeID SMALLINT,
- typeName TEXT,
- descriptioin TEXT,
- PRIMARY KEY (typeID)
-);
-INSERT INTO PGRASTER_TYPES_DATATYPE VALUES(-1,'DT_UNKNOWN','Unkown Data Type');
-INSERT INTO PGRASTER_TYPES_DATATYPE VALUES(0,'DT_1BIT','A Boolean Data Unit having A Cell Depth of 1 Bits');
-INSERT INTO PGRASTER_TYPES_DATATYPE VALUES(1,'DT_2BIT','2 Bits Unsigned Integer having A Cell Depth of 2 Bits');
-INSERT INTO PGRASTER_TYPES_DATATYPE VALUES(2,'DT_4BIT','4 Bits Unsigned Integer having A Cell Depth of 4 Bits');
-INSERT INTO PGRASTER_TYPES_DATATYPE VALUES(3,'DT_8BIT_U','8 Bits Unsigned Integer having A Cell Depth of 8 Bits');
-INSERT INTO PGRASTER_TYPES_DATATYPE VALUES(4,'DT_8BIT_S','8 Bits Signed Integer having A Cell Depth of 8 Bits');
-INSERT INTO PGRASTER_TYPES_DATATYPE VALUES(5,'DT_16BIT_U','16 Bits Unsigned Integer having A Cell Depth of 16 Bits');
-INSERT INTO PGRASTER_TYPES_DATATYPE VALUES(6,'DT_16BIT_S','16 Bits Signed Integer having A Cell Depth of 16 Bits');
-INSERT INTO PGRASTER_TYPES_DATATYPE VALUES(7,'DT_32BIT_U','32 Bits Unsigned Integer having A Cell Depth of 32 Bits');
-INSERT INTO PGRASTER_TYPES_DATATYPE VALUES(8,'DT_32BIT_S','32 Bits Signed Integer having A Cell Depth of 32 Bits');
-INSERT INTO PGRASTER_TYPES_DATATYPE VALUES(9,'DT_24BIT_RGB','24 Bits RGB data having A Cell Depth of 24 Bits');
-INSERT INTO PGRASTER_TYPES_DATATYPE VALUES(10,'DT_32BIT_RGBA','32 Bits RGBA data having A Cell Depth of 32 Bits');
-INSERT INTO PGRASTER_TYPES_DATATYPE VALUES(11,'DT_32BIT_REAL','32 Bits Real/Single Floating data having A Cell Depth of 32 Bits');
-INSERT INTO PGRASTER_TYPES_DATATYPE VALUES(12,'DT_64BIT_REAL','64 Bits Real/Double Floating data having A Cell Depth of 64 Bits');
-
---VALUE TYPE CODES & NAMES
-CREATE TABLE PGRASTER_TYPES_VALUETYPE
-(
- typeID SMALLINT,
- typeName TEXT,
- descriptioin TEXT,
- PRIMARY KEY (typeID)
-);
-INSERT INTO PGRASTER_TYPES_VALUETYPE VALUES(-1, 'VT_UNKNOWN','Unknown Value Type');
-INSERT INTO PGRASTER_TYPES_VALUETYPE VALUES(0, 'VT_NOMINAL','Nominal Value Type');
-INSERT INTO PGRASTER_TYPES_VALUETYPE VALUES(1, 'VT_ORDINAL','Ordinal Value Type');
-INSERT INTO PGRASTER_TYPES_VALUETYPE VALUES(2, 'VT_INTERVAL','Interval Value Type');
-INSERT INTO PGRASTER_TYPES_VALUETYPE VALUES(3, 'VT_RATIO','Ratio Value Type');
-INSERT INTO PGRASTER_TYPES_VALUETYPE VALUES(4, 'VT_IMAGE','Image Value Type');
-
---BANDINTERLEAVING CODES & NAMES
-CREATE TABLE PGRASTER_TYPES_BANDINTERLEAVINGTYPE
-(
- typeID SMALLINT NOT NULL,
- typeName TEXT NOT NULL,
- descriptioin TEXT NOT NULL,
- PRIMARY KEY (typeID)
-);
-INSERT INTO PGRASTER_TYPES_BANDINTERLEAVINGTYPE VALUES(-1, 'BI_UNKNOWN','Unknown Band Interleaving Type');
-INSERT INTO PGRASTER_TYPES_BANDINTERLEAVINGTYPE VALUES(0, 'BI_BSQ','BSQ Band Interleaving Type');
-INSERT INTO PGRASTER_TYPES_BANDINTERLEAVINGTYPE VALUES(1, 'BI_BIL','BIL Band Interleaving Type');
-INSERT INTO PGRASTER_TYPES_BANDINTERLEAVINGTYPE VALUES(2, 'BI_BIP','BIP Band Interleaving Type');
-
---COMPRESSION CODES & NAMES
-CREATE TABLE PGRASTER_TYPES_COMPRESSIONTYPE
-(
- typeID SMALLINT NOT NULL,
- typeName TEXT NOT NULL,
- descriptioin TEXT NOT NULL,
- PRIMARY KEY (typeID)
-);
-INSERT INTO PGRASTER_TYPES_COMPRESSIONTYPE VALUES(-1, 'CT_UNKNOWN','Unknown Compression Type');
-INSERT INTO PGRASTER_TYPES_COMPRESSIONTYPE VALUES(0, 'CT_JPEG_B','JPEG-B Compression Type');
-INSERT INTO PGRASTER_TYPES_COMPRESSIONTYPE VALUES(1, 'CT_JPEG_F','JPEG-F Compression Type');
-INSERT INTO PGRASTER_TYPES_COMPRESSIONTYPE VALUES(2, 'CT_LZW','LZW/LZ77 Compression Type');
-INSERT INTO PGRASTER_TYPES_COMPRESSIONTYPE VALUES(3, 'CT_NONE','UNCOMPRESSION/PLAIN Compression Type');
-
--- DEFINITION OF PGRASTER_METADATA
-
-CREATE TABLE PGRASTER_METADATA
-(
- rasterObjectID BIGSERIAL PRIMARY KEY,
- name TEXT NOT NULL,
- captureDate DATE NOT NULL DEFAULT NOW(),
- rasterDimensions SMALLINT NOT NULL DEFAULT 0,
- rasterBandType SMALLINT NOT NULL DEFAULT -1 REFERENCES pgraster_types_bandtype (typeid),
- rasterDataType SMALLINT NOT NULL DEFAULT -1 REFERENCES pgraster_types_datatype (typeid),
- rasterValueType SMALLINT NOT NULL DEFAULT -1 REFERENCES pgraster_types_valuetype (typeid),
- rasterSchema TEXT NOT NULL DEFAULT 'PUBLIC',
- rasterDataTable TEXT NOT NULL DEFAULT 'PGRASTER_DATA',
- rasterBandCount INTEGER NOT NULL DEFAULT 0,
- rasterRowCount INTEGER NOT NULL DEFAULT 0,
- rasterColumnCount INTEGER NOT NULL DEFAULT 0,
- rasterCellDepth INTEGER NOT NULL DEFAULT 0,
- rasterPyramidEnabled BOOLEAN NOT NULL DEFAULT TRUE,
- rasterPyramidDepth INTEGER NOT NULL DEFAULT 0,
- blockSizeBands INTEGER NOT NULL DEFAULT -1, -- (-1: means all bands included)
- blockSizeRows INTEGER NOT NULL DEFAULT 256,
- blockSizeColumns INTEGER NOT NULL DEFAULT 256,
- blockPadding BOOLEAN NOT NULL DEFAULT TRUE,
- blockBandInterleaving SMALLINT NOT NULL DEFAULT 0, -- (0: BSQ Band Interleaving Approach)
- blockCompression SMALLINT NOT NULL DEFAULT 3 REFERENCES pgraster_types_compressiontype (typeid), -- (3: CT_LZW)
- blockQuality INTEGER NOT NULL DEFAULT 100,
- nodataValue NUMERIC NOT NULL DEFAULT -1,
- SRID INTEGER NOT NULL DEFAULT -1, -- (-1: means no spatial reference attached)
- geoReferenced BOOLEAN NOT NULL DEFAULT FALSE,
- spatialExtent GEOMETRY DEFAULT NULL
-
- -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- -- Foreign Key and Other Constraints should be added Here
- -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-);
-
-
---DEFINITION OF PGRASTER_DATA
-
-CREATE TABLE PGRASTER_DATA
-(
- rasterObjectID INTEGER REFERENCES pgraster_metadata (rasterObjectID),
- pyramidLevel INTEGER NOT NULL DEFAULT -1,
- bandBlockNumber INTEGER NOT NULL DEFAULT -1,
- rowBlockNumber INTEGER NOT NULL DEFAULT -1,
- columnBlockNumber INTEGER NOT NULL DEFAULT -1,
- blockBandSize INTEGER NOT NULL DEFAULT -1, --(-1: means ALL bands included.)
- blockRowSize INTEGER NOT NULL DEFAULT 256,
- blockColumnSize INTEGER NOT NULL DEFAULT 256,
- blockMBR GEOMETRY NOT NULL,
- dataBlock BYTEA NOT NULL,
-
- -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- -- Primary Key Constraints should be added Here
- -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- PRIMARY KEY (rasterObjectID,pyramidLevel,bandBlockNumber,rowBlockNumber,columnBlockNumber)
-
- -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- -- Foreign Key And Other Constraints should be added Here
- -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-);
-
--- CREATE GiST-R Tree index on PGRASTER_DATA (blockMBR)
-CREATE INDEX index_pgraster_data ON pgraster_data using GIST ( blockMBR GIST_GEOMETRY_OPS);
-
---DEFINITION OF PGRASTER_SRS
-CREATE TABLE PGRASTER_SRS
-(
- rasterObjectID INTEGER REFERENCES pgraster_metadata (rasterObjectID),
- isReferenced BOOLEAN NOT NULL DEFAULT FALSE,
- isOrthoRectified BOOLEAN NOT NULL DEFAULT FALSE,
- SRID INTEGER NOT NULL DEFAULT -1,
- spatialResolutionX NUMERIC NOT NULL DEFAULT 0.0,
- spatialResolutionY NUMERIC NOT NULL DEFAULT 0.0,
- spatialResolutionZ NUMERIC NOT NULL DEFAULT 0.0,
- spatialTolerance NUMERIC NOT NULL DEFAULT 0.0,
- coordLocation SMALLINT NOT NULL DEFAULT 0,
- rowOff NUMERIC NOT NULL DEFAULT 0,
- columnOff NUMERIC NOT NULL DEFAULT 0,
- heightOff NUMERIC NOT NULL DEFAULT 0,
- xOff NUMERIC NOT NULL DEFAULT 0,
- yOff NUMERIC NOT NULL DEFAULT 0,
- zOff NUMERIC NOT NULL DEFAULT 0,
- rowScale NUMERIC NOT NULL DEFAULT 1,
- columnScale NUMERIC NOT NULL DEFAULT 1,
- heightScale NUMERIC NOT NULL DEFAULT 1,
- xScale NUMERIC NOT NULL DEFAULT 1,
- yScale NUMERIC NOT NULL DEFAULT 1,
- zScale NUMERIC NOT NULL DEFAULT 1,
- columnRMS NUMERIC NOT NULL DEFAULT 0.0,
- totalRMS NUMERIC NOT NULL DEFAULT 0.0,
- -- parameters for afline transformation from cell coordinates systems to ground / local coordinates
- rowNumberator NUMERIC[] DEFAULT NULL,
- rowDenominator NUMERIC[] DEFAULT NULL,
- columnNumerator NUMERIC[] DEFAULT NULL,
- columnDenominator NUMERIC[] DEFAULT NULL,
-
- -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- -- Primary Key Constraints should be added Here
- -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- PRIMARY KEY (rasterObjectID)
-
- -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- -- Foreign Key And Other Constraints should be added Here
- -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
-);
-
---DEFINITION OF PGRASTER_GCPS
-CREATE TABLE PGRASTER_GCPS
-(
- rasterObjectID INTEGER REFERENCES pgraster_metadata (rasterObjectID),
- gcpID BIGSERIAL,
- cellColumn NUMERIC NOT NULL DEFAULT 0.0,
- cellRow NUMERIC NOT NULL DEFAULT 0.0,
- cellHeight NUMERIC NOT NULL DEFAULT 0.0,
- groundX NUMERIC NOT NULL DEFAULT 0.0,
- groundY NUMERIC NOT NULL DEFAULT 0.0,
- groundZ NUMERIC NOT NULL DEFAULT 0.0,
- rmsError NUMERIC NOT NULL DEFAULT 0.0,
- -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- -- Primary Key Constraints should be added Here
- -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- PRIMARY KEY (rasterObjectID,gcpID)
-);
-
-
---DEFINITIOF OF PGRASTER_VATS
-CREATE TABLE PGRASTER_VATS
-(
- rasterObjectID INTEGER REFERENCES pgraster_metadata(rasterObjectID),
- bandID INTEGER NOT NULL DEFAULT 0,
- value NUMERIC NOT NULL,
- attribute TEXT NOT NULL DEFAULT '',
- -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- -- Primary Key Constraints should be added Here
- -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- PRIMARY KEY (rasterObjectID,bandID,value)
-);
-
---DEFINITION OF PGRASTER_STATITICS
-CREATE TABLE PGRASTER_STATISTICS
-(
- rasterObjectID INTEGER REFERENCES pgraster_metadata(rasterObjectID),
- bandID INTEGER NOT NULL DEFAULT 0,
- maxValue NUMERIC NOT NULL DEFAULT 0,
- minValue NUMERIC NOT NULL DEFAULT 0,
- avgValue NUMERIC NOT NULL DEFAULT 0,
- modeValue NUMERIC NOT NULL DEFAULT 0,
- stdValue NUMERIC NOT NULL DEFAULT 0,
- lastUpdate TIMESTAMP DEFAULT NOW(),
- -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- -- Primary Key Constraints should be added Here
- -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- PRIMARY KEY (rasterObjectID,bandID)
-);
-
---DEFINITION OF PGRASTER_HISTOGRAM
-CREATE TABLE PGRASTER_HISTOGRAM
-(
- rasterObjectID INTEGER REFERENCES pgraster_metadata(rasterObjectID),
- bandID INTEGER NOT NULL DEFAULT 0,
- value NUMERIC NOT NULL DEFAULT 0,
- count NUMERIC NOT NULL DEFAULT 0,
- -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- -- Primary Key Constraints should be added Here
- -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- PRIMARY KEY (rasterObjectID,bandID,value)
-);
-
-
---DEFINITION OF PGRASTER_UTILITIES_PALETTE
-CREATE TABLE PGRASTER_UTILS_PALETTE
-(
- id SERIAL,
- name TEXT NOT NULL,
- description TEXT NOT NULL DEFAULT '',
- colorNumber INTEGER NOT NULL,
- colorDepth INTEGER NOT NULL, -- RGB=24, RGBA=32
- colorData BYTEA NOT NULL,
- PRIMARY KEY (id)
-
- -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- -- Foreign Key And Other Constraints should be added Here
- -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
-);
-
----------------------------------------------------------------------------
------Add Functions for PGRaster to add/remove/alter pgraster datasets------
----------------------------------------------------------------------------
-
--- PGRASTER_VERSION() Function
-CREATE OR REPLACE FUNCTION pgraster_version() RETURNS TEXT AS
-$$
- SELECT '0.0.1'
-$$
-LANGUAGE SQL IMMUTABLE STRICT;
-
-
--- AddRasterTableFull(<schema>, <table>, <xsize>, <ysize>, <bands>,<rows>,<columns>,<data_type>,<band_type>,<value_type>,<bil_type>,<compress_type>,<srid>)
--- Other Missing Parameter will using recommened or default values.
--- This function will also update the Spatial Referenced System(SRS) information.
-CREATE OR REPLACE FUNCTION AddRasterTableFull(varchar, varchar, numeric, numeric, integer, integer,integer,integer, integer,integer,integer,integer, integer)
-RETURNS TEXT
-AS
-'
-DECLARE
- schema alias for $1;
- table alias for $2;
- xsize alias for $3;
- ysize alias for $4;
- bands alias for $5;
- rows alias for $6;
- columns alias for $7;
- data_type alias for $8;
- band_type alias for $9;
- value_type alias for $10;
- bil_type alias for $11;
- compress_type alias for $12;
- new_srid alias for $13;
-
- dimension INTEGER;
- rec RECORD;
- schema_ok BOOL;
- table_exist BOOL;
- cell_depth INTEGER;
- geo_referenced BOOL;
- real_schema NAME;
- object_name NAME;
- object_id INTEGER;
- object_oldid INTEGER;
-
-BEGIN
-
- -- Check if table name is empty
- IF (table = '''') THEN
- RAISE EXCEPTION ''Table name could not be empty. Please try another again!'';
- return ''fail'';
- END IF;
-
- -- By default, only the 2-dimensional raster model is supported.
- dimension := 2;
-
- --Check Type ID Valid?
- IF ( ( NOT ( data_type>=-1 AND data_type <=12 ) )
- OR (NOT (band_type >=-1 AND band_type <=4 ) )
- OR (NOT (value_type >=-1 AND value_type <=4 ) )
- OR (NOT (bil_type >=-1 AND bil_type<=2 ) )
- OR (NOT (compress_type>= -1 AND compress_type <= 3))
- )
- THEN
- RAISE EXCEPTION ''Invalid type ids - valid ones are:
- DATA_TYPE=[-1,11]; BAND_TYPE=[-1,4];VALUE_TYPE=[-1,4];
- BANDINTERLEAVING_TYPE=[-1,2];COMPRESSION_TYPE=[-1,3];
- '';
- return ''fail'';
- END IF;
-
- --Check dimension number
- IF ( NOT(dimension=2)) THEN
- RAISE EXCEPTION ''Invalid dimension. Only 2 dimensions is supported currently.'';
- return ''fail'';
- END IF;
-
- --Check and replace as an valid schema name
- IF ( NOT( schema='''') ) THEN
- schema_ok := ''f'';
- FOR rec IN SELECT nspname FROM pg_namespace WHERE text(nspname) = schema LOOP
- schema_ok := ''t'';
- END LOOP;
-
- if ( NOT(schema_ok =''t'') ) THEN
- RAISE NOTICE ''Invalid schema name - using current_schema()'';
- SELECT current_schema() into real_schema;
- ELSE
- real_schema := schema;
- END IF;
- ELSE
- --Using current_schema for empty schema.
- SELECT current_schema() into real_schema;
- END IF;
-
-
- -- Check if current data table exist. If NOT, create a new one from PGRASTER_DATA.
- table_exist := ''f'';
-
- FOR rec IN SELECT tablename FROM pg_tables WHERE text(schemaname) = real_schema AND lower(text(tablename))=lower(table) LOOP
- table_exist := ''t'';
- END LOOP;
-
- IF ( table_exist = ''f'') THEN
- -- CREATE A NEW PGRASTER_DATA table
- EXECUTE ''CREATE TABLE '' || quote_ident(real_schema) || ''.'' || quote_ident(table)
- || ''(LIKE public.pgraster_data)'' ;
-
- ELSE
- --It is not recommended to put several dataset into one data table,
- --Keep the table but clear all the data in existing tables
- --Or shoud we pop up a message telling that the table exists already.
- EXECUTE '' DELETE FROM '' || quote_ident(real_schema) || ''.'' || quote_ident(table);
- END IF;
-
- -- Calculate celldepth from data type
- cell_depth := 0;
- IF (data_type = -1) THEN
- cell_depth := 0;
- ELSE
- IF (data_type = 0) THEN
- cell_depth := 1;
- ELSE
- IF (data_type = 1) THEN
- cell_depth := 2;
- ELSE
- IF (data_type = 2) THEN
- cell_depth := 4;
- ELSE
- IF (data_type=3 OR data_type=4) THEN
- cell_depth := 8;
- ELSE
- IF (data_type=5 OR data_type=6) THEN
- cell_depth := 16;
- ELSE
- IF (data_type=7 OR data_type=8 OR data_type=10) THEN
- cell_depth := 32;
- ELSE
- IF (data_type=9) THEn
- cell_depth := 24;
- ELSE
- cell_depth :=64;
- END IF;
- END IF;
- END IF;
- END IF;
- END IF;
- END IF;
- END IF;
- END IF;
-
-
- -- Delete stale record in pgraster_metadata(if any)
- object_name := real_schema || ''.'' || table;
-
- --Retrieve OLD object id from pgraster_metadata table
- SELECT rasterObjectID INTO object_oldid FROM pgraster_metadata WHERE name=object_name;
- EXECUTE ''DELETE FROM PGRASTER_METADATA WHERE
- rasterSchema = '' || quote_literal(real_schema) ||'' AND rasterDataTable = '' || quote_literal(table);
-
- -- Check Information in other data table (SRS)
- geo_referenced := ''f'';
- IF (NOT(new_srid=-1)) THEN
- geo_referenced := ''t'';
- END IF;
-
- -- Add record in pgraster_metadata table
- EXECUTE ''INSERT INTO public.pgraster_metadata(name,rasterDimensions,rasterBandType,rasterDataType,rasterValueType ,rasterSchema,rasterDataTable,rasterBandCount,rasterRowCount,rasterColumnCount,rasterCellDepth,blockBandInterleaving ,blockCompression,SRID,geoReferenced) VALUES ( '' ||
- quote_literal(object_name) || '','' ||
- dimension || '','' ||
- band_type || '','' ||
- data_type || '','' ||
- value_type || '','' ||
- quote_literal(real_schema) || '','' ||
- quote_literal(table) || '','' ||
- bands || '','' ||
- rows || '','' ||
- columns || '','' ||
- cell_depth || '','' ||
- bil_type || '','' ||
- compress_type || '','' ||
- new_srid || '','' ||
- quote_literal(geo_referenced) || '')'';
-
- --Retrieve Newly create object id from pgraster_metadata table
- SELECT rasterObjectID INTO object_id FROM pgraster_metadata WHERE name=object_name;
-
- --Update SRS informatio in pgraster_srs
- IF ( object_oldid IS NOT NULL) THEN
- EXECUTE ''DELETE FROM pgraster_srs WHERE rasterObjectID = '' || object_id || '' OR rasterObjectID = '' || object_oldid;
- ELSE
- EXECUTE ''DELETE FROM pgraster_srs WHERE rasterObjectID = '' || object_id;
- END IF;
-
- EXECUTE ''INSERT INTO public.pgraster_srs(rasterObjectID,isReferenced,SRID,spatialResolutionX,spatialResolutionY) values('' || object_id || '','' || quote_literal(geo_referenced) || '','' || new_srid || '','' || xsize || '','' || ysize || '')'';
-
- --return results
- RETURN
- real_schema || ''.'' || table ;
-END;
-'
-LANGUAGE 'plpgsql' VOLATILE STRICT; -- WITH (isstrict);
-
-
--- AddRasterTableCompact(<schema>, <table>, <xsize>, <ysize>, <bands>,<rows>,<columns>,<data_type>,<storage_type>,<srid>)
-CREATE OR REPLACE FUNCTION AddRasterTableCompact(varchar, varchar, numeric, numeric, integer, integer, integer, integer, integer,integer)
-RETURNS TEXT
-AS
-'
-DECLARE
- schema alias for $1;
- table alias for $2;
- xsize alias for $3;
- ysize alias for $4;
- bands alias for $5;
- rows alias for $6;
- columns alias for $7;
- data_type alias for $8;
- bil_type alias for $9;
- new_srid alias for $10;
-
- band_type INTEGER;
- value_type INTEGER;
- compress_type INTEGER;
-
-BEGIN
-
- -- Using recommended value for missing parameters.
- band_type := 3; -- BT_MULTISPECTRAL
- value_type := 2; -- VT_NOMINAL
- compress_type := 2; -- CT_LZW
-
- RETURN AddRasterTableFull(schema, table, xsize, ysize, bands,rows, columns, data_type, band_type, value_type, bil_type, compress_type, new_srid );
-
-END;
-'
-LANGUAGE 'plpgsql' VOLATILE STRICT; -- WITH (isstrict);
-
-
--- AddRasterTable(<schema>, <table>, <xsize>, <ysize>, <bands>, <storage_type>,<srid>)
-CREATE OR REPLACE FUNCTION AddRasterTable(varchar, varchar, numeric, numeric, integer, integer,integer)
-RETURNS TEXT
-AS
-'
-DECLARE
- schema alias for $1;
- table alias for $2;
- xsize alias for $3;
- ysize alias for $4;
- bands alias for $5;
- bil_type alias for $6;
- new_srid alias for $7;
-
- data_type INTEGER;
- columns INTEGER;
- rows INTEGER;
- band_type INTEGER;
- value_type INTEGER;
- dimension_type INTEGER;
- compress_type INTEGER;
-
-BEGIN
-
- -- Using default (not-real) value for missing parameters.
- data_type := 3; -- DT_8BIT_U
- columns := 0; -- O Columns
- rows := 0; -- 0 Rows
-
- -- Using recommended value for missing parameters.
- band_type := 3; -- BT_MULTISPECTRAL
- value_type := 2; -- VT_NOMINAL
- compress_type := 2; -- CT_LZW
-
- RETURN AddRasterTableFull(schema, table, xsize, ysize, bands,rows, columns, data_type, band_type, value_type, bil_type, compress_type, new_srid );
-
-END;
-'
-LANGUAGE 'plpgsql' VOLATILE STRICT; -- WITH (isstrict);
-
---DropRasterTableByName(<schema>,<table>)
-CREATE OR REPLACE FUNCTION DropRasterTableByName(varchar, varchar)
-RETURNS TEXT
-AS
-'
-DECLARE
- schema alias for $1;
- table alias for $2;
-BEGIN
-
- -- Clear metadata from pgraster metadata table.
- EXECUTE ''DELETE FROM pgraster_metadata WHERE rasterSchema='' || quote_literal(schema) || '' AND rasterDataTable = '' || quote_literal(table);
-
- -- Drop data table if there is not any data table.
- EXECUTE ''DROP TABLE '' || quote_ident(schema) || ''.'' || quote_ident(table);
-
- -- % Clear data table from pgraster data table.
- -- EXECUTE "DELETE FROM " || quote_ident(schema) || "." || quote_ident(table);
- -- SELECT count(*) INTO counter FROM quote_ident(schema) || "." || quote_ident(table);
- -- IF we support multi-dataset share a same datatable, we need a different approach.
-
- RETURN ''TRUE'';
-
-END;
-'
-LANGUAGE 'plpgsql' VOLATILE STRICT; -- WITH (isstrict);
-
-
---DropRasterTableByID(<object_id>::INTEGER)
-CREATE OR REPLACE FUNCTION DropRasterTableByID(integer)
-RETURNS TEXT
-AS
-'
-DECLARE
- object_id alias for $1;
-
- schema name;
- table name;
-BEGIN
-
- -- Get Schema and Table from metadata table
- SELECT rasterSchema, rasterDataTable INTO schema,table FROM pgraster_metadata WHERE rasterObjectID=object_id;
-
- -- Clear metadata from pgraster metadata table.
- EXECUTE ''DELETE FROM pgraster_metadata WHERE rasterObjectID='' || object_id;
-
- -- Drop data table if there is not any data table.
- EXECUTE ''DROP TABLE '' || quote_ident(schema) || ''.'' || quote_ident(table);
-
- -- % Clear data table from pgraster data table.
- -- EXECUTE "DELETE FROM " || quote_ident(schema) || "." || quote_ident(table);
- -- SELECT count(*) INTO counter FROM quote_ident(schema) || "." || quote_ident(table);
- -- IF we support multi-dataset share a same datatable, we need a different approach.
-
- RETURN ''TRUE'';
-
-END;
-'
-LANGUAGE 'plpgsql' VOLATILE STRICT; -- WITH (isstrict);
-
-
---UpdateMetadataByID(<object_id>::INTEGER, <attr_name>::varchar, <attr_value>::varchar);
-CREATE OR REPLACE FUNCTION UpdateMetadataByID(integer,varchar,varchar)
-RETURNS TEXT
-AS
-'
-DECLARE
-
- object_id alias for $1;
- attr_name alias for $2;
- attr_value alias for $3;
-
- count2 INTEGER;
-
-BEGIN
-
- SELECT count(*) INTO count2 FROM pgraster_metadata WHERE rasterObjectID=object_id;
-
- IF (count2 = 0) THEN
- RAISE EXCEPTION ''No record in pgraster_metadata with id=%'', object_id;
- ELSE
- EXECUTE "UPDATE pgraster_metadata SET " || quote_ident(attr_name) || ''='' || quote_literal(attr_value) || '' WHERE rasterObjectID='' || object_id;
-
- END IF;
-
-EXCEPTION
-
- WHEN UNDEFINED_COLUMN THEN
- RAISE EXCEPTION ''Undefined column % in table pgraster_metadata'', attr_name;
-
-END;
-'
-LANGUAGE 'plpgsql' VOLATILE STRICT; -- WITH (isstrict);
-
-
---UpdateMetadataByName(<schema>, <table>, <attr_name>::varchar, <attr_name>::varchar);
-CREATE OR REPLACE FUNCTION UpdateMetadataByName(varchar,varchar,varchar,varchar)
-RETURNS TEXT
-AS
-'
-DECLARE
-
- schema alias for $1;
- table alias for $2;
- attr_name alias for $3;
- attr_value alias for $4;
-
- count2 INTEGER;
-
-BEGIN
-
-
- SELECT count(*) INTO count2 FROM pgraster_metadata WHERE rasterSchema=quote_literal(schema) AND rasterDataTable=quote_literal(table);
-
- IF (count2 = 0) THEN
- RAISE NOTICE ''No record in pgraster_metadata with schema=% and table=%.'', schema,table;
- ELSE
- EXECUTE ''UPDATE pgraster_metadata SET '' || quote_ident(attr_name) || ''='' || quote_literal(attr_value) || '' WHERE rasterSchema='' || quote_literal(schema) || '' AND rasterDataTable='' || quote_literal(table);
- END IF;
-
-EXCEPTION
-
- WHEN UNDEFINED_COLUMN THEN
- RAISE EXCEPTION ''Undefined column % in table pgraster_metadata'',attr_name;
-
-END;
-'
-LANGUAGE 'plpgsql' VOLATILE STRICT; -- WITH (isstrict);
Added: branches/gSoC2007_raster/pgraster/pgraster_tables.sql
===================================================================
--- branches/gSoC2007_raster/pgraster/pgraster_tables.sql 2007-08-01 22:17:04 UTC (rev 2673)
+++ branches/gSoC2007_raster/pgraster/pgraster_tables.sql 2007-08-02 08:07:27 UTC (rev 2674)
@@ -0,0 +1,730 @@
+-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+--
+-- $Id: PGRASTER_TABLES.sql 2007-07-15 Xing Lin $
+--
+-- PostGIS - Spatial Types for PostgreSQL
+-- http://postgis.refractions.net
+-- Copyright 2001-2003 Refractions Research Inc.
+--
+-- This is free software; you can redistribute and/or modify it under
+-- the terms of the GNU General Public Licence. See the COPYING file.
+--
+-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+--
+-- INFORMATION ABOUT PGRASTER:
+--
+-- PGRaster is a component of PostGIS that handels the management of
+-- raster datasets in PostgreSQL/PostGIS spatial database. Currently,
+-- this extension is still under designing and development. You can
+-- download it from SVN site of PostGIS for a preview, or you can install
+-- it with PostGIS after it is publicly released.
+--
+-- PGRaster is also serviced as one of Gooogle SoC 2007 projects.
+--
+-- Any advices or bugs reports, please write to Xing at solo.lin at gmail.com
+--
+-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+--
+-- WARNING: Any change in this file must be evaluated for compatibility.
+-- Changes cleanly handled by lwpostgis_uptrade.sql are fine,
+-- other changes will require a bump in Major version.
+-- Currently only function replaceble by CREATE OR REPLACE
+-- are cleanly handled.
+--
+-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+-- ABOUT THIS FILE --
+-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+-- NAME: PGRASTER_TABLES.SQL
+-- AUTHOR: XING LIN
+-- CREATED_DATE: 2007-07-15
+-- LAST_UPDATED: 2007-07-15
+-- PURPORSE: This SQL file include the definition of the metadata table for
+-- PGRaster extension. You can use this file as an template to
+-- create your own metadata table for your raster database in
+-- PostgreSQL/PostGIS.
+-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+-- CHANGE LOG --
+-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+-- 1) 2007-07-15 File created by Xing Lin. (Xing)
+-- 2) 2007-07-22 Add "SCHEMA" column to PGRASTER_METADATA table. (Xing)
+-- 3) 2007-07-30 Add several functions and triggers to help create/modify/
+-- remove pgraster dataset in PGRASTER database. (Xing)
+-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+--DEFINITION OF TYPE INFORMATION
+-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+
+--BAND TYPE CODES & NAMES
+CREATE TABLE PGRASTER_TYPES_BANDTYPE
+(
+ typeID SMALLINT,
+ typeName TEXT,
+ descriptioin TEXT,
+ PRIMARY KEY (typeID)
+);
+INSERT INTO PGRASTER_TYPES_BANDTYPE VALUES(-1,'BT_UNKNOWN','Unknown Band Type');
+INSERT INTO PGRASTER_TYPES_BANDTYPE VALUES(0,'BT_SINGLE','Single Band Raster Dataset');
+INSERT INTO PGRASTER_TYPES_BANDTYPE VALUES(1,'BT_RGB','RGB Image Raster Dataset');
+INSERT INTO PGRASTER_TYPES_BANDTYPE VALUES(2,'BT_RGBA','RGBA Image Raster Dataset');
+INSERT INTO PGRASTER_TYPES_BANDTYPE VALUES(3,'BT_MULTI','Multi-bands Raster Dataset');
+INSERT INTO PGRASTER_TYPES_BANDTYPE VALUES(4,'BT_OTHERS','Other Image Raster Dataset');
+
+--DATA TYPE CODES & NAMES
+CREATE TABLE PGRASTER_TYPES_DATATYPE
+(
+ typeID SMALLINT,
+ typeName TEXT,
+ descriptioin TEXT,
+ PRIMARY KEY (typeID)
+);
+INSERT INTO PGRASTER_TYPES_DATATYPE VALUES(-1,'DT_UNKNOWN','Unkown Data Type');
+INSERT INTO PGRASTER_TYPES_DATATYPE VALUES(0,'DT_1BIT','A Boolean Data Unit having A Cell Depth of 1 Bits');
+INSERT INTO PGRASTER_TYPES_DATATYPE VALUES(1,'DT_2BIT','2 Bits Unsigned Integer having A Cell Depth of 2 Bits');
+INSERT INTO PGRASTER_TYPES_DATATYPE VALUES(2,'DT_4BIT','4 Bits Unsigned Integer having A Cell Depth of 4 Bits');
+INSERT INTO PGRASTER_TYPES_DATATYPE VALUES(3,'DT_8BIT_U','8 Bits Unsigned Integer having A Cell Depth of 8 Bits');
+INSERT INTO PGRASTER_TYPES_DATATYPE VALUES(4,'DT_8BIT_S','8 Bits Signed Integer having A Cell Depth of 8 Bits');
+INSERT INTO PGRASTER_TYPES_DATATYPE VALUES(5,'DT_16BIT_U','16 Bits Unsigned Integer having A Cell Depth of 16 Bits');
+INSERT INTO PGRASTER_TYPES_DATATYPE VALUES(6,'DT_16BIT_S','16 Bits Signed Integer having A Cell Depth of 16 Bits');
+INSERT INTO PGRASTER_TYPES_DATATYPE VALUES(7,'DT_32BIT_U','32 Bits Unsigned Integer having A Cell Depth of 32 Bits');
+INSERT INTO PGRASTER_TYPES_DATATYPE VALUES(8,'DT_32BIT_S','32 Bits Signed Integer having A Cell Depth of 32 Bits');
+INSERT INTO PGRASTER_TYPES_DATATYPE VALUES(9,'DT_24BIT_RGB','24 Bits RGB data having A Cell Depth of 24 Bits');
+INSERT INTO PGRASTER_TYPES_DATATYPE VALUES(10,'DT_32BIT_RGBA','32 Bits RGBA data having A Cell Depth of 32 Bits');
+INSERT INTO PGRASTER_TYPES_DATATYPE VALUES(11,'DT_32BIT_REAL','32 Bits Real/Single Floating data having A Cell Depth of 32 Bits');
+INSERT INTO PGRASTER_TYPES_DATATYPE VALUES(12,'DT_64BIT_REAL','64 Bits Real/Double Floating data having A Cell Depth of 64 Bits');
+
+--VALUE TYPE CODES & NAMES
+CREATE TABLE PGRASTER_TYPES_VALUETYPE
+(
+ typeID SMALLINT,
+ typeName TEXT,
+ descriptioin TEXT,
+ PRIMARY KEY (typeID)
+);
+INSERT INTO PGRASTER_TYPES_VALUETYPE VALUES(-1, 'VT_UNKNOWN','Unknown Value Type');
+INSERT INTO PGRASTER_TYPES_VALUETYPE VALUES(0, 'VT_NOMINAL','Nominal Value Type');
+INSERT INTO PGRASTER_TYPES_VALUETYPE VALUES(1, 'VT_ORDINAL','Ordinal Value Type');
+INSERT INTO PGRASTER_TYPES_VALUETYPE VALUES(2, 'VT_INTERVAL','Interval Value Type');
+INSERT INTO PGRASTER_TYPES_VALUETYPE VALUES(3, 'VT_RATIO','Ratio Value Type');
+INSERT INTO PGRASTER_TYPES_VALUETYPE VALUES(4, 'VT_IMAGE','Image Value Type');
+
+--BANDINTERLEAVING CODES & NAMES
+CREATE TABLE PGRASTER_TYPES_BANDINTERLEAVINGTYPE
+(
+ typeID SMALLINT NOT NULL,
+ typeName TEXT NOT NULL,
+ descriptioin TEXT NOT NULL,
+ PRIMARY KEY (typeID)
+);
+INSERT INTO PGRASTER_TYPES_BANDINTERLEAVINGTYPE VALUES(-1, 'BI_UNKNOWN','Unknown Band Interleaving Type');
+INSERT INTO PGRASTER_TYPES_BANDINTERLEAVINGTYPE VALUES(0, 'BI_BSQ','BSQ Band Interleaving Type');
+INSERT INTO PGRASTER_TYPES_BANDINTERLEAVINGTYPE VALUES(1, 'BI_BIL','BIL Band Interleaving Type');
+INSERT INTO PGRASTER_TYPES_BANDINTERLEAVINGTYPE VALUES(2, 'BI_BIP','BIP Band Interleaving Type');
+
+--COMPRESSION CODES & NAMES
+CREATE TABLE PGRASTER_TYPES_COMPRESSIONTYPE
+(
+ typeID SMALLINT NOT NULL,
+ typeName TEXT NOT NULL,
+ descriptioin TEXT NOT NULL,
+ PRIMARY KEY (typeID)
+);
+INSERT INTO PGRASTER_TYPES_COMPRESSIONTYPE VALUES(-1, 'CT_UNKNOWN','Unknown Compression Type');
+INSERT INTO PGRASTER_TYPES_COMPRESSIONTYPE VALUES(0, 'CT_JPEG_B','JPEG-B Compression Type');
+INSERT INTO PGRASTER_TYPES_COMPRESSIONTYPE VALUES(1, 'CT_JPEG_F','JPEG-F Compression Type');
+INSERT INTO PGRASTER_TYPES_COMPRESSIONTYPE VALUES(2, 'CT_LZW','LZW/LZ77 Compression Type');
+INSERT INTO PGRASTER_TYPES_COMPRESSIONTYPE VALUES(3, 'CT_NONE','UNCOMPRESSION/PLAIN Compression Type');
+
+-- DEFINITION OF PGRASTER_METADATA
+
+CREATE TABLE PGRASTER_METADATA
+(
+ rasterObjectID BIGSERIAL PRIMARY KEY,
+ name TEXT NOT NULL,
+ captureDate DATE NOT NULL DEFAULT NOW(),
+ rasterDimensions SMALLINT NOT NULL DEFAULT 0,
+ rasterBandType SMALLINT NOT NULL DEFAULT -1 REFERENCES pgraster_types_bandtype (typeid),
+ rasterDataType SMALLINT NOT NULL DEFAULT -1 REFERENCES pgraster_types_datatype (typeid),
+ rasterValueType SMALLINT NOT NULL DEFAULT -1 REFERENCES pgraster_types_valuetype (typeid),
+ rasterSchema TEXT NOT NULL DEFAULT 'PUBLIC',
+ rasterDataTable TEXT NOT NULL DEFAULT 'PGRASTER_DATA',
+ rasterBandCount INTEGER NOT NULL DEFAULT 0,
+ rasterRowCount INTEGER NOT NULL DEFAULT 0,
+ rasterColumnCount INTEGER NOT NULL DEFAULT 0,
+ rasterCellDepth INTEGER NOT NULL DEFAULT 0,
+ rasterPyramidEnabled BOOLEAN NOT NULL DEFAULT TRUE,
+ rasterPyramidDepth INTEGER NOT NULL DEFAULT 0,
+ blockSizeBands INTEGER NOT NULL DEFAULT -1, -- (-1: means all bands included)
+ blockSizeRows INTEGER NOT NULL DEFAULT 256,
+ blockSizeColumns INTEGER NOT NULL DEFAULT 256,
+ blockPadding BOOLEAN NOT NULL DEFAULT TRUE,
+ blockBandInterleaving SMALLINT NOT NULL DEFAULT 0, -- (0: BSQ Band Interleaving Approach)
+ blockCompression SMALLINT NOT NULL DEFAULT 3 REFERENCES pgraster_types_compressiontype (typeid), -- (3: CT_LZW)
+ blockQuality INTEGER NOT NULL DEFAULT 100,
+ nodataValue NUMERIC NOT NULL DEFAULT -1,
+ SRID INTEGER NOT NULL DEFAULT -1, -- (-1: means no spatial reference attached)
+ geoReferenced BOOLEAN NOT NULL DEFAULT FALSE,
+ spatialExtent GEOMETRY DEFAULT NULL
+
+ -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+ -- Foreign Key and Other Constraints should be added Here
+ -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+);
+
+
+--DEFINITION OF PGRASTER_DATA
+
+CREATE TABLE PGRASTER_DATA
+(
+ rasterObjectID INTEGER REFERENCES pgraster_metadata (rasterObjectID),
+ pyramidLevel INTEGER NOT NULL DEFAULT -1,
+ bandBlockNumber INTEGER NOT NULL DEFAULT -1,
+ rowBlockNumber INTEGER NOT NULL DEFAULT -1,
+ columnBlockNumber INTEGER NOT NULL DEFAULT -1,
+ blockBandSize INTEGER NOT NULL DEFAULT -1, --(-1: means ALL bands included.)
+ blockRowSize INTEGER NOT NULL DEFAULT 256,
+ blockColumnSize INTEGER NOT NULL DEFAULT 256,
+ blockMBR GEOMETRY NOT NULL,
+ dataBlock BYTEA NOT NULL,
+
+ -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+ -- Primary Key Constraints should be added Here
+ -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+ PRIMARY KEY (rasterObjectID,pyramidLevel,bandBlockNumber,rowBlockNumber,columnBlockNumber)
+
+ -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+ -- Foreign Key And Other Constraints should be added Here
+ -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+);
+
+-- CREATE GiST-R Tree index on PGRASTER_DATA (blockMBR)
+CREATE INDEX index_pgraster_data ON pgraster_data using GIST ( blockMBR GIST_GEOMETRY_OPS);
+
+--DEFINITION OF PGRASTER_SRS
+CREATE TABLE PGRASTER_SRS
+(
+ rasterObjectID INTEGER REFERENCES pgraster_metadata (rasterObjectID),
+ isReferenced BOOLEAN NOT NULL DEFAULT FALSE,
+ isOrthoRectified BOOLEAN NOT NULL DEFAULT FALSE,
+ SRID INTEGER NOT NULL DEFAULT -1,
+ spatialResolutionX NUMERIC NOT NULL DEFAULT 0.0,
+ spatialResolutionY NUMERIC NOT NULL DEFAULT 0.0,
+ spatialResolutionZ NUMERIC NOT NULL DEFAULT 0.0,
+ spatialTolerance NUMERIC NOT NULL DEFAULT 0.0,
+ coordLocation SMALLINT NOT NULL DEFAULT 0,
+ rowOff NUMERIC NOT NULL DEFAULT 0,
+ columnOff NUMERIC NOT NULL DEFAULT 0,
+ heightOff NUMERIC NOT NULL DEFAULT 0,
+ xOff NUMERIC NOT NULL DEFAULT 0,
+ yOff NUMERIC NOT NULL DEFAULT 0,
+ zOff NUMERIC NOT NULL DEFAULT 0,
+ rowScale NUMERIC NOT NULL DEFAULT 1,
+ columnScale NUMERIC NOT NULL DEFAULT 1,
+ heightScale NUMERIC NOT NULL DEFAULT 1,
+ xScale NUMERIC NOT NULL DEFAULT 1,
+ yScale NUMERIC NOT NULL DEFAULT 1,
+ zScale NUMERIC NOT NULL DEFAULT 1,
+ columnRMS NUMERIC NOT NULL DEFAULT 0.0,
+ totalRMS NUMERIC NOT NULL DEFAULT 0.0,
+ -- parameters for afline transformation from cell coordinates systems to ground / local coordinates
+ rowNumberator NUMERIC[] DEFAULT NULL,
+ rowDenominator NUMERIC[] DEFAULT NULL,
+ columnNumerator NUMERIC[] DEFAULT NULL,
+ columnDenominator NUMERIC[] DEFAULT NULL,
+
+ -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+ -- Primary Key Constraints should be added Here
+ -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+ PRIMARY KEY (rasterObjectID)
+
+ -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+ -- Foreign Key And Other Constraints should be added Here
+ -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+
+);
+
+--DEFINITION OF PGRASTER_GCPS
+CREATE TABLE PGRASTER_GCPS
+(
+ rasterObjectID INTEGER REFERENCES pgraster_metadata (rasterObjectID),
+ gcpID BIGSERIAL,
+ cellColumn NUMERIC NOT NULL DEFAULT 0.0,
+ cellRow NUMERIC NOT NULL DEFAULT 0.0,
+ cellHeight NUMERIC NOT NULL DEFAULT 0.0,
+ groundX NUMERIC NOT NULL DEFAULT 0.0,
+ groundY NUMERIC NOT NULL DEFAULT 0.0,
+ groundZ NUMERIC NOT NULL DEFAULT 0.0,
+ rmsError NUMERIC NOT NULL DEFAULT 0.0,
+ -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+ -- Primary Key Constraints should be added Here
+ -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+ PRIMARY KEY (rasterObjectID,gcpID)
+);
+
+
+--DEFINITIOF OF PGRASTER_VATS
+CREATE TABLE PGRASTER_VATS
+(
+ rasterObjectID INTEGER REFERENCES pgraster_metadata(rasterObjectID),
+ bandID INTEGER NOT NULL DEFAULT 0,
+ value NUMERIC NOT NULL,
+ attribute TEXT NOT NULL DEFAULT '',
+ -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+ -- Primary Key Constraints should be added Here
+ -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+ PRIMARY KEY (rasterObjectID,bandID,value)
+);
+
+--DEFINITION OF PGRASTER_STATITICS
+CREATE TABLE PGRASTER_STATISTICS
+(
+ rasterObjectID INTEGER REFERENCES pgraster_metadata(rasterObjectID),
+ bandID INTEGER NOT NULL DEFAULT 0,
+ maxValue NUMERIC NOT NULL DEFAULT 0,
+ minValue NUMERIC NOT NULL DEFAULT 0,
+ avgValue NUMERIC NOT NULL DEFAULT 0,
+ modeValue NUMERIC NOT NULL DEFAULT 0,
+ stdValue NUMERIC NOT NULL DEFAULT 0,
+ lastUpdate TIMESTAMP DEFAULT NOW(),
+ -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+ -- Primary Key Constraints should be added Here
+ -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+ PRIMARY KEY (rasterObjectID,bandID)
+);
+
+--DEFINITION OF PGRASTER_HISTOGRAM
+CREATE TABLE PGRASTER_HISTOGRAM
+(
+ rasterObjectID INTEGER REFERENCES pgraster_metadata(rasterObjectID),
+ bandID INTEGER NOT NULL DEFAULT 0,
+ value NUMERIC NOT NULL DEFAULT 0,
+ count NUMERIC NOT NULL DEFAULT 0,
+ -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+ -- Primary Key Constraints should be added Here
+ -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+ PRIMARY KEY (rasterObjectID,bandID,value)
+);
+
+
+--DEFINITION OF PGRASTER_UTILITIES_PALETTE
+CREATE TABLE PGRASTER_UTILS_PALETTE
+(
+ id SERIAL,
+ name TEXT NOT NULL,
+ description TEXT NOT NULL DEFAULT '',
+ colorNumber INTEGER NOT NULL,
+ colorDepth INTEGER NOT NULL, -- RGB=24, RGBA=32
+ colorData BYTEA NOT NULL,
+ PRIMARY KEY (id)
+
+ -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+ -- Foreign Key And Other Constraints should be added Here
+ -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+
+);
+
+---------------------------------------------------------------------------
+-----Add Functions for PGRaster to add/remove/alter pgraster datasets------
+---------------------------------------------------------------------------
+
+-- PGRASTER_VERSION() Function
+CREATE OR REPLACE FUNCTION pgraster_version() RETURNS TEXT AS
+$$
+ SELECT '0.0.1'
+$$
+LANGUAGE SQL IMMUTABLE STRICT;
+
+
+-- AddRasterTableFull(<schema>, <table>, <xsize>, <ysize>, <bands>,<rows>,<columns>,<data_type>,<band_type>,<value_type>,<bil_type>,<compress_type>,<srid>)
+-- Other Missing Parameter will using recommened or default values.
+-- This function will also update the Spatial Referenced System(SRS) information.
+CREATE OR REPLACE FUNCTION AddRasterTableFull(varchar, varchar, numeric, numeric, integer, integer,integer,integer, integer,integer,integer,integer, integer)
+RETURNS TEXT
+AS
+'
+DECLARE
+ schema alias for $1;
+ table alias for $2;
+ xsize alias for $3;
+ ysize alias for $4;
+ bands alias for $5;
+ rows alias for $6;
+ columns alias for $7;
+ data_type alias for $8;
+ band_type alias for $9;
+ value_type alias for $10;
+ bil_type alias for $11;
+ compress_type alias for $12;
+ new_srid alias for $13;
+
+ dimension INTEGER;
+ rec RECORD;
+ schema_ok BOOL;
+ table_exist BOOL;
+ cell_depth INTEGER;
+ geo_referenced BOOL;
+ real_schema NAME;
+ object_name NAME;
+ object_id INTEGER;
+ object_oldid INTEGER;
+
+BEGIN
+
+ -- Check if table name is empty
+ IF (table = '''') THEN
+ RAISE EXCEPTION ''Table name could not be empty. Please try another again!'';
+ return ''fail'';
+ END IF;
+
+ -- By default, only the 2-dimensional raster model is supported.
+ dimension := 2;
+
+ --Check Type ID Valid?
+ IF ( ( NOT ( data_type>=-1 AND data_type <=12 ) )
+ OR (NOT (band_type >=-1 AND band_type <=4 ) )
+ OR (NOT (value_type >=-1 AND value_type <=4 ) )
+ OR (NOT (bil_type >=-1 AND bil_type<=2 ) )
+ OR (NOT (compress_type>= -1 AND compress_type <= 3))
+ )
+ THEN
+ RAISE EXCEPTION ''Invalid type ids - valid ones are:
+ DATA_TYPE=[-1,11]; BAND_TYPE=[-1,4];VALUE_TYPE=[-1,4];
+ BANDINTERLEAVING_TYPE=[-1,2];COMPRESSION_TYPE=[-1,3];
+ '';
+ return ''fail'';
+ END IF;
+
+ --Check dimension number
+ IF ( NOT(dimension=2)) THEN
+ RAISE EXCEPTION ''Invalid dimension. Only 2 dimensions is supported currently.'';
+ return ''fail'';
+ END IF;
+
+ --Check and replace as an valid schema name
+ IF ( NOT( schema='''') ) THEN
+ schema_ok := ''f'';
+ FOR rec IN SELECT nspname FROM pg_namespace WHERE text(nspname) = schema LOOP
+ schema_ok := ''t'';
+ END LOOP;
+
+ if ( NOT(schema_ok =''t'') ) THEN
+ RAISE NOTICE ''Invalid schema name - using current_schema()'';
+ SELECT current_schema() into real_schema;
+ ELSE
+ real_schema := schema;
+ END IF;
+ ELSE
+ --Using current_schema for empty schema.
+ SELECT current_schema() into real_schema;
+ END IF;
+
+
+ -- Check if current data table exist. If NOT, create a new one from PGRASTER_DATA.
+ table_exist := ''f'';
+
+ FOR rec IN SELECT tablename FROM pg_tables WHERE text(schemaname) = real_schema AND lower(text(tablename))=lower(table) LOOP
+ table_exist := ''t'';
+ END LOOP;
+
+ IF ( table_exist = ''f'') THEN
+ -- CREATE A NEW PGRASTER_DATA table
+ EXECUTE ''CREATE TABLE '' || quote_ident(real_schema) || ''.'' || quote_ident(table)
+ || ''(LIKE public.pgraster_data)'' ;
+
+ ELSE
+ --It is not recommended to put several dataset into one data table,
+ --Keep the table but clear all the data in existing tables
+ --Or shoud we pop up a message telling that the table exists already.
+ EXECUTE '' DELETE FROM '' || quote_ident(real_schema) || ''.'' || quote_ident(table);
+ END IF;
+
+ -- Calculate celldepth from data type
+ cell_depth := 0;
+ IF (data_type = -1) THEN
+ cell_depth := 0;
+ ELSE
+ IF (data_type = 0) THEN
+ cell_depth := 1;
+ ELSE
+ IF (data_type = 1) THEN
+ cell_depth := 2;
+ ELSE
+ IF (data_type = 2) THEN
+ cell_depth := 4;
+ ELSE
+ IF (data_type=3 OR data_type=4) THEN
+ cell_depth := 8;
+ ELSE
+ IF (data_type=5 OR data_type=6) THEN
+ cell_depth := 16;
+ ELSE
+ IF (data_type=7 OR data_type=8 OR data_type=10) THEN
+ cell_depth := 32;
+ ELSE
+ IF (data_type=9) THEn
+ cell_depth := 24;
+ ELSE
+ cell_depth :=64;
+ END IF;
+ END IF;
+ END IF;
+ END IF;
+ END IF;
+ END IF;
+ END IF;
+ END IF;
+
+
+ -- Delete stale record in pgraster_metadata(if any)
+ object_name := real_schema || ''.'' || table;
+
+ --Retrieve OLD object id from pgraster_metadata table
+ SELECT rasterObjectID INTO object_oldid FROM pgraster_metadata WHERE name=object_name;
+ EXECUTE ''DELETE FROM PGRASTER_METADATA WHERE
+ rasterSchema = '' || quote_literal(real_schema) ||'' AND rasterDataTable = '' || quote_literal(table);
+
+ -- Check Information in other data table (SRS)
+ geo_referenced := ''f'';
+ IF (NOT(new_srid=-1)) THEN
+ geo_referenced := ''t'';
+ END IF;
+
+ -- Add record in pgraster_metadata table
+ EXECUTE ''INSERT INTO public.pgraster_metadata(name,rasterDimensions,rasterBandType,rasterDataType,rasterValueType ,rasterSchema,rasterDataTable,rasterBandCount,rasterRowCount,rasterColumnCount,rasterCellDepth,blockBandInterleaving ,blockCompression,SRID,geoReferenced) VALUES ( '' ||
+ quote_literal(object_name) || '','' ||
+ dimension || '','' ||
+ band_type || '','' ||
+ data_type || '','' ||
+ value_type || '','' ||
+ quote_literal(real_schema) || '','' ||
+ quote_literal(table) || '','' ||
+ bands || '','' ||
+ rows || '','' ||
+ columns || '','' ||
+ cell_depth || '','' ||
+ bil_type || '','' ||
+ compress_type || '','' ||
+ new_srid || '','' ||
+ quote_literal(geo_referenced) || '')'';
+
+ --Retrieve Newly create object id from pgraster_metadata table
+ SELECT rasterObjectID INTO object_id FROM pgraster_metadata WHERE name=object_name;
+
+ --Update SRS informatio in pgraster_srs
+ IF ( object_oldid IS NOT NULL) THEN
+ EXECUTE ''DELETE FROM pgraster_srs WHERE rasterObjectID = '' || object_id || '' OR rasterObjectID = '' || object_oldid;
+ ELSE
+ EXECUTE ''DELETE FROM pgraster_srs WHERE rasterObjectID = '' || object_id;
+ END IF;
+
+ EXECUTE ''INSERT INTO public.pgraster_srs(rasterObjectID,isReferenced,SRID,spatialResolutionX,spatialResolutionY) values('' || object_id || '','' || quote_literal(geo_referenced) || '','' || new_srid || '','' || xsize || '','' || ysize || '')'';
+
+ --return results
+ RETURN
+ real_schema || ''.'' || table ;
+END;
+'
+LANGUAGE 'plpgsql' VOLATILE STRICT; -- WITH (isstrict);
+
+
+-- AddRasterTableCompact(<schema>, <table>, <xsize>, <ysize>, <bands>,<rows>,<columns>,<data_type>,<storage_type>,<srid>)
+CREATE OR REPLACE FUNCTION AddRasterTableCompact(varchar, varchar, numeric, numeric, integer, integer, integer, integer, integer,integer)
+RETURNS TEXT
+AS
+'
+DECLARE
+ schema alias for $1;
+ table alias for $2;
+ xsize alias for $3;
+ ysize alias for $4;
+ bands alias for $5;
+ rows alias for $6;
+ columns alias for $7;
+ data_type alias for $8;
+ bil_type alias for $9;
+ new_srid alias for $10;
+
+ band_type INTEGER;
+ value_type INTEGER;
+ compress_type INTEGER;
+
+BEGIN
+
+ -- Using recommended value for missing parameters.
+ band_type := 3; -- BT_MULTISPECTRAL
+ value_type := 2; -- VT_NOMINAL
+ compress_type := 2; -- CT_LZW
+
+ RETURN AddRasterTableFull(schema, table, xsize, ysize, bands,rows, columns, data_type, band_type, value_type, bil_type, compress_type, new_srid );
+
+END;
+'
+LANGUAGE 'plpgsql' VOLATILE STRICT; -- WITH (isstrict);
+
+
+-- AddRasterTable(<schema>, <table>, <xsize>, <ysize>, <bands>, <storage_type>,<srid>)
+CREATE OR REPLACE FUNCTION AddRasterTable(varchar, varchar, numeric, numeric, integer, integer,integer)
+RETURNS TEXT
+AS
+'
+DECLARE
+ schema alias for $1;
+ table alias for $2;
+ xsize alias for $3;
+ ysize alias for $4;
+ bands alias for $5;
+ bil_type alias for $6;
+ new_srid alias for $7;
+
+ data_type INTEGER;
+ columns INTEGER;
+ rows INTEGER;
+ band_type INTEGER;
+ value_type INTEGER;
+ dimension_type INTEGER;
+ compress_type INTEGER;
+
+BEGIN
+
+ -- Using default (not-real) value for missing parameters.
+ data_type := 3; -- DT_8BIT_U
+ columns := 0; -- O Columns
+ rows := 0; -- 0 Rows
+
+ -- Using recommended value for missing parameters.
+ band_type := 3; -- BT_MULTISPECTRAL
+ value_type := 2; -- VT_NOMINAL
+ compress_type := 2; -- CT_LZW
+
+ RETURN AddRasterTableFull(schema, table, xsize, ysize, bands,rows, columns, data_type, band_type, value_type, bil_type, compress_type, new_srid );
+
+END;
+'
+LANGUAGE 'plpgsql' VOLATILE STRICT; -- WITH (isstrict);
+
+--DropRasterTableByName(<schema>,<table>)
+CREATE OR REPLACE FUNCTION DropRasterTableByName(varchar, varchar)
+RETURNS TEXT
+AS
+'
+DECLARE
+ schema alias for $1;
+ table alias for $2;
+BEGIN
+
+ -- Clear metadata from pgraster metadata table.
+ EXECUTE ''DELETE FROM pgraster_metadata WHERE rasterSchema='' || quote_literal(schema) || '' AND rasterDataTable = '' || quote_literal(table);
+
+ -- Drop data table if there is not any data table.
+ EXECUTE ''DROP TABLE '' || quote_ident(schema) || ''.'' || quote_ident(table);
+
+ -- % Clear data table from pgraster data table.
+ -- EXECUTE "DELETE FROM " || quote_ident(schema) || "." || quote_ident(table);
+ -- SELECT count(*) INTO counter FROM quote_ident(schema) || "." || quote_ident(table);
+ -- IF we support multi-dataset share a same datatable, we need a different approach.
+
+ RETURN ''TRUE'';
+
+END;
+'
+LANGUAGE 'plpgsql' VOLATILE STRICT; -- WITH (isstrict);
+
+
+--DropRasterTableByID(<object_id>::INTEGER)
+CREATE OR REPLACE FUNCTION DropRasterTableByID(integer)
+RETURNS TEXT
+AS
+'
+DECLARE
+ object_id alias for $1;
+
+ schema name;
+ table name;
+BEGIN
+
+ -- Get Schema and Table from metadata table
+ SELECT rasterSchema, rasterDataTable INTO schema,table FROM pgraster_metadata WHERE rasterObjectID=object_id;
+
+ -- Clear metadata from pgraster metadata table.
+ EXECUTE ''DELETE FROM pgraster_metadata WHERE rasterObjectID='' || object_id;
+
+ -- Drop data table if there is not any data table.
+ EXECUTE ''DROP TABLE '' || quote_ident(schema) || ''.'' || quote_ident(table);
+
+ -- % Clear data table from pgraster data table.
+ -- EXECUTE "DELETE FROM " || quote_ident(schema) || "." || quote_ident(table);
+ -- SELECT count(*) INTO counter FROM quote_ident(schema) || "." || quote_ident(table);
+ -- IF we support multi-dataset share a same datatable, we need a different approach.
+
+ RETURN ''TRUE'';
+
+END;
+'
+LANGUAGE 'plpgsql' VOLATILE STRICT; -- WITH (isstrict);
+
+
+--UpdateMetadataByID(<object_id>::INTEGER, <attr_name>::varchar, <attr_value>::varchar);
+CREATE OR REPLACE FUNCTION UpdateMetadataByID(integer,varchar,varchar)
+RETURNS TEXT
+AS
+'
+DECLARE
+
+ object_id alias for $1;
+ attr_name alias for $2;
+ attr_value alias for $3;
+
+ count2 INTEGER;
+
+BEGIN
+
+ SELECT count(*) INTO count2 FROM pgraster_metadata WHERE rasterObjectID=object_id;
+
+ IF (count2 = 0) THEN
+ RAISE EXCEPTION ''No record in pgraster_metadata with id=%'', object_id;
+ ELSE
+ EXECUTE "UPDATE pgraster_metadata SET " || quote_ident(attr_name) || ''='' || quote_literal(attr_value) || '' WHERE rasterObjectID='' || object_id;
+
+ END IF;
+
+EXCEPTION
+
+ WHEN UNDEFINED_COLUMN THEN
+ RAISE EXCEPTION ''Undefined column % in table pgraster_metadata'', attr_name;
+
+END;
+'
+LANGUAGE 'plpgsql' VOLATILE STRICT; -- WITH (isstrict);
+
+
+--UpdateMetadataByName(<schema>, <table>, <attr_name>::varchar, <attr_name>::varchar);
+CREATE OR REPLACE FUNCTION UpdateMetadataByName(varchar,varchar,varchar,varchar)
+RETURNS TEXT
+AS
+'
+DECLARE
+
+ schema alias for $1;
+ table alias for $2;
+ attr_name alias for $3;
+ attr_value alias for $4;
+
+ count2 INTEGER;
+
+BEGIN
+
+
+ SELECT count(*) INTO count2 FROM pgraster_metadata WHERE rasterSchema=quote_literal(schema) AND rasterDataTable=quote_literal(table);
+
+ IF (count2 = 0) THEN
+ RAISE NOTICE ''No record in pgraster_metadata with schema=% and table=%.'', schema,table;
+ ELSE
+ EXECUTE ''UPDATE pgraster_metadata SET '' || quote_ident(attr_name) || ''='' || quote_literal(attr_value) || '' WHERE rasterSchema='' || quote_literal(schema) || '' AND rasterDataTable='' || quote_literal(table);
+ END IF;
+
+EXCEPTION
+
+ WHEN UNDEFINED_COLUMN THEN
+ RAISE EXCEPTION ''Undefined column % in table pgraster_metadata'',attr_name;
+
+END;
+'
+LANGUAGE 'plpgsql' VOLATILE STRICT; -- WITH (isstrict);
More information about the postgis-commits
mailing list