-
Notifications
You must be signed in to change notification settings - Fork 2
/
export_functions.py
83 lines (72 loc) · 4.11 KB
/
export_functions.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
import geojson
from geojson import Feature, MultiPolygon
def dat_export(file_name, output_dir):
print('file_name {}'.format(file_name))
print('output_dir {}'.format(output_dir))
with open(file_name) as f:
gj = geojson.load(f)
split_id = []
u_id = {}
new_feature_list = []
print(gj['name'])
for feature in gj['features']:
if feature['properties']['id'] not in u_id.keys():
u_id[feature['properties']['id']] = feature['properties']['oceanic']
feature['properties']['extension'] = '0'
else:
if u_id[feature['properties']['id']] == feature['properties']['oceanic']:
feature['properties']['extension'] = '1'
else:
feature['properties']['extension'] = '0'
# need to be split
if len(feature['geometry']['coordinates']) > 1:
extension = False
for polygon in feature['geometry']['coordinates']:
if feature['properties']['id'] not in split_id:
extension = '0'
split_id.append(feature['properties']['id'])
else:
extension = '1'
polygon = MultiPolygon([[[tuple(x) for x in polygon[0]]]])
new_feature = Feature(geometry=polygon, properties={'id': feature['properties']['id'],
'oceanic': feature['properties'][
'oceanic'],
'extension': extension,
'label_lon': feature['properties'][
'label_lon'],
'label_lat': feature['properties'][
'label_lat']
})
new_feature_list.append(new_feature)
else:
# no need to be split
new_feature_list.append(feature)
gj['features'] = new_feature_list
# start building output
f = open(output_dir, 'w')
for feature in gj['features']:
coordinates = feature['geometry']['coordinates'][0][0]
properties = feature['properties']
point_count = len(coordinates)
min_lat = min(coordinates, key=lambda x: x[1])[1]
min_lon = min(coordinates, key=lambda x: x[0])[0]
max_lat = max(coordinates, key=lambda x: x[1])[1]
max_lon = max(coordinates, key=lambda x: x[0])[0]
header_line = '{}|{}|{}|{}|{}|{}|{}|{}|{}|{}\n'.format(properties['id'], properties['oceanic'],
properties['extension'], point_count,
min_lat,
min_lon,
max_lat,
max_lon,
properties['label_lat'],
properties['label_lon'])
f.write(header_line)
for coord in coordinates:
val1 = round(coord[1], 6)
val2 = round(coord[0], 6)
coord_line = '{}|{}\n'.format(val1, val2)
f.write(coord_line)
f.close()
if __name__ == "__main__":
dat_export(r'C:\Users\niels\PycharmProjects\vatspy-geojson\newdatafile_test\DAT\test\converted to gj\firboundaries.geojson',
r'C:\Users\niels\PycharmProjects\vatspy-geojson\newdatafile_test\DAT\Firboundaries.dat')