Skip to content

Draft : Feat/Sensitivity #352

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 26 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
c571627
feat(map): Add layerGroups by id_type
Jul 12, 2021
56dac33
feat(map): Zoom, snogylop and features
Jul 12, 2021
16f83aa
feat(map): Updated sql to match db modifications
Jul 13, 2021
15e5bff
style(map): add hover and click effect
Jul 15, 2021
fd8c979
refactor: changed lastObservationsCommuneMaille
Jul 19, 2021
bc3a976
style: improved style function and changed colors
Jul 19, 2021
eb2c73e
style: Add main-color-rgb to liste espece
Jul 20, 2021
246639e
feat: better layering and click management
Jul 20, 2021
c05b02f
style: removed unsused css directive added before
Jul 20, 2021
59146e5
style: forgot to put back rgba for tabEspece.hover
Jul 20, 2021
f847c92
fix: corrected sql query and corrected id_type
Jul 21, 2021
0482100
refactor: add function to gather better city obs
Jul 21, 2021
c0a66e8
feat: worked on communes to make the sensi work
Jul 21, 2021
f9aa473
fix: removed unused import
Jul 21, 2021
1e43d77
chore: remove useless case
Jul 26, 2021
dc551be
fix(sql): returns the date object, not a str
Jul 26, 2021
f85ef5b
fix(sql): corrected bug when no species are found
Jul 26, 2021
ec88d60
feat(sql): Changed vm to be compatible with sensib
Jul 26, 2021
5e91d4f
style(species): Add css variable for species tab
Jul 26, 2021
c486cdc
fix(map): bump leaflet version and rm snogilop
Jul 26, 2021
79218de
style(species): add rgb equivalent primary color
Jul 26, 2021
0a644cf
fix(species): Corrected a city bug in FicheEspece
Jul 29, 2021
1163790
feat(popup): Removes duplicates in popup
Jul 29, 2021
358bd88
feat(sql): observations_maille.sql is more robust
Aug 3, 2021
09f7a53
Merge remote-tracking branch 'mvergez/feat/sensibility' into sensitivity
Adrien-Pajot Sep 21, 2021
e294565
vm observation mailles
Adrien-Pajot Sep 21, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions atlas/atlasRoutes.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,11 @@ def ficheEspece(cd_ref):
altitudes = vmAltitudesRepository.getAltitudesChilds(connection, cd_ref)
months = vmMoisRepository.getMonthlyObservationsChilds(connection, cd_ref)
synonyme = vmTaxrefRepository.getSynonymy(connection, cd_ref)
communes = vmCommunesRepository.getCommunesObservationsChilds(connection, cd_ref)
taxonomyHierarchy = vmTaxrefRepository.getAllTaxonomy(db_session, cd_ref)
if current_app.config["AFFICHAGE_MAILLE"]:
communes = vmCommunesRepository.getCommunesObservationsChildsMailles(connection, cd_ref)
else:
communes = vmCommunesRepository.getCommunesObservationsChilds(connection, cd_ref)
taxonomyHierarchy = vmTaxrefRepository.getAllTaxonomy(session, cd_ref)
firstPhoto = vmMedias.getFirstPhoto(
connection, cd_ref, current_app.config["ATTR_MAIN_PHOTO"]
)
Expand Down Expand Up @@ -305,7 +308,7 @@ def ficheCommune(insee):
session = utils.loadSession()
connection = utils.engine.connect()

listTaxons = vmTaxonsRepository.getTaxonsCommunes(connection, insee)

