Skip to content

Commit

Permalink
Merge commit from PR #555, #556, #557, issues #533, #531, #532.
Browse files Browse the repository at this point in the history
Merge branch 'feat/sinp' into feat/sinp-aura
  • Loading branch information
jpm-cbna committed May 7, 2024
2 parents c56a667 + fa4a7fc commit 996ba91
Show file tree
Hide file tree
Showing 8 changed files with 242 additions and 135 deletions.
4 changes: 2 additions & 2 deletions atlas/atlasAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def searchTaxonAPI():
session = utils.loadSession()
search = request.args.get("search", "")
limit = request.args.get("limit", 50)
results = vmSearchTaxonRepository.listeTaxonsSearch(session, search, limit)
results = vmSearchTaxonRepository.searchTaxons(session, search, limit)
session.close()
return jsonify(results)

Expand All @@ -30,7 +30,7 @@ def searchCommuneAPI():
session = utils.loadSession()
search = request.args.get("search", "")
limit = request.args.get("limit", 50)
results = vmCommunesRepository.getCommunesSearch(session, search, limit)
results = vmCommunesRepository.searchMunicipalities(session, search, limit)
session.close()
return jsonify(results)

Expand Down
1 change: 1 addition & 0 deletions atlas/modeles/entities/vmSearchTaxon.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class VmSearchTaxon(Base):
metadata,
Column("cd_ref", Integer, primary_key=True, unique=True),
Column("cd_nom", Integer),
Column("display_name", String),
Column("search_name", String),
schema="atlas",
autoload=True,
Expand Down
27 changes: 14 additions & 13 deletions atlas/modeles/repositories/vmCommunesRepository.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import ast

from flask import current_app
from sqlalchemy import distinct
from sqlalchemy.sql import text
from sqlalchemy.sql.expression import func
Expand All @@ -19,20 +18,22 @@ def getAllCommunes(session):
return communeList


def getCommunesSearch(session, search, limit=50):
req = session.query(
distinct(VmCommunes.commune_maj), VmCommunes.insee, func.length(VmCommunes.commune_maj)
).filter(VmCommunes.commune_maj.ilike("%" + search + "%"))
def searchMunicipalities(session, search, limit=50):
like_search = "%" + search.replace(" ", "%") + "%"

req = req.order_by(VmCommunes.commune_maj)
query = (
session.query(
distinct(VmCommunes.commune_maj),
VmCommunes.insee,
func.length(VmCommunes.commune_maj),
)
.filter(func.unaccent(VmCommunes.commune_maj).ilike(func.unaccent(like_search)))
.order_by(VmCommunes.commune_maj)
.limit(limit)
)
results = query.all()

req = req.limit(limit).all()

communeList = list()
for r in req:
temp = {"label": r[0], "value": r[1]}
communeList.append(temp)
return communeList
return [{"label": r[0], "value": r[1]} for r in results]


def getCommuneFromInsee(connection, insee):
Expand Down
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 + " | " + 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
Expand Down
32 changes: 16 additions & 16 deletions atlas/modeles/repositories/vmSearchTaxonRepository.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def listeTaxons(session):
return taxonList


def listeTaxonsSearch(session, search, limit=50):
def searchTaxons(session, search, limit=50):
"""
Recherche dans la VmSearchTaxon en ilike
Utilisé pour l'autocomplétion de la recherche de taxon
Expand All @@ -34,20 +34,20 @@ def listeTaxonsSearch(session, search, limit=50):
label = search_name
value = cd_ref
"""

req = session.query(
VmSearchTaxon.search_name,
VmSearchTaxon.cd_ref,
func.similarity(VmSearchTaxon.search_name, search).label("idx_trgm"),
).distinct()

search = search.replace(" ", "%")
req = (
req.filter(VmSearchTaxon.search_name.ilike("%" + search + "%"))
.order_by(desc("idx_trgm"))
.order_by(VmSearchTaxon.cd_ref == VmSearchTaxon.cd_nom)
.limit(limit)
like_search = "%" + search.replace(" ", "%") + "%"

query = (
session.query(
VmSearchTaxon.display_name,
VmSearchTaxon.cd_ref,
func.similarity(VmSearchTaxon.search_name, search).label("idx_trgm"),
)
.distinct()
.filter(func.unaccent(VmSearchTaxon.search_name).ilike(func.unaccent(like_search)))
.order_by(desc("idx_trgm"))
.order_by(VmSearchTaxon.cd_ref == VmSearchTaxon.cd_nom)
.limit(limit)
)
data = req.all()
results = query.all()

return [{"label": d[0], "value": d[1]} for d in data]
return [{"label": r[0], "value": r[1]} for r in results]
101 changes: 54 additions & 47 deletions atlas/static/mapGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,17 +130,17 @@ function generateMap(zoomHomeButton) {
fullScreenButton.attr("data-toggle", "tooltip");
fullScreenButton.attr("data-original-title", "Fullscreen");
$(".leaflet-control-fullscreen-button").removeAttr("title");

// Add scale depending on the configuration
if (configuration.MAP.ENABLE_SCALE) {
L.control.scale(
{
imperial: false,
imperial: false,
position: 'bottomright'
}
).addTo(map);
}

return map;
}

Expand Down Expand Up @@ -425,7 +425,10 @@ function onEachFeaturePointLastObs(feature, layer) {
popupContent +
"</br> <a href='" +
configuration.URL_APPLICATION +

<<<<<<< HEAD

=======
>>>>>>> 8da0f00 (fix: show right taxons names for municipality default meshes map view)
language +
"/espece/" +
feature.properties.cd_ref +
Expand Down Expand Up @@ -537,31 +540,28 @@ 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) {
<<<<<<< HEAD
popupContent =
"<b>Espèces observées dans la maille: </b> <ul> " +
printEspece(feature.properties.list_taxon, feature.properties.list_cdref) +
"</ul>";
=======
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>`;
>>>>>>> 8da0f00 (fix: show right taxons names for municipality default meshes map view)

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

function styleMailleLastObs() {
Expand All @@ -574,34 +574,41 @@ function styleMailleLastObs() {
}

function generateGeoJsonMailleLastObs(observations) {
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
Loading

0 comments on commit 996ba91

Please sign in to comment.