Skip to content

Commit

Permalink
feat: change 'commune' to 'area' to have different 'geometry' page (#545
Browse files Browse the repository at this point in the history
)
  • Loading branch information
juggler31 committed Dec 19, 2024
1 parent 83ba84f commit bdb1748
Show file tree
Hide file tree
Showing 48 changed files with 367 additions and 316 deletions.
24 changes: 12 additions & 12 deletions atlas/atlasAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
vmObservationsRepository,
vmObservationsMaillesRepository,
vmMedias,
vmCommunesRepository,
tZonesRepository,
)
from atlas.env import cache, db

Expand All @@ -25,12 +25,12 @@ def searchTaxonAPI():
return jsonify(results)


@api.route("/searchCommune", methods=["GET"])
def searchCommuneAPI():
@api.route("/searchZone", methods=["GET"])
def searchZoneAPI():
session = db.session
search = request.args.get("search", "")
limit = request.args.get("limit", 50)
results = vmCommunesRepository.searchMunicipalities(session, search, limit)
results = tZonesRepository.searchMunicipalities(session, search, limit)
session.close()
return jsonify(results)

Expand Down Expand Up @@ -110,21 +110,21 @@ def getObservationsGenericApi(cd_ref: int):

if not current_app.config["AFFICHAGE_MAILLE"]:

