8
8
from atlas .modeles .utils import deleteAccent , findPath
9
9
10
10
11
+ def format_taxon_name (observation ):
12
+ if observation .nom_vern :
13
+ inter = observation .nom_vern .split ("," )
14
+ taxon_name_formated = inter [0 ] + " | <i>" + observation .lb_nom + "</i>"
15
+ else :
16
+ taxon_name_formated = "<i>" + observation .lb_nom + "</i>"
17
+ return taxon_name_formated
18
+
19
+
11
20
def getObservationsMaillesChilds (session , cd_ref , year_min = None , year_max = None ):
12
21
"""
13
22
Retourne les mailles et le nombre d'observation par maille pour un taxon et ses enfants
@@ -23,6 +32,7 @@ def getObservationsMaillesChilds(session, cd_ref, year_min=None, year_max=None):
23
32
TMaillesTerritoire .geojson_maille ,
24
33
func .max (VmObservationsMailles .annee ).label ("last_obs_year" ),
25
34
func .sum (VmObservationsMailles .nbr ).label ("obs_nbr" ),
35
+ VmObservationsMailles .type_code ,
26
36
)
27
37
.join (
28
38
TMaillesTerritoire ,
@@ -32,6 +42,7 @@ def getObservationsMaillesChilds(session, cd_ref, year_min=None, year_max=None):
32
42
.group_by (
33
43
VmObservationsMailles .id_maille ,
34
44
TMaillesTerritoire .geojson_maille ,
45
+ VmObservationsMailles .type_code ,
35
46
)
36
47
)
37
48
if year_min and year_max :
@@ -44,6 +55,7 @@ def getObservationsMaillesChilds(session, cd_ref, year_min=None, year_max=None):
44
55
geometry = json .loads (o .geojson_maille ),
45
56
properties = {
46
57
"id_maille" : o .id_maille ,
58
+ "type_code" : o .type_code ,
47
59
"nb_observations" : int (o .obs_nbr ),
48
60
"last_observation" : o .last_obs_year ,
49
61
},
@@ -60,13 +72,13 @@ def lastObservationsMailles(connection, mylimit, idPhoto):
60
72
tax.lb_nom, tax.nom_vern, tax.group2_inpn,
61
73
o.dateobs, o.altitude_retenue, o.id_observation,
62
74
medias.url, medias.chemin, medias.id_media,
63
- m.geojson_maille
75
+ m.geojson_4326 AS geom
64
76
FROM atlas.vm_observations_mailles obs
65
77
JOIN atlas.vm_taxons tax ON tax.cd_ref = obs.cd_ref
66
78
JOIN atlas.vm_observations o ON o.id_observation=ANY(obs.id_observations)
67
- JOIN atlas.t_mailles_territoire m ON m.id_maille=obs.id_maille
79
+ JOIN atlas.vm_cor_area_synthese m ON m.id_synthese=o.id_observation AND m.is_blurred_geom IS TRUE
68
80
LEFT JOIN atlas.vm_medias medias
69
- ON medias.cd_ref = obs.cd_ref AND medias.id_type = :thisID
81
+ ON medias.cd_ref = obs.cd_ref AND medias.id_type = 1
70
82
WHERE o.dateobs >= (CURRENT_TIMESTAMP - INTERVAL :thislimit)
71
83
ORDER BY o.dateobs DESC
72
84
"""
@@ -82,11 +94,12 @@ def lastObservationsMailles(connection, mylimit, idPhoto):
82
94
temp = {
83
95
"id_observation" : o .id_observation ,
84
96
"id_maille" : o .id_maille ,
97
+ "type_code" : o .type_code ,
85
98
"cd_ref" : o .cd_ref ,
86
99
"dateobs" : o .dateobs ,
87
100
"altitude_retenue" : o .altitude_retenue ,
88
101
"taxon" : taxon ,
89
- "geojson_maille" : json .loads (o .geojson_maille ),
102
+ "geojson_maille" : json .loads (o .geom ),
90
103
"group2_inpn" : deleteAccent (o .group2_inpn ),
91
104
"pathImg" : findPath (o ),
92
105
"id_media" : o .id_media ,
@@ -97,38 +110,33 @@ def lastObservationsMailles(connection, mylimit, idPhoto):
97
110
98
111
def lastObservationsCommuneMaille (connection , obs_limit , insee_code ):
99
112
sql = """
100
- WITH last_obs AS (
101
113
SELECT
102
- obs.id_observation , obs.cd_ref, obs.dateobs ,
114
+ obs.id_observations , obs.cd_ref, obs.type_code, obs.nbr, c.insee ,
103
115
COALESCE(t.nom_vern || ' | ', '') || t.lb_nom AS display_name,
104
- obs.the_geom_point AS l_geom
105
- FROM atlas.vm_observations AS obs
116
+ m.the_geom AS l_geom,
117
+ t.nom_vern, m.the_geom as l_geom,
118
+ m.geojson_maille, obs.id_maille
119
+ FROM atlas.vm_observations_mailles obs
120
+ JOIN atlas.t_mailles_territoire m ON m.id_maille = obs.id_maille
106
121
JOIN atlas.vm_communes AS c
107
- ON ST_Intersects(obs.the_geom_point , c.the_geom)
122
+ ON ST_Intersects(m.the_geom, c.the_geom) AND NOT ST_Touches(m.the_geom , c.the_geom)
108
123
JOIN atlas.vm_taxons AS t
109
124
ON obs.cd_ref = t.cd_ref
110
125
WHERE c.insee = :inseeCode
111
- ORDER BY obs.dateobs DESC
112
126
LIMIT :obsLimit
113
- )
114
- SELECT
115
- l.id_observation, l.cd_ref, l.display_name, m.id_maille, m.geojson_maille
116
- FROM atlas.t_mailles_territoire AS m
117
- JOIN last_obs AS l
118
- ON st_intersects(m.the_geom, l.l_geom)
119
- GROUP BY l.id_observation, l.cd_ref, l.display_name, m.id_maille, m.geojson_maille
120
- ORDER BY l.display_name
121
127
"""
122
128
results = connection .execute (text (sql ), inseeCode = insee_code , obsLimit = obs_limit )
123
129
observations = list ()
124
130
for r in results :
125
- # taxon = (r.nom_vern + " | " + r.lb_nom) if r.nom_vern else r.lb_nom
126
131
infos = {
127
132
"cd_ref" : r .cd_ref ,
133
+ "insee" : r .insee ,
128
134
"taxon" : r .display_name ,
129
135
"geojson_maille" : json .loads (r .geojson_maille ),
130
136
"id_maille" : r .id_maille ,
131
- "id_observation" : r .id_observation ,
137
+ "id_observation" : r .id_observations ,
138
+ "nb_observations" : r .nbr ,
139
+ "type_code" : r .type_code ,
132
140
}
133
141
observations .append (infos )
134
142
return observations
@@ -139,23 +147,29 @@ def getObservationsTaxonCommuneMaille(connection, insee, cd_ref):
139
147
sql = """
140
148
SELECT
141
149
o.cd_ref,
142
- t.id_maille,
143
- t.geojson_maille,
144
- extract(YEAR FROM o.dateobs)::INT AS annee
145
- FROM atlas.vm_observations AS o
146
- JOIN atlas.vm_communes AS c
147
- ON ST_INTERSECTS(o.the_geom_point, c.the_geom)
148
- JOIN atlas.t_mailles_territoire AS t
149
- ON ST_INTERSECTS(t.the_geom, o.the_geom_point)
150
+ o.id_maille,
151
+ o.type_code,
152
+ o.annee,
153
+ m.geojson_maille,
154
+ m.the_geom,
155
+ t.cd_ref,
156
+ t.nom_vern,
157
+ t.lb_nom
158
+ FROM atlas.vm_observations_mailles AS o
159
+ JOIN atlas.vm_taxons AS t ON t.cd_ref = o.cd_ref
160
+ JOIN atlas.t_mailles_territoire m ON m.id_maille = o.id_maille
161
+ JOIN atlas.vm_communes AS c ON c.insee = :thisInsee AND st_intersects(c.the_geom, m.the_geom) AND NOT st_touches(c.the_geom, m.the_geom)
150
162
WHERE o.cd_ref = :thiscdref
151
- AND c.insee = :thisInsee
152
163
ORDER BY id_maille
153
164
"""
154
165
observations = connection .execute (text (sql ), thisInsee = insee , thiscdref = cd_ref )
155
166
tabObs = list ()
156
167
for o in observations :
157
168
temp = {
158
169
"id_maille" : o .id_maille ,
170
+ "cd_ref" : o .cd_ref ,
171
+ "taxon" : format_taxon_name (o ),
172
+ "type_code" : o .type_code ,
159
173
"nb_observations" : 1 ,
160
174
"annee" : o .annee ,
161
175
"geojson_maille" : json .loads (o .geojson_maille ),
0 commit comments