-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathparser.py
56 lines (47 loc) · 1.48 KB
/
parser.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
# -*- coding: utf-8 -*-
import sys
from datetime import datetime
import ijson
def create_feature(obj):
return {
'type': 'Feature',
'geometry': {
'type': 'Point',
'coordinates': [
obj['longitudeE7'] / 10000000.0,
obj['latitudeE7'] / 10000000.0
]
},
'properties': {
'accuracy': obj.get('accuracy', None),
'timestamp': datetime.fromtimestamp(int(obj['timestampMs']) / 1000.0).isoformat()
}
}
def parse_location(stream):
parser = ijson.parse(stream)
reading = False
obj = {}
key = None
value = None
for prefix, event, value in parser:
if prefix == 'locations' and event == 'start_array':
reading = True
elif prefix == 'locations' and event == 'end_array':
reading = False
elif reading:
if event == 'start_map' and prefix == 'locations.item':
obj = {}
elif event == 'end_map' and prefix == 'locations.item':
yield create_feature(obj)
elif event == 'map_key':
key = value
elif prefix == 'locations.item.%s' % key and value is not None:
obj[key] = value
if __name__ == '__main__':
if len(sys.argv) == 1:
print 'Specify filname'
exit(0)
filename = sys.argv[1]
with open(filename, 'r') as file:
for feature in parse_location(file):
print feature