@api.route("/observations/<insee>/<int(signed=True):cd_ref>", methods=["GET"])
def getObservationsCommuneTaxonAPI(insee, cd_ref):
@api.route("/observations/<id_zone>/<int(signed=True):cd_ref>", methods=["GET"])
def getObservationsZoneTaxonAPI(id_zone, cd_ref):
connection = db.engine.connect()
observations = vmObservationsRepository.getObservationTaxonCommune(
connection, insee, cd_ref
observations = vmObservationsRepository.getObservationTaxonZone(
connection, id_zone, cd_ref
)
connection.close()
return jsonify(observations)


@api.route("/observationsMaille/<insee>/<int(signed=True):cd_ref>", methods=["GET"])
def getObservationsCommuneTaxonMailleAPI(insee, cd_ref):
@api.route("/observationsMaille/<id_zone>/<int(signed=True):cd_ref>", methods=["GET"])
def getObservationsZoneTaxonMailleAPI(id_zone, cd_ref):
connection = db.engine.connect()
observations = vmObservationsMaillesRepository.getObservationsTaxonCommuneMaille(
connection, insee, cd_ref
observations = vmObservationsMaillesRepository.getObservationsTaxonZoneMaille(
connection, id_zone, cd_ref
)
connection.close()
return jsonify(observations)
Expand Down
41 changes: 20 additions & 21 deletions atlas/atlasRoutes.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@

from atlas.env import db
from atlas import utils
from atlas.modeles.entities import vmTaxons, vmCommunes
from atlas.modeles.entities import vmTaxons, t_zoning
from atlas.modeles.repositories import (
vmOrganismsRepository,
vmTaxonsRepository,
vmObservationsRepository,
vmAltitudesRepository,
vmMoisRepository,
vmTaxrefRepository,
vmCommunesRepository,
tZonesRepository,
vmObservationsMaillesRepository,
vmMedias,
vmCorTaxonAttribut,
Expand Down Expand Up @@ -125,9 +125,9 @@ def ficheOrganism(id_organism):


@main.route(
"/commune/" + current_app.config["REMOTE_MEDIAS_PATH"] + "<image>", methods=["GET", "POST"]
"/zone/" + current_app.config["REMOTE_MEDIAS_PATH"] + "<image>", methods=["GET", "POST"]
)
def communeMedias(image):
def zoneMedias(image):
return redirect(
current_app.config["REMOTE_MEDIAS_URL"] + current_app.config["REMOTE_MEDIAS_PATH"] + image
)
Expand Down Expand Up @@ -227,13 +227,12 @@ def ficheEspece(cd_nom):
# Redirect to cd_ref if cd_nom is a synonym. Redirection is better for SEO.
if cd_ref != cd_nom:
return redirect(url_for(request.endpoint, cd_nom=cd_ref))

# Get data to render template
taxon = vmTaxrefRepository.searchEspece(connection, 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)
zone = tZonesRepository.getZonesObservationsChilds(connection, cd_ref)
taxonomyHierarchy = vmTaxrefRepository.getAllTaxonomy(db_session, cd_ref)
firstPhoto = vmMedias.getFirstPhoto(connection, cd_ref, current_app.config["ATTR_MAIN_PHOTO"])
photoCarousel = vmMedias.getPhotoCarousel(
Expand Down Expand Up @@ -275,7 +274,7 @@ def ficheEspece(cd_nom):
altitudes=altitudes,
months=months,
synonyme=synonyme,
communes=communes,
zone=zone,
taxonomyHierarchy=taxonomyHierarchy,
firstPhoto=firstPhoto,
photoCarousel=photoCarousel,
Expand All @@ -287,39 +286,39 @@ def ficheEspece(cd_nom):
)


@main.route("/commune/<insee>", methods=["GET", "POST"])
def ficheCommune(insee):
@main.route("/zone/<id_zone>", methods=["GET", "POST"])
def ficheZone(id_zone):
session = db.session
connection = db.engine.connect()

listTaxons = vmTaxonsRepository.getTaxonsCommunes(connection, insee)
commune = vmCommunesRepository.getCommuneFromInsee(connection, insee)
listTaxons = vmTaxonsRepository.getTaxonsZones(connection, id_zone)

zone = tZonesRepository.getZoneFromIdZone(connection, id_zone)
if current_app.config["AFFICHAGE_MAILLE"]:
observations = vmObservationsMaillesRepository.lastObservationsCommuneMaille(
connection, current_app.config["NB_LAST_OBS"], str(insee)
observations = vmObservationsMaillesRepository.lastObservationsZoneMaille(
connection, current_app.config["NB_LAST_OBS"], str(id_zone)
)
else:
observations = vmObservationsRepository.lastObservationsCommune(
connection, current_app.config["NB_LAST_OBS"], insee
observations = vmObservationsRepository.lastObservationsZone(
connection, current_app.config["NB_LAST_OBS"], id_zone
)

surroundingAreas = []

observers = vmObservationsRepository.getObserversCommunes(connection, insee)
observers = vmObservationsRepository.getObserversZone(connection, id_zone)

session.close()
connection.close()

return render_template(
"templates/areaSheet/_main.html",
sheetType="commune",
surroundingAreas=surroundingAreas,
listTaxons=listTaxons,
areaInfos=commune,
areaInfos=zone,
observations=observations,
observers=observers,
DISPLAY_EYE_ON_LIST=True,
insee=insee,
id_zone=id_zone,
)


Expand Down Expand Up @@ -420,10 +419,10 @@ def sitemap():
pages.append([url, modified_time])

municipalities = (
session.query(vmCommunes.VmCommunes).order_by(vmCommunes.VmCommunes.insee).all()
session.query(vmZones.t_zoning).order_by(vmZones.t_zoning.id_code).all()
)
for municipalitie in municipalities:
url = url_root + url_for("main.ficheCommune", insee=municipalitie.insee)
url = url_root + url_for("main.ficheZone", id_code=municipalitie.id_code)
modified_time = ten_days_ago
pages.append([url, modified_time])

Expand Down
4 changes: 2 additions & 2 deletions atlas/configuration/config.py.example
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ NB_DAY_LAST_OBS = '7'
# Texte à afficher pour décrire la cartographie des 'dernières observations'
TEXT_LAST_OBS = 'Les observations des agents ces 7 derniers jours |'

# Carte de la fiche commune : nombre des 'x' dernières observations affichées
# Carte de la fiche "zone" : nombre des 'x' dernières observations affichées
NB_LAST_OBS=100

###########################
Expand Down Expand Up @@ -237,7 +237,7 @@ ATTR_VIMEO = 9
SPLIT_NOM_VERN = True

############################################
#### FICHE COMMUNE ET RANG TAXONOMIQUE #####
#### FICHE "ZONE" ET RANG TAXONOMIQUE #####
############################################

# Permet d'afficher ou non les colonnes Protection et/ou Patrimonialité/Enjeux dans les listes de taxons
Expand Down
17 changes: 9 additions & 8 deletions atlas/configuration/settings.ini.sample
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,19 @@ type_territoire="'PEC'"

########### Si ref_geo = False #############

#### COMMUNES ####
#### ZONE ####

# Creer la table des communes à partir d'un shapefile ?
# Si false, modifiez la creation de 'atlas.vm_communes' dans data/atlas/atlas.vm_communes.sql
# Creer la table des zones à partir d'un shapefile ?
# Si false, modifiez la creation de 'atlas.zoning' dans data/atlas/7.atlas.zoning.sql
# PARAMETRE OBSOLETE OBLIGATOIRE SI PAS DE REF_GEO
# import_commune_shp=true
#
import_commune_shp=true

# Chemin et nom des colonnes du SHP des communes du territoire. Laisser tel quel (en modifiant uniquement MYUSERLINUX)
# pour utiliser les communes du PnEcrins par défaut
communes_shp=/home/`whoami`/atlas/data/ref/communes.shp
# Chemin et nom des colonnes du SHP des zones du territoire. Laisser tel quel (en modifiant uniquement MYUSERLINUX)
# pour utiliser les zones du PnEcrins par défaut
zones_shp=/home/`whoami`/atlas/data/ref/zones.shp
colonne_insee=insee
colonne_nom_commune=nom_com
colonne_nom_zone=nom_com

#### TERRITOIRE ####

Expand Down
2 changes: 1 addition & 1 deletion atlas/messages.pot
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ msgstr ""
#: static/custom/templates/navbar.html:47
#: static/custom/templates/navbar.html.sample:47
#: templates/home/globalStats.html:55
msgid "search.city"
msgid "search.zone"
msgstr ""

#: static/custom/templates/presentation.html:4
Expand Down
2 changes: 1 addition & 1 deletion atlas/modeles/entities/tCommunes.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ class LCommune(Base):
__table_args__ = {"schema": "layers"}

insee = Column(String(5), primary_key=True)
commune_maj = Column(String(50))
area_name = Column(String(50))
commune_min = Column(String(50))
the_geom = Column(Geometry)
27 changes: 27 additions & 0 deletions atlas/modeles/entities/t_zoning.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# coding: utf-8
from geoalchemy2.types import Geometry
from sqlalchemy import Column, MetaData, String, Table, Sequence, Integer, Text
from sqlalchemy.ext.declarative import declarative_base

from atlas.env import db

metadata = MetaData()
Base = declarative_base()


class t_zoning(Base):
__table__ = Table(
"zoning",
metadata,
Column("id", Sequence('zoning_id_seq'), primary_key=True, unique=True),
Column("id_zone", Integer()),
Column("area_name", String(50)),
Column("the_geom", Geometry("MULTIPOLYGON"), index=True),
Column("zone_geojson", Text),
Column("id_zoning_type", Integer()),
Column("id_parent", Integer()),
schema="atlas",
autoload=True,
autoload_with=db.engine,
)
# id = Column(Integer, Sequence('user_id_seq'), primary_key=True)
23 changes: 0 additions & 23 deletions atlas/modeles/entities/vmCommunes.py

This file was deleted.

6 changes: 3 additions & 3 deletions atlas/modeles/repositories/tCommunesRepository.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
from sqlalchemy.sql import text


def getCommunesObservationsChilds(connection, cd_ref):
def getZonesObservationsChilds(connection, cd_ref):
sql = """
SELECT DISTINCT(com.insee) AS insee, com.commune_maj
SELECT DISTINCT(com.insee) AS insee, com.area_name
FROM layers.l_communes com
JOIN atlas.vm_observations obs ON obs.insee = com.insee
WHERE obs.cd_ref IN (
SELECT * FROM atlas.find_all_taxons_childs(:thiscdref)
) OR obs.cd_ref = :thiscdref
GROUP BY com.commune_maj, com.insee
GROUP BY com.area_name, com.insee
""".encode(
"UTF-8"
)
Expand Down
83 changes: 83 additions & 0 deletions atlas/modeles/repositories/tZonesRepository.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# -*- coding:utf-8 -*-

import ast

from sqlalchemy import distinct
from sqlalchemy.sql import text
from sqlalchemy.sql.expression import func

from atlas.modeles.entities.t_zoning import t_zoning


def getAllZones(session):
req = session.query(distinct(t_zoning.area_name), t_zoning.id_zone).all()
zoneList = list()
for r in req:
temp = {"label": r[0], "value": r[1]}
zoneList.append(temp)
return zoneList


def searchMunicipalities(session, search, limit=50):
like_search = "%" + search.replace(" ", "%") + "%"

query = (
session.query(
distinct(t_zoning.area_name),
t_zoning.id_zone,
func.length(t_zoning.area_name),
)
.filter(func.unaccent(t_zoning.area_name).ilike(func.unaccent(like_search)))
.order_by(t_zoning.area_name)
.limit(limit)
)
results = query.all()

return [{"label": r[0], "value": r[1]} for r in results]


def getZoneFromIdZone(connection, id_zone):
sql = """
SELECT c.area_name,
c.id_zone,
c.zone_geojson,
bib.type_name
FROM atlas.zoning c
JOIN ref_geo.bib_areas_types bib ON bib.id_type = c.id_zoning_type
WHERE c.id_zone = :thisIdZone
"""
req = connection.execute(text(sql), thisIdZone=id_zone)
zone_obj = dict()
for r in req:
zone_obj = {
"areaName": r.area_name,
"areaCode": str(r.id_zone),
"areaGeoJson": ast.literal_eval(r.zone_geojson),
"typeName": r.type_name
}
return zone_obj


def getZonesObservationsChilds(connection, cd_ref):
sql = "SELECT * FROM atlas.find_all_taxons_childs(:thiscdref) AS taxon_childs(cd_nom)"
results = connection.execute(text(sql), thiscdref=cd_ref)
taxons = [cd_ref]
for r in results:
taxons.append(r.cd_nom)

sql = """
SELECT DISTINCT
zone.area_name,
zone.id_zone
FROM atlas.vm_observations AS obs
JOIN atlas.zoning AS zone
ON st_intersects(obs.the_geom_point, zone.the_geom_4326)
WHERE obs.cd_ref = ANY(:taxonsList)
ORDER BY zone.area_name ASC
"""
results = connection.execute(text(sql), taxonsList=taxons)
municipalities = list()
for r in results:
municipality = {"id_zone": r.id_zone, "area_name": r.area_name}
municipalities.append(municipality)
return municipalities
Loading

0 comments on commit bdb1748

Please sign in to comment.