Skip to content

Commit aad4419

Browse files
committed
Fix data/vm_communes: add VM for subdivided areas
Improve the creation of t_mailles_territoire. Fix #372. fix(data): improve the creation of t_mailles_territoire
1 parent 5a918b1 commit aad4419

File tree

4 files changed

+118
-24
lines changed

4 files changed

+118
-24
lines changed
Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
11
DROP TABLE IF EXISTS atlas.t_mailles_territoire;
22

33
-- MV for having only meshs of the territory
4-
CREATE TABLE atlas.t_mailles_territoire
5-
AS SELECT
6-
st_transform(c.geom, 4326) AS the_geom,
7-
st_asgeojson(st_transform(c.geom, 4326)) AS geojson_maille,
8-
c.id_area AS id_maille
9-
FROM ref_geo.l_areas c
10-
JOIN ref_geo.bib_areas_types t ON t.id_type = c.id_type
11-
JOIN atlas.t_layer_territoire mt ON ST_intersects(c.geom,st_transform(mt.the_geom, find_srid('ref_geo', 'l_areas', 'geom')))
12-
WHERE c.enable = true AND t.type_code = :type_maille;
4+
CREATE TABLE atlas.t_mailles_territoire AS
5+
WITH areas AS (
6+
SELECT
7+
id_area,
8+
st_transform(c.geom, 4326) AS geom,
9+
geojson_4326 AS geojson
10+
FROM ref_geo.l_areas AS c
11+
JOIN ref_geo.bib_areas_types AS t
12+
ON t.id_type = c.id_type
13+
WHERE c.enable = true
14+
AND t.type_code = :type_maille
15+
)
16+
SELECT
17+
a.geom AS the_geom,
18+
a.geojson AS geojson_maille,
19+
a.id_area AS id_maille
20+
FROM areas AS a
21+
WHERE EXISTS (
22+
SELECT 'X'
23+
FROM atlas.vm_subdivided_area AS sa
24+
WHERE sa.code = 'territory'
25+
AND st_intersects(a.geom, sa.geom)
26+
);
1327

14-
CREATE UNIQUE INDEX t_mailles_territoire_id_maille_idx ON atlas.t_mailles_territoire USING btree (id_maille);
28+
CREATE UNIQUE INDEX t_mailles_territoire_id_maille_idx ON atlas.t_mailles_territoire USING btree (id_maille);

data/atlas/7.atlas.vm_communes.sql

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
-- Communes contenues entièrement dans le territoire
22

33
CREATE MATERIALIZED VIEW atlas.vm_communes AS
4-
SELECT c.insee,
5-
c.commune_maj,
6-
c.the_geom,
7-
st_asgeojson(st_transform(c.the_geom, 4326)) as commune_geojson
8-
FROM atlas.l_communes c
9-
JOIN atlas.t_layer_territoire t ON ST_CONTAINS(ST_BUFFER(t.the_geom,200), c.the_geom);
4+
SELECT
5+
c.insee,
6+
c.commune_maj,
7+
c.the_geom,
8+
st_asgeojson(st_transform(c.the_geom, 4326)) AS commune_geojson
9+
FROM atlas.l_communes AS c
10+
WHERE EXISTS (
11+
SELECT 'X'
12+
FROM atlas.vm_subdivided_area AS sa
13+
WHERE sa.code = 'territory_buffer-200'
14+
AND st_intersects(sa.geom, c.the_geom)
15+
)
16+
WITH DATA;
17+
18+
CREATE UNIQUE INDEX ON atlas.vm_communes (insee) ;
19+
CREATE INDEX index_gist_vm_communes_the_geom ON atlas.vm_communes USING gist (the_geom) ;
1020

11-
CREATE UNIQUE INDEX ON atlas.vm_communes (insee);
12-
CREATE INDEX index_gist_vm_communes_the_geom ON atlas.vm_communes USING gist (the_geom);

data/gn2/atlas_ref_geo.sql

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,10 @@ CREATE UNIQUE INDEX l_communes_insee_idx
3333
(insee COLLATE pg_catalog."default");
3434

3535

36-
--################################
3736
--################################
3837
--###Mailles
3938
--################################
40-
--################################
39+
4140

4241
DO $$
4342
BEGIN
@@ -46,11 +45,10 @@ EXCEPTION WHEN others THEN
4645
RAISE NOTICE 'view atlas.t_mailles_territoire does not exist';
4746
END$$;
4847

49-
--################################
5048
--################################
5149
--###Territoires
5250
--################################
53-
--################################
51+
5452

