forked from answerquest/pmgsy_osm_comparison
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_geospatial.py
292 lines (219 loc) · 9.7 KB
/
api_geospatial.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# api_geospatial
import os, time, json, requests
from typing import Optional, List
from pydantic import BaseModel
from fastapi.responses import FileResponse
from fastapi import HTTPException, Header, Path
import pandas as pd
import geopandas as gpd
import io
from shapely.geometry import shape
from geosadak_api_launch import app
import commonfuncs as cf
import dbconnect
from api_habitations import habitations
from globalvars import logIP
METERS_CRS = 7755
root = os.path.dirname(__file__)
gpxFolder = os.path.join(root,'gpx')
os.makedirs(gpxFolder, exist_ok=True)
OSM_additional_columns = ['name','place','population','postal_code','wikidata','wikipedia','source','is_in','addr:country','addr:postcode']
#########
# FUNCTIONS
def makegpd(x,lat='latitude',lon='longitude'):
gdf = gpd.GeoDataFrame(x, geometry=gpd.points_from_xy(x[lon],x[lat]), crs="EPSG:4326")
# gdf.drop(columns=[lat,lon], inplace=True)
return gdf
def fetchConvexHull(B, proximity=1000):
global METERS_CRS
s5 = f"""select ST_AsGeoJSON(
ST_Transform(
ST_Buffer(
ST_Transform(
ST_ConvexHull( ST_Collect(geometry) )
, {METERS_CRS}
), {proximity}
),4326
)
) as geometry
from habitation
where "BLOCK_ID"='{B}'
"""
holder2 = dbconnect.makeQuery(s5, output='oneValue')
return holder2
def processOverpassResult(data):
global OSM_additional_columns
collector = []
for e in data:
if e['type'] == 'node':
# exclude entries which are just nodes but no tags {} - those are nodes under some other object and not actual OSM places
if not e.get('tags',False):
continue
row = {'osmId': e['id'], 'lat':e['lat'], 'lon':e['lon'], 'type':e['type']}
# row.update(e.get('tags',{}))
for col in OSM_additional_columns:
if e.get('tags',{}).get(col):
row[col] = e['tags'][col]
collector.append(row)
elif e['type'] == 'way' and e.get('tags',{}):
# if polygon, then take its centroid
if e.get('center',False):
row = {'osmId': e['id'], 'lat':e['center']['lat'], 'lon':e['center']['lon'], 'type':e['type']}
for col in OSM_additional_columns:
if e.get('tags',{}).get(col):
row[col] = e['tags'][col]
collector.append(row)
if len(collector):
df1 = pd.DataFrame(collector).fillna('')
return df1
else:
return []
# TO DO: whitelist of accepted column names, or upper limit on variety of tags
# returnD = {'num': len(df1), 'osm_locations':df1.to_csv(index=False)}
# else:
# returnD = {'num':0 }
# return returnD
##################
# APIs
@app.get("/API/loadRegion/{BLOCK_ID}", tags=["geospatial"])
def loadRegion(BLOCK_ID: str, interservice:Optional[bool] = False, X_Forwarded_For: Optional[str] = Header(None) ):
if not interservice: logIP(X_Forwarded_For,'loadRegion', limit=3)
calculatedFlag = False
s1 = f"""select ST_AsGeoJSON(geometry) from block
where "BLOCK_ID" = '{BLOCK_ID}'
"""
res = dbconnect.makeQuery(s1, output='oneValue')
if not res:
cf.logmessage(f"No block boundary found for {BLOCK_ID}, fallback to habitations data buffered convex hull")
res = fetchConvexHull(BLOCK_ID, proximity=1000)
calculatedFlag = True
if not res:
raise HTTPException(status_code=400, detail="No boundary data found for selected block")
try:
geo = json.loads(res)
except:
raise HTTPException(status_code=400, detail="Could not load geo data")
return { 'calculated':calculatedFlag, 'geodata':geo }
@app.get("/API/boundaryGPX/{BLOCK_ID}", tags=["geospatial"])
def boundaryGPX(BLOCK_ID: str, X_Forwarded_For: Optional[str] = Header(None) ):
# to do: if not already made, create a (simplified!) .GPX file of a region and save it in the gpx static folder for access from OSM editor
logIP(X_Forwarded_For,'boundaryGPX')
if os.path.isfile(os.path.join(gpxFolder,f"{BLOCK_ID}.gpx")):
return {'created':False }
s1 = f"""select ST_AsGeoJSON(geometry) from block
where "BLOCK_ID" = '{BLOCK_ID}'
"""
res = dbconnect.makeQuery(s1, output='oneValue')
if not res:
cf.logmessage(f"No block boundary found for {BLOCK_ID}, fallback to habitations data buffered convex hull")
res = fetchConvexHull(BLOCK_ID, proximity=1000)
if not res:
raise HTTPException(status_code=400, detail="No data")
try:
geo = json.loads(res)
except:
raise HTTPException(status_code=400, detail="Could not load geo data")
bdf1 = gpd.GeoDataFrame({'geometry':[shape(geo).simplify(0.001)]}, crs="EPSG:4326")
# simplify it also - makes for a much smaller size
bdf1.boundary.to_file(os.path.join(gpxFolder,f"{BLOCK_ID}.gpx"), 'GPX')
return {'created':True }
##################
@app.get("/API/blockFromMap/{lat}/{lon}", tags=["geospatial"])
def blockFromMap(lat: float, lon:float, X_Forwarded_For: Optional[str] = Header(None) ):
logIP(X_Forwarded_For,'blockFromMap', limit=5)
s1 = f"""select "BLOCK_ID", "BLOCK_NAME", "DISTRICT_ID", "DISTRICT_NAME",
"STATE_ID", "STATE_NAME"
from block
where ST_Contains(geometry, ST_Point({lon},{lat},4326))
"""
regionD = dbconnect.makeQuery(s1, output='oneJson')
# to do: in case nothing found, do nearby search in habitation folder
return regionD
##################################3
class comparison1_payload(BaseModel):
STATE_ID: str
BLOCK_ID: str
osmData: list = []
proximity: Optional[int] = 1000
outlier_habitations: Optional[bool] = False
# shape_buffer: Optional[int] = 1000
@app.post("/API/comparison1", tags=["geospatial"])
def comparison1(r: comparison1_payload, X_Forwarded_For: Optional[str] = Header(None) ):
logIP(X_Forwarded_For,'comparison1', limit=20)
global METERS_CRS
returnD = {}
# step 1: fetch boundary
t1 = time.time()
regionHolder = loadRegion(BLOCK_ID=r.BLOCK_ID, interservice=True)
boundary1 = regionHolder.get('geodata')
boundsT = shape(boundary1).bounds
# looks like: (79.686591005, 10.982368874, 79.858169814, 11.193869573) so lon, lat, lon, lat
# step 2: fetch data from OSM
t2 = time.time()
# osmHolder = overpass(boundsT[1], boundsT[0], boundsT[3], boundsT[2])
# odf1 = pd.read_csv(io.BytesIO(osmHolder.get('osm_locations').encode('UTF-8'))).fillna('')
odf1 = processOverpassResult(r.osmData)
if not len(odf1):
# no data from OSM? quit here only
raise HTTPException(status_code=400, detail="No overpass data found")
odf2 = makegpd(odf1, lat='lat',lon='lon')
# step 3: fetch habitations data
# do habitations fetch after overpass, to save the trouble in case there's no data from overpass
t3 = time.time()
habHolder = habitations(STATE_ID=r.STATE_ID, BLOCK_ID=r.BLOCK_ID, interservice=True)
hdf1 = pd.read_csv(io.BytesIO(habHolder.get('data').encode('UTF-8'))).fillna('')
hdf2 = makegpd(hdf1)
# ok NOW start the geospatial work
t4 = time.time()
# step 4: clip OSM data down to the boundary
# https://geopandas.org/en/stable/docs/reference/api/geopandas.GeoSeries.within.html
# for many-to-one, get target in shapely shape format, not gpd
odf3 = odf2[odf2.within(shape(boundary1))].copy().reset_index(drop=True)
if(len(odf3) < len(odf1)):
cf.logmessage(f"OSM: {len(odf1)} places to {len(odf3)} after clipping by buffered boundary")
# check if nothing in odf3 ?
if not len(odf3):
returnD['num_OSM_near'] = 0
returnD['num_OSM_far'] = 0
return returnD
# step 5: buffer the habitation data to <proximity> radius
hdf3 = hdf2.to_crs(METERS_CRS).buffer(r.proximity).to_crs(4326)
# step 6: turn it to a single blob
buffer1 = hdf3.unary_union
# from https://pygis.io/docs/e_buffer_neighbors.html
# step 7: get the OSM data points that fall within this buffered blob
odf4 = odf3[odf3.within(buffer1)].copy()
returnD['num_OSM_near'] = len(odf4)
if len(odf4):
odf4['proximity'] = 'near'
returnD['OSM_near'] = odf4.drop(columns='geometry').to_csv(index=False)
# step 8: get the OSM data points that fall outside of this buffered blob
odf5 = odf3[~odf3.within(buffer1)].copy()
returnD['num_OSM_far'] = len(odf5)
if len(odf5):
odf5['proximity'] = 'far'
returnD['OSM_far'] = odf5.drop(columns='geometry').to_csv(index=False)
t5 = time.time()
#######
if r.outlier_habitations:
# do reverse proximity check: buffer and make a blob of all the OSM places,
# then do a within check with Habitations data.
# find which Habitations are within proximity of OSM places, and which are the outliers.
odf10 = odf3.to_crs(METERS_CRS).buffer(r.proximity).to_crs(4326)
buffer2 = odf10.unary_union
hdf10 = hdf2[hdf2.within(buffer1)].copy()
returnD['num_Hab_near'] = len(hdf10)
if len(hdf10):
returnD['habitations_near'] = hdf10['id'].tolist()
hdf11 = hdf2[~hdf2.within(buffer1)].copy()
returnD['num_Hab_far'] = len(hdf11)
if len(hdf11):
returnD['habitations_far'] = hdf11['id'].tolist()
t6 = time.time()
returnD['time_region_fetch'] = round(t2-t1,3)
returnD['time_osm_fetch'] = round(t3-t2,3)
returnD['time_habitations_fetch'] = round(t4-t3,3)
returnD['time_geospatial1'] = round(t5-t4,3)
if r.outlier_habitations: returnD['time_geospatial2'] = round(t6-t5,3)
returnD['time_total'] = round(t6-t1,3)
return returnD