-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserve.py
54 lines (46 loc) · 1.66 KB
/
serve.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
from flask import Flask
from osgeo import ogr
import json
app = Flask(__name__)
# global vars
PATH = 'census_tracts/census_reproj4326.shp'
LAYER = 'census_reproj4326'
DRIVER = ogr.GetDriverByName ("ESRI Shapefile")
ogr_ds = DRIVER.Open(PATH)
# This is the URL pattern (where 'fid' is dynamic)
@app.route("/eq/<int:fid>")
def query_eq(fid):
startFC = "{\"type\": \"FeatureCollection\", \"features\": ["
endFC = "] }"
sql = "SELECT * FROM %s WHERE OBJECTID=%s" %(LAYER, fid)
queryLayer = ogr_ds.ExecuteSQL(sql)
for (idx, feature) in enumerate(queryLayer):
if idx == 0:
startFC = startFC + feature.ExportToJson()
else:
startFC = startFC + "," + feature.ExportToJson()
return startFC + endFC
@app.route("/lt/<int:fid>")
def query_lt(fid):
startFC = "{\"type\": \"FeatureCollection\", \"features\": ["
endFC = "] }"
sql = "SELECT * FROM %s WHERE OBJECTID<%s LIMIT 20" %(LAYER, fid)
queryLayer = ogr_ds.ExecuteSQL(sql)
for (idx, feature) in enumerate(queryLayer):
if idx == 0:
startFC = startFC + feature.ExportToJson()
else:
startFC = startFC + "," + feature.ExportToJson()
return startFC + endFC
@app.route("/gt/<int:fid>")
def query_gt(fid):
startFC = "{\"type\": \"FeatureCollection\", \"features\": ["
endFC = "] }"
sql = "SELECT * FROM %s WHERE OBJECTID>%s LIMIT 20" %(LAYER, fid)
queryLayer = ogr_ds.ExecuteSQL(sql)
for (idx, feature) in enumerate(queryLayer):
if idx == 0:
startFC = startFC + feature.ExportToJson()
else:
startFC = startFC + "," + feature.ExportToJson()
return startFC + endFC