-
Notifications
You must be signed in to change notification settings - Fork 1
/
csv2geojson.py
42 lines (32 loc) · 935 Bytes
/
csv2geojson.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
import csv
import json
def point(ts, tid, ip, lat, lon, alt, speed):
"""GeoJSON point feature from csv row"""
return {
"type":"Feature",
"geometry": {
"type": "Point",
"coordinates": [float(lon), float(lat)],
"properties": {
"timestamp": round(float(ts)),
"ip": str(ip),
"altitude": float(alt),
"speed": float(speed)
}
}
}
def feature_collection(lst):
"""GeoJSON feature collection."""
return {
'type': 'FeatureCollection',
'features': lst
}
def extract_features(src_csv):
"""list of features from csv."""
with open(src_csv, 'r') as f:
reader = csv.reader(f)
next(reader)
return [point(*row) for row in reader]
features = extract_features('push.csv')
fc = feature_collection(features)
print(json.dumps(fc, indent=2))