Skip to content

Commit

Permalink
feat: restrict area codes & taxon list interact
Browse files Browse the repository at this point in the history
Restrict area codes: filter out the query to search and the one that returns
geoms
Taxon list interaction now works with the AreaSheet
  • Loading branch information
Maxime Vergez committed Oct 12, 2022
1 parent a63eb44 commit ee64457
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 29 deletions.
35 changes: 21 additions & 14 deletions atlas/atlasAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,22 @@ def search_area():
search = request.args.get("search")
type_code = request.args.get("type")
limit = request.args.get("limit", 50)
results = vmAreasRepository.search_area_by_type(session=session, search=search, type_code=type_code, limit=limit)
results = vmAreasRepository.search_area_by_type(session=session,
search=search,
type_code=type_code,
filter_area_codes=current_app.config['AREAS_LIST'],
limit=limit)
session.close()
return jsonify(results)


@api.route("/area/geom", methods=["GET"])
def get_areas_geom():
session = utils.loadSession()
results = vmAreasRepository.get_areas_geometries(session=session)
limit = request.args.get("limit", 50)
results = vmAreasRepository.get_areas_geometries(session=session,
filter_area_codes=current_app.config['AREAS_LIST'],
limit=limit)
session.close()
return jsonify(results)

Expand Down Expand Up @@ -125,23 +132,23 @@ def getObservationsGenericApi(cd_ref: int):


if not current_app.config['AFFICHAGE_MAILLE']:
@api.route("/observations/<insee>/<int:cd_ref>", methods=["GET"])
def getObservationsCommuneTaxonAPI(insee, cd_ref):
connection = utils.engine.connect()
observations = vmObservationsRepository.getObservationTaxonCommune(
connection, insee, cd_ref
@api.route("/observations/<area_code>/<int:cd_ref>", methods=["GET"])
def getObservationsCommuneTaxonAPI(area_code, cd_ref):
session = utils.loadSession()
observations = vmAreasRepository.get_areas_observations_by_cd_ref(
session, area_code, cd_ref
)
connection.close()
session.close()
return jsonify(observations)


@api.route("/observationsMaille/<insee>/<int:cd_ref>", methods=["GET"])
def getObservationsCommuneTaxonMailleAPI(insee, cd_ref):
connection = utils.engine.connect()
observations = vmObservationsMaillesRepository.getObservationsTaxonCommuneMaille(
connection, insee, cd_ref
@api.route("/observationsMaille/<area_code>/<int:cd_ref>", methods=["GET"])
def getObservationsCommuneTaxonMailleAPI(area_code, cd_ref):
session = utils.loadSession()
observations = vmAreasRepository.get_areas_grid_observations_by_cd_ref(
session, area_code, cd_ref
)
connection.close()
session.close()
return jsonify(observations)


Expand Down
2 changes: 2 additions & 0 deletions atlas/configuration/config_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ class Meta:
TEMPLATES_AUTO_RELOAD = fields.Boolean(load_default=False)
# Enable the search on other areas
EXTENDED_AREAS = fields.Boolean(load_default=False)
# Restricts the search (/area) and the /area/geom API
AREAS_LIST = fields.List(fields.String, load_default=[])

@validates_schema
def validate_url_taxhub(self, data, **kwargs):
Expand Down
24 changes: 15 additions & 9 deletions atlas/modeles/repositories/vmAreasRepository.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def get_id_area(session, type_code, area_code):
def get_area_from_id(session, id_area):
query = (
session.query(
VmAreas.id_area,
VmAreas.area_name,
VmAreas.area_code,
VmAreas.area_geojson,
Expand All @@ -63,6 +64,7 @@ def get_area_from_id(session, id_area):

result = query.first()
return {
"areaId": result.id_area,
"areaName": result.area_name,
"areaCode": str(result.area_code),
"areaGeoJson": json.loads(result.area_geojson),
Expand Down Expand Up @@ -168,7 +170,7 @@ def get_observers_area(session, id_area):
return query.all()


def search_area_by_type(session, search=None, type_code=None, limit=50):
def search_area_by_type(session, search=None, type_code=None, filter_area_codes=[], limit=50):
query = (
session.query(
VmAreas.id_area,
Expand All @@ -186,7 +188,8 @@ def search_area_by_type(session, search=None, type_code=None, limit=50):
query = query.filter(or_(
VmAreas.area_name.ilike("%" + search + "%"),
VmAreas.area_code.ilike("%" + search + "%")))

if len(filter_area_codes) > 0:
query = query.filter(VmBibAreasTypes.type_code.in_(filter_area_codes))
query = query.limit(limit)
current_app.logger.debug("<search_area_by_type> query {}".format(query))

Expand All @@ -197,7 +200,7 @@ def search_area_by_type(session, search=None, type_code=None, limit=50):
return areaList


def get_areas_geometries(session, type_code=None, limit=50):
def get_areas_geometries(session, type_code=None, filter_area_codes=[], limit=50):
query = (
session.query(
VmAreas.area_name,
Expand All @@ -211,6 +214,8 @@ def get_areas_geometries(session, type_code=None, limit=50):
)
if type_code is not None:
query = query.filter(VmBibAreasTypes.type_code == type_code)
if len(filter_area_codes) > 0:
query = query.filter(VmBibAreasTypes.type_code.in_(filter_area_codes))
query = query.limit(limit)
return FeatureCollection(
[
Expand Down Expand Up @@ -259,7 +264,7 @@ def get_areas_observations(session, limit, id_area):
return result


def get_areas_observations_by_cdnom(session, id_area):
def get_areas_observations_by_cd_ref(session, area_code, cd_ref):
req = (
session.query(
VmObservations.id_observation,
Expand All @@ -275,19 +280,20 @@ def get_areas_observations_by_cdnom(session, id_area):
VmCorAreaObservation,
VmObservations.id_observation == VmCorAreaObservation.id_observation,
)
.filter(VmCorAreaObservation.id_area == id_area)
.join(VmAreas, VmAreas.id_area == VmCorAreaObservation.id_area)
.filter(VmAreas.area_code == area_code, VmObservations.cd_ref == cd_ref)
).all()
result = []
for r in req:
temp = r._asdict()
temp["geometry"] = json.loads(r.geometry or "{}")
temp["geojson_point"] = json.loads(r.geometry or "{}")
temp["dateobs"] = str(r.dateobs)
temp["group2_inpn"] = utils.deleteAccent(r.group2_inpn)
result.append(temp)
return result


def get_areas_grid_observations_by_cdnom(session, id_area, cd_nom):
def get_areas_grid_observations_by_cd_ref(session, area_code, cd_ref):
query = (
session.query(
TGrid.id_maille,
Expand All @@ -298,7 +304,7 @@ def get_areas_grid_observations_by_cdnom(session, id_area, cd_nom):
)
.join(VmAreas, VmAreas.the_geom.st_intersects(VmObservations.the_geom_point))
.join(TGrid, TGrid.the_geom.st_intersects(VmObservations.the_geom_point))
.filter(and_(VmObservations.cd_ref == cd_nom, VmAreas.area_code == id_area))
.filter(and_(VmObservations.cd_ref == cd_ref, VmAreas.area_code == area_code))
.order_by(TGrid.id_maille)
)

Expand Down Expand Up @@ -385,7 +391,7 @@ def get_surrounding_areas(session, id_area):
VmBibAreasTypes.type_name,
)
.join(VmBibAreasTypes, VmAreas.id_type == VmBibAreasTypes.id_type)
.filter(and_(VmAreas.the_geom.st_intersects(subquery.c.the_geom.st_buffer(0)), VmAreas != id_area))
.filter(and_(VmAreas != id_area, VmAreas.the_geom.st_intersects(subquery.c.the_geom.st_buffer(0))))
)

return query.all()
Expand Down
6 changes: 1 addition & 5 deletions atlas/static/mapAreas.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,7 @@ function displayObsPreciseBaseUrl(areaCode, cd_ref) {
}

function displayObsGridBaseUrl() {
if (sheetType === 'commune') {
return configuration.URL_APPLICATION + "/api/observationsMaille/"
} else {
return configuration.URL_APPLICATION + "/api/observationsMaille/area/"
}
return configuration.URL_APPLICATION + "/api/observationsMaille/"
}

// display observation on click
Expand Down
2 changes: 1 addition & 1 deletion atlas/templates/core/listTaxons.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<div id="taxonList" class="d-flex align-content-stretch bg-white flex-grow-1">
<ul class="list-group w-100">
{% for taxon in listTaxons.taxons %}
<li id="taxonListItem" class="media border-bottom p-2" area-code="{{ insee }}"
<li id="taxonListItem" class="media border-bottom p-2" area-code="{{ areaInfos.areaCode }}"
cdRef="{{ taxon.cd_ref }}" {% if configuration.INTERACTIVE_MAP_LIST %}data-toggle="tooltip"
data-original-title="{{ _('display.municipality.obs') }}"{% endif %}>
<span class="d-none">{{ taxon.group2_inpn }}</span>
Expand Down

0 comments on commit ee64457

Please sign in to comment.