-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeopackage.py
More file actions
72 lines (67 loc) · 2.3 KB
/
geopackage.py
File metadata and controls
72 lines (67 loc) · 2.3 KB
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
from shapely.geometry import Point
import geopandas as gpd
import pandas as pd
class GeoPackage:
driver = 'GPKG'
crs = 'EPSG:4326'
template = {
"geometry": [None],
"altitude": [None],
"battery_level": [None],
"battery_state": [None],
"course": [None],
"course_accuracy": [None],
"horizontal_accuracy": [None],
"motion": [[]],
"speed": [None],
"speed_accuracy": [None],
"timestamp": [None],
"trip_id": [None],
"vertical_accuracy": [None],
"wifi": [None]
}
def __init__(self, path, layer):
self.path = path
self.layer = layer
def json_to_frame(self, json):
data = self.template
data['geometry'] = [Point(json['geometry']['coordinates'])]
for key, value in json['properties'].items():
if value is None:
continue
match key:
case 'altitude':
value = int(value)
case 'battery_level':
value = int(value)
case 'course':
value = int(value)
case 'course_accuracy':
value = int(value)
case 'end':
value = pd.to_datetime(value)
case 'horizontal_accuracy':
value = int(value)
case 'speed':
value = int(value)
case 'speed_accuracy':
value = int(value)
case 'start':
value = pd.to_datetime(value)
case 'steps':
value = int(value)
case 'timestamp':
value = pd.to_datetime(value)
case 'trip_id':
value = pd.to_datetime(value)
case 'vertical_accuracy':
value = int(value)
data[key] = [value]
return gpd.GeoDataFrame(data, crs=self.crs)
def save_frames_to_file(self, frames):
combined = gpd.GeoDataFrame(pd.concat(frames, ignore_index=True),
crs=self.crs)
combined.to_file(self.path,
layer=self.layer,
driver=self.driver,
mode='a')