Skip to content

Commit f165150

Browse files
committed
Merge commit from PR #555, #556, #557, issues #533, #531, #532.
Merge branch 'feat/sinp' into feat/sinp-paca
2 parents ca7458e + fa4a7fc commit f165150

8 files changed

+242
-135
lines changed

atlas/atlasAPI.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def searchTaxonAPI():
2020
session = utils.loadSession()
2121
search = request.args.get("search", "")
2222
limit = request.args.get("limit", 50)
23-
results = vmSearchTaxonRepository.listeTaxonsSearch(session, search, limit)
23+
results = vmSearchTaxonRepository.searchTaxons(session, search, limit)
2424
session.close()
2525
return jsonify(results)
2626

@@ -30,7 +30,7 @@ def searchCommuneAPI():
3030
session = utils.loadSession()
3131
search = request.args.get("search", "")
3232
limit = request.args.get("limit", 50)
33-
results = vmCommunesRepository.getCommunesSearch(session, search, limit)
33+
results = vmCommunesRepository.searchMunicipalities(session, search, limit)
3434
session.close()
3535
return jsonify(results)
3636

atlas/modeles/entities/vmSearchTaxon.py

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class VmSearchTaxon(Base):
2020
metadata,
2121
Column("cd_ref", Integer, primary_key=True, unique=True),
2222
Column("cd_nom", Integer),
23+
Column("display_name", String),
2324
Column("search_name", String),
2425
schema="atlas",
2526
autoload=True,

atlas/modeles/repositories/vmCommunesRepository.py

+14-13
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import ast
44

5-
from flask import current_app
65
from sqlalchemy import distinct
76
from sqlalchemy.sql import text
87
from sqlalchemy.sql.expression import func
@@ -19,20 +18,22 @@ def getAllCommunes(session):
1918
return communeList
2019

2120

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

27-
req = req.order_by(VmCommunes.commune_maj)
24+
query = (
25+
session.query(
26+
distinct(VmCommunes.commune_maj),
27+
VmCommunes.insee,
28+
func.length(VmCommunes.commune_maj),
29+
)
30+
.filter(func.unaccent(VmCommunes.commune_maj).ilike(func.unaccent(like_search)))
31+
.order_by(VmCommunes.commune_maj)
32+
.limit(limit)
33+
)
34+
results = query.all()
2835

29-
req = req.limit(limit).all()
30-
31-
communeList = list()
32-
for r in req:
33-
temp = {"label": r[0], "value": r[1]}
34-
communeList.append(temp)
35-
return communeList
36+
return [{"label": r[0], "value": r[1]} for r in results]
3637

3738

3839
def getCommuneFromInsee(connection, insee):

atlas/modeles/repositories/vmObservationsMaillesRepository.py

+30-29
Original file line numberDiff line numberDiff line change
@@ -93,42 +93,43 @@ def lastObservationsMailles(connection, mylimit, idPhoto):
9393
return obsList
9494

9595

96-
def lastObservationsCommuneMaille(connection, mylimit, insee):
96+
def lastObservationsCommuneMaille(connection, obs_limit, insee_code):
9797
sql = """
9898
WITH last_obs AS (
9999
SELECT
100-
obs.cd_ref, obs.dateobs, t.lb_nom,
101-
t.nom_vern, obs.the_geom_point AS l_geom
102-
FROM atlas.vm_observations obs
103-
JOIN atlas.vm_communes c
104-
ON ST_Intersects(obs.the_geom_point, c.the_geom)
105-
JOIN atlas.vm_taxons t
106-
ON obs.cd_ref = t.cd_ref
107-
WHERE c.insee = :thisInsee
100+
obs.id_observation, obs.cd_ref, obs.dateobs,
101+
COALESCE(t.nom_vern || ' | ', '') || t.lb_nom AS display_name,
102+
obs.the_geom_point AS l_geom
103+
FROM atlas.vm_observations AS obs
104+
JOIN atlas.vm_communes AS c
105+
ON ST_Intersects(obs.the_geom_point, c.the_geom)
106+
JOIN atlas.vm_taxons AS t
107+
ON obs.cd_ref = t.cd_ref
108+
WHERE c.insee = :inseeCode
108109
ORDER BY obs.dateobs DESC
109-
LIMIT :thislimit
110+
LIMIT :obsLimit
110111
)
111-
SELECT l.lb_nom, l.nom_vern, l.cd_ref, m.id_maille, m.geojson_maille
112-
FROM atlas.t_mailles_territoire m
113-
JOIN last_obs l
114-
ON st_intersects(m.the_geom, l.l_geom)
115-
GROUP BY l.lb_nom, l.cd_ref, m.id_maille, l.nom_vern, m.geojson_maille
112+
SELECT
113+
l.id_observation, l.cd_ref, l.display_name, m.id_maille, m.geojson_maille
114+
FROM atlas.t_mailles_territoire AS m
115+
JOIN last_obs AS l
116+
ON st_intersects(m.the_geom, l.l_geom)
117+
GROUP BY l.id_observation, l.cd_ref, l.display_name, m.id_maille, m.geojson_maille
118+
ORDER BY l.display_name
116119
"""
117-
observations = connection.execute(text(sql), thisInsee=insee, thislimit=mylimit)
118-
obsList = list()
119-
for o in observations:
120-
if o.nom_vern:
121-
taxon = o.nom_vern + " | " + o.lb_nom
122-
else:
123-
taxon = o.lb_nom
124-
temp = {
125-
"cd_ref": o.cd_ref,
126-
"taxon": taxon,
127-
"geojson_maille": json.loads(o.geojson_maille),
128-
"id_maille": o.id_maille,
120+
results = connection.execute(text(sql), inseeCode=insee_code, obsLimit=obs_limit)
121+
observations = list()
122+
for r in results:
123+
# taxon = (r.nom_vern + " | " + r.lb_nom) if r.nom_vern else r.lb_nom
124+
infos = {
125+
"cd_ref": r.cd_ref,
126+
"taxon": r.display_name,
127+
"geojson_maille": json.loads(r.geojson_maille),
128+
"id_maille": r.id_maille,
129+
"id_observation": r.id_observation,
129130
}
130-
obsList.append(temp)
131-
return obsList
131+
observations.append(infos)
132+
return observations
132133

133134

134135
# Use for API

atlas/modeles/repositories/vmSearchTaxonRepository.py

+16-16
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def listeTaxons(session):
1919
return taxonList
2020

2121

22-
def listeTaxonsSearch(session, search, limit=50):
22+
def searchTaxons(session, search, limit=50):
2323
"""
2424
Recherche dans la VmSearchTaxon en ilike
2525
Utilisé pour l'autocomplétion de la recherche de taxon
@@ -34,20 +34,20 @@ def listeTaxonsSearch(session, search, limit=50):
3434
label = search_name
3535
value = cd_ref
3636
"""
37-
38-
req = session.query(
39-
VmSearchTaxon.search_name,
40-
VmSearchTaxon.cd_ref,
41-
func.similarity(VmSearchTaxon.search_name, search).label("idx_trgm"),
42-
).distinct()
43-
44-
search = search.replace(" ", "%")
45-
req = (
46-
req.filter(VmSearchTaxon.search_name.ilike("%" + search + "%"))
47-
.order_by(desc("idx_trgm"))
48-
.order_by(VmSearchTaxon.cd_ref == VmSearchTaxon.cd_nom)
49-
.limit(limit)
37+
like_search = "%" + search.replace(" ", "%") + "%"
38+
39+
query = (
40+
session.query(
41+
VmSearchTaxon.display_name,
42+
VmSearchTaxon.cd_ref,
43+
func.similarity(VmSearchTaxon.search_name, search).label("idx_trgm"),
44+
)
45+
.distinct()
46+
.filter(func.unaccent(VmSearchTaxon.search_name).ilike(func.unaccent(like_search)))
47+
.order_by(desc("idx_trgm"))
48+
.order_by(VmSearchTaxon.cd_ref == VmSearchTaxon.cd_nom)
49+
.limit(limit)
5050
)
51-
data = req.all()
51+
results = query.all()
5252

53-
return [{"label": d[0], "value": d[1]} for d in data]
53+
return [{"label": r[0], "value": r[1]} for r in results]

atlas/static/mapGenerator.js

+54-47
Original file line numberDiff line numberDiff line change
@@ -130,17 +130,17 @@ function generateMap(zoomHomeButton) {
130130
fullScreenButton.attr("data-toggle", "tooltip");
131131
fullScreenButton.attr("data-original-title", "Fullscreen");
132132
$(".leaflet-control-fullscreen-button").removeAttr("title");
133-
133+
134134
// Add scale depending on the configuration
135135
if (configuration.MAP.ENABLE_SCALE) {
136136
L.control.scale(
137137
{
138-
imperial: false,
138+
imperial: false,
139139
position: 'bottomright'
140140
}
141141
).addTo(map);
142142
}
143-
143+
144144
return map;
145145
}
146146

@@ -425,7 +425,10 @@ function onEachFeaturePointLastObs(feature, layer) {
425425
popupContent +
426426
"</br> <a href='" +
427427
configuration.URL_APPLICATION +
428-
428+
<<<<<<< HEAD
429+
430+
=======
431+
>>>>>>> 8da0f00 (fix: show right taxons names for municipality default meshes map view)
429432
language +
430433
"/espece/" +
431434
feature.properties.cd_ref +
@@ -537,31 +540,28 @@ function compare(a, b) {
537540
return 0;
538541
}
539542

540-
function printEspece(tabEspece, tabCdRef) {
541-
stringEspece = "";
542-
i = 0;
543-
while (i < tabEspece.length) {
544-
stringEspece +=
545-
"<li> <a href='" +
546-
configuration.URL_APPLICATION +
547-
"/espece/" +
548-
tabCdRef[i] +
549-
"'>" +
550-
tabEspece[i] +
551-
"</li>";
552-
553-
i = i + 1;
554-
}
555-
return stringEspece;
543+
function buildSpeciesEntries(taxons) {
544+
rows = [];
545+
taxons.forEach(taxon => {
546+
href = `${configuration.URL_APPLICATION}/espece/${taxon.cdRef}`
547+
rows.push(`<li><a href="${href}">${taxon.name}</li>`);
548+
});
549+
return rows.join('\n');
556550
}
557551

558552
function onEachFeatureMailleLastObs(feature, layer) {
553+
<<<<<<< HEAD
559554
popupContent =
560555
"<b>Espèces observées dans la maille: </b> <ul> " +
561556
printEspece(feature.properties.list_taxon, feature.properties.list_cdref) +
562557
"</ul>";
558+
=======
559+
title = `${feature.properties.taxons.length} espèces observées dans la maille &nbsp;: `;
560+
rows = buildSpeciesEntries(feature.properties.taxons);
561+
popupContent = `<b>${title}</b><ul>${rows}</ul>`;
562+
>>>>>>> 8da0f00 (fix: show right taxons names for municipality default meshes map view)
563563

564-
layer.bindPopup(popupContent);
564+
layer.bindPopup(popupContent, { maxHeight: 300 });
565565
}
566566

567567
function styleMailleLastObs() {
@@ -574,34 +574,41 @@ function styleMailleLastObs() {
574574
}
575575

576576
function generateGeoJsonMailleLastObs(observations) {
577-
var i = 0;
578-
myGeoJson = { type: "FeatureCollection", features: [] };
579-
while (i < observations.length) {
580-
geometry = observations[i].geojson_maille;
581-
idMaille = observations[i].id_maille;
582-
properties = {
583-
id_maille: idMaille,
584-
list_taxon: [observations[i].taxon],
585-
list_cdref: [observations[i].cd_ref],
586-
list_id_observation: [observations[i].id_observation],
587-
};
588-
var j = i + 1;
589-
while (j < observations.length && observations[j].id_maille == idMaille) {
590-
properties.list_taxon.push(observations[j].taxon);
591-
properties.list_cdref.push(observations[j].cd_ref);
592-
properties.list_id_observation.push(observations[j].id_observation);
593-
j = j + 1;
577+
var features = [];
578+
observations.forEach((obs) => {
579+
findedFeature = features.find(
580+
(feat) => feat.properties.meshId === obs.id_maille
581+
);
582+
if (!findedFeature) {
583+
features.push({
584+
type: "Feature",
585+
geometry: obs.geojson_maille,
586+
properties: {
587+
meshId: obs.id_maille,
588+
taxons: [
589+
{
590+
cdRef: obs.cd_ref,
591+
name: obs.taxon,
592+
},
593+
],
594+
},
595+
});
596+
} else if (
597+
!findedFeature.properties.taxons.find(
598+
(taxon) => taxon.cdRef === obs.cd_ref
599+
)
600+
) {
601+
findedFeature.properties.taxons.push({
602+
cdRef: obs.cd_ref,
603+
name: obs.taxon,
604+
});
594605
}
595-
myGeoJson.features.push({
596-
type: "Feature",
597-
properties: properties,
598-
geometry: geometry,
599-
});
600-
// on avance jusqu' à j
601-
i = j;
602-
}
606+
});
603607

604-
return myGeoJson;
608+
return {
609+
type: "FeatureCollection",
610+
features: features,
611+
};
605612
}
606613

607614
function find_id_observation_in_array(tab_id, id_observation) {

0 commit comments

Comments
 (0)