@@ -118,43 +118,38 @@ def lastObservationsMailles(connection, mylimit, idPhoto):
118118
119119def lastObservationsAreaMaille (connection , obs_limit , id_area ):
120120 sql = """
121- WITH obs_in_area AS (
122- SELECT
123- obs.id_observation,
124- obs.cd_ref,
125- date_part('year', obs.dateobs) AS annee
126- FROM atlas.vm_observations obs
127- JOIN atlas.vm_cor_area_synthese AS cas ON cas.id_synthese = obs.id_observation
128- WHERE cas.id_area = :idAreaCode
129- )
121+ WITH obs_in_area AS (
130122 SELECT
131- obs_in_area.id_observation,
132- obs_in_area.cd_ref,
133- COALESCE(t.nom_vern || ' | ', '') || t.lb_nom AS display_name,
134- obs_in_area.annee,
135- obs.type_code,
136- obs.id_maille,
137- vla.area_geojson AS geojson_4326
138- FROM obs_in_area
139- JOIN atlas.vm_observations_mailles obs ON obs_in_area.id_observation = ANY(obs.id_observations)
140- JOIN atlas.vm_l_areas vla ON vla.id_area=obs.id_maille
141- JOIN atlas.vm_taxons AS t ON t.cd_ref = obs_in_area.cd_ref
142- ORDER BY annee DESC
143- LIMIT :obsLimit;
123+ obs.id_observation,
124+ obs.cd_ref,
125+ date_part('year', obs.dateobs) AS annee
126+ FROM atlas.vm_observations obs
127+ JOIN atlas.vm_cor_area_synthese AS cas ON cas.id_synthese = obs.id_observation
128+ WHERE cas.id_area = :idAreaCode
129+ )
130+ SELECT
131+ json_build_object(
132+ 'type', 'FeatureCollection',
133+ 'features', json_agg(ST_AsGeoJSON(features.*)::json)
134+ ) AS observations_features
135+ FROM (
136+ SELECT
137+ COUNT(obs_in_area.id_observation) AS nb_observations,
138+ COUNT(DISTINCT obs_in_area.cd_ref) AS nb_cd_ref,
139+ json_agg(DISTINCT jsonb_build_object(
140+ 'name', (COALESCE(t.nom_vern || ' | ', '') || t.lb_nom),
141+ 'cdRef', t.cd_ref)) AS taxons,
142+ obs.type_code,
143+ obs.id_maille,
144+ vla.the_geom
145+ FROM obs_in_area
146+ JOIN atlas.vm_observations_mailles obs ON obs_in_area.id_observation = ANY(obs.id_observations)
147+ JOIN atlas.vm_l_areas vla ON vla.id_area=obs.id_maille
148+ JOIN atlas.vm_taxons AS t ON t.cd_ref = obs_in_area.cd_ref
149+ GROUP BY obs.type_code, obs.id_maille, vla.the_geom) AS features
144150 """
145- results = connection .execute (text (sql ), idAreaCode = id_area , obsLimit = obs_limit )
146- observations = list ()
147- for r in results :
148- infos = {
149- "cd_ref" : r .cd_ref ,
150- "taxon" : r .display_name ,
151- "geojson_maille" : json .loads (r .geojson_4326 ),
152- "id_maille" : r .id_maille ,
153- "id_observation" : r .id_observation ,
154- "type_code" : r .type_code ,
155- }
156- observations .append (infos )
157- return observations
151+ query = connection .execute (text (sql ), idAreaCode = id_area , obsLimit = obs_limit )
152+ return dict (query .all ()[0 ])
158153
159154
160155# Use for API
0 commit comments