Reputation: 369
I have latitude, longitude and altitude data and want to make a plot such as in the image below using Python. The map can be left out, it is unnecessary.
I have tried using the mplot3d polygon plot tutorial but can't figure out how to have varying x and y values such that it is not a straight line. Any ideas?
Upvotes: 3
Views: 6807
Reputation: 369
It seems that the easiest way to do this is to use Google Earth.
Now there are two ways to tackle this. The first method is simple if you want a static picture. The second method allows animation of the flight path. Both are explained in this answer.
Using the following Python code I have made a KML
file which allows me to create a static visualization.
f = open('flight.kml', 'w')
#Writing the kml file.
f.write("<?xml version='1.0' encoding='UTF-8'?>\n")
f.write("<kml xmlns='http://earth.google.com/kml/2.2'>\n")
f.write("<Document>\n")
f.write("<Placemark>\n")
f.write(" <name>flight</name>\n")
f.write(" <LineString>\n")
f.write(" <extrude>1</extrude>\n")
f.write(" <altitudeMode>absolute</altitudeMode>\n")
f.write(" <coordinates>\n")
for i in range(0,len(data['altitude']),10): #Here I skip some data
f.write(" "+str(data['LON_GPS'][i]) + ","+ str(data['LAT_GPS'][i]) + "," + str(data['altitude'][i]) +"\n")
f.write(" </coordinates>\n")
f.write(" </LineString>\n")
f.write("</Placemark>\n")
f.write("</Document>")
f.write("</kml>\n")
f.close()
The code results in a KML
file which in general looks like this:
<?xml version='1.0' encoding='UTF-8'?>
<kml xmlns='http://earth.google.com/kml/2.2'>
<Document>
<Placemark>
<name>flight</name>
<LineString>
<extrude>1</extrude>
<altitudeMode>absolute</altitudeMode>
<coordinates>
54.321976,-4.90948,39232.0
54.320946,-4.90621,39232.0
...
...
52.329865,4.71601,0
52.329693,4.71619,0
</coordinates>
</LineString>
</Placemark>
</Document></kml>
Once the *.kml
file is made using the code above, in Google Earth one can simply import it using File, Import... Google Earth then automatically displays the image you see below.
I also finally figured out how to animate the flight. The solution I found is to split the single <Placemark>
from the static answer up into several Placemarks. Adding <TimeSpan>
info into each placemark then allows an animation to take place. In order to get the same visual I had to have beginning coordinates and end coordinates for each Placemark in order to create a proper <LineString>
. The result can be found in this video. The video is created using the Record a Tour
button in Google Earth
f = open(fname+'.kml', 'w')
#Writing the kml file.
f.write("<?xml version='1.0' encoding='UTF-8'?>\n")
f.write("<kml xmlns='http://earth.google.com/kml/2.2'>\n")
f.write("<Document>\n")
f.write(" <name>flight</name>\n")
for i in range(1,len(data['altitude'])):
f.write("<Placemark>\n")
f.write(" <TimeSpan>\n <begin>" + '2015-12-02T%02i:%02i:%02iZ' % (data['UTC_HOUR'][i], data['UTC_MIN'][i], data['UTC_SEC'][i]) + "</begin>\n </TimeSpan>\n")
f.write(" <LineString>\n")
f.write(" <extrude>1</extrude>\n")
f.write(" <altitudeMode>absolute</altitudeMode>\n")
f.write(" <coordinates>" +str(data['LON_GPS'][i-1]) + ","+ str(data['LAT_GPS'][i-1]) + "," + str(data['altitude'][i-1]) + " " +str(data['LON_GPS'][i]) + ","+ str(data['LAT_GPS'][i]) + "," + str(data['altitude'][i]) +"</coordinates>\n")
f.write(" </LineString>\n")
f.write("</Placemark>\n")
f.write("</Document>")
f.write("</kml>\n")
f.close()
The resulting KML
file looks like this:
<?xml version='1.0' encoding='UTF-8'?>
<kml xmlns='http://earth.google.com/kml/2.2'>
<Document>
<name>A332_Conventional</name>
<Placemark>
<TimeSpan>
<begin>2015-12-02T08:45:13Z</begin>
</TimeSpan>
<LineString>
<extrude>1</extrude>
<altitudeMode>absolute</altitudeMode>
<coordinates>-0.85058,53.338535,39200 -0.81538,53.332012,39200</coordinates>
</LineString>
</Placemark>
...
...
<Placemark>
<TimeSpan>
<begin>2015-12-02T09:27:03Z</begin>
</TimeSpan>
<LineString>
<extrude>1</extrude>
<altitudeMode>absolute</altitudeMode>
<coordinates>4.71361,52.331066,0 4.71498,52.330379,0</coordinates>
</LineString>
</Placemark>
</Document></kml>
Upvotes: 8