commune = vmCommunesRepository.getCommuneFromInsee(connection, insee)
if current_app.config["AFFICHAGE_MAILLE"]:
observations = vmObservationsMaillesRepository.lastObservationsCommuneMaille(
Expand All @@ -315,6 +318,9 @@ def ficheCommune(insee):
observations = vmObservationsRepository.lastObservationsCommune(
connection, current_app.config["NB_LAST_OBS"], insee
)

# listTaxons = vmTaxonsRepository.getTaxonsCommunes(connection, insee)
listTaxons = vmTaxonsRepository.get_taxons_from_obs(connection, observations)

if current_app.config["EXTENDED_AREAS"]:
id_area = vmAreasRepository.get_id_area(session, "COM", insee)
Expand Down
1 change: 1 addition & 0 deletions atlas/modeles/entities/vmObservations.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class VmObservationsMailles(Base):
metadata,
Column("id_observation", Integer, primary_key=True, unique=True),
Column("id_maille", Integer),
Column("id_type", Integer),
Column("the_geom", Geometry),
Column("geojson_maille", Text),
Column("annee", String(1000)),
Expand Down
19 changes: 19 additions & 0 deletions atlas/modeles/repositories/vmCommunesRepository.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,22 @@ def getCommunesObservationsChilds(connection, cd_ref):
temp = {"insee": r.insee, "commune_maj": r.commune_maj}
listCommunes.append(temp)
return listCommunes

def getCommunesObservationsChildsMailles(connection, cd_ref):
sql = """
SELECT DISTINCT (com.insee) as insee, com.commune_maj
FROM atlas.vm_communes com
JOIN atlas.vm_observations_mailles obs
ON st_intersects(obs.the_geom, com.the_geom)
WHERE obs.cd_ref in (
SELECT * from atlas.find_all_taxons_childs(:thiscdref)
)
OR obs.cd_ref = :thiscdref
ORDER BY com.commune_maj ASC
"""
req = connection.execute(text(sql), thiscdref=cd_ref)
listCommunes = list()
for r in req:
temp = {'insee': r.insee, 'commune_maj': r.commune_maj}
listCommunes.append(temp)
return listCommunes
39 changes: 19 additions & 20 deletions atlas/modeles/repositories/vmObservationsMaillesRepository.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ def getObservationsMaillesChilds(session, cd_ref, year_min=None, year_max=None):
func.count(VmObservationsMailles.id_observation).label("nb_obs"),
func.max(VmObservationsMailles.annee).label("last_observation"),
VmObservationsMailles.id_maille,
VmObservationsMailles.geojson_maille,
VmObservationsMailles.id_type,
VmObservationsMailles.geojson_maille
)
.group_by(VmObservationsMailles.id_maille, VmObservationsMailles.geojson_maille)
.filter(
.group_by(VmObservationsMailles.id_maille, VmObservationsMailles.geojson_maille, VmObservationsMailles.id_type)
.filter(
or_(
VmObservationsMailles.cd_ref.in_(subquery),
VmObservationsMailles.cd_ref == cd_ref,
Expand All @@ -38,6 +39,7 @@ def getObservationsMaillesChilds(session, cd_ref, year_min=None, year_max=None):
geometry=json.loads(o.geojson_maille),
properties={
"id_maille": o.id_maille,
"id_type": o.id_type,
"nb_observations": o.nb_obs,
"last_observation": o.last_observation,
},
Expand Down Expand Up @@ -74,6 +76,7 @@ def lastObservationsMailles(connection, mylimit, idPhoto):
temp = {
"id_observation": o.id_observation,
"id_maille": o.id_maille,
"id_type": o.id_type,
"cd_ref": o.cd_ref,
"dateobs": str(o.dateobs),
"altitude_retenue": o.altitude_retenue,
Expand All @@ -89,24 +92,19 @@ def lastObservationsMailles(connection, mylimit, idPhoto):

def lastObservationsCommuneMaille(connection, mylimit, insee):
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
t.nom_vern, obs.the_geom as l_geom,
obs.geojson_maille, obs.id_maille,
obs.id_type
FROM atlas.vm_observations_mailles obs
JOIN atlas.vm_communes c
ON ST_Intersects(obs.the_geom_point, c.the_geom)
ON ST_Intersects(obs.the_geom, c.the_geom)
JOIN atlas.vm_taxons t
ON obs.cd_ref = t.cd_ref
WHERE c.insee = :thisInsee
ORDER BY obs.dateobs DESC
LIMIT :thislimit
)
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(st_transform(l.l_geom, 2154), m.the_geom)
GROUP BY l.lb_nom, l.cd_ref, m.id_maille, l.nom_vern, m.geojson_maille
"""
observations = connection.execute(text(sql), thisInsee=insee, thislimit=mylimit)
obsList = list()
Expand All @@ -117,9 +115,11 @@ def lastObservationsCommuneMaille(connection, mylimit, insee):
taxon = o.lb_nom
temp = {
"cd_ref": o.cd_ref,
"dateobs": o.dateobs,
"taxon": taxon,
"geojson_maille": json.loads(o.geojson_maille),
"id_maille": o.id_maille,
"id_type": o.id_type,
}
obsList.append(temp)
return obsList
Expand All @@ -128,14 +128,12 @@ def lastObservationsCommuneMaille(connection, mylimit, insee):
# Use for API
def getObservationsTaxonCommuneMaille(connection, insee, cd_ref):
sql = """
SELECT
o.cd_ref, t.id_maille, t.geojson_maille,
extract(YEAR FROM o.dateobs) AS annee
FROM atlas.vm_observations o
SELECT
o.cd_ref, o.id_maille, o.id_type, o.geojson_maille, o.the_geom,
extract(YEAR FROM o.dateobs) as annee
FROM atlas.vm_observations_mailles o
JOIN atlas.vm_communes c
ON ST_INTERSECTS(st_transform(o.the_geom_point, 3857), c.the_geom)
JOIN atlas.t_mailles_territoire t
ON ST_INTERSECTS(t.the_geom, st_transform(o.the_geom_point, 2154))
ON ST_INTERSECTS(o.the_geom, c.the_geom)
WHERE o.cd_ref = :thiscdref AND c.insee = :thisInsee
ORDER BY id_maille
"""
Expand All @@ -144,6 +142,7 @@ def getObservationsTaxonCommuneMaille(connection, insee, cd_ref):
for o in observations:
temp = {
"id_maille": o.id_maille,
"id_type": o.id_type,
"nb_observations": 1,
"annee": o.annee,
"geojson_maille": json.loads(o.geojson_maille),
Expand Down
2 changes: 1 addition & 1 deletion atlas/modeles/repositories/vmObservationsRepository.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def lastObservationsCommune(connection, mylimit, insee):
temp = dict(o)
temp.pop("the_geom_point", None)
temp["geojson_point"] = json.loads(o.geojson_point or "{}")
temp["dateobs"] = str(o.dateobs)
temp["dateobs"] = o.dateobs
obsList.append(temp)
return obsList

Expand Down
75 changes: 74 additions & 1 deletion atlas/modeles/repositories/vmTaxonsRepository.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,83 @@
# -*- coding:utf-8 -*-

from collections import Counter
from flask import current_app
from sqlalchemy.sql import text

from atlas.modeles import utils

def get_taxons_from_obs(connection, observations: dict):
"""
Gets infos on taxons present in the observations

Args:
observations (dict): observation dictionnary

Returns:
dict: dictionnary of all info on taxons. Contains:
- taxons
- 'nom_complet_html'
- 'nb_obs'
- 'nom_vern'
- 'cd_ref'
- 'last_obs'
- 'group2_inpn'
- 'patrimonial'
- 'protection_stricte'
- 'path'
- 'id_media'
- nbObsTotal
- nb_obs_total
"""
# Init
taxon_commune_list = []
nb_obs_total = 0

# Get the nb of observations for each cd_ref to have the nb_obs
count = Counter(obs['cd_ref'] for obs in observations)
cd_refs = tuple(count.keys())

if not cd_refs:
return {'taxons': taxon_commune_list,
'nbObsTotal': nb_obs_total}

# Get the most recent observation date for each cd_ref.
# TODO: there must be a better way than 2 loops...
max_obs = {cd_ref: max(obs['dateobs'] for obs in observations if obs['cd_ref'] == cd_ref) for cd_ref in cd_refs}

# Request
sql = """
SELECT DISTINCT
t.cd_ref, t.nom_complet_html, t.nom_vern,
t.group2_inpn, t.patrimonial, t.protection_stricte,
m.url, m.chemin, m.id_media
from atlas.vm_taxons t
LEFT JOIN atlas.vm_medias m ON m.cd_ref=t.cd_ref AND m.id_type={}
WHERE t.cd_ref in ({})
GROUP BY t.cd_ref, t.nom_vern, t.nom_complet_html, t.group2_inpn,
t.patrimonial, t.protection_stricte, m.url, m.chemin, m.id_media
ORDER BY t.cd_ref
""".format(current_app.config['ATTR_MAIN_PHOTO'], ','.join(str(k) for k in cd_refs))

req = connection.execute(text(sql))

for r in req:
nb_obs = count[r.cd_ref]
temp = {
'nom_complet_html': r.nom_complet_html,
'nb_obs': nb_obs,
'nom_vern': r.nom_vern,
'cd_ref': r.cd_ref,
'last_obs': max_obs[r.cd_ref].year,
'group2_inpn': utils.deleteAccent(r.group2_inpn),
'patrimonial': r.patrimonial,
'protection_stricte': r.protection_stricte,
'path': utils.findPath(r),
'id_media': r.id_media
}
taxon_commune_list.append(temp)
nb_obs_total = nb_obs_total + nb_obs
return {'taxons': taxon_commune_list, 'nbObsTotal': nb_obs_total}


# With distinct the result in a array not an object, 0: lb_nom, 1: nom_vern
def getTaxonsCommunes(connection, insee):
Expand Down
Loading