-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathimport_functions.py
88 lines (73 loc) · 2.84 KB
/
import_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
84
85
86
87
88
import csv
import json
airspace_count = 0
fir_list = []
firpoint_list = None
firheader_list = None
# file_name = 'source/FIRBoundaries_test.dat'
# file_name = 'source/FIRBoundaries.dat'
def fir_header(header):
global airspace_count, firheader_list
airspace_count += 1
firheader_list = header
def airspace_pnt(firpoint_list, point):
coords = [float(point[0]), float(point[1])]
coords.reverse()
point = [coords]
if firpoint_list == None:
firpoint_list = point
else:
firpoint_list = firpoint_list + point
return firpoint_list
def airspace_close():
global firheader_list, firpoint_list
header = firheader_list + [firpoint_list]
keys = ['ICAO', 'IsOceanic', 'IsExtension', 'PointCount', 'MinLat', 'MinLon', 'MaxLat', 'MaxLon', 'CenterLat', 'CenterLon', 'Coordinates']
fir_dict = dict(zip(keys, header))
firpoint_list = None
return fir_dict
def geojson_writer(features, output_dir):
'''Create GeoJSON Header'''
print(output_dir)
basedict = {}
crsdict = {}
propertydict = {}
feature_list = []
basedict['type'] = "FeatureCollection"
basedict['name'] = ''
propertydict['name'] = "urn:ogc:def:crs:OGC:1.3:CRS84"
crsdict['type'] = "name"
crsdict['properties'] = propertydict
basedict['crs'] = crsdict
for section in features:
feature_list = feature_list + section[1]
basedict['features'] = feature_list
'''write to text file'''
print(output_dir)
outputfile = open(output_dir, 'w')
outputfile.write(json.dumps(basedict, indent=4))
# print(json.dumps(basedict, indent=4))
outputfile.close()
print_string = 'GeoJSON saved to {}'.format(output_dir)
print(print_string)
def dat_import(file_name, output_dir):
global airspace_count, firpoint_list
with open(file_name) as fir_file:
csv_reader = csv.reader(fir_file, delimiter='|')
for row in csv_reader:
if len(row) > 2:
if airspace_count > 0:
fir_list.append(airspace_close())
fir_header(row)
else:
firpoint_list = airspace_pnt(firpoint_list, row)
fir_list.append(airspace_close())
features = []
airspace_count = 0
for item in fir_list:
airspace_count += 1
filename = 'firboundaries'
section = [12582, [
{'type': 'Feature', 'properties': {'id': airspace_count, 'ICAO': item['ICAO'], 'IsOceanic': item['IsOceanic'], 'IsExtension': item['IsExtension'], 'PointCount': item['PointCount'], 'MinLat': item['MinLat'], 'MinLon': item['MinLon'], 'MaxLat': item['MaxLat'], 'MaxLon': item['MaxLon'], 'CenterLat': item['CenterLat'], 'CenterLon': item['CenterLon']}, 'geometry': {'type': 'MultiPolygon', 'coordinates': [[item['Coordinates']]]}}], filename]
features.append(section)
geojson_writer(features, output_dir)