Skip to content

Commit 93a6c03

Browse files
committed
feat: Add territory part on home page
Ajout d'une carte territoire ainsi que la liste des espèces sur celui-ci. L'affichage de cette carte est en mailles.
1 parent de3994f commit 93a6c03

File tree

11 files changed

+272
-47
lines changed

11 files changed

+272
-47
lines changed

atlas/atlasAPI.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,12 @@ def getObservationsGenericApi(cd_ref: int):
9494
[type]: [description]
9595
"""
9696
session = db.session
97-
if current_app.config["AFFICHAGE_MAILLE"]:
97+
if current_app.config["AFFICHAGE_TERRITOIRE_OBS"]:
98+
observations = vmObservationsMaillesRepository.getObservationsMaillesTerritorySpecies(
99+
session,
100+
cd_ref,
101+
)
102+
elif current_app.config["AFFICHAGE_MAILLE"]:
98103
observations = vmObservationsMaillesRepository.getObservationsMaillesChilds(
99104
session,
100105
cd_ref,

atlas/atlasRoutes.py

+11
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,13 @@ def indexMedias(image):
158158
)
159159

160160

161+
def get_territory_mailles_obs(connection):
162+
current_app.logger.debug("start AFFICHAGE_TERRITORY")
163+
observations = vmObservationsMaillesRepository.territoryObservationsMailles(connection)
164+
current_app.logger.debug("end AFFICHAGE_TERRITORY")
165+
return observations
166+
167+
161168
@main.route("/", methods=["GET", "POST"])
162169
def index():
163170
session = db.session
@@ -180,6 +187,8 @@ def index():
180187
current_app.config["ATTR_MAIN_PHOTO"],
181188
)
182189
current_app.logger.debug("end AFFICHAGE_PRECIS")
190+
elif current_app.config["AFFICHAGE_TERRITOIRE_OBS"]:
191+
observations = get_territory_mailles_obs(connection)
183192
else:
184193
observations = []
185194

@@ -204,6 +213,7 @@ def index():
204213
else:
205214
lastDiscoveries = []
206215

216+
listTaxons = vmTaxonsRepository.getTaxonsTerritory(connection)
207217
connection.close()
208218
session.close()
209219

@@ -214,6 +224,7 @@ def index():
214224

215225
return render_template(
216226
"templates/home/_main.html",
227+
listTaxons=listTaxons,
217228
observations=observations,
218229
mostViewTaxon=mostViewTaxon,
219230
customStatMedias=customStatMedias,

atlas/configuration/config_schema.py

+1
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ class Meta:
149149
OREJIME_TRANSLATIONS = fields.Dict(load_default=orijime_default_translations)
150150
AFFICHAGE_STAT_GLOBALES = fields.Boolean(load_default=True)
151151
AFFICHAGE_DERNIERES_OBS = fields.Boolean(load_default=True)
152+
AFFICHAGE_TERRITOIRE_OBS = fields.Boolean(load_default=False)
152153
AFFICHAGE_EN_CE_MOMENT = fields.Boolean(load_default=True)
153154
AFFICHAGE_RANG_STAT = fields.Boolean(load_default=True)
154155
AFFICHAGE_NOUVELLES_ESPECES = fields.Boolean(load_default=True)

atlas/modeles/repositories/vmObservationsMaillesRepository.py

+98
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,67 @@
55

66
from atlas.modeles.entities.vmObservations import VmObservationsMailles
77
from atlas.modeles.entities.vmAreas import VmAreas
8+
from atlas.modeles.entities.tMaillesTerritoire import TMaillesTerritoire
9+
from atlas.modeles.entities.vmTaxons import VmTaxons
810
from atlas.modeles.utils import deleteAccent, findPath
911

1012

13+
def getObservationsMaillesTerritorySpecies(session, cd_ref):
14+
"""
15+
Retourne les mailles et le nombre d'observation par maille pour un taxon et ses enfants
16+
sous forme d'un geojson
17+
"""
18+
query = func.atlas.find_all_taxons_childs(cd_ref)
19+
taxons_ids = session.scalars(query).all()
20+
taxons_ids.append(cd_ref)
21+
22+
query = (
23+
session.query(
24+
VmObservationsMailles.id_maille,
25+
TMaillesTerritoire.geojson_maille,
26+
func.max(VmObservationsMailles.annee).label("last_obs_year"),
27+
func.sum(VmObservationsMailles.nbr).label("obs_nbr"),
28+
VmObservationsMailles.type_code,
29+
VmTaxons.cd_ref,
30+
VmTaxons.nom_vern,
31+
VmTaxons.lb_nom,
32+
)
33+
.join(
34+
TMaillesTerritoire,
35+
TMaillesTerritoire.id_maille == VmObservationsMailles.id_maille,
36+
)
37+
.join(
38+
VmTaxons,
39+
VmTaxons.cd_ref == VmObservationsMailles.cd_ref,
40+
)
41+
.filter(VmObservationsMailles.cd_ref == any_(taxons_ids))
42+
.group_by(
43+
VmObservationsMailles.id_maille,
44+
TMaillesTerritoire.geojson_maille,
45+
VmObservationsMailles.type_code,
46+
VmTaxons.cd_ref,
47+
VmTaxons.nom_vern,
48+
VmTaxons.lb_nom,
49+
)
50+
)
51+
52+
return FeatureCollection(
53+
[
54+
Feature(
55+
id=o.id_maille,
56+
geojson_maille=json.loads(o.geojson_maille),
57+
id_maille=o.id_maille,
58+
type_code=o.type_code,
59+
nb_observations=int(o.obs_nbr),
60+
last_observation=o.last_obs_year,
61+
cd_ref=o.cd_ref,
62+
taxon=format_taxon_name(o),
63+
)
64+
for o in query.all()
65+
]
66+
)
67+
68+
1169
def format_taxon_name(observation):
1270
if observation.nom_vern:
1371
inter = observation.nom_vern.split(",")
@@ -65,6 +123,46 @@ def getObservationsMaillesChilds(session, cd_ref, year_min=None, year_max=None):
65123
)
66124

67125

126+
def territoryObservationsMailles(connection):
127+
sql = """
128+
SELECT obs.cd_ref, obs.id_maille, obs.nbr, obs.type_code,
129+
tax.lb_nom, tax.nom_vern, tax.group2_inpn,
130+
medias.url, medias.chemin, medias.id_media,
131+
st_asgeojson(m.geojson_maille) AS geom
132+
FROM atlas.vm_observations_mailles obs
133+
JOIN atlas.vm_taxons tax ON tax.cd_ref = obs.cd_ref
134+
JOIN atlas.t_mailles_territoire m ON m.id_maille=obs.id_maille
135+
LEFT JOIN atlas.vm_medias medias
136+
ON medias.cd_ref = obs.cd_ref AND medias.id_type = 1
137+
GROUP BY obs.cd_ref, obs.id_maille, obs.nbr,
138+
tax.lb_nom, tax.nom_vern, tax.group2_inpn,
139+
medias.url, medias.chemin, medias.id_media,
140+
m.geojson_maille,
141+
obs.type_code
142+
"""
143+
144+
observations = connection.execute(text(sql))
145+
obsList = list()
146+
for o in observations:
147+
if o.nom_vern:
148+
inter = o.nom_vern.split(",")
149+
taxon = inter[0] + " | <i>" + o.lb_nom + "</i>"
150+
else:
151+
taxon = "<i>" + o.lb_nom + "</i>"
152+
temp = {
153+
"id_maille": o.id_maille,
154+
"type_code": o.type_code,
155+
"cd_ref": o.cd_ref,
156+
"nb_observations": o.nbr,
157+
"taxon": taxon,
158+
"geojson_maille": json.loads(o.geom),
159+
"group2_inpn": deleteAccent(o.group2_inpn),
160+
"pathImg": findPath(o),
161+
}
162+
obsList.append(temp)
163+
return obsList
164+
165+
68166
# last observation for index.html
69167
def lastObservationsMailles(connection, mylimit, idPhoto):
70168
sql = """

