-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadvanced_state_server.py
57 lines (47 loc) · 1.57 KB
/
advanced_state_server.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
from django.http import HttpResponse
from django.urls import re_path
from django.views.decorators.csrf import csrf_exempt
import geopandas
from shapely.geometry import Point
import json
SECRET_KEY = "nl$7nsm@_7=2bs=^37j)n%+yb+patcb8!5zjk+%p$mqb^=)k&r"
ROOT_URLCONF = __name__
ALLOWED_HOSTS = ["127.0.0.1", "localhost"]
LOGGING = {
'version': 1,
'disable_existing_loggers': True,
'handlers': {
'null': {
'class': 'logging.NullHandler',
},
},
'loggers': {
'django': {
'handlers': ['null'],
'propagate': False,
'level': 'CRITICAL',
},
},
}
SHAPEFILE = 'shapefile/cb_2018_us_state_20m.shp'
GEOMETRY_DATAFRAME = geopandas.read_file(SHAPEFILE)
def find_state(longitude, latitude):
point = Point(longitude, latitude) # Construct a point with given long and lat
for index, row in GEOMETRY_DATAFRAME.iterrows():
if row['geometry'].contains(point): # Check if the geometry contains the point
return(row['NAME'])
@csrf_exempt
def index(request):
if request.method == "POST":
longitude = request.POST['longitude']
latitude = request.POST['latitude']
match = find_state(longitude, latitude)
if match == None:
return HttpResponse("[]\n", content_type="text/plain")
else:
return HttpResponse(f"[{json.dumps(match)}]\n", content_type="text/plain")
else:
return HttpResponse("This endpoint only accepts POST requests.", content_type="text/plain")
urlpatterns = [
re_path(r"^$", index, name="index"),
]