Skip to content

Commit

Permalink
fix: show right taxons names for municipality default meshes map view
Browse files Browse the repository at this point in the history
Fix #533.
  • Loading branch information
jpm-cbna committed May 7, 2024
1 parent 3978263 commit 8da0f00
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 81 deletions.
59 changes: 30 additions & 29 deletions atlas/modeles/repositories/vmObservationsMaillesRepository.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 + " | " + "<i>" + o.lb_nom + "</i>"
else:
taxon = "<i>" + o.lb_nom + "</i>"
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
Expand Down
96 changes: 44 additions & 52 deletions atlas/static/mapGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,6 @@ function onEachFeaturePointLastObs(feature, layer) {
popupContent +
"</br> <a href='" +
configuration.URL_APPLICATION +

language +
"/espece/" +
feature.properties.cd_ref +
Expand Down Expand Up @@ -546,32 +545,21 @@ function compare(a, b) {
return 0;
}

function printEspece(tabEspece, tabCdRef) {
stringEspece = "";
i = 0;
while (i < tabEspece.length) {
stringEspece +=
"<li> <a href='" +
configuration.URL_APPLICATION +
"/espece/" +
tabCdRef[i] +
"'>" +
tabEspece[i] +
"</li>";

i = i + 1;
}
return stringEspece;
function buildSpeciesEntries(taxons) {
rows = [];
taxons.forEach(taxon => {
href = `${configuration.URL_APPLICATION}/espece/${taxon.cdRef}`
rows.push(`<li><a href="${href}">${taxon.name}</li>`);
});
return rows.join('\n');
}

function onEachFeatureMailleLastObs(feature, layer) {
// Add class to be able to scroll the species list
popupContent =
"<b>Espèces observées dans la maille: </b> <div class=\"species-grid-popup\"><ul> " +
printEspece(feature.properties.list_taxon, feature.properties.list_cdref) +
"</ul></div>";
title = `${feature.properties.taxons.length} espèces observées dans la maille &nbsp;: `;
rows = buildSpeciesEntries(feature.properties.taxons);
popupContent = `<b>${title}</b><ul>${rows}</ul>`;

layer.bindPopup(popupContent);
layer.bindPopup(popupContent, { maxHeight: 300 });
}

function styleMailleLastObs() {
Expand All @@ -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) {
Expand Down

0 comments on commit 8da0f00

Please sign in to comment.