Skip to content

Commit

Permalink
wip: fix municipality default meshes map view
Browse files Browse the repository at this point in the history
  • Loading branch information
jpm-cbna committed May 2, 2024
1 parent c560a91 commit 59c725f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 64 deletions.
61 changes: 32 additions & 29 deletions atlas/modeles/repositories/vmObservationsMaillesRepository.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,42 +93,45 @@ 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
61 changes: 26 additions & 35 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,7 +545,7 @@ function compare(a, b) {
return 0;
}

function printEspece(tabEspece, tabCdRef) {
function buildSpeciesEntries(tabEspece, tabCdRef) {
stringEspece = "";
i = 0;
while (i < tabEspece.length) {
Expand All @@ -567,11 +566,12 @@ function printEspece(tabEspece, tabCdRef) {
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>";
"<b>" + feature.properties.list_taxon.length + " espèces observées dans la maille&nbsp;: </b>" +
"<ul> " +
buildSpeciesEntries(feature.properties.list_taxon, feature.properties.list_cdref) +
"</ul>";

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

function styleMailleLastObs() {
Expand All @@ -584,37 +584,28 @@ 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) => {
var key = `${obs.id_maille}`;
if (!Object.hasOwn(features, key) ) {
features[key] = {
"type": "Feature",
"geometry": obs.geojson_maille,
"properties": {
"id_maille": obs.id_maille,
"list_cdref": [obs.cd_ref],
"list_taxon": [obs.taxon],
}
};
} else if (!features[key]["properties"]["list_cdref"].includes(obs.cd_ref)) {
features[key]["properties"]["list_cdref"].push(obs.cd_ref);
features[key]["properties"]["list_taxon"].push(obs.taxon);
}
myGeoJson.features.push({
type: "Feature",
properties: properties,
geometry: geometry,
});
// on avance jusqu' à j
i = j;
}
});

return myGeoJson;
geoJson = { type: "FeatureCollection", features: [] };
Object.keys(features).forEach((idMaille) => geoJson.features.push(features[idMaille]));
return geoJson;
}

function find_id_observation_in_array(tab_id, id_observation) {
Expand Down

0 comments on commit 59c725f

Please sign in to comment.