5553
DO $$
5654
BEGIN
@@ -81,19 +79,48 @@ CREATE INDEX index_gist_t_layer_territoire_the_geom
8179
ON atlas.t_layer_territoire
8280
USING gist
8381
(the_geom);
84-
82+
8583
CREATE UNIQUE INDEX t_layer_territoire_gid_idx
8684
ON atlas.t_layer_territoire
8785
USING btree (gid);
8886

87+
--################################
88+
--### Subdivision de zones
89+
--################################
90+
91+
DROP MATERIALIZED VIEW IF EXISTS atlas.vm_subdivided_area ;
92+
93+
CREATE MATERIALIZED VIEW atlas.vm_subdivided_area AS
94+
SELECT
95+
random() AS gid,
96+
'territory' AS code,
97+
st_subdivide(t.the_geom, 255) AS geom
98+
FROM atlas.t_layer_territoire AS t
99+
100+
UNION
101+
102+
SELECT
103+
random() AS gid,
104+
'territory_buffer-200' AS code,
105+
st_subdivide(st_buffer(t.the_geom::geography, -200)::geometry, 255) AS geom
106+
FROM atlas.t_layer_territoire AS t
107+
WITH DATA;
108+
109+
CREATE UNIQUE INDEX ON atlas.vm_subdivided_area USING btree (gid);
110+
CREATE INDEX ON atlas.vm_subdivided_area USING btree (code);
111+
CREATE INDEX ON atlas.vm_subdivided_area USING gist (geom);
112+
113+
--################################
114+
--### Fonctions
115+
--################################
89116

90117
-- Rafraichissement des vues contenant les données de l'atlas
91118
CREATE OR REPLACE FUNCTION atlas.refresh_materialized_view_ref_geo()
92119
RETURNS VOID AS $$
93120
BEGIN
94121

95122
REFRESH MATERIALIZED VIEW atlas.t_layer_territoire;
96-
REFRESH MATERIALIZED VIEW atlas.t_mailles_territoire;
123+
REFRESH MATERIALIZED VIEW atlas.vm_subdivided_area;
97124
REFRESH MATERIALIZED VIEW atlas.l_communes;
98125
REFRESH MATERIALIZED VIEW atlas.vm_communes;
99126

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
BEGIN;
2+
3+
CREATE MATERIALIZED VIEW atlas.vm_subdivided_area AS
4+
SELECT
5+
random() AS gid,
6+
'territory' AS code,
7+
st_subdivide(t.the_geom, 255) AS geom
8+
FROM atlas.t_layer_territoire AS t
9+
10+
UNION
11+
12+
SELECT
13+
random() AS gid,
14+
'territory_buffer-200' AS code,
15+
st_subdivide(st_buffer(t.the_geom::geography, -200)::geometry, 255) AS geom
16+
FROM atlas.t_layer_territoire AS t
17+
WITH DATA;
18+
19+
CREATE UNIQUE INDEX ON atlas.vm_subdivided_area USING btree (gid);
20+
CREATE INDEX ON atlas.vm_subdivided_area USING btree (code);
21+
CREATE INDEX ON atlas.vm_subdivided_area USING gist (geom);
22+
23+
24+
DROP FUNCTION IF EXISTS atlas.refresh_materialized_view_ref_geo;
25+
26+
CREATE OR REPLACE FUNCTION atlas.refresh_materialized_view_ref_geo()
27+
RETURNS VOID AS $$
28+
BEGIN
29+
30+
REFRESH MATERIALIZED VIEW atlas.t_layer_territoire;
31+
REFRESH MATERIALIZED VIEW atlas.vm_subdivided_area;
32+
REFRESH MATERIALIZED VIEW atlas.l_communes;
33+
REFRESH MATERIALIZED VIEW atlas.vm_communes;
34+
35+
END
36+
$$ LANGUAGE plpgsql;
37+
38+
39+
-- TODO : ajouter ce script au futur script d'update de GeoNature Atlas.
40+
-- TODO : trouver une solution pour pouvoir regénérer les VMs et table via les scripts présent dans data/atlas/.
41+
-- TODO : il faudrait aussi relancer la génération de la VM vm_communes à l'aide du script '7.atlas.vm_communes.sql
42+
-- TODO : il faudrait aussi relancer la génération de la table t_mailles_territoire à l'aide du script '12.atlas.t_mailles_teritoire.sql'
43+
44+
45+
COMMIT;

0 commit comments

Comments
 (0)