-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathplot_paths.py
More file actions
148 lines (124 loc) · 4.82 KB
/
plot_paths.py
File metadata and controls
148 lines (124 loc) · 4.82 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
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
#!/usr/bin/env python3
from arducopter_extract import arducopter_extract
from dji_extract import dji_extract
import shapefile
import os
import glob
import numpy as np
from multiprocessing import Pool
import argparse
def plot_ardupath(log, output_filename, overwrite = False):
'''Plots the specified ArduCopter log as an ESRI shapefile at the specified
output path.
param: log ArduCopter binary log file path
param: output_filename Output path
'''
if os.path.getsize(log) == 0:
return
if os.path.isfile("%s.shp" % (os.path.splitext(output_filename)[0])) and not overwrite:
return
logstruct = arducopter_extract.ArduLog(log)
try:
positions_unfiltered = logstruct.extract_6dof3()[:,(5, 6, 4)]
except KeyError:
badlogs = open('badlogs.txt', 'a')
badlogs.write('%s\n' % (log))
badlogs.close()
return
positions = []
for i in range(len(positions_unfiltered)):
if positions_unfiltered[i,0] != 0:
positions.append(positions_unfiltered[i,:])
if len(positions) == 0:
return
coords = [[position[1], position[0]] for position in positions]
alts = [position[2] for position in positions]
writer = shapefile.Writer(output_filename)
writer.field('log', 'C')
writer.line([coords])
writer.record(log)
writer.close()
proj = open("%s.prj" % (os.path.splitext(output_filename)[0]), 'w')
epsg1 = 'GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433]]'
proj.write(epsg1)
proj.close()
def plot_djipath(log, output_filename, overwrite = False):
'''Plots the specified DJI log as an ESRI shapefile at the specified
output path.
param: log DJI binary log file path
param: output_filename Output path
'''
if os.path.getsize(log) == 0:
return
if os.path.isfile("%s.shp" % (os.path.splitext(output_filename)[0])) and not overwrite:
return
logstruct = dji_extract.DJILog(log)
try:
positions_unfiltered = logstruct.extract_6dof()
except KeyError:
badlogs = open('badlogs.txt', 'a')
badlogs.write('%s\n' % (logfile))
badlogs.close()
return
positions = []
for i in range(len(positions_unfiltered)):
if positions_unfiltered[i,0] != 0:
positions.append(positions_unfiltered[i,(2,1,3)])
if len(positions) == 0:
return
coords = [list(position[0:2]) for position in positions]
alts = [position[2] for position in positions]
writer = shapefile.Writer(output_filename)
writer.field('log', 'C')
writer.line([coords])
writer.record(log)
writer.close()
proj = open("%s.prj" % (os.path.splitext(output_filename)[0]), 'w')
epsg1 = 'GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433]]'
proj.write(epsg1)
proj.close()
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Plots the flight paths of the given flight logs')
parser.add_argument('--input_dir', '-i', dest='data_dir', help='Directory containing all flight logs to process', default='/root/gdrive')
parser.add_argument('--output_dir', '-o', dest='output_dir', help='Directory to store all shapefiles in', default='./outputs')
args = parser.parse_args()
data_dir = args.data_dir
arducopterlogs = glob.glob(os.path.join(data_dir, "**", "*.BIN"), recursive=True) + glob.glob(os.path.join(data_dir, "**", "*.bin"), recursive=True)
djilogs = glob.glob(os.path.join(data_dir, "**", "*.csv"), recursive=True)
output_dir = args.output_dir
assert os.path.isdir(output_dir), "Output directory is not a directory!"
overwrite = True
output_filenames = set()
arduprocess_input = []
for log in arducopterlogs:
output_filename = os.path.join(output_dir, os.path.basename(log))
if output_filename not in output_filenames:
output_filenames.add(output_filename)
else:
# name collision
i = 1
for i in range(1, len(arducopterlogs)):
output_filename = os.path.join(output_dir, "%s-%d%s" % (os.path.splitext(os.path.basename(log))[0], i, os.path.splitext(os.path.basename(log))[1]))
if output_filename not in output_filenames:
output_filenames.add(output_filename)
break
arduprocess_input.append([log, output_filename, overwrite])
djiprocess_input = []
for log in djilogs:
output_filename = os.path.join(output_dir, os.path.basename(log))
if output_filename not in output_filenames:
output_filenames.add(output_filename)
else:
# name collision
i = 1
for i in range(1, len(arducopterlogs)):
output_filename = os.path.join(output_dir, "%s-%d%s" % (os.path.splitext(os.path.basename(log))[0], i, os.path.splitext(os.path.basename(log))[1]))
if output_filename not in output_filenames:
output_filenames.add(output_filename)
break
djiprocess_input.append([log, output_filename, overwrite])
p = Pool(7)
print("Processing Arducopter Logs")
p.starmap(plot_ardupath, arduprocess_input)
print("Processing DJI Logs")
p.starmap(plot_djipath, djiprocess_input)