Skip to content

Commit

Permalink
I updated dfo_add_conformance_band.sql because before it was calculat…
Browse files Browse the repository at this point in the history
…ing conformance for outside of the sectors.

correcting function to only calculate conformance for inside the sectors.
  • Loading branch information
miladmoradi89 authored Sep 14, 2021
1 parent 22ee49e commit 592edb4
Showing 1 changed file with 76 additions and 52 deletions.
128 changes: 76 additions & 52 deletions sql/wis/dfo_add_conformance_band.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
-- FUNCTION: public.dfo_add_conformance_band(text, text, text, numeric, text, text)

-- DROP FUNCTION public.dfo_add_conformance_band(text, text, text, numeric, text, text);

CREATE OR REPLACE FUNCTION public.dfo_add_conformance_band(
Expand All @@ -6,80 +8,102 @@ CREATE OR REPLACE FUNCTION public.dfo_add_conformance_band(
sectorgeom text,
resolution numeric,
filename text,
schema_ text default 'public')
schema_ text DEFAULT 'public'::text)
RETURNS text
LANGUAGE 'plpgsql'

COST 100
VOLATILE
AS $BODY$

DECLARE
state text;
nbands integer;
rastcol text;
sounding record;
sounding_for_id record;
num_intersection_sectors integer;
strsql text;
rtn text;
nb_rows integer;
i integer;
rastcol text;
BEGIN

--get the raster column name from catalog
--SELECT r_raster_column FROM raster_columns WHERE r_table_name = rastertable INTO rastcol;
strsql := concat('SELECT r_raster_column FROM raster_columns WHERE r_table_name =',quote_literal(rastertable),
' AND r_table_schema=',quote_literal(schema_));
RAISE notice 'sql = %', strsql;
' AND r_table_schema=', quote_literal(schema_));
--RAISE notice 'sql = %', strsql;

EXECUTE strsql INTO rastcol;

nb_rows := 0;
--raise notice 'raster column name : %', rastcol;

-- Create a temporary table being the clip of the raster on his intersecting geometries
-- of the sector table (containing the disign grade) for a particular resolution and filename.
strsql := concat( 'DROP TABLE IF EXISTS tmp_clip_to_conformance;
CREATE TEMPORARY TABLE tmp_clip_to_conformance AS
SELECT r.id,ST_Clip(',rastcol,', ',sectorgeom,' ,false) AS raster , maintained
FROM ',schema_,'.' ,rasterTable,' r JOIN ',schema_,'.',sectorTable,' s ON ST_Intersects(tile_extent , ',sectorgeom,')
WHERE filename LIKE ' , quote_literal(filename||'%'), ' AND resolution = ',resolution);
EXECUTE strsql;
EXECUTE 'SELECT count(*) FROM tmp_clip_to_conformance' INTO nb_rows;
RAISE notice 'Creat tmp table for sector table in %', strsql;
--RAISE LOG 'The DFO_ADD_CONFORMANCE_BAND() is called';
strsql := 'SELECT * FROM ' || schema_ ||'.' || rastertable ||' WHERE filename LIKE ' || quote_literal(filename||'%') || ' AND resolution = ' || resolution;
--RAISE LOG 'the sql is: %', strsql;
FOR sounding IN EXECUTE strsql
LOOP

--RAISE notice 'id = %', sounding.id;

--RAISE LOG ' The input parameters are : % and %', rastertable, filename;
-- if there is no intersection between the raster and any of the sectors, we continue with the loop

strsql := ' SELECT COUNT(dg.id) FROM wis.' || sectortable || ' dg, ' || schema_ ||'.' || rastertable ||
' rt where rt.id = '|| sounding.id || ' AND ST_intersects(tile_geom, geom_3979 )';
--RAISE LOG 'strsql = %', strsql;

--create a second temporary table calculating the "conformance" by map algebra.
DROP TABLE IF EXISTS tmp_conformance_algebra;
CREATE TEMPORARY TABLE tmp_conformance_algebra AS
SELECT id, ST_MapAlgebra(raster, 1, NULL, concat(maintained,'+[rast.val]')) AS raster
FROM tmp_clip_to_conformance;
EXECUTE strsql
INTO num_intersection_sectors;

--RAISE NOTICE 'numer of intersections with sectors: % ', num_intersection_sectors;


IF num_intersection_sectors = 0 THEN

strsql := 'UPDATE '|| schema_ ||'.' || rastertable ||' SET ' || rastcol || ' = ST_AddBand(ST_Band(' || rastcol || ' , ''{1,2,3}''::int[]), ' ||
'ST_AsRaster( ST_Envelope(' || rastcol || '), ' || rastcol || ', ''32BF'', 3.40282346638529e+38, 3.40282346638529e+38 ) , 1, 4) WHERE id = ' ||sounding.id;
--RAISE LOG 'strsql when there is no intersection = %', strsql;
EXECUTE strsql;
CONTINUE;

--create a third temporary table containing the union of all potentiel part of the same tile
--having clipped on several design grade.
DROP TABLE IF EXISTS tmp_conformance;
CREATE TEMPORARY TABLE tmp_conformance AS
SELECT id, ST_Union(raster) AS raster FROM tmp_conformance_algebra GROUP BY id;
END IF;


---- if the tile has intersection with at least one sector then:
DROP TABLE IF EXISTS table_for_doing_st_union_onsounding_and_conformance_1745;
strsql := 'CREATE TEMPORARY TABLE table_for_doing_st_union_onsounding_and_conformance_1745 AS( select ST_Union(ST_MapAlgebra(ST_Clip('||
rastcol || ' , 1, geom_3979, 3.40282346638529e+38, FALSE ), 1 , ''32BF'' , quote_literal(maintained) , 3.40282346638529e+38) ) rast FROM '||
schema_ ||'.' || rastertable || ' rt, ' ||' wis.' || sectortable || ' WHERE rt.id = '|| sounding.id ||
' AND ST_intersects(' || rastcol || ', geom_3979 ))';
--RAISE LOG 'strsql: % ', strsql;
EXECUTE strsql;

--Update the original raster by adding the band genarated in the tmp_conformance table.
strsql := concat('UPDATE ',schema_,'.',rasterTable,' o
SET ',rastcol,' = ST_AddBand(o.',rastcol,', co.raster , 1)
FROM tmp_conformance co WHERE co.id=o.id');
RAISE notice 'Update rasterTable with: %', strsql;
EXECUTE strsql;

-- inserting the clipped version of the original raster into the temp table to do ST_Union
-- at the end
-- band 1 is used which is minimum depth
strsql := 'INSERT INTO table_for_doing_st_union_onsounding_and_conformance_1745 SELECT ST_Union(ST_Clip(' || rastcol || ', 1, geom_3979, 3.40282346638529e+38, FALSE ))'
|| ' FROM wis.' || sectortable || ' , ' || schema_ ||'.' || rastertable || ' rt WHERE rt.id = ' || sounding.id
|| ' AND ST_intersects(' || rastcol || ', geom_3979 )' ;
--RAISE LOG 'strsql: % ', strsql;
EXECUTE strsql;


-- Doing the calculation for conformance
DROP TABLE IF EXISTS table_for_doing_st_union_onsounding_and_conformance_1745_union;
CREATE TEMPORARY TABLE table_for_doing_st_union_onsounding_and_conformance_1745_union
AS(
SELECT ST_Union(rast, 'SUM') rast from table_for_doing_st_union_onsounding_and_conformance_1745
);


-- updating the rastertable table
strsql := 'UPDATE ' || schema_ || '.' || rastertable || ' rt SET ' || rastcol || ' = ST_AddBand(ST_Band(rt.' || rastcol || ', ''{1,2,3}''::int[]), tmp.rast, 1, 4) '
|| 'FROM table_for_doing_st_union_onsounding_and_conformance_1745_union tmp WHERE id = ' || sounding.id;
--RAISE LOG 'strsql: % ', strsql;
EXECUTE strsql;

-- Add un null conformance band for tile that are not intersecting any zone
-- (possible for soundings witch excede the chanel or zones without design grades)
strsql = concat ('UPDATE ',schema_,'.' ,rasterTable,
' SET ',rastcol,' = ST_AddBand(',rastcol,', --destination raster.
ST_BandPixelType(',rastcol,',1), --pixel type
ST_BandNoDataValue(',rastcol,',1), -- assign nodata value to all pixels
ST_BandNoDataValue(',rastcol,',1)) -- assign nodata value to raster
WHERE filename LIKE ' , quote_literal(filename||'%'), '
AND resolution = ',resolution, ' AND ST_Numbands(',rastcol,')=3');
RAISE notice 'Update Band with: %', strsql;
EXECUTE strsql;

END LOOP;

RETURN 'Update conformance band for ' || nb_rows || ' rows.';
RETURN 'Run successfully';

EXCEPTION WHEN OTHERS THEN
RAISE EXCEPTION' % ', SQLERRM;
RAISE LOG 'an exception happened while running this file %', SQLERRM ;

END;
$BODY$;
Expand Down

0 comments on commit 592edb4

Please sign in to comment.