-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv_to_json.py
82 lines (77 loc) · 2.02 KB
/
csv_to_json.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
import csv
import json
from datetime import (datetime, timezone)
from dateutil import parser
def gotojson(filename):
# init the rows by key 'vessels1' or etc
ROWS = {}
for x in range(1,30):
key = 'vessels' + str(x)
ROWS[key] = []
# read the file
with open(filename) as csvFile:
csvReader = csv.DictReader(csvFile)
for row in csvReader:
locfilter = float(row['longitude']) < 154 and float(row['longitude']) > 0
if locfilter:
continue
# separate rows based on `vesselsN` column
for x in range(1,30):
key = 'vessels' + str(x)
if row[key] == 'true':
ROWS[key].append(row)
OUTPUT = {}
for x in range(1,30):
key = 'vessels' + str(x)
OUTPUT[key] = {}
# now we ditch the older timestamps per MMSI for each vessel frame
for key, rows in ROWS.items():
for row in rows:
# get mmsi
mmsi = row['mmsi']
# get timestamp
ts = parser.parse(row['timestamp'])
# dummy time in past
latest = datetime(2020, 4, 1, 0, tzinfo=timezone.utc)
# see if we have a real latest value
if mmsi in OUTPUT[key]:
latest = OUTPUT[key][mmsi]['latest']
# check current value against latest
if ts > latest:
OUTPUT[key][mmsi] = {
'row': row,
'latest': ts
}
for key, rows in OUTPUT.items():
outson = {
'type': 'FeatureCollection',
'features': []
}
with open('json/{}.js'.format(key), 'w') as outfile:
for mmsi, data in rows.items():
row = data['row']
degrees = row['heading']
if degrees == '' or float(degrees) == 511:
degrees = 0
radians = float(degrees) * 0.0174533
props = {}
for p, v in row.items():
props[p] = v
feature = {
'type': 'Feature',
'properties': {
'heading_radians': radians,
# 'data': props
},
'geometry': {
'type': 'Point',
'coordinates': [
row['longitude'],
row['latitude']
]
}
}
outson['features'].append(feature)
outfile.write('var {} = {};'.format(key, json.dumps(outson)))
if __name__ == '__main__':
gotojson('CSV/ASP-hours-column-label.csv')