-
Notifications
You must be signed in to change notification settings - Fork 1
/
atlas.sql
495 lines (427 loc) · 21.4 KB
/
atlas.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
--VUES MATERIALISEES--
--Copie du contenu de taxref (à partir du schéma taxonomie de TaxHub)
--DROP materialized view taxonomie.vm_taxref;
CREATE materialized view atlas.vm_taxref AS
SELECT * FROM taxonomie.taxref;
CREATE UNIQUE INDEX ON atlas.vm_taxref (cd_nom);
CREATE INDEX ON atlas.vm_taxref (cd_ref);
CREATE INDEX ON atlas.vm_taxref (cd_taxsup);
CREATE INDEX ON atlas.vm_taxref (lb_nom);
CREATE INDEX ON atlas.vm_taxref (nom_complet);
CREATE INDEX ON atlas.vm_taxref (nom_valide);
--Vue noms des structures pour remplacer observateurs
CREATE OR REPLACE VIEW synthese.obs_structures
AS SELECT selection.id_synthese,
string_agg(selection.nom_structures::text, ', '::text) AS nom_structures
FROM ( SELECT s.id_synthese,
bo.nom_organisme AS nom_structures
FROM synthese.synthese s
LEFT JOIN gn_meta.cor_dataset_actor cda ON cda.id_dataset = s.id_dataset
LEFT JOIN utilisateurs.bib_organismes bo ON bo.id_organisme = cda.id_organism
WHERE cda.id_organism IS NOT NULL
UNION
SELECT s.id_synthese,
concat_ws(' '::text, tr.nom_role, tr.prenom_role) AS nom_structures
FROM synthese.synthese s
LEFT JOIN gn_meta.cor_dataset_actor cda ON cda.id_dataset = s.id_dataset
LEFT JOIN utilisateurs.t_roles tr ON tr.id_role = cda.id_role
WHERE cda.id_role IS NOT NULL) selection
GROUP BY selection.id_synthese;
--Toutes les observations
--DROP materialized view atlas.vm_observations;
CREATE MATERIALIZED VIEW atlas.vm_observations AS
SELECT s.id_synthese AS id_observation,
s.insee,
s.dateobs,
os.nom_structures AS observateurs,
s.altitude_retenue,
s.the_geom_point::geometry('POINT',3857),
s.effectif_total,
tx.cd_ref,
st_asgeojson(ST_Transform(ST_SetSrid(s.the_geom_point, 3857), 4326)) as geojson_point,
diffusion_level
FROM synthese.syntheseff s
LEFT JOIN atlas.vm_taxref tx ON tx.cd_nom = s.cd_nom
LEFT JOIN synthese.obs_structures os ON os.id_synthese = s.id_synthese
JOIN atlas.t_layer_territoire m ON ST_Intersects(m.the_geom, s.the_geom_point);
CREATE UNIQUE INDEX ON atlas.vm_observations (id_observation);
CREATE INDEX ON atlas.vm_observations (cd_ref);
CREATE INDEX ON atlas.vm_observations (insee);
CREATE INDEX ON atlas.vm_observations (altitude_retenue);
CREATE INDEX ON atlas.vm_observations (dateobs);
CREATE INDEX index_gist_vm_observations_the_geom_point ON atlas.vm_observations USING gist (the_geom_point);
--Tous les taxons ayant au moins une observation
--DROP MATERIALIZED VIEW atlas.vm_taxons;
CREATE MATERIALIZED VIEW atlas.vm_taxons AS
WITH obs_min_taxons AS (
SELECT vm_observations.cd_ref,
min(date_part('year'::text, vm_observations.dateobs)) AS yearmin,
max(date_part('year'::text, vm_observations.dateobs)) AS yearmax,
COUNT(vm_observations.id_observation) AS nb_obs
FROM atlas.vm_observations
GROUP BY vm_observations.cd_ref
), tx_ref AS (
SELECT tx_1.cd_ref,
tx_1.regne,
tx_1.phylum,
tx_1.classe,
tx_1.ordre,
tx_1.famille,
tx_1.cd_taxsup,
tx_1.lb_nom,
tx_1.lb_auteur,
tx_1.nom_complet,
tx_1.nom_valide,
tx_1.nom_vern,
tx_1.nom_vern_eng,
tx_1.group1_inpn,
tx_1.group2_inpn,
tx_1.nom_complet_html,
tx_1.id_rang
FROM atlas.vm_taxref tx_1
WHERE (tx_1.cd_ref IN ( SELECT obs_min_taxons.cd_ref
FROM obs_min_taxons)) AND tx_1.cd_nom = tx_1.cd_ref
), my_taxons AS (
SELECT DISTINCT n.cd_ref,
pat.valeur_attribut AS patrimonial,
pr.valeur_attribut AS protection_stricte
FROM tx_ref n
LEFT JOIN taxonomie.cor_taxon_attribut pat ON pat.cd_ref = n.cd_ref AND pat.id_attribut = 1
LEFT JOIN taxonomie.cor_taxon_attribut pr ON pr.cd_ref = n.cd_ref AND pr.id_attribut = 2
WHERE n.cd_ref IN ( SELECT obs_min_taxons.cd_ref
FROM obs_min_taxons)
)
SELECT tx.cd_ref,
tx.regne,
tx.phylum,
tx.classe,
tx.ordre,
tx.famille,
tx.cd_taxsup,
tx.lb_nom,
tx.lb_auteur,
tx.nom_complet,
tx.nom_valide,
tx.nom_vern,
tx.nom_vern_eng,
tx.group1_inpn,
tx.group2_inpn,
tx.nom_complet_html,
tx.id_rang,
t.patrimonial,
t.protection_stricte,
omt.yearmin,
omt.yearmax,
omt.nb_obs
FROM tx_ref tx
LEFT JOIN obs_min_taxons omt ON omt.cd_ref = tx.cd_ref
LEFT JOIN my_taxons t ON t.cd_ref = tx.cd_ref
WITH DATA;
CREATE UNIQUE INDEX ON atlas.vm_taxons (cd_ref);
--Classes d'altitudes, modifiables selon votre contexte
--DROP TABLE atlas.bib_altitudes;
CREATE TABLE atlas.bib_altitudes
(
id_altitude integer NOT NULL,
altitude_min integer NOT NULL,
altitude_max integer NOT NULL,
label_altitude character varying(255),
CONSTRAINT bib_altitudes_pk PRIMARY KEY (id_altitude)
);
INSERT_ALTITUDE
UPDATE atlas.bib_altitudes set label_altitude = '_' || altitude_min || '_' || altitude_max+1;
-- Fonction qui permet de créer la VM contenant le nombre d'observations par classes d'altitude pour chaque taxon
-- DROP FUNCTION atlas.create_vm_altitudes();
CREATE OR REPLACE FUNCTION atlas.create_vm_altitudes()
RETURNS text AS
$BODY$
DECLARE
monsql text;
mesaltitudes RECORD;
BEGIN
DROP MATERIALIZED VIEW IF EXISTS atlas.vm_altitudes;
monsql = 'CREATE materialized view atlas.vm_altitudes AS WITH ';
FOR mesaltitudes IN SELECT * FROM atlas.bib_altitudes ORDER BY id_altitude LOOP
IF mesaltitudes.id_altitude = 1 THEN
monsql = monsql || 'alt' || mesaltitudes.id_altitude ||' AS (SELECT cd_ref, count(*) as nb FROM atlas.vm_observations WHERE altitude_retenue <' || mesaltitudes.altitude_max || ' GROUP BY cd_ref) ';
ELSE
monsql = monsql || ',alt' || mesaltitudes.id_altitude ||' AS (SELECT cd_ref, count(*) as nb FROM atlas.vm_observations WHERE altitude_retenue BETWEEN ' || mesaltitudes.altitude_min || ' AND ' || mesaltitudes.altitude_max || ' GROUP BY cd_ref)';
END IF;
END LOOP;
monsql = monsql || ' SELECT DISTINCT o.cd_ref';
FOR mesaltitudes IN SELECT * FROM atlas.bib_altitudes LOOP
monsql = monsql || ',COALESCE(a' ||mesaltitudes.id_altitude || '.nb::integer, 0) as '|| mesaltitudes.label_altitude;
END LOOP;
monsql = monsql || ' FROM atlas.vm_observations o';
FOR mesaltitudes IN SELECT * FROM atlas.bib_altitudes LOOP
monsql = monsql || ' LEFT JOIN alt' || mesaltitudes.id_altitude ||' a' || mesaltitudes.id_altitude || ' ON a' || mesaltitudes.id_altitude || '.cd_ref = o.cd_ref';
END LOOP;
monsql = monsql || ' WHERE o.cd_ref is not null ORDER BY o.cd_ref;';
EXECUTE monsql;
create unique index ON atlas.vm_altitudes (cd_ref);
RETURN monsql;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
select atlas.create_vm_altitudes();
-- Taxons observés et de tous leurs synonymes (utilisés pour la recherche d'une espèce)
CREATE MATERIALIZED VIEW atlas.vm_search_taxon AS
SELECT row_number() OVER (ORDER BY t.cd_nom,t.cd_ref,t.search_name)::integer AS fid,
t.cd_nom,
t.cd_ref,
t.search_name,
t.nom_valide,
t.lb_nom
FROM (
SELECT t_1.cd_nom,
t_1.cd_ref,
concat(t_1.lb_nom, ' = <i> ', t_1.nom_valide, '</i>') AS search_name,
t_1.nom_valide,
t_1.lb_nom
FROM atlas.vm_taxref t_1
UNION
SELECT t_1.cd_nom,
t_1.cd_ref,
concat(t_1.nom_vern, ' = <i> ', t_1.nom_valide, '</i>' ) AS search_name,
t_1.nom_valide,
t_1.lb_nom
FROM atlas.vm_taxref t_1
WHERE t_1.nom_vern IS NOT NULL AND t_1.cd_nom = t_1.cd_ref
) t
JOIN atlas.vm_taxons taxons ON taxons.cd_ref = t.cd_ref;
CREATE UNIQUE INDEX ON atlas.vm_search_taxon(fid);
CREATE INDEX ON atlas.vm_search_taxon(cd_nom);
create INDEX ON atlas.vm_search_taxon(cd_ref);
CREATE INDEX trgm_idx ON atlas.vm_search_taxon USING GIST (search_name gist_trgm_ops);
CREATE UNIQUE INDEX ON atlas.vm_search_taxon (cd_nom, search_name);
-- Nombre d'observations mensuelles pour chaque taxon observé
CREATE materialized view atlas.vm_mois AS
WITH
_01 AS (SELECT cd_ref, count(*) as nb FROM atlas.vm_observations WHERE date_part('month'::text, dateobs) = '01' GROUP BY cd_ref),
_02 AS (SELECT cd_ref, count(*) as nb FROM atlas.vm_observations WHERE date_part('month'::text, dateobs) = '02' GROUP BY cd_ref),
_03 AS (SELECT cd_ref, count(*) as nb FROM atlas.vm_observations WHERE date_part('month'::text, dateobs) = '03' GROUP BY cd_ref),
_04 AS (SELECT cd_ref, count(*) as nb FROM atlas.vm_observations WHERE date_part('month'::text, dateobs) = '04' GROUP BY cd_ref),
_05 AS (SELECT cd_ref, count(*) as nb FROM atlas.vm_observations WHERE date_part('month'::text, dateobs) = '05' GROUP BY cd_ref),
_06 AS (SELECT cd_ref, count(*) as nb FROM atlas.vm_observations WHERE date_part('month'::text, dateobs) = '06' GROUP BY cd_ref),
_07 AS (SELECT cd_ref, count(*) as nb FROM atlas.vm_observations WHERE date_part('month'::text, dateobs) = '07' GROUP BY cd_ref),
_08 AS (SELECT cd_ref, count(*) as nb FROM atlas.vm_observations WHERE date_part('month'::text, dateobs) = '08' GROUP BY cd_ref),
_09 AS (SELECT cd_ref, count(*) as nb FROM atlas.vm_observations WHERE date_part('month'::text, dateobs) = '09' GROUP BY cd_ref),
_10 AS (SELECT cd_ref, count(*) as nb FROM atlas.vm_observations WHERE date_part('month'::text, dateobs) = '10' GROUP BY cd_ref),
_11 AS (SELECT cd_ref, count(*) as nb FROM atlas.vm_observations WHERE date_part('month'::text, dateobs) = '11' GROUP BY cd_ref),
_12 AS (SELECT cd_ref, count(*) as nb FROM atlas.vm_observations WHERE date_part('month'::text, dateobs) = '12' GROUP BY cd_ref)
SELECT DISTINCT o.cd_ref
,COALESCE(a.nb::integer, 0) as _01
,COALESCE(b.nb::integer, 0) as _02
,COALESCE(c.nb::integer, 0) as _03
,COALESCE(d.nb::integer, 0) as _04
,COALESCE(e.nb::integer, 0) as _05
,COALESCE(f.nb::integer, 0) as _06
,COALESCE(g.nb::integer, 0) as _07
,COALESCE(h.nb::integer, 0) as _08
,COALESCE(i.nb::integer, 0) as _09
,COALESCE(j.nb::integer, 0) as _10
,COALESCE(k.nb::integer, 0) as _11
,COALESCE(l.nb::integer, 0) as _12
FROM atlas.vm_observations o
LEFT JOIN _01 a ON a.cd_ref = o.cd_ref
LEFT JOIN _02 b ON b.cd_ref = o.cd_ref
LEFT JOIN _03 c ON c.cd_ref = o.cd_ref
LEFT JOIN _04 d ON d.cd_ref = o.cd_ref
LEFT JOIN _05 e ON e.cd_ref = o.cd_ref
LEFT JOIN _06 f ON f.cd_ref = o.cd_ref
LEFT JOIN _07 g ON g.cd_ref = o.cd_ref
LEFT JOIN _08 h ON h.cd_ref = o.cd_ref
LEFT JOIN _09 i ON i.cd_ref = o.cd_ref
LEFT JOIN _10 j ON j.cd_ref = o.cd_ref
LEFT JOIN _11 k ON k.cd_ref = o.cd_ref
LEFT JOIN _12 l ON l.cd_ref = o.cd_ref
WHERE o.cd_ref is not null
ORDER BY o.cd_ref;
CREATE UNIQUE INDEX ON atlas.vm_mois (cd_ref);
-- Communes contenues entièrement dans le territoire
CREATE MATERIALIZED VIEW atlas.vm_communes AS
SELECT c.insee,
c.commune_maj,
c.the_geom,
st_asgeojson(st_transform(c.the_geom, 4326)) as commune_geojson
FROM atlas.l_communes c
JOIN atlas.t_layer_territoire t ON ST_CONTAINS(ST_BUFFER(t.the_geom,200), c.the_geom);
CREATE UNIQUE INDEX ON atlas.vm_communes (insee);
CREATE INDEX index_gist_vm_communes_the_geom ON atlas.vm_communes USING gist (the_geom);
-- Rangs de taxref ordonnés
CREATE TABLE atlas.bib_taxref_rangs (
id_rang character(4) NOT NULL,
nom_rang character varying(20) NOT NULL,
tri_rang integer
);
INSERT INTO atlas.bib_taxref_rangs (id_rang, nom_rang, tri_rang) VALUES ('Dumm', 'Domaine', 1);
INSERT INTO atlas.bib_taxref_rangs (id_rang, nom_rang, tri_rang) VALUES ('SPRG', 'Super-Règne', 2);
INSERT INTO atlas.bib_taxref_rangs (id_rang, nom_rang, tri_rang) VALUES ('KD ', 'Règne', 3);
INSERT INTO atlas.bib_taxref_rangs (id_rang, nom_rang, tri_rang) VALUES ('SSRG', 'Sous-Règne', 4);
INSERT INTO atlas.bib_taxref_rangs (id_rang, nom_rang, tri_rang) VALUES ('IFRG', 'Infra-Règne', 5);
INSERT INTO atlas.bib_taxref_rangs (id_rang, nom_rang, tri_rang) VALUES ('PH ', 'Embranchement', 6);
INSERT INTO atlas.bib_taxref_rangs (id_rang, nom_rang, tri_rang) VALUES ('SBPH', 'Sous-Phylum', 7);
INSERT INTO atlas.bib_taxref_rangs (id_rang, nom_rang, tri_rang) VALUES ('IFPH', 'Infra-Phylum', 8);
INSERT INTO atlas.bib_taxref_rangs (id_rang, nom_rang, tri_rang) VALUES ('DV ', 'Division', 9);
INSERT INTO atlas.bib_taxref_rangs (id_rang, nom_rang, tri_rang) VALUES ('SBDV', 'Sous-division', 10);
INSERT INTO atlas.bib_taxref_rangs (id_rang, nom_rang, tri_rang) VALUES ('SPCL', 'Super-Classe', 11);
INSERT INTO atlas.bib_taxref_rangs (id_rang, nom_rang, tri_rang) VALUES ('CLAD', 'Cladus', 12);
INSERT INTO atlas.bib_taxref_rangs (id_rang, nom_rang, tri_rang) VALUES ('CL ', 'Classe', 13);
INSERT INTO atlas.bib_taxref_rangs (id_rang, nom_rang, tri_rang) VALUES ('SBCL', 'Sous-Classe', 14);
INSERT INTO atlas.bib_taxref_rangs (id_rang, nom_rang, tri_rang) VALUES ('IFCL', 'Infra-classe', 15);
INSERT INTO atlas.bib_taxref_rangs (id_rang, nom_rang, tri_rang) VALUES ('LEG ', 'Legio', 16);
INSERT INTO atlas.bib_taxref_rangs (id_rang, nom_rang, tri_rang) VALUES ('SPOR', 'Super-Ordre', 17);
INSERT INTO atlas.bib_taxref_rangs (id_rang, nom_rang, tri_rang) VALUES ('COH ', 'Cohorte', 18);
INSERT INTO atlas.bib_taxref_rangs (id_rang, nom_rang, tri_rang) VALUES ('OR ', 'Ordre', 19);
INSERT INTO atlas.bib_taxref_rangs (id_rang, nom_rang, tri_rang) VALUES ('SBOR', 'Sous-Ordre', 20);
INSERT INTO atlas.bib_taxref_rangs (id_rang, nom_rang, tri_rang) VALUES ('IFOR', 'Infra-Ordre', 21);
INSERT INTO atlas.bib_taxref_rangs (id_rang, nom_rang, tri_rang) VALUES ('SPFM', 'Super-Famille', 22);
INSERT INTO atlas.bib_taxref_rangs (id_rang, nom_rang, tri_rang) VALUES ('FM ', 'Famille', 23);
INSERT INTO atlas.bib_taxref_rangs (id_rang, nom_rang, tri_rang) VALUES ('SBFM', 'Sous-Famille', 24);
INSERT INTO atlas.bib_taxref_rangs (id_rang, nom_rang, tri_rang) VALUES ('TR ', 'Tribu', 26);
INSERT INTO atlas.bib_taxref_rangs (id_rang, nom_rang, tri_rang) VALUES ('SSTR', 'Sous-Tribu', 27);
INSERT INTO atlas.bib_taxref_rangs (id_rang, nom_rang, tri_rang) VALUES ('GN ', 'Genre', 28);
INSERT INTO atlas.bib_taxref_rangs (id_rang, nom_rang, tri_rang) VALUES ('SSGN', 'Sous-Genre', 29);
INSERT INTO atlas.bib_taxref_rangs (id_rang, nom_rang, tri_rang) VALUES ('SC ', 'Section', 30);
INSERT INTO atlas.bib_taxref_rangs (id_rang, nom_rang, tri_rang) VALUES ('SBSC', 'Sous-Section', 31);
INSERT INTO atlas.bib_taxref_rangs (id_rang, nom_rang, tri_rang) VALUES ('SER', 'Série', 32);
INSERT INTO atlas.bib_taxref_rangs (id_rang, nom_rang, tri_rang) VALUES ('SSER', 'Sous-Série', 33);
INSERT INTO atlas.bib_taxref_rangs (id_rang, nom_rang, tri_rang) VALUES ('AGES', 'Agrégat', 34);
INSERT INTO atlas.bib_taxref_rangs (id_rang, nom_rang, tri_rang) VALUES ('ES ', 'Espèce', 35);
INSERT INTO atlas.bib_taxref_rangs (id_rang, nom_rang, tri_rang) VALUES ('SMES', 'Semi-espèce', 36);
INSERT INTO atlas.bib_taxref_rangs (id_rang, nom_rang, tri_rang) VALUES ('MES ', 'Micro-Espèce',37);
INSERT INTO atlas.bib_taxref_rangs (id_rang, nom_rang, tri_rang) VALUES ('SSES', 'Sous-espèce', 38);
INSERT INTO atlas.bib_taxref_rangs (id_rang, nom_rang, tri_rang) VALUES ('NAT ', 'Natio', 39);
INSERT INTO atlas.bib_taxref_rangs (id_rang, nom_rang, tri_rang) VALUES ('VAR ', 'Variété', 40);
INSERT INTO atlas.bib_taxref_rangs (id_rang, nom_rang, tri_rang) VALUES ('SVAR ', 'Sous-Variété', 41);
INSERT INTO atlas.bib_taxref_rangs (id_rang, nom_rang, tri_rang) VALUES ('FO ', 'Forme', 42);
INSERT INTO atlas.bib_taxref_rangs (id_rang, nom_rang, tri_rang) VALUES ('SSFO', 'Sous-Forme', 43);
INSERT INTO atlas.bib_taxref_rangs (id_rang, nom_rang, tri_rang) VALUES ('FOES', 'Forma species', 44);
INSERT INTO atlas.bib_taxref_rangs (id_rang, nom_rang, tri_rang) VALUES ('LIN ', 'Linea', 45);
INSERT INTO atlas.bib_taxref_rangs (id_rang, nom_rang, tri_rang) VALUES ('CLO ', 'Clône', 46);
INSERT INTO atlas.bib_taxref_rangs (id_rang, nom_rang, tri_rang) VALUES ('RACE', 'Race', 47);
INSERT INTO atlas.bib_taxref_rangs (id_rang, nom_rang, tri_rang) VALUES ('CAR ', 'Cultivar', 48);
INSERT INTO atlas.bib_taxref_rangs (id_rang, nom_rang, tri_rang) VALUES ('MO ', 'Morpha', 49);
INSERT INTO atlas.bib_taxref_rangs (id_rang, nom_rang, tri_rang) VALUES ('AB ', 'Abberatio',50);
--n'existe plus dans taxref V9
INSERT INTO atlas.bib_taxref_rangs (id_rang, nom_rang) VALUES ('CVAR', 'Convariété');
INSERT INTO atlas.bib_taxref_rangs (id_rang, nom_rang) VALUES ('HYB ', 'Hybride');
--non documenté dans la doc taxref
INSERT INTO atlas.bib_taxref_rangs (id_rang, nom_rang, tri_rang) VALUES ('SPTR', 'Supra-Tribu', 25);
INSERT INTO atlas.bib_taxref_rangs (id_rang, nom_rang) VALUES ('SCO ', '?');
INSERT INTO atlas.bib_taxref_rangs (id_rang, nom_rang) VALUES ('PVOR', '?');
INSERT INTO atlas.bib_taxref_rangs (id_rang, nom_rang) VALUES ('SSCO', '?');
-- Médias de chaque taxon
CREATE MATERIALIZED VIEW atlas.vm_medias AS
SELECT t_medias.id_media,
t_medias.cd_ref,
t_medias.titre,
t_medias.url,
t_medias.chemin,
t_medias.auteur,
t_medias.desc_media,
t_medias.date_media,
t_medias.id_type,
t_medias.licence,
t_medias.source
FROM taxonomie.t_medias;
CREATE UNIQUE INDEX ON atlas.vm_medias (id_media);
-- Attributs de chaque taxon (description, commentaire, milieu et chorologie)
CREATE MATERIALIZED VIEW atlas.vm_cor_taxon_attribut AS
SELECT id_attribut,
valeur_attribut,
cd_ref
FROM taxonomie.cor_taxon_attribut
WHERE id_attribut IN (100, 101, 102, 103, 104);
CREATE UNIQUE INDEX ON atlas.vm_cor_taxon_attribut (cd_ref,id_attribut);
-- 12 taxons les plus observés sur la période en cours (par défaut -15 jours +15 jours toutes années confondues)
-- MODIFIE pour avoir plus de taxons, avec sélection aléatoire (+rafraichissement avec tache cron), et que si photo
CREATE MATERIALIZED VIEW atlas.vm_taxons_plus_observes AS
SELECT *
from (SELECT count(*) AS nb_obs,
obs.cd_ref,
tax.lb_nom,
tax.group2_inpn,
tax.nom_vern,
m.id_media,
m.url,
m.chemin,
m.id_type
FROM atlas.vm_observations obs
JOIN atlas.vm_taxons tax ON tax.cd_ref = obs.cd_ref
LEFT JOIN atlas.vm_medias m ON m.cd_ref = obs.cd_ref AND m.id_type = 1
WHERE date_part('day'::text, obs.dateobs) >= date_part('day'::text, 'now'::text::date - 15) AND date_part('month'::text, obs.dateobs) = date_part('month'::text, 'now'::text::date - 15) OR date_part('day'::text, obs.dateobs) <= date_part('day'::text, 'now'::text::date + 15) AND date_part('month'::text, obs.dateobs) = date_part('day'::text, 'now'::text::date + 15)
GROUP BY obs.cd_ref, tax.lb_nom, tax.nom_vern, m.url, m.chemin, tax.group2_inpn, m.id_type, m.id_media
ORDER BY (count(*)) desc
LIMIT 36) as selection
WHERE selection.id_media IS NOT NULL
ORDER BY (random()) desc
LIMIT 12;
-- DROP INDEX atlas.vm_taxons_plus_observes_cd_ref_idx;
CREATE UNIQUE INDEX vm_taxons_plus_observes_cd_ref_idx
ON atlas.vm_taxons_plus_observes
USING btree
(cd_ref);
--Fonction qui permet de lister tous les taxons enfants d'un taxon
CREATE OR REPLACE FUNCTION atlas.find_all_taxons_childs(id integer)
RETURNS SETOF integer AS
$BODY$
--Param : cd_nom ou cd_ref d'un taxon quelque soit son rang
--Retourne le cd_nom de tous les taxons enfants sous forme d'un jeu de données utilisable comme une table
--Usage SELECT atlas.find_all_taxons_childs(197047);
--ou SELECT * FROM atlas.vm_taxons WHERE cd_ref IN(SELECT * FROM atlas.find_all_taxons_childs(197047))
DECLARE
inf RECORD;
c integer;
BEGIN
SELECT INTO c count(*) FROM atlas.vm_taxref WHERE cd_taxsup = id;
IF c > 0 THEN
FOR inf IN
WITH RECURSIVE descendants AS (
SELECT tx1.cd_nom FROM atlas.vm_taxref tx1 WHERE tx1.cd_taxsup = id
UNION ALL
SELECT tx2.cd_nom FROM descendants d JOIN atlas.vm_taxref tx2 ON tx2.cd_taxsup = d.cd_nom
)
SELECT cd_nom FROM descendants
LOOP
RETURN NEXT inf.cd_nom;
END LOOP;
END IF;
END;
$BODY$
LANGUAGE plpgsql IMMUTABLE
COST 100
ROWS 1000;
--Fonction pour rafraichir toutes les vues matérialisées d'un schéma
--USAGE : SELECT RefreshAllMaterializedViews('atlas');
CREATE OR REPLACE FUNCTION RefreshAllMaterializedViews(schema_arg TEXT DEFAULT 'public')
RETURNS INT AS $$
DECLARE
r RECORD;
BEGIN
RAISE NOTICE 'Refreshing materialized view in schema %', schema_arg;
FOR r IN SELECT matviewname FROM pg_matviews WHERE schemaname = schema_arg
LOOP
RAISE NOTICE 'Refreshing %.%', schema_arg, r.matviewname;
--EXECUTE 'REFRESH MATERIALIZED VIEW ' || schema_arg || '.' || r.matviewname; --Si vous utilisez une version inférieure à PostgreSQL 9.4
EXECUTE 'REFRESH MATERIALIZED VIEW CONCURRENTLY ' || schema_arg || '.' || r.matviewname;
END LOOP;
RETURN 1;
END
$$ LANGUAGE plpgsql;
-- Rafraichissement des vues contenant les données de l'atlas
CREATE OR REPLACE FUNCTION atlas.refresh_materialized_view_data()
RETURNS VOID AS $$
BEGIN
REFRESH MATERIALIZED VIEW CONCURRENTLY atlas.vm_observations;
REFRESH MATERIALIZED VIEW CONCURRENTLY atlas.vm_observations_mailles;
REFRESH MATERIALIZED VIEW CONCURRENTLY atlas.vm_mois;
REFRESH MATERIALIZED VIEW CONCURRENTLY atlas.vm_altitudes;
REFRESH MATERIALIZED VIEW CONCURRENTLY atlas.vm_taxons;
REFRESH MATERIALIZED VIEW CONCURRENTLY atlas.vm_cor_taxon_attribut;
REFRESH MATERIALIZED VIEW CONCURRENTLY atlas.vm_search_taxon;
REFRESH MATERIALIZED VIEW CONCURRENTLY atlas.vm_medias;
REFRESH MATERIALIZED VIEW CONCURRENTLY atlas.vm_taxons_plus_observes;
END
$$ LANGUAGE plpgsql;