Skip to content

Commit 11ad283

Browse files
committed
Merge branch 'add_home_territory_pnx' into recette
2 parents e6f7a53 + 1b2be4f commit 11ad283

File tree

3 files changed

+49
-48
lines changed

3 files changed

+49
-48
lines changed

atlas/modeles/repositories/vmObservationsMaillesRepository.py

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -75,34 +75,38 @@ def getObservationsMaillesChilds(session, cd_ref, year_min=None, year_max=None):
7575

7676
def territoryObservationsMailles(connection):
7777
sql = """
78+
WITH obs_in_area AS (
79+
SELECT
80+
obs.id_observation,
81+
obs.cd_ref,
82+
date_part('year', obs.dateobs) AS annee
83+
FROM atlas.vm_observations obs
84+
)
7885
SELECT
79-
obs.id_maille,
80-
obs.nbr AS obs_nbr,
81-
obs.type_code,
82-
area.area_geojson,
83-
MAX(extract(YEAR FROM o.dateobs)) AS last_observation
84-
FROM atlas.vm_observations_mailles obs
85-
JOIN atlas.vm_l_areas area ON area.id_area=obs.id_maille
86-
JOIN atlas.vm_observations AS o ON o.id_observation = ANY(obs.id_observations)
87-
GROUP BY obs.id_maille, obs.nbr, obs.type_code, area.area_geojson
86+
json_build_object(
87+
'type', 'FeatureCollection',
88+
'features', json_agg(ST_AsGeoJSON(features.*)::json)
89+
) AS observations_features
90+
FROM (
91+
SELECT
92+
COUNT(obs_in_area.id_observation) AS nb_observations,
93+
COUNT(DISTINCT obs_in_area.cd_ref) AS nb_cd_ref,
94+
json_agg(DISTINCT jsonb_build_object(
95+
'name', (COALESCE(t.nom_vern || ' | ', '') || t.lb_nom),
96+
'cdRef', t.cd_ref)) AS taxons,
97+
obs.type_code,
98+
obs.id_maille,
99+
vla.the_geom,
100+
MAX(obs_in_area.annee)
101+
FROM obs_in_area
102+
JOIN atlas.vm_observations_mailles obs ON obs_in_area.id_observation = ANY(obs.id_observations)
103+
JOIN atlas.vm_l_areas vla ON vla.id_area=obs.id_maille
104+
JOIN atlas.vm_taxons AS t ON t.cd_ref = obs_in_area.cd_ref
105+
GROUP BY obs.type_code, obs.id_maille, vla.the_geom) AS features
88106
"""
89107

90-
observations = connection.execute(text(sql))
91-
return FeatureCollection(
92-
[
93-
Feature(
94-
id=o.id_maille,
95-
geometry=json.loads(o.area_geojson),
96-
properties={
97-
"id_maille": o.id_maille,
98-
"type_code": o.type_code,
99-
"nb_observations": int(o.obs_nbr),
100-
"last_observation": o.last_observation,
101-
},
102-
)
103-
for o in observations
104-
]
105-
)
108+
query = connection.execute(text(sql))
109+
return dict(query.all()[0])
106110

107111

108112
# last observation for index.html

atlas/static/mapAreas.js

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -156,31 +156,29 @@ function displayObsTaxonMaille(areaCode, cd_ref) {
156156
});
157157
}
158158

159-
function refreshObsArea() {
160-
$("#taxonList ul").on("click", "#taxonListItem", function () {
159+
function refreshObsArea(elem) {
160+
$(this)
161+
.siblings()
162+
.removeClass("current");
163+
$(this).addClass("current");
164+
if (configuration.AFFICHAGE_MAILLE) {
165+
displayObsTaxonMaille(elem.currentTarget.getAttribute("area-code"), elem.currentTarget.getAttribute("cdref"));
166+
} else {
167+
displayObsTaxon(elem.currentTarget.getAttribute("area-code"), elem.currentTarget.getAttribute("cdref"));
168+
}
169+
const name = elem.currentTarget.querySelector("#name").innerHTML;
170+
$("#titleMap").fadeOut(500, function () {
161171
$(this)
162-
.siblings()
163-
.removeClass("current");
164-
$(this).addClass("current");
165-
if (configuration.AFFICHAGE_MAILLE) {
166-
displayObsTaxonMaille(this.getAttribute("area-code"), this.getAttribute("cdref"));
167-
} else {
168-
displayObsTaxon(this.getAttribute("area-code"), this.getAttribute("cdref"));
169-
}
170-
var name = $(this)
171-
.find("#name")
172-
.html();
173-
$("#titleMap").fadeOut(500, function () {
174-
$(this)
175-
.html("Observations du taxon : " + name)
176-
.fadeIn(500);
177-
});
172+
.html("Observations du taxon : " + name)
173+
.fadeIn(500);
178174
});
179175
}
180176

181177
$(document).ready(function () {
182178
$("#loaderSpinner").hide();
183179
if (configuration.INTERACTIVE_MAP_LIST) {
184-
refreshObsArea();
180+
$("#taxonList ul").on("click", "#taxonListItem", elem => {
181+
refreshObsArea(elem);
182+
});
185183
}
186184
});

atlas/static/mapHome.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,11 @@ $(function(){
2525
// display maille layer
2626
fetch(`/api/observationsMailleTerritory`)
2727
.then(response => response.json())
28-
.then(data => {
29-
observations = data
30-
displayMailleLayer(observations);
28+
.then(observations => {
29+
displayMailleLayer(observations.observations_features);
3130
})
3231
.catch(error => {
33-
console.log('Error fetching data: ', error);
32+
console.log('Error fetching observations: ', error);
3433
});
3534

3635

0 commit comments

Comments
 (0)