forked from marcin-osowski/igc_lib
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathigc2geojson.py
243 lines (173 loc) · 8.01 KB
/
igc2geojson.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#!/usr/bin/env python
from __future__ import print_function
import sys
import igc_lib
import lib.dumpers as dumpers
import geojson as gjson
import zipfile
import numpy as np
import io
import pathlib
from io import BytesIO, StringIO
from datetime import datetime
from datetime import datetime, date, time, timedelta
from google.cloud import storage # Imports the Google Cloud client library
from HashHelper import HashHelper
google_storage_bucket_id = "bucket_heatmap" # Google storage bucket name
def print_flight_details(flight):
print("Flight:", flight)
print("Takeoff:", flight.takeoff_fix)
thermals = flight.thermals
for i in range(len(thermals)):
thermal = thermals[i]
print(" thermal[%d]:" % i, thermals[i])
print("Landing:", flight.landing_fix)
def dump_flight(flight, input_file):
input_base_file = os.path.splitext(input_file)[0]
wpt_file = "%s-thermals.wpt" % input_base_file
cup_file = "%s-thermals.cup" % input_base_file
thermals_csv_file = "%s-thermals.csv" % input_base_file
flight_csv_file = "%s-flight.csv" % input_base_file
kml_file = "%s-flight.kml" % input_base_file
print("Dumping thermals to %s, %s and %s" %
(wpt_file, cup_file, thermals_csv_file))
dumpers.dump_thermals_to_wpt_file(flight, wpt_file, True)
dumpers.dump_thermals_to_cup_file(flight, cup_file)
print("Dumping flight to %s and %s" % (kml_file, flight_csv_file))
dumpers.dump_flight_to_csv(flight, flight_csv_file, thermals_csv_file)
dumpers.dump_flight_to_kml(flight, kml_file)
def get_geojson_feature_collection(list_thermals):
features = []
for thermal in list_thermals:
lat = thermal.enter_fix.lat
lon = thermal.enter_fix.lon
vario = round(thermal.vertical_velocity(), 2)
altitude_enter = int(thermal.enter_fix.alt)
ts = thermal.enter_fix.timestamp
json_point = gjson.Point((lon, lat, altitude_enter))
features.append(gjson.Feature(geometry=json_point, properties={"vario": vario,
"alt_in": altitude_enter,
"ts": ts}))
feature_collection = gjson.FeatureCollection(features)
return feature_collection
def get_geojson_track_collection_full(list_flights, isIncludeProperties=True):
tracks = []
for flight in list_flights:
for i in range(0, len(flight.fixes)-2):
lat1 = flight.fixes[i].lat
lon1 = flight.fixes[i].lon
lat2 = flight.fixes[i+1].lat
lon2 = flight.fixes[i+1].lon
alt = flight.fixes[i].alt # Altitude
ts = flight.fixes[i].timestamp # Timestamp
json_line = gjson.LineString([(lon1, lat1), (lon2, lat2)])
if isIncludeProperties:
tracks.append(gjson.Feature(geometry=json_line, properties={"alt": alt,
"ts": ts}))
else:
tracks.append(gjson.Feature(geometry=json_line))
feature_collection = gjson.FeatureCollection(tracks)
return feature_collection
# -------- Features ------------------------------------------------------
def get_geojson_feature_track_collection_simple(flight, flight_id=None):
coordinates = []
for i in range(0, len(flight.fixes)-2):
lat1 = flight.fixes[i].lat
lon1 = flight.fixes[i].lon
lat2 = flight.fixes[i+1].lat
lon2 = flight.fixes[i+1].lon
coordinates.append([lon1, lat1])
coordinates.append([lon2, lat2])
json_line = gjson.LineString(coordinates)
# Self computed flightId
if flight_id is None:
computed_flight_id_string = f"{flight.date_timestamp}_{flight.duration}_{flight.pilot_name}_{flight.glider_type}_{flight.glider_id}_{flight.fr_recorder_type}"
flight_id = HashHelper.ComputeHashForList(computed_flight_id_string)
# Create feature
feature = gjson.Feature(geometry=json_line, properties={"flightId": flight_id,
"takeoff": flight.takeoff_location
})
del coordinates
return feature
def getFeaturesAsFeatureCollection(listFeatures):
featureCollection = gjson.FeatureCollection(listFeatures)
return featureCollection
def getJsonFromFeatures(listFeatures):
featureCollection = getFeaturesAsFeatureCollection(listFeatures)
json = gjson.dumps(featureCollection)
return json
# --------------------- Dump to ... -----------------------------------
# --- Features ---
def dumpFeaturesToFile(output_filename, listFeatures):
feature_collection = getFeaturesAsFeatureCollection(listFeatures)
dump_feature_collection_to_file(output_filename, feature_collection)
def dump_to_geojson(output_filename, list_thermals):
# Dump thermals
feature_collection = get_geojson_feature_collection(list_thermals)
# Write output: thermals
with open('{}.geojson'.format(output_filename), 'w') as f:
gjson.dump(feature_collection, f)
def dump_feature_collection_to_file(output_filename, featureCollection):
# Write output: Tracks
with open('{}.geojson'.format(output_filename), 'w') as f:
gjson.dump(featureCollection, f)
script_time = datetime.now()
print("dump_tracks_to_file: end={}".format(script_time))
def dump_tracks_to_file(output_filename, list_flights):
script_time = datetime.now()
print("dump_tracks_to_file: start={}".format(script_time))
# Dump thermals
feature_collection = get_geojson_track_collection_full(list_flights)
# Write output: Tracks
with open('{}.geojson'.format(output_filename), 'w') as f:
gjson.dump(feature_collection, f)
script_time = datetime.now()
print("dump_tracks_to_file: end={}".format(script_time))
def dump_track_to_feature_collection(flight):
list_flights = [flight]
# Dump thermals
feature_collection = get_geojson_track_collection_full(list_flights)
return feature_collection
def dump_to_google_storage(output_filename, list_thermals):
# Dump thermals
feature_collection = get_geojson_feature_collection(list_thermals)
output_filename = '{}.geojson'.format(output_filename)
# Instantiates a client
storage_client = storage.Client()
bucket = storage_client.get_bucket(google_storage_bucket_id)
if bucket.exists():
blob = bucket.blob(output_filename)
blob.upload_from_string(str(feature_collection))
def dump_to_ftp(ftp_client, output_directory, output_filename, list_thermals):
# Dump thermals
feature_collection = get_geojson_feature_collection(list_thermals)
geojson_file_content = str(feature_collection)
content_as_bytes = BytesIO(bytes(geojson_file_content, encoding='utf-8'))
#output_filename = '{}.geojson'.format(output_filename)
# cd to directory
if output_directory:
ftp_client.cwd(output_directory)
# Dump to FTP
return_code = ftp_client.storbinary(
'STOR ' + output_filename, content_as_bytes)
return return_code
def dump_thermals_to_file(output_filename, list_thermals):
# Dump thermals
feature_collection = get_geojson_feature_collection(list_thermals)
geojson_file_content = str(feature_collection)
#content_as_bytes = BytesIO(bytes(geojson_file_content,encoding='utf-8'))
output_filename = '{}.geojson'.format(output_filename)
# Write output: Tracks
with open('{}.geojson'.format(output_filename), 'w') as f:
gjson.dump(feature_collection, f)
script_time = datetime.now()
print("dump_tracks_to_file: end={}".format(script_time))
def dump_string_to_ftp(ftp_client, output_directory, output_filename, string_content):
content_as_bytes = BytesIO(bytes(string_content, encoding='utf-8'))
# cd to directory
if output_directory:
ftp_client.cwd(output_directory)
# Dump to FTP
return_code = ftp_client.storbinary(
'STOR ' + output_filename, content_as_bytes)
return return_code