-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
172 lines (128 loc) · 4.38 KB
/
app.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
#
# References
# https://en.wikipedia.org/wiki/Geohash
# http://geojson.org/geojson-spec.html
# http://www.macwright.org/2015/03/23/geojson-second-bite.html
#
from flask import Flask, jsonify, redirect, request
import pygeohash as pygeohash
import geojson
import json
app = Flask(__name__)
@app.route('/geohash/encode', methods=['GET'])
def encode():
lat = request.args.get("lat")
lon = request.args.get("lon")
if lat is None or lon is None:
msg = {'ERROR': 'Missing parameter.'}
return jsonify(msg), 400
try:
lat = float(lat)
except ValueError:
msg = {'ERROR': 'The specified latitude is not a number.'}
return jsonify(msg), 400
try:
lon = float(lon)
except ValueError:
msg = {'ERROR': 'The specified longitude is not a number.'}
return jsonify(msg), 400
if lat < -90.0 or lat > 90.0:
msg = {'ERROR': 'The specified latitude is invalid.'}
return jsonify(msg), 400
if lon < -180.0 or lon > 180.0:
msg = {'ERROR': 'The specified longitude is invalid.'}
return jsonify(msg), 400
result = pygeohash.encode(lat, lon)
data = {'geohash': result}
response = jsonify(data)
return response
@app.route('/geohash/encode', methods=['POST'])
def encode_from_geojson():
try:
data = request.json
if data['type'] == 'Point':
# remember that geohash uses lat,lon
# but geojson uses lon,lat
lon = data['coordinates'][0]
lat = data['coordinates'][1]
result = pygeohash.encode(lat, lon)
data = {'geohash': result}
response = jsonify(data)
return response
else:
msg = {'ERROR': 'Geojson type is not a Point'}
return jsonify(msg), 400
except Exception as e:
print(e)
msg = {'ERROR': 'Could not parse geojson.'}
return jsonify(msg), 400
return ""
@app.route('/geohash/decode/<geohash>')
def decode(geohash):
if geohash is None:
msg = {'ERROR': 'Missing geohash ! Try /decode/<geohash>'}
return jsonify(msg), 400
exact_mode = request.args.get("exact")
if exact_mode:
try:
# Returns four float values: latitude, longitude, the plus/minus error
# for latitude (as a positive number) and the plus/minus
# error for longitude (as a positive number).
lat, lon, lat_err, lon_err = pygeohash.decode_exactly(geohash)
# 1 ---- 4
# | |
# 2 ---- 3
#
box1 = (lon - lon_err, lat + lat_err)
box2 = (lon - lon_err, lat - lat_err)
box3 = (lon + lon_err, lat - lat_err)
box4 = (lon + lon_err, lat + lat_err)
response = jsonify(geojson.Polygon([[box1, box2, box3, box4, box1]]))
return response
except Exception as e:
print(e)
msg = {'ERROR': 'Could not decode the geohash'}
return jsonify(msg), 400
else:
try:
# Returning two float with latitude and longitude
# containing only relevant digits and with trailing zeroes removed
lat, lon = pygeohash.decode(geohash)
#lat = result[0]
#lon = result[1]
# lat, lon to geojson point
response = jsonify(geojson.Point([lon, lat]))
return response
except Exception as e:
print(e)
msg = {'ERROR': 'Could not decode the geohash'}
return jsonify(msg), 400
@app.route('/geohash/precision/<geohash>')
def precision(geohash):
if geohash is None:
msg = {'ERROR': 'Missing geohash ! Try /precision/<geohash>'}
return jsonify(msg), 400
geohash_length = len(geohash)
if geohash_length > 10:
geohash_length = 10
# Precision lookup table for geohashes (in meters)
# from https://github.com/wdm0006/pygeohash/blob/master/pygeohash/distances.py
PRECISION = {
0: 20000000,
1: 5003530,
2: 625441,
3: 123264,
4: 19545,
5: 3803,
6: 610,
7: 118,
8: 19,
9: 3.71,
10: 0.6,
}
result = PRECISION[geohash_length]
data = {'precision' : { 'number': result, 'unit': 'meters'}}
response = jsonify(data)
return response
if __name__ == "__main__":
app.run(port=8080, threaded=True)