diff --git a/atlas/atlasAPI.py b/atlas/atlasAPI.py index 9af3beacb..de661edce 100644 --- a/atlas/atlasAPI.py +++ b/atlas/atlasAPI.py @@ -56,7 +56,7 @@ def getObservationsMailleAndPointAPI(cd_ref): @api.route("/observationsMaille/", methods=["GET"]) -def getObservationsMailleAPI(cd_ref, year_min=None, year_max=None): +def getObservationsMailleAPI(cd_ref): """ Retourne les observations d'un taxon par maille (et le nombre d'observation par maille) @@ -94,17 +94,17 @@ def getObservationsGenericApi(cd_ref: int): [type]: [description] """ session = db.session - observations = ( - vmObservationsMaillesRepository.getObservationsMaillesChilds( + if current_app.config["AFFICHAGE_MAILLE"]: + observations = vmObservationsMaillesRepository.getObservationsMaillesChilds( session, cd_ref, year_min=request.args.get("year_min"), year_max=request.args.get("year_max"), ) - if current_app.config["AFFICHAGE_MAILLE"] - else vmObservationsRepository.searchObservationsChilds(session, cd_ref) - ) + else: + observations = vmObservationsRepository.searchObservationsChilds(session, cd_ref) session.close() + return jsonify(observations) diff --git a/atlas/modeles/entities/tMaillesTerritoire.py b/atlas/modeles/entities/tMaillesTerritoire.py new file mode 100644 index 000000000..0e8a83513 --- /dev/null +++ b/atlas/modeles/entities/tMaillesTerritoire.py @@ -0,0 +1,22 @@ +# coding: utf-8 +from geoalchemy2.types import Geometry +from sqlalchemy import Column, Integer, MetaData, Table, Text +from sqlalchemy.ext.declarative import declarative_base + +from atlas.env import db + +metadata = MetaData() +Base = declarative_base() + + +class TMaillesTerritoire(Base): + __table__ = Table( + "t_mailles_territoire", + metadata, + Column("id_maille", Integer, primary_key=True, unique=True), + Column("the_geom", Geometry()), + Column("geojson_maille", Text), + schema="atlas", + autoload=True, + autoload_with=db.engine, + ) diff --git a/atlas/modeles/entities/vmObservations.py b/atlas/modeles/entities/vmObservations.py index 782664297..141fd7514 100644 --- a/atlas/modeles/entities/vmObservations.py +++ b/atlas/modeles/entities/vmObservations.py @@ -36,11 +36,10 @@ class VmObservationsMailles(Base): __table__ = Table( "vm_observations_mailles", metadata, - Column("id_observation", Integer, primary_key=True, unique=True), - Column("id_maille", Integer), - Column("the_geom", Geometry), - Column("geojson_maille", Text), - Column("annee", String(1000)), + Column("cd_ref", Integer, primary_key=True, index=True), + Column("annee", String(1000), primary_key=True, index=True), + Column("id_maille", Integer, primary_key=True, index=True), + Column("nbr", Integer), schema="atlas", autoload=True, autoload_with=db.engine, diff --git a/atlas/modeles/repositories/vmObservationsMaillesRepository.py b/atlas/modeles/repositories/vmObservationsMaillesRepository.py index 0a37ae047..d2ec6fdc6 100644 --- a/atlas/modeles/repositories/vmObservationsMaillesRepository.py +++ b/atlas/modeles/repositories/vmObservationsMaillesRepository.py @@ -1,9 +1,10 @@ import json from geojson import Feature, FeatureCollection -from sqlalchemy.sql import text, func, or_ +from sqlalchemy.sql import text, func, any_ from atlas.modeles.entities.vmObservations import VmObservationsMailles +from atlas.modeles.entities.tMaillesTerritoire import TMaillesTerritoire from atlas.modeles.utils import deleteAccent, findPath @@ -12,17 +13,25 @@ def getObservationsMaillesChilds(session, cd_ref, year_min=None, year_max=None): Retourne les mailles et le nombre d'observation par maille pour un taxon et ses enfants sous forme d'un geojson """ - subquery = session.query(func.atlas.find_all_taxons_childs(cd_ref)) + query = func.atlas.find_all_taxons_childs(cd_ref) + taxons_ids = session.scalars(query).all() + taxons_ids.append(cd_ref) + query = ( session.query( - func.count(VmObservationsMailles.id_observation).label("nb_obs"), - func.max(VmObservationsMailles.annee).label("last_observation"), VmObservationsMailles.id_maille, - VmObservationsMailles.geojson_maille, + TMaillesTerritoire.geojson_maille, + func.max(VmObservationsMailles.annee).label("last_obs_year"), + func.sum(VmObservationsMailles.nbr).label("obs_nbr"), + ) + .join( + TMaillesTerritoire, + TMaillesTerritoire.id_maille == VmObservationsMailles.id_maille, ) - .group_by(VmObservationsMailles.id_maille, VmObservationsMailles.geojson_maille) - .filter( - or_(VmObservationsMailles.cd_ref.in_(subquery), VmObservationsMailles.cd_ref == cd_ref) + .filter(VmObservationsMailles.cd_ref == any_(taxons_ids)) + .group_by( + VmObservationsMailles.id_maille, + TMaillesTerritoire.geojson_maille, ) ) if year_min and year_max: @@ -35,8 +44,8 @@ def getObservationsMaillesChilds(session, cd_ref, year_min=None, year_max=None): geometry=json.loads(o.geojson_maille), properties={ "id_maille": o.id_maille, - "nb_observations": o.nb_obs, - "last_observation": o.last_observation, + "nb_observations": int(o.obs_nbr), + "last_observation": o.last_obs_year, }, ) for o in query.all() diff --git a/atlas/modeles/repositories/vmTaxrefRepository.py b/atlas/modeles/repositories/vmTaxrefRepository.py index a5e5b51d0..8eab16957 100644 --- a/atlas/modeles/repositories/vmTaxrefRepository.py +++ b/atlas/modeles/repositories/vmTaxrefRepository.py @@ -11,29 +11,34 @@ def searchEspece(connection, cd_ref): """ recherche l espece corespondant au cd_nom et tout ces fils """ - sql = """ + query = """ WITH limit_obs AS ( SELECT - :thiscdref AS cd_ref, min(yearmin) AS yearmin, - max(yearmax) AS yearmax, SUM(nb_obs) AS nb_obs + :cdRef AS cd_ref, + MIN(yearmin) AS yearmin, + MAX(yearmax) AS yearmax, + SUM(nb_obs) AS nb_obs FROM atlas.vm_taxons - WHERE - cd_ref IN (SELECT * FROM atlas.find_all_taxons_childs(:thiscdref)) - OR cd_ref = :thiscdref + WHERE cd_ref IN (SELECT * FROM atlas.find_all_taxons_childs(:cdRef)) + OR cd_ref = :cdRef ) SELECT taxref.*, - l.cd_ref, l.yearmin, l.yearmax, COALESCE(l.nb_obs, 0) AS nb_obs, - t2.patrimonial, t2.protection_stricte - FROM atlas.vm_taxref taxref - JOIN limit_obs l - ON l.cd_ref = taxref.cd_nom - LEFT JOIN atlas.vm_taxons t2 - ON t2.cd_ref = taxref.cd_ref - WHERE taxref.cd_nom = :thiscdref + l.cd_ref, + l.yearmin, + l.yearmax, + COALESCE(l.nb_obs, 0) AS nb_obs, + t2.patrimonial, + t2.protection_stricte + FROM atlas.vm_taxref AS taxref + JOIN limit_obs AS l + ON l.cd_ref = taxref.cd_nom + LEFT JOIN atlas.vm_taxons AS t2 + ON t2.cd_ref = taxref.cd_ref + WHERE taxref.cd_nom = :cdRef """ - req = connection.execute(text(sql), thiscdref=cd_ref) + results = connection.execute(text(query), cdRef=cd_ref) taxonSearch = dict() - for r in req: + for r in results: nom_vern = None if r.nom_vern: nom_vern = ( @@ -54,7 +59,7 @@ def searchEspece(connection, cd_ref): "protection": r.protection_stricte, } - sql = """ + query = """ SELECT tax.lb_nom, tax.nom_vern, @@ -64,16 +69,17 @@ def searchEspece(connection, cd_ref): tax.patrimonial, tax.protection_stricte, tax.nb_obs - FROM atlas.vm_taxons tax - JOIN atlas.bib_taxref_rangs br - ON br.id_rang = tax.id_rang + FROM atlas.vm_taxons AS tax + JOIN atlas.bib_taxref_rangs AS br + ON br.id_rang = tax.id_rang WHERE tax.cd_ref IN ( - SELECT * FROM atlas.find_all_taxons_childs(:thiscdref) + SELECT * FROM atlas.find_all_taxons_childs(:cdRef) ) + ORDER BY tax.lb_nom ASC, tax.nb_obs DESC """ - req = connection.execute(text(sql), thiscdref=cd_ref) + results = connection.execute(text(query), cdRef=cd_ref) listTaxonsChild = list() - for r in req: + for r in results: temp = { "lb_nom": r.lb_nom, "nom_vern": r.nom_vern, diff --git a/atlas/static/mapAreas.js b/atlas/static/mapAreas.js index 66e00b73f..1baeb8a15 100644 --- a/atlas/static/mapAreas.js +++ b/atlas/static/mapAreas.js @@ -148,11 +148,6 @@ function displayObsTaxonMaille(areaCode, cd_ref) { dataType: "json", beforeSend: function () { $("#loaderSpinner").show(); - // $("#loadingGif").show(); - // $("#loadingGif").attr( - // "src", - // configuration.URL_APPLICATION + "/static/images/loading.svg" - // ); } }).done(function (observations) { $("#loaderSpinner").hide(); diff --git a/atlas/static/mapGenerator.js b/atlas/static/mapGenerator.js index 58c5892cb..e26446eb6 100644 --- a/atlas/static/mapGenerator.js +++ b/atlas/static/mapGenerator.js @@ -340,7 +340,9 @@ function displayGridLayerArea(observations) { style: styleMaille, }); currentLayer.addTo(map); - map.fitBounds(currentLayer.getBounds()); + if (currentLayer.getBounds().isValid()) { + map.fitBounds(currentLayer.getBounds()); + } // ajout de la légende generateLegendMaille(); diff --git a/atlas/templates/core/sideBar.html b/atlas/templates/core/sideBar.html index d9c9b4a8e..09285c98e 100644 --- a/atlas/templates/core/sideBar.html +++ b/atlas/templates/core/sideBar.html @@ -5,14 +5,24 @@ {% for page_key, page_values in configuration.STATIC_PAGES.items()|sort(attribute='1.order') %} {% if 'url' in page_values %} - - + + {% else %} - - + + {% endif %} {% endfor %} diff --git a/atlas/templates/speciesSheet/identityCard.html b/atlas/templates/speciesSheet/identityCard.html index 9d7c4d273..f54a82bc8 100644 --- a/atlas/templates/speciesSheet/identityCard.html +++ b/atlas/templates/speciesSheet/identityCard.html @@ -109,17 +109,19 @@

{{ taxon.taxonSearch.nom_complet_html|safe }}