CLC Join: ticket#56.sql

File ticket#56.sql, 15.6 KB (added by cedric, 15 years ago)
Line 
1-- extraction of a clc table for Britany
2DROP TABLE IF EXISTS clc.clc00_v2_Bretagne;
3CREATE TABLE clc.clc00_v2_Bretagne AS
4SELECT * FROM clc.clc00_v2_europe where gid IN (
5SELECT gid FROM  clc.clc00_v2_europe clc JOIN
6    (SELECT the_geom FROM france.region where code_reg='53') as sub
7    ON ST_Intersects(sub.the_geom,clc.the_geom));
8   
9ALTER TABLE clc.clc00_v2_Bretagne ADD CONSTRAINT c_pk_gid PRIMARY KEY  (gid);
10alter table clc.clc00_v2_Bretagne add CONSTRAINT enforce_dims_the_geom CHECK (ndims(the_geom) = 2);
11alter table clc.clc00_v2_Bretagne add  CONSTRAINT enforce_geotype_the_geom CHECK (geometrytype(the_geom) = 'MULTIPOLYGON'::text OR the_geom IS NULL);
12alter table clc.clc00_v2_Bretagne add  CONSTRAINT enforce_srid_the_geom CHECK (srid(the_geom) = 3035);
13CREATE INDEX indexclc00_v2_Bretagne ON clc.clc00_v2_Bretagne
14  USING GIST ( the_geom GIST_GEOMETRY_OPS ); 
15
16INSERT INTO geometry_columns(f_table_catalog, f_table_schema, f_table_name, f_geometry_column, coord_dimension, srid, "type")
17SELECT '', 'clc', 'clc00_v2_Bretagne', 'the_geom', ST_CoordDim(the_geom), ST_SRID(the_geom), GeometryType(the_geom)
18FROM clc.clc00_v2_europe LIMIT 1;
19
20
21-- Creating a table for riversegments Bretagne
22
23DROP TABLE IF EXISTS ccm21.riversegments_Bretagne;   
24CREATE TABLE  ccm21.riversegments_Bretagne AS
25    SELECT * FROM ccm21.riversegments
26        WHERE wso_id IN (SELECT wso_id FROM france.wso WHERE area='Bretagne');
27       
28ALTER TABLE ccm21.riversegments_Bretagne ADD CONSTRAINT c_pk_gid_riversegments_Bretagne PRIMARY KEY  (gid);
29
30INSERT INTO geometry_columns(f_table_catalog, f_table_schema, f_table_name, f_geometry_column, coord_dimension, srid, "type")
31SELECT '', 'ccm21', 'riversegments_Bretagne', 'the_geom', ST_CoordDim(the_geom), ST_SRID(the_geom), GeometryType(the_geom)
32FROM ccm21.riversegments_Bretagne LIMIT 1;
33
34-- creating a table for catchments Bretagne.
35DROP TABLE IF EXISTS ccm21.catchments_Bretagne;   
36CREATE TABLE  ccm21.catchments_Bretagne AS
37    SELECT * FROM ccm21.catchments
38        WHERE wso_id IN (SELECT wso_id FROM france.wso WHERE area='Bretagne'); 
39       
40ALTER TABLE ccm21.catchments_Bretagne ADD CONSTRAINT c_pk_gid_catchments_Bretagne PRIMARY KEY  (gid);
41
42INSERT INTO geometry_columns(f_table_catalog, f_table_schema, f_table_name, f_geometry_column, coord_dimension, srid, "type")
43SELECT '', 'ccm21', 'catchments_Bretagne', 'the_geom', ST_CoordDim(the_geom), ST_SRID(the_geom), GeometryType(the_geom)
44FROM ccm21.catchments_Bretagne LIMIT 1;
45
46
47-- Un essai sur la Bretagne pour voir
48
49
50-- la requete globale
51SELECT intersected.clcgid, intersected.gid, the_geom
52FROM (SELECT clc.gid as clcgid, c.gid, ST_Intersection(clc.the_geom, c.the_geom) AS the_geom
53        FROM  clc.clc00_v2_europe clc INNER JOIN ccm21.catchments c
54        ON  ST_Intersects (c.the_geom,clc.the_geom)
55        where substring(code_00 from 1 for 1)='1' )  AS intersected;           
56
57-- script de départ (modifié ensuite)
58CREATE TABLE clc.clipped_bretagne AS
59SELECT intersected.clcgid, intersected.gid, clipped_geom
60FROM (SELECT clc.gid as clcgid, c.gid, ST_Multi(ST_Intersection(clc.the_geom, c.the_geom)) clipped_geom
61        FROM  clc.clc00_v2_europe clc INNER JOIN ccm21.catchments c
62        ON  ST_Intersects (c.the_geom,clc.the_geom)
63        WHERE c.wso_id IN (SELECT wso_id FROM france.wso WHERE area='Bretagne')
64        AND substring(code_00 from 1 for 1)='1')  AS intersected;--3721 lines 
65ALTER TABLE clc.clipped_bretagne add column id serial PRIMARY KEY;
66
67-3721 lines 
68ALTER TABLE clc.clipped_bretagne add column id serial PRIMARY KEY;
69-- ajout dans la table geometry_columns pour référencement rapide sous Qgis
70INSERT INTO geometry_columns(f_table_catalog, f_table_schema, f_table_name, f_geometry_column, coord_dimension, srid, "type")
71SELECT '', 'clc', 'clipped_bretagne', 'the_geom', ST_CoordDim(clipped_geom), ST_SRID(clipped_geom), MULTIPOLYGON
72FROM clc.clipped_bretagne  LIMIT 1;
73
74-- ci dessous j'essaye d'analyser la structure interne des données pour comprendre quels sont les problèmes
75
76SELECT intersected.clcgid, intersected.gid, intersected.the_geom
77FROM (SELECT clc.gid as clcgid, c.gid, CAST(ST_AsText(ST_Multi(ST_Intersection(clc.the_geom, c.the_geom))) as VARCHAR(150)) as the_geom
78        FROM  clc.clc00_v2_europe clc INNER JOIN ccm21.catchments c
79        ON  ST_Intersects (c.the_geom,clc.the_geom)
80        WHERE c.wso_id IN (SELECT wso_id FROM france.wso WHERE area='Bretagne')
81        AND substring(code_00 from 1 for 1)='1')  AS intersected;
82
83
84-- Pour comparaison (ressemble beaucoup)
85
86SELECT CAST(ST_AsText(the_geom) as varchar(150)) from clc.clc00_v2_europe clc limit 20;
87
88
89-- Je rééssaye
90DROP TABLE IF EXISTS clc.clipped_bretagne;
91CREATE TABLE clc.clipped_bretagne AS
92SELECT intersected.clcgid, intersected.gid, the_geom
93FROM (SELECT clc.gid as clcgid, c.gid, ST_Multi(ST_Intersection(clc.the_geom, c.the_geom)) the_geom
94        FROM  clc.clc00_v2_europe clc INNER JOIN ccm21.catchments c
95        ON  ST_Intersects (c.the_geom,clc.the_geom)
96        WHERE c.wso_id IN (SELECT wso_id FROM france.wso WHERE area='Bretagne')
97        AND substring(code_00 from 1 for 1)='1')  AS intersected;--3721 lines 
98ALTER TABLE clc.clipped_bretagne add column id serial PRIMARY KEY;
99-- Here to analyse the structure of data in the created table
100SELECT ST_AsText(the_geom) from clc.clipped_bretagne limit 20;
101
102-- celui là marche sur Postgis (et je ne comprends pas ce qui ne marchait pas peut être le as the_geom a la place de the_geom, ou alors mon Qgis....)
103
104-- I'm adding some original data tables restricted for an area for Qgis easy use
105
106
107
108
109SELECT * FROM clc.clipped_bretagne;
110
111SELECT SUM(ST_Area(the_geom)) as area, gid, FROM clc.clipped_bretagne;
112
113SELECT ST_AsText(the_geom) from clc.clipped_bretagne limit 20;
114--MULTIPOLYGONS
115/*
116Unfortunately geometry collections are not well-supported by GIS tools.
117To prevent ST_Collect from returning a Geometry Collection when collecting MULTI geometries,
118 one can use the below trick that utilizes ST_Dump  to expand the MULTIs out to singles and then regroup them.
119 */
120 
121DROP TABLE IF EXISTS clc.clipped_bretagne1;
122CREATE TABLE clc.clipped_bretagne1 AS (
123SELECT gid,
124           ST_Multi(ST_Collect(f.the_geom)) as the_geom
125         FROM (SELECT gid, (ST_Dump(the_geom)).geom As the_geom
126                                FROM
127                                 clc.clipped_bretagne
128                                ) As f
129GROUP BY gid);
130ALTER TABLE clc.clipped_bretagne1 add column id serial PRIMARY KEY;
131
132SELECT ST_AsText(the_geom) from clc.clipped_bretagne1 ;
133
134INSERT INTO geometry_columns(f_table_catalog, f_table_schema, f_table_name, f_geometry_column, coord_dimension, srid, "type")
135SELECT '', 'clc', 'clipped_bretagne1', 'the_geom', ST_CoordDim(the_geom), ST_SRID(the_geom), 'MULTIPOLYGON'
136FROM clc.clipped_bretagne1  LIMIT 1;
137
138-- j'ai des trous
139
140SELECT ST_AsText(the_geom) FROM (SELECT gid, (ST_Dump(the_geom)).geom As the_geom
141                                FROM
142                                 clc.clipped_bretagne) AS f
143
144-- J'ai bien des trous mais the geom is null ne marche pas 4889
145
146SELECT ST_IsValid (the_geom) FROM (SELECT gid, (ST_Dump(the_geom)).geom As the_geom
147                                FROM
148                                 clc.clipped_bretagne) AS f
149
150                                 -- TRUE partout
151
152SELECT ST_IsEmpty(the_geom) FROM (SELECT gid, (ST_Dump(the_geom)).geom As the_geom
153                                FROM
154                                 clc.clipped_bretagne) AS f;
155                                 -- FALSE partout...; ????
156
157--* bon j'essaye dans Qgis
158-- ca marche a priori pas de trous
159select count(*) as count, code_00  from clc.clc00_v2_europe group by code_00 order by code_00; --
160
161Delete from clc.clc00_v2_europe where code_00='121'
162
163select * from clc.clc00_v2_Bretagne
164--------------------------------------
165--------------------------------------
166--DECOUPAGE DES SURFACES
167--------------------------------------
168--------------------------------------
169DROP TABLE IF EXISTS clc.clipped_bretagne;
170CREATE TABLE clc.clipped_bretagne AS
171SELECT intersected.clcgid, intersected.gid, code_00,the_geom
172FROM (SELECT clc.gid as clcgid, c.gid,code_00, ST_Multi(ST_Intersection(clc.the_geom, c.the_geom)) the_geom
173        FROM  clc.clc00_v2_europe clc INNER JOIN ccm21.catchments c
174        ON  ST_Intersects (c.the_geom,clc.the_geom)
175        WHERE c.wso_id IN (SELECT wso_id FROM france.wso WHERE area='Bretagne')
176       -- AND substring(code_00 from 1 for 1)='1'
177       )  AS intersected; --1h12 min
178ALTER TABLE clc.clipped_bretagne ADD column id serial PRIMARY KEY;
179alter table clc.clipped_bretagne add CONSTRAINT enforce_dims_the_geom CHECK (ndims(the_geom) = 2);
180alter table clc.clipped_bretagne add  CONSTRAINT enforce_geotype_the_geom CHECK (geometrytype(the_geom) = 'MULTIPOLYGON'::text OR the_geom IS NULL);
181alter table clc.clipped_bretagne add  CONSTRAINT enforce_srid_the_geom CHECK (srid(the_geom) = 3035);
182CREATE INDEX indexclc00clipped_bretagne ON clc.clipped_bretagne
183  USING GIST ( the_geom GIST_GEOMETRY_OPS ); 
184  -- les limites sont bien meilleures
185-- Here to analyse the structure of data in the created table
186SELECT ST_AsText(the_geom) from clc.clipped_bretagne limit 20;
187--------------------------------------
188--------------------------------------
189--REGROUPEMENT
190--------------------------------------
191--------------------------------------
192
193DROP TABLE IF EXISTS clc.clipped_bretagne1;
194CREATE TABLE clc.clipped_bretagne1 AS (
195SELECT gid,code_00,
196           ST_Multi(ST_Collect(f.the_geom)) as the_geom
197         FROM (SELECT gid, code_00,(ST_Dump(the_geom)).geom As the_geom
198                                FROM
199                                 clc.clipped_bretagne
200                                ) As f
201GROUP BY gid,code_00); -- 5s
202ALTER TABLE clc.clipped_bretagne1 add column id serial PRIMARY KEY;
203alter table clc.clipped_bretagne1 add CONSTRAINT enforce_dims_the_geom CHECK (ndims(the_geom) = 2);
204alter table clc.clipped_bretagne1 add  CONSTRAINT enforce_geotype_the_geom CHECK (geometrytype(the_geom) = 'MULTIPOLYGON'::text OR the_geom IS NULL);
205alter table clc.clipped_bretagne1 add  CONSTRAINT enforce_srid_the_geom CHECK (srid(the_geom) = 3035);
206CREATE INDEX indexclc00clipped_bretagne1 ON clc.clipped_bretagne1
207  USING GIST ( the_geom GIST_GEOMETRY_OPS );
208ALTER TABLE clc.clipped_bretagne1 add constraint c_ck_uk  UNIQUE(gid,code_00); -- contrainte d'unicité OK !
209
210
211ALTER TABLE clc.clipped_bretagne1 add column area numeric;
212UPDATE clc.clipped_bretagne1 set area=ST_Area(the_geom); -- 9s
213
214SELECT gid,code_00, id,round(area) as area FROM clc.clipped_Bretagne1 order by gid, code_00 limit 10;
215-- here I'm keeping the surface so that we will b able to calculate for any basin
216-- or group of basins a percentage of surface according to the basin surface
217DROP TABLE IF EXISTS clc.surf_area;
218CREATE TABLE clc.surf_area AS (
219SELECT DISTINCT ON (init.gid) init.gid,
220        artificial_surfaces_11_13,
221        artificial_vegetated_14,
222         arable_land_21,
223         permanent_crops_22,
224         pastures_23,
225         heterogeneous_agricultural_24,
226         forest_31,
227         natural_32_33,
228         wetlands_4,
229         inland_waterbodies_51 ,
230         marine_water_52
231        -- SELECT *
232         FROM (
233        SELECT  gid from clc.clipped_bretagne1  ) as init       
234        FULL OUTER JOIN (SELECT gid,sum(area) AS artificial_surfaces_11_13 FROM clc.clipped_bretagne1 WHERE 
235                        substring(code_00 from 1 for 2)='11' 
236                        OR  substring(code_00 from 1 for 2)='12'
237                        OR substring(code_00 from 1 for 2)='13' 
238                        GROUP BY gid) AS artificial_surfaces
239                       on (init.gid) =(artificial_surfaces.gid)         
240        FULL OUTER JOIN (SELECT gid,sum(area) AS artificial_vegetated_14 FROM clc.clipped_bretagne1 WHERE 
241                        substring(code_00 from 1 for 2)='14'
242                        GROUP BY gid) AS artificial_vegetated
243                        on artificial_vegetated.gid =init.gid
244        FULL OUTER JOIN (SELECT gid,sum(area) AS arable_land_21 FROM clc.clipped_bretagne1 WHERE 
245                        substring(code_00 from 1 for 2)='21'
246                        GROUP BY gid) AS arable_land
247                        on arable_land.gid =init.gid
248        FULL OUTER JOIN (SELECT gid, sum(area) AS permanent_crops_22 FROM clc.clipped_bretagne1 WHERE 
249                        substring(code_00 from 1 for 2)='22'
250                        GROUP BY gid) AS permanent_crops
251                        on permanent_crops.gid =init.gid
252        FULL OUTER JOIN (SELECT gid,sum(area) AS pastures_23 FROM clc.clipped_bretagne1 WHERE 
253                        substring(code_00 from 1 for 2)='23'
254                        GROUP BY gid) AS pastures
255                        on pastures.gid =init.gid
256        FULL OUTER JOIN (SELECT gid, sum(area) AS heterogeneous_agricultural_24 FROM clc.clipped_bretagne1 WHERE 
257                        substring(code_00 from 1 for 2)='24'
258                        GROUP BY gid) AS heterogeneous_agricultural
259                        on heterogeneous_agricultural.gid =init.gid
260        FULL OUTER JOIN (SELECT gid,sum(area) AS forest_31 FROM clc.clipped_bretagne1 WHERE 
261                        substring(code_00 from 1 for 2)='31'
262                        GROUP BY gid) AS forest
263                        ON forest.gid =init.gid
264        FULL OUTER JOIN (SELECT gid,sum(area) AS natural_32_33 FROM clc.clipped_bretagne1 WHERE 
265                        substring(code_00 from 1 for 2)='32'
266                        OR  substring(code_00 from 1 for 2)='33'
267                        GROUP BY gid) AS nature
268                        ON nature.gid =init.gid
269        FULL OUTER JOIN (SELECT gid, sum(area) AS wetlands_4  FROM clc.clipped_bretagne1 WHERE 
270                        substring(code_00 from 1 for 1)='4'
271                        GROUP BY gid) AS wetlands
272                        on wetlands.gid =init.gid
273        FULL OUTER JOIN (SELECT gid,sum(area) AS inland_waterbodies_51 FROM clc.clipped_bretagne1 WHERE 
274                        substring(code_00 from 1 for 2)='51'
275                        GROUP BY gid) AS waterbodies
276                        on waterbodies.gid =init.gid
277        FULL OUTER JOIN (SELECT gid,sum(area) AS marine_water_52 FROM clc.clipped_bretagne1 WHERE 
278                        substring(code_00 from 1 for 2)='52'
279                        GROUP BY gid) AS marine_water
280                        on marine_water.gid =init.gid); --375 ms
281ALTER TABLE clc.surf_area ADD CONSTRAINT c_pk_gid_surf_area PRIMARY KEY (gid);
282SELECT * FROM clc.surf_area;
283
284
285DROP TABLE IF EXISTS clc.surf_area1;
286CREATE TABLE clc.surf_area1 AS( 
287SELECT 
288        r.gid,
289        area/1e6 as catchment_area,
290        CASE WHEN artificial_surfaces_11_13 IS NOT NULL THEN artificial_surfaces_11_13/1e6
291        ELSE 0
292        END AS artificial_surfaces_11_13,
293        CASE WHEN artificial_vegetated_14 IS NOT NULL THEN artificial_vegetated_14/1e6
294        ELSE 0
295        END AS artificial_vegetated_14,
296        CASE WHEN arable_land_21 IS NOT NULL THEN arable_land_21/1e6
297        ELSE 0
298        END AS arable_land_21,
299        CASE WHEN permanent_crops_22 IS NOT NULL THEN permanent_crops_22/1e6
300        ELSE 0
301        END AS permanent_crops_22,
302        CASE WHEN pastures_23 IS NOT NULL THEN pastures_23/1e6
303        ELSE 0
304        END AS pastures_23,
305        CASE WHEN heterogeneous_agricultural_24 IS NOT NULL THEN heterogeneous_agricultural_24/1e6
306        ELSE 0
307        END AS heterogeneous_agricultural_24,
308        CASE WHEN forest_31 IS NOT NULL THEN forest_31/1e6
309        ELSE 0
310        END AS forest_31,
311        CASE WHEN natural_32_33 IS NOT NULL THEN natural_32_33/1e6
312        ELSE 0
313        END AS natural_32_33,
314        CASE WHEN wetlands_4 IS NOT NULL THEN wetlands_4/1e6
315        ELSE 0
316        END AS wetlands_4,
317        CASE WHEN inland_waterbodies_51 IS NOT NULL THEN inland_waterbodies_51 /1e6
318        ELSE 0
319        END AS inland_waterbodies_51,
320        CASE WHEN  marine_water_52 IS NOT NULL THEN marine_water_52/1e6
321        ELSE 0
322        END AS marine_water_52,
323        c.wso1_id,
324        c.the_geom     
325FROM clc.surf_area p
326JOIN ccm21.catchments c ON c.gid=p.gid
327JOIN ccm21.riversegments r on r.wso1_id=c.wso1_id
328);
329
330SELECT * FROM clc.surf_area1;
331
332DROP TABLE IF EXISTS clc.surf_area_analyse;
333CREATE TABLE clc.surf_area_analyse AS( 
334SELECT 
335        gid,
336        wso1_id,
337        catchment_area,
338        artificial_surfaces_11_13+
339         artificial_vegetated_14+
340         arable_land_21+
341         permanent_crops_22+
342         pastures_23+
343         heterogeneous_agricultural_24+
344         forest_31+
345         natural_32_33+
346         wetlands_4+
347         inland_waterbodies_51 +
348         marine_water_52 as sum_clc_area ,
349        (artificial_surfaces_11_13+
350         artificial_vegetated_14+
351         arable_land_21+
352         permanent_crops_22+
353         pastures_23+
354         heterogeneous_agricultural_24+
355         forest_31+
356         natural_32_33+
357         wetlands_4+
358         inland_waterbodies_51 +
359         marine_water_52)/catchment_area AS pourc_clc,
360         the_geom
361         FROM clc.surf_area1);
362ALTER TABLE clc.surf_area_analyse add CONSTRAINT c_pk_gid_area_analyse PRIMARY KEY (gid);
363alter table clc.surf_area_analyse add CONSTRAINT enforce_dims_the_geom CHECK (ndims(the_geom) = 2);
364alter table clc.surf_area_analyse add  CONSTRAINT enforce_geotype_the_geom CHECK (geometrytype(the_geom) = 'MULTIPOLYGON'::text OR the_geom IS NULL);
365alter table clc.surf_area_analyse add  CONSTRAINT enforce_srid_the_geom CHECK (srid(the_geom) = 3035);
366CREATE INDEX indexclc00area_analyse ON clc.surf_area_analyse
367  USING GIST ( the_geom GIST_GEOMETRY_OPS );
368
369
370