From 8da0f0008cce69020a65ad6318d06d23624158e4 Mon Sep 17 00:00:00 2001 From: Jean-Pascal MILCENT Date: Thu, 2 May 2024 11:45:46 +0200 Subject: [PATCH] fix: show right taxons names for municipality default meshes map view Fix #533. --- .../vmObservationsMaillesRepository.py | 59 ++++++------ atlas/static/mapGenerator.js | 96 +++++++++---------- 2 files changed, 74 insertions(+), 81 deletions(-) diff --git a/atlas/modeles/repositories/vmObservationsMaillesRepository.py b/atlas/modeles/repositories/vmObservationsMaillesRepository.py index d2ec6fdc..da1b3576 100644 --- a/atlas/modeles/repositories/vmObservationsMaillesRepository.py +++ b/atlas/modeles/repositories/vmObservationsMaillesRepository.py @@ -93,42 +93,43 @@ def lastObservationsMailles(connection, mylimit, idPhoto): return obsList -def lastObservationsCommuneMaille(connection, mylimit, insee): +def lastObservationsCommuneMaille(connection, obs_limit, insee_code): sql = """ WITH last_obs AS ( SELECT - obs.cd_ref, obs.dateobs, t.lb_nom, - t.nom_vern, obs.the_geom_point AS l_geom - FROM atlas.vm_observations obs - JOIN atlas.vm_communes c - ON ST_Intersects(obs.the_geom_point, c.the_geom) - JOIN atlas.vm_taxons t - ON obs.cd_ref = t.cd_ref - WHERE c.insee = :thisInsee + obs.id_observation, obs.cd_ref, obs.dateobs, + COALESCE(t.nom_vern || ' | ', '') || t.lb_nom AS display_name, + obs.the_geom_point AS l_geom + FROM atlas.vm_observations AS obs + JOIN atlas.vm_communes AS c + ON ST_Intersects(obs.the_geom_point, c.the_geom) + JOIN atlas.vm_taxons AS t + ON obs.cd_ref = t.cd_ref + WHERE c.insee = :inseeCode ORDER BY obs.dateobs DESC - LIMIT :thislimit + LIMIT :obsLimit ) - SELECT l.lb_nom, l.nom_vern, l.cd_ref, m.id_maille, m.geojson_maille - FROM atlas.t_mailles_territoire m - JOIN last_obs l - ON st_intersects(m.the_geom, l.l_geom) - GROUP BY l.lb_nom, l.cd_ref, m.id_maille, l.nom_vern, m.geojson_maille + SELECT + l.id_observation, l.cd_ref, l.display_name, m.id_maille, m.geojson_maille + FROM atlas.t_mailles_territoire AS m + JOIN last_obs AS l + ON st_intersects(m.the_geom, l.l_geom) + GROUP BY l.id_observation, l.cd_ref, l.display_name, m.id_maille, m.geojson_maille + ORDER BY l.display_name """ - observations = connection.execute(text(sql), thisInsee=insee, thislimit=mylimit) - obsList = list() - for o in observations: - if o.nom_vern: - taxon = o.nom_vern + " | " + "" + o.lb_nom + "" - else: - taxon = "" + o.lb_nom + "" - temp = { - "cd_ref": o.cd_ref, - "taxon": taxon, - "geojson_maille": json.loads(o.geojson_maille), - "id_maille": o.id_maille, + results = connection.execute(text(sql), inseeCode=insee_code, obsLimit=obs_limit) + observations = list() + for r in results: + # taxon = (r.nom_vern + " | " + r.lb_nom) if r.nom_vern else r.lb_nom + infos = { + "cd_ref": r.cd_ref, + "taxon": r.display_name, + "geojson_maille": json.loads(r.geojson_maille), + "id_maille": r.id_maille, + "id_observation": r.id_observation, } - obsList.append(temp) - return obsList + observations.append(infos) + return observations # Use for API diff --git a/atlas/static/mapGenerator.js b/atlas/static/mapGenerator.js index 58c5892c..55b5fd75 100644 --- a/atlas/static/mapGenerator.js +++ b/atlas/static/mapGenerator.js @@ -435,7 +435,6 @@ function onEachFeaturePointLastObs(feature, layer) { popupContent + "
" + - tabEspece[i] + - ""; - - i = i + 1; - } - return stringEspece; +function buildSpeciesEntries(taxons) { + rows = []; + taxons.forEach(taxon => { + href = `${configuration.URL_APPLICATION}/espece/${taxon.cdRef}` + rows.push(`
  • ${taxon.name}
  • `); + }); + return rows.join('\n'); } function onEachFeatureMailleLastObs(feature, layer) { - // Add class to be able to scroll the species list - popupContent = - "Espèces observées dans la maille:
      " + - printEspece(feature.properties.list_taxon, feature.properties.list_cdref) + - "
    "; + title = `${feature.properties.taxons.length} espèces observées dans la maille  : `; + rows = buildSpeciesEntries(feature.properties.taxons); + popupContent = `${title}`; - layer.bindPopup(popupContent); + layer.bindPopup(popupContent, { maxHeight: 300 }); } function styleMailleLastObs() { @@ -584,37 +572,41 @@ function styleMailleLastObs() { } function generateGeoJsonMailleLastObs(observations) { - // sort it because at each change of idMaille, the - // list_taxon is reset so not all species are displayed - observations = observations.sort((a,b) => compare(a, b)) - var i = 0; - myGeoJson = { type: "FeatureCollection", features: [] }; - while (i < observations.length) { - geometry = observations[i].geojson_maille; - idMaille = observations[i].id_maille; - properties = { - id_maille: idMaille, - list_taxon: [observations[i].taxon], - list_cdref: [observations[i].cd_ref], - list_id_observation: [observations[i].id_observation], - }; - var j = i + 1; - while (j < observations.length && observations[j].id_maille == idMaille) { - properties.list_taxon.push(observations[j].taxon); - properties.list_cdref.push(observations[j].cd_ref); - properties.list_id_observation.push(observations[j].id_observation); - j = j + 1; + var features = []; + observations.forEach((obs) => { + findedFeature = features.find( + (feat) => feat.properties.meshId === obs.id_maille + ); + if (!findedFeature) { + features.push({ + type: "Feature", + geometry: obs.geojson_maille, + properties: { + meshId: obs.id_maille, + taxons: [ + { + cdRef: obs.cd_ref, + name: obs.taxon, + }, + ], + }, + }); + } else if ( + !findedFeature.properties.taxons.find( + (taxon) => taxon.cdRef === obs.cd_ref + ) + ) { + findedFeature.properties.taxons.push({ + cdRef: obs.cd_ref, + name: obs.taxon, + }); } - myGeoJson.features.push({ - type: "Feature", - properties: properties, - geometry: geometry, - }); - // on avance jusqu' à j - i = j; - } + }); - return myGeoJson; + return { + type: "FeatureCollection", + features: features, + }; } function find_id_observation_in_array(tab_id, id_observation) {