[postgis-commits] svn - r2673 - branches/gSoC2007_raster/pgraster

postgis-commits at postgis.refractions.net postgis-commits at postgis.refractions.net
Wed Aug 1 15:17:05 PDT 2007


Author: xingkth
Date: 2007-08-01 15:17:04 -0700 (Wed, 01 Aug 2007)
New Revision: 2673

Modified:
   branches/gSoC2007_raster/pgraster/pgraster_tables.sql
Log:
PgRaster: AddRasterTable/DropRasterTable/UpdateMetadataTable.

Modified: branches/gSoC2007_raster/pgraster/pgraster_tables.sql
===================================================================
--- branches/gSoC2007_raster/pgraster/pgraster_tables.sql	2007-07-31 20:40:04 UTC (rev 2672)
+++ branches/gSoC2007_raster/pgraster/pgraster_tables.sql	2007-08-01 22:17:04 UTC (rev 2673)
@@ -43,8 +43,13 @@
 -- 					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
 -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 
@@ -316,3 +321,410 @@
 
 );
 
+---------------------------------------------------------------------------
+-----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