atlas/modeles/repositories/vmTaxonsRepository.py

+37
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,43 @@
66
from atlas.modeles import utils
77

88

9+
def getTaxonsTerritory(connection):
10+
sql = """
11+
SELECT DISTINCT
12+
o.cd_ref, max(date_part('year'::text, o.dateobs)) as last_obs,
13+
COUNT(o.id_observation) AS nb_obs, t.nom_complet_html, t.nom_vern,
14+
t.group2_inpn, t.patrimonial, t.protection_stricte,
15+
m.url, m.chemin, m.id_media
16+
FROM atlas.vm_observations o
17+
JOIN atlas.vm_taxons t ON t.cd_ref=o.cd_ref
18+
LEFT JOIN atlas.vm_medias m ON m.cd_ref=o.cd_ref AND m.id_type={}
19+
GROUP BY o.cd_ref, t.nom_vern, t.nom_complet_html, t.group2_inpn,
20+
t.patrimonial, t.protection_stricte, m.url, m.chemin, m.id_media
21+
ORDER BY nb_obs DESC
22+
""".format(
23+
current_app.config["ATTR_MAIN_PHOTO"]
24+
)
25+
req = connection.execute(text(sql))
26+
taxonCommunesList = list()
27+
nbObsTotal = 0
28+
for r in req:
29+
temp = {
30+
"nom_complet_html": r.nom_complet_html,
31+
"nb_obs": r.nb_obs,
32+
"nom_vern": r.nom_vern,
33+
"cd_ref": r.cd_ref,
34+
"last_obs": r.last_obs,
35+
"group2_inpn": utils.deleteAccent(r.group2_inpn),
36+
"patrimonial": r.patrimonial,
37+
"protection_stricte": r.protection_stricte,
38+
"path": utils.findPath(r),
39+
"id_media": r.id_media,
40+
}
41+
taxonCommunesList.append(temp)
42+
nbObsTotal = nbObsTotal + r.nb_obs
43+
return {"taxons": taxonCommunesList, "nbObsTotal": nbObsTotal}
44+
45+
946
# With distinct the result in a array not an object, 0: lb_nom, 1: nom_vern
1047
def getTaxonsCommunes(connection, insee):
1148
sql = """

atlas/static/mapAreas.js

+41-43
Original file line numberDiff line numberDiff line change
@@ -113,31 +113,31 @@ function displayObsGridBaseUrl() {
113113

114114
// display observation on click
115115
function displayObsTaxon(insee, cd_ref) {
116-
$.ajax({
117-
url:
118-
configuration.URL_APPLICATION +
119-
"/api/observations/" +
120-
insee +
121-
"/" +
122-
cd_ref,
123-
dataType: "json",
124-
beforeSend: function() {
125-
$("#loadingGif").show();
126-
$("#loadingGif").attr(
127-
"src",
128-
configuration.URL_APPLICATION + "/static/images/loading.svg"
129-
);
130-
}
131-
}).done(function(observations) {
132-
$("#loadingGif").hide();
133-
map.removeLayer(currentLayer);
116+
$.ajax({
117+
url:
118+
configuration.URL_APPLICATION +
119+
"/api/observations/" +
120+
insee +
121+
"/" +
122+
cd_ref,
123+
dataType: "json",
124+
beforeSend: function() {
125+
$("#loadingGif").show();
126+
$("#loadingGif").attr(
127+
"src",
128+
configuration.URL_APPLICATION + "/static/images/loading.svg"
129+
);
130+
}
131+
}).done(function(observations) {
132+
$("#loadingGif").hide();
133+
map.removeLayer(currentLayer);
134134
clearOverlays()
135-
if (configuration.AFFICHAGE_MAILLE) {
136-
displayMailleLayerLastObs(observations);
137-
} else {
138-
displayMarkerLayerPointCommune(observations);
139-
}
140-
});
135+
if (configuration.AFFICHAGE_MAILLE) {
136+
displayMailleLayerLastObs(observations);
137+
} else {
138+
displayMarkerLayerPointCommune(observations);
139+
}
140+
});
141141
}
142142

143143

@@ -162,31 +162,29 @@ function displayObsTaxonMaille(areaCode, cd_ref) {
162162
});
163163
}
164164

165-
function refreshObsArea() {
166-
$("#taxonList ul").on("click", "#taxonListItem", function () {
165+
function refreshObsArea(elem) {
166+
$(this)
167+
.siblings()
168+
.removeClass("current");
169+
$(this).addClass("current");
170+
if (configuration.AFFICHAGE_MAILLE) {
171+
displayObsTaxonMaille(elem.currentTarget.getAttribute("area-code"), elem.currentTarget.getAttribute("cdref"));
172+
} else {
173+
displayObsTaxon(elem.currentTarget.getAttribute("area-code"), elem.currentTarget.getAttribute("cdref"));
174+
}
175+
const name = elem.currentTarget.querySelector("#name").innerHTML;
176+
$("#titleMap").fadeOut(500, function () {
167177
$(this)
168-
.siblings()
169-
.removeClass("current");
170-
$(this).addClass("current");
171-
if (configuration.AFFICHAGE_MAILLE) {
172-
displayObsTaxonMaille($(this).attr("area-code"), $(this).attr("cdRef"));
173-
} else {
174-
displayObsTaxon($(this).attr("area-code"), $(this).attr("cdRef"));
175-
}
176-
var name = $(this)
177-
.find("#name")
178-
.html();
179-
$("#titleMap").fadeOut(500, function () {
180-
$(this)
181-
.html("Observations du taxon&nbsp;:&nbsp;" + name)
182-
.fadeIn(500);
183-
});
178+
.html("Observations du taxon&nbsp;:&nbsp;" + name)
179+
.fadeIn(500);
184180
});
185181
}
186182

187183
$(document).ready(function () {
188184
$("#loaderSpinner").hide();
189185
if (configuration.INTERACTIVE_MAP_LIST) {
190-
refreshObsArea();
186+
$("#taxonList ul").on("click", "#taxonListItem", elem => {
187+
refreshObsArea(elem);
188+
});
191189
}
192190
});

atlas/static/mapHome.js

+44-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ $('#map').click(function(){
1515

1616
$(function(){
1717

18-
if (configuration.AFFICHAGE_MAILLE){
18+
if (configuration.AFFICHAGE_MAILLE || configuration.AFFICHAGE_TERRITOIRE_OBS){
1919
// display maille layer
2020
displayMailleLayerLastObs(observations);
2121

@@ -71,6 +71,49 @@ $(function(){
7171

7272
});
7373

74+
function displayObsTaxonMaille(cd_ref) {
75+
$.ajax({
76+
url: `${configuration.URL_APPLICATION}/api/observations/${cd_ref}`,
77+
dataType: "json",
78+
beforeSend: function () {
79+
$("#loaderSpinner").show();
80+
}
81+
}).done(function (observations) {
82+
$("#loaderSpinner").hide();
83+
map.removeLayer(currentLayer);
84+
clearOverlays()
85+
const geojsonMaille = generateGeoJsonMailleLastObs(observations, true);
86+
87+
displayMailleLayerFicheEspece(geojsonMaille);
88+
});
89+
}
90+
91+
function refreshTerritoryArea(elem) {
92+
document.querySelector("#taxonList .current")?.classList.remove("current")
93+
elem.currentTarget.classList.add('current');
94+
if (configuration.AFFICHAGE_TERRITOIRE_OBS) {
95+
displayObsTaxonMaille(elem.currentTarget.getAttribute("cdref"));
96+
}
97+
const name = $(this)
98+
.find("#name")
99+
.html();
100+
$("#titleMap").fadeOut(500, function () {
101+
$(this)
102+
.html("Observations du taxon&nbsp;:&nbsp;" + name)
103+
.fadeIn(500);
104+
});
105+
}
106+
107+
$(document).ready(function () {
108+
$("#loaderSpinner").hide();
109+
if (configuration.INTERACTIVE_MAP_LIST) {
110+
$("#taxonList").on("click", "#taxonListItem", function (elem) {
111+
refreshTerritoryArea(elem);
112+
});
113+
}
114+
});
115+
116+
74117
// Generate legends and check configuration to choose which to display (Maille ou Point)
75118

76119
htmlLegendMaille = "<i style='border: solid 1px red;'> &nbsp; &nbsp; &nbsp;</i> Maille comportant au moins une observation <br> <br>" +

atlas/templates/home/_main.html

+5
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@
6666
{% include 'templates/home/lastObs.html' %}
6767
{% endif %}
6868

69+
{% if configuration.AFFICHAGE_TERRITOIRE_OBS %}
70+
<!-- Toutes les espèces observées sur le territoire (Carte et liste) -->
71+
{% include 'templates/home/territory.html' %}
72+
{% endif %}
73+
6974
{% if configuration.AFFICHAGE_NOUVELLES_ESPECES %}
7075
<!--Nouvelles espèces observées-->
7176
{% include 'templates/home/newSpecies.html' %}

0 commit comments

Comments
 (0)