| 1 | {{{ |
| 2 | #!sql |
| 3 | -- Un essai sur la Bretagne pour voir |
| 4 | DROP TABLE IF EXISTS clc.clipped_bretagne; |
| 5 | CREATE TABLE clc.clipped_bretagne AS |
| 6 | SELECT intersected.clcgid, intersected.gid, clipped_geom |
| 7 | FROM (SELECT clc.gid as clcgid, c.gid, ST_Multi(ST_Intersection(clc.the_geom, c.the_geom)) AS clipped_geom |
| 8 | FROM clc.clc00_v2_europe clc INNER JOIN ccm21.catchments c |
| 9 | ON ST_Intersects (c.the_geom,clc.the_geom) |
| 10 | WHERE c.wso_id IN (SELECT wso_id FROM france.wso WHERE area='Bretagne') |
| 11 | AND substring(code_00 from 1 for 1)='1') AS intersected;--3721 lines |
| 12 | ALTER TABLE clc.clipped_bretagne add column id serial PRIMARY KEY; |
| 13 | -- ajout dans la table geometry_columns pour référencement rapide sous Qgis |
| 14 | INSERT INTO geometry_columns(f_table_catalog, f_table_schema, f_table_name, f_geometry_column, coord_dimension, srid, "type") |
| 15 | SELECT '', 'clc', 'clipped_bretagne', 'the_geom', ST_CoordDim(clipped_geom), ST_SRID(clipped_geom), MULTIPOLYGON |
| 16 | FROM clc.clipped_bretagne LIMIT 1; |
| 17 | |
| 18 | -- il ne s'affiche pas... essai réduit OK |
| 19 | DROP TABLE IF EXISTS clc.clipped_bretagne; |
| 20 | CREATE TABLE clc.clipped_bretagne AS |
| 21 | SELECT clc.gid as clcgid,c.gid, clc.the_geom FROM clc.clc00_v2_europe clc INNER JOIN ccm21.catchments c |
| 22 | ON ST_Intersects (c.the_geom,clc.the_geom) |
| 23 | WHERE c.wso_id IN (SELECT wso_id FROM france.wso WHERE area='Bretagne') |
| 24 | AND substring(code_00 from 1 for 1)='1'; |
| 25 | ALTER TABLE clc.clipped_bretagne add column id serial PRIMARY KEY; |
| 26 | |
| 27 | -- la requete globale |
| 28 | SELECT intersected.clcgid, intersected.gid, clipped_geom |
| 29 | FROM (SELECT clc.gid as clcgid, c.gid, ST_Intersection(clc.the_geom, c.the_geom) AS clipped_geom |
| 30 | FROM clc.clc00_v2_europe clc INNER JOIN ccm21.catchments c |
| 31 | ON ST_Intersects (c.the_geom,clc.the_geom) |
| 32 | where substring(code_00 from 1 for 1)='1' ) AS intersected; |
| 33 | |
| 34 | -- script de départ (modifié ensuite) |
| 35 | CREATE TABLE clc.clipped_bretagne AS |
| 36 | SELECT intersected.clcgid, intersected.gid, clipped_geom |
| 37 | FROM (SELECT clc.gid as clcgid, c.gid, ST_Multi(ST_Intersection(clc.the_geom, c.the_geom)) clipped_geom |
| 38 | FROM clc.clc00_v2_europe clc INNER JOIN ccm21.catchments c |
| 39 | ON ST_Intersects (c.the_geom,clc.the_geom) |
| 40 | WHERE c.wso_id IN (SELECT wso_id FROM france.wso WHERE area='Bretagne') |
| 41 | AND substring(code_00 from 1 for 1)='1') AS intersected;--3721 lines |
| 42 | ALTER TABLE clc.clipped_bretagne add column id serial PRIMARY KEY; |
| 43 | |
| 44 | -3721 lines |
| 45 | ALTER TABLE clc.clipped_bretagne add column id serial PRIMARY KEY; |
| 46 | -- ajout dans la table geometry_columns pour référencement rapide sous Qgis |
| 47 | INSERT INTO geometry_columns(f_table_catalog, f_table_schema, f_table_name, f_geometry_column, coord_dimension, srid, "type") |
| 48 | SELECT '', 'clc', 'clipped_bretagne', 'the_geom', ST_CoordDim(clipped_geom), ST_SRID(clipped_geom), MULTIPOLYGON |
| 49 | FROM clc.clipped_bretagne LIMIT 1; |
| 50 | |
| 51 | -- ci dessous j'essaye d'analyser la structure interne des données pour comprendre quels sont les problèmes |
| 52 | |
| 53 | SELECT intersected.clcgid, intersected.gid, intersected.the_geom |
| 54 | FROM (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 |
| 55 | FROM clc.clc00_v2_europe clc INNER JOIN ccm21.catchments c |
| 56 | ON ST_Intersects (c.the_geom,clc.the_geom) |
| 57 | WHERE c.wso_id IN (SELECT wso_id FROM france.wso WHERE area='Bretagne') |
| 58 | AND substring(code_00 from 1 for 1)='1') AS intersected; |
| 59 | |
| 60 | |
| 61 | -- Pour comparaison (ressemble beaucoup) |
| 62 | |
| 63 | SELECT CAST(ST_AsText(the_geom) as varchar(150)) from clc.clc00_v2_europe clc limit 20; |
| 64 | |
| 65 | |
| 66 | -- Je rééssaye |
| 67 | DROP TABLE IF EXISTS clc.clipped_bretagne; |
| 68 | CREATE TABLE clc.clipped_bretagne AS |
| 69 | SELECT intersected.clcgid, intersected.gid, the_geom |
| 70 | FROM (SELECT clc.gid as clcgid, c.gid, ST_Multi(ST_Intersection(clc.the_geom, c.the_geom)) the_geom |
| 71 | FROM clc.clc00_v2_europe clc INNER JOIN ccm21.catchments c |
| 72 | ON ST_Intersects (c.the_geom,clc.the_geom) |
| 73 | WHERE c.wso_id IN (SELECT wso_id FROM france.wso WHERE area='Bretagne') |
| 74 | AND substring(code_00 from 1 for 1)='1') AS intersected;--3721 lines |
| 75 | ALTER TABLE clc.clipped_bretagne add column id serial PRIMARY KEY; |
| 76 | -- Here to analyse the structure of data in the created table |
| 77 | SELECT ST_AsText(the_geom) from clc.clipped_bretagne limit 20; |
| 78 | |
| 79 | -- 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....) |
| 80 | |
| 81 | -- I'm adding some original data tables restricted for an area for Qgis easy use |
| 82 | -- extraction of a clc table for Britany |
| 83 | DROP TABLE IF EXISTS clc.clc00_v2_Bretagne; |
| 84 | CREATE TABLE clc.clc00_v2_Bretagne AS |
| 85 | SELECT * FROM clc.clc00_v2_europe where gid IN ( |
| 86 | SELECT gid FROM clc.clc00_v2_europe clc JOIN |
| 87 | (SELECT the_geom FROM france.region where code_reg='53') as sub |
| 88 | ON ST_Contains(sub.the_geom,clc.the_geom)); |
| 89 | ALTER TABLE clc.clc00_v2_Bretagne ADD CONSTRAINT c_pk_gid PRIMARY KEY (gid); |
| 90 | INSERT INTO geometry_columns(f_table_catalog, f_table_schema, f_table_name, f_geometry_column, coord_dimension, srid, "type") |
| 91 | SELECT '', 'clc', 'clc00_v2_Bretagne', 'the_geom', ST_CoordDim(the_geom), ST_SRID(the_geom), GeometryType(the_geom) |
| 92 | FROM clc.clc00_v2_europe LIMIT 1; |
| 93 | |
| 94 | -- Creating a table for riversegments Bretagne |
| 95 | |
| 96 | DROP TABLE IF EXISTS ccm21.riversegments_Bretagne; |
| 97 | CREATE TABLE ccm21.riversegments_Bretagne AS |
| 98 | SELECT * FROM ccm21.riversegments |
| 99 | WHERE wso_id IN (SELECT wso_id FROM france.wso WHERE area='Bretagne'); |
| 100 | ALTER TABLE ccm21.riversegments_Bretagne ADD CONSTRAINT c_pk_gid_riversegments_Bretagne PRIMARY KEY (gid); |
| 101 | INSERT INTO geometry_columns(f_table_catalog, f_table_schema, f_table_name, f_geometry_column, coord_dimension, srid, "type") |
| 102 | SELECT '', 'ccm21', 'riversegments_Bretagne', 'the_geom', ST_CoordDim(the_geom), ST_SRID(the_geom), GeometryType(the_geom) |
| 103 | FROM ccm21.riversegments_Bretagne LIMIT 1; |
| 104 | |
| 105 | -- creating a table for catchments Bretagne. |
| 106 | DROP TABLE IF EXISTS ccm21.catchments_Bretagne; |
| 107 | CREATE TABLE ccm21.catchments_Bretagne AS |
| 108 | SELECT * FROM ccm21.catchments |
| 109 | WHERE wso_id IN (SELECT wso_id FROM france.wso WHERE area='Bretagne'); |
| 110 | ALTER TABLE ccm21.catchments_Bretagne ADD CONSTRAINT c_pk_gid_catchments_Bretagne PRIMARY KEY (gid); |
| 111 | INSERT INTO geometry_columns(f_table_catalog, f_table_schema, f_table_name, f_geometry_column, coord_dimension, srid, "type") |
| 112 | SELECT '', 'ccm21', 'catchments_Bretagne', 'the_geom', ST_CoordDim(the_geom), ST_SRID(the_geom), GeometryType(the_geom) |
| 113 | FROM ccm21.catchments_Bretagne LIMIT 1; |
| 114 | }}} |