如何在Python中进行飞行路径投影?

时间:2016-07-21 14:31:51

标签: python animation 3d kml altitude

我有纬度,经度和海拔数据,想要使用Python制作如下图所示的情节。可以省略地图,这是不必要的。 enter image description here

我尝试过使用mplot3d polygon plot tutorial,但无法弄清楚如何使用不同的x和y值,使其不是直线。有什么想法吗?

1 个答案:

答案 0 :(得分:2)

似乎最简单的方法是使用Google地球。

现在有两种方法可以解决这个问题。如果你想要静态图片,第一种方法很简单。第二种方法允许飞行路径的动画。这两个都在这个答案中解释。

方法1

使用以下Python代码,我创建了一个KML文件,允许我创建静态可视化。

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()

代码生成KML文件,通常如下所示:

<?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>

使用上面的代码创建*.kml文件后,在Google地球中只需使用文件,导入... Google地球导入即可自动显示下面显示的图像

Result

方法2

我终于想出了如何为这次飞行制作动画。我找到的解决方案是将单个<Placemark>从静态答案拆分为几个地标。在每个地标中添加<TimeSpan>信息然后允许动画发生。为了获得相同的视觉效果,我必须为每个地标创建起始坐标和结束坐标,以便创建正确的<LineString>。结果可以找到in this video。该视频是使用Google地球中的Record a Tour按钮

创建的
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()

生成的KML文件如下所示:

<?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>