forked from cquest/rpg_as_api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpg_as_api.py
76 lines (62 loc) · 3.08 KB
/
rpg_as_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#! /usr/bin/python3
# modules additionnels
import falcon
import psycopg2
SOURCE = 'ASP / Registre Parcellaire Graphique'
DERNIER_MILLESIME = '2017'
LICENCE = 'Licence Ouverte'
class rpg(object):
def getRPG(self, req, resp):
db = psycopg2.connect("") # connexion à la base PG locale
cur = db.cursor()
where = ''
where2 = b''
annee = req.params.get('annee', None)
if annee:
where2 = cur.mogrify(' AND annee = %s ', (annee,))
code_culture = req.params.get('code_culture', None)
if code_culture and len(code_culture) == 3:
where2 = where2 + \
cur.mogrify(' AND code_culture = %s ', (code_culture,))
lat,lon = (req.params.get('lat', None), req.params.get('lon', None))
dist = min(int(req.params.get('dist',100)),1000)
epsg = 4326
x,y = (req.params.get('x', None), req.params.get('y', None))
if x and y:
# on a des coordonnées en lambert...
lat,lon = (y, x)
epsg = 2154
if lat and lon: # recherche géographique
query = cur.mogrify("""
select json_build_object('source', %s,
'derniere_maj', %s,
'licence', %s,
'type','Featurecollection',
'features', case when count(*)=0 then array[]::json[] else array_agg(json_build_object('type','Feature',
'properties',json_strip_nulls(row_to_json(p))::jsonb - 'wkb_geometry'
|| json_build_object('bbox', st_asgeojson(st_transform(st_envelope(wkb_geometry),%s),6,0)::json)::jsonb
|| json_build_object('centroid', st_asgeojson(st_transform(st_centroid(wkb_geometry),%s),6,0)::json)::jsonb
,
'geometry',st_asgeojson(st_transform(wkb_geometry,%s),6,0)::json)) end )::text
from rpg_parcelles p
where st_buffer(st_transform(st_setsrid(st_makepoint(%s, %s),%s),4326)::geography, %s)::geometry && wkb_geometry
and ST_DWithin(st_transform(st_setsrid(st_makepoint(%s, %s),%s),4326)::geography, wkb_geometry::geography, %s)
""", (SOURCE, DERNIER_MILLESIME, LICENCE, epsg, epsg, epsg, lon, lat, epsg, dist, lon, lat, epsg, dist)) + where2
cur.execute(query)
dvf = cur.fetchone()
resp.status = falcon.HTTP_200
resp.set_header('X-Powered-By', 'rpg_as_api')
resp.set_header('Access-Control-Allow-Origin', '*')
resp.set_header("Access-Control-Expose-Headers","Access-Control-Allow-Origin")
resp.set_header('Access-Control-Allow-Headers','Origin, X-Requested-With, Content-Type, Accept')
resp.set_header('X-Robots-Tag', 'noindex, nofollow')
resp.body = dvf[0]
else:
resp.status = falcon.HTTP_413
resp.body = '{"erreur": "aucun critère de recherche indiqué"}'
db.close()
def on_get(self, req, resp):
self.getRPG(req, resp)
# instance WSGI et route vers notre API
app = falcon.API()
app.add_route('/rpg', rpg())