-
Notifications
You must be signed in to change notification settings - Fork 2
/
geojsoncompat.py
executable file
·47 lines (41 loc) · 1.61 KB
/
geojsoncompat.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
import json
from math import radians, cos, sin, asin, sqrt
import string
def distBetween(lon1, lat1, lon2, lat2):
lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
# haversine formula
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
c = 2 * asin(sqrt(a))
r = 3956 # Radius of earth in kilometers. Use 6371 for kilometers.
return(c * r)
def filterByRadiusAndTime(dict, lat, long, radius, past_day):
python_obj = dict.get('result').get('records')
geoJsonDicts = {"type": "FeatureCollection"}
arrayOfObjects = []
for object in python_obj:
if (object["lat"] == None or object["long"] == None or object["month"] == None or object["year"] == None or object["street"] == None or object["occurred_on_date"] == None):
continue
lat1 = float(object["lat"])
long1 = float(object["long"])
month = object["month"]
year = object["year"]
date = object["occurred_on_date"]
if (distBetween(long1, lat1, long, lat) <= radius) and (date > past_day):
arrayOfObjects.append({
"geometry": {
"type": "Point",
"coordinates": [long1, lat1]
},
"type": "Feature",
"properties": {
"Offense description": object["offense_description"][0] + object["offense_description"][1:].lower(),
"Date": month + "/" + date[8:10] + "/" + year,
"Time": date[11:],
"District": object["district"],
"Street": object["street"].title()
}
})
geoJsonDicts["features"] = arrayOfObjects
return (geoJsonDicts)