-
Notifications
You must be signed in to change notification settings - Fork 1
/
poly_convert.py
109 lines (80 loc) · 3.25 KB
/
poly_convert.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env python3
import glob
import json
import os
from geographiclib.geodesic import Geodesic
from konverter import Frame
def is_too_far(lat1, lon1, lat2, lon2):
path = Geodesic.WGS84.Inverse(lat1, lon1, lat2, lon2)
return path['s12'] / 1852 > 300
def fix_polygon(feature, frame):
lon, lat = feature['geometry']['coordinates'][0][0]
changed = False
if is_too_far(lat, lon, frame.lat, frame.lon):
for coords in feature['geometry']['coordinates']:
for point in coords:
point[0], point[1] = point[1], point[0]
changed = True
return feature, changed
def fix_linestring(feature, frame):
lon, lat = feature['geometry']['coordinates'][0][0]
changed = False
if is_too_far(lat, lon, frame.lat, frame.lon):
for coords in feature['geometry']['coordinates']:
coords[0], coords[1] = coords[1], coords[0]
changed = True
return feature, changed
def fix_point(feature, frame):
lon, lat = feature['geometry']['coordinates']
changed = False
if is_too_far(lat, lon, frame.lat, frame.lon):
feature['geometry']['coordinates'][0], feature['geometry']['coordinates'][1] = \
feature['geometry']['coordinates'][1], feature['geometry']['coordinates'][0]
changed = True
return feature, changed
def fix_feature(feature, frame):
if feature['geometry']['type'] == 'Polygon':
return fix_polygon(feature, frame)
if feature['geometry']['type'] == 'Point':
return fix_point(feature, frame)
if feature['geometry']['type'] == 'LineString':
return fix_linestring(feature, frame)
def check_constraints_file(file, frame):
with open(file) as f:
data = json.loads(f.read())
any_changed = False
for i in range(len(data['features'])):
data['features'][i], changed = fix_feature(data['features'][i], frame)
any_changed = any_changed or changed
with open(file, 'w') as f:
json.dump(data, f)
if any_changed:
print('{} fixed'.format(os.path.abspath(file)))
return any_changed
def run_directory(datadir):
os.chdir(datadir)
try:
with open(os.path.join(datadir, 'nav-data.json')) as f:
nav_data = json.loads(f.read())
frame = Frame(nav_data['lat'], nav_data['lon'])
except FileNotFoundError:
with open(os.path.join(datadir, 'navigation.json')) as f:
nav_data = json.loads(f.read())
frame = Frame(nav_data['lat'], nav_data['lon'])
file_list = glob.glob('constraints*.json')
any_changed = False
for constr_file in file_list:
changed = check_constraints_file(constr_file, frame)
any_changed = any_changed or changed
return any_changed
def fix_from_root(data_directory):
for root, dirs, files in os.walk(data_directory):
if "nav-data.json" in files or 'navigation.json' in files:
run_directory(os.path.join(data_directory, root))
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="BKS report generator")
parser.add_argument("root_dir", type=str, nargs='?', help="Path cases root", default=os.getcwd())
args = parser.parse_args()
cur_dir = os.path.abspath(args.root_dir)
fix_from_root(cur_dir)