kml相關

一簡介

  KML(Keyhole Markup Language,Keyhole 標記語言)最初是由Google 旗下的Keyhole 公司開發和維護的一種基於XML 的標記語言,利用XML 語法格式描述地理空間數據(如點、線、面、多邊形和模型等),適合網絡環境下的地理信息協作與共享。2008 年4月,KML的最新版本2.2 被OGC 宣佈爲開放地理信息編碼標準,並改由OGC 維護和發展。

二、基本用法:

(1) 畫點:

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
  <Placemark>
    <name>Simple placemark</name>
    <description>Attached to the ground. Intelligently places itself 
       at the height of the underlying terrain.</description>
    <Point>
      <coordinates>-122.0822035425683,37.42228990140251,0</coordinates>
    </Point>
  </Placemark>
</kml>

 (2) 畫多項式:

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
  <Document>
    <name>Paths</name>
    <description>Examples of paths. Note that the tessellate tag is by default
      set to 0. If you want to create tessellated lines, they must be authored
      (or edited) directly in KML.</description>
    <Style id="yellowLineGreenPoly">
      <LineStyle>
        <color>7f00ffff</color>
        <width>4</width>
      </LineStyle>
      <PolyStyle>
        <color>7f00ff00</color>
      </PolyStyle>
    </Style>
    <Placemark>
      <name>Absolute Extruded</name>
      <description>Transparent green wall with yellow outlines</description>
      <styleUrl>#yellowLineGreenPoly</styleUrl>
      <LineString>
        <extrude>1</extrude>
        <tessellate>1</tessellate>
        <altitudeMode>absolute</altitudeMode>
        <coordinates> -112.2550785337791,36.07954952145647,2357
          -112.2549277039738,36.08117083492122,2357
          -112.2552505069063,36.08260761307279,2357
          -112.2564540158376,36.08395660588506,2357
          -112.2580238976449,36.08511401044813,2357
          -112.2595218489022,36.08584355239394,2357
          -112.2608216347552,36.08612634548589,2357
          -112.262073428656,36.08626019085147,2357
          -112.2633204928495,36.08621519860091,2357
          -112.2644963846444,36.08627897945274,2357
          -112.2656969554589,36.08649599090644,2357 
        </coordinates>
      </LineString>
    </Placemark>
  </Document>
</kml>

xml 文件註釋 

單行註釋

<!--註釋內容-->

多行註釋

<![CDATA[
註釋內容(包含其他註釋符)
]]>

 三、繪製軌跡

軌跡文件 (格式: 緯度, 經度): /tmp/route.txt

24.1764,117.8250
24.1766,117.8252
24.1761,117.8255
24.1762,117.8257
24.1759,117.8259
24.1687,117.8271
24.1735,117.8368
24.1761,117.8387
24.1747,117.8416
24.1703,117.8451
24.1637,117.856
24.1600,117.8718
24.1650,117.8839
24.1675,117.8957
24.1692,117.9002
24.1694,117.9094
24.1770,117.9234
24.1842,117.9245
24.1921,117.9201
24.2074,117.9280
24.2114,117.9280
24.2157,117.9339
24.2178,117.9434
24.2353,117.9627
24.2322,117.9780
24.2395,117.9804
24.2411,117.9977
24.2370,118.0014
24.2357,118.0066
24.2318,118.0100

/tmp/test.py

#! /usr/bin/env python

def run():
  file_path = "/tmp/route.txt"
  route = []
  for line in open(file_path):
    latlon = line.split(',')
    lat = float(latlon[0])
    lon = float(latlon[1])
    route.append([lat, lon])

  KML = \
'''
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
  <Document>
    <name>%s</name>
    <description>%s</description>
    <Style id="yellowLineGreenPoly">
      <LineStyle>
        <color>7f00ffff</color>
        <width>4</width>
      </LineStyle>
      <PolyStyle>
        <color>7f00ff00</color>
      </PolyStyle>
    </Style>
    %s
  </Document>
</kml>
'''

  POINT = \
    '''
    <Placemark>
      <name>%s</name>
      <description>%s</description>
      <Point>
        <coordinates>%.4f,%.4f</coordinates>
      </Point>
    </Placemark>
    '''

  POLYLINE = \
    '''
    <Placemark>
      <name>%s</name>
      <description>%s</description>
      <styleUrl>#yellowLineGreenPoly</styleUrl>
      <LineString>
        <extrude>1</extrude>
        <tessellate>1</tessellate>
        <altitudeMode>absolute</altitudeMode>
        <coordinates>
%s
        </coordinates>
      </LineString>
    </Placemark>
    '''

  placemarks=[]

  begin_point = POINT % ('BEGIN', 'alan home', route[0][1], route[0][0])
  placemarks.append(begin_point)

  route_points = ['          %.4f, %.4f, %.4f' % (latlon[1], latlon[0], 0) for latlon in route]
  polyline = POLYLINE % ('ROUTE', 'route', '\n'.join(route_points))
  placemarks.append(polyline)

  end_point = POINT % ('END', 'ayun home', route[-1][1], route[-1][0])
  placemarks.append(end_point)

  kml = KML % ('route', '', '\n'.join(placemarks))

  kml_path = '/tmp/test.kml'
  with open(kml_path, 'w') as f:
    f.write(kml)

if __name__ == '__main__':
  run()

運行 $ python /tmp/test.py,生成的 /tmp/test.kml 文件內容如下:


<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
  <Document>
    <name>route</name>
    <description></description>
    <Style id="yellowLineGreenPoly">
      <LineStyle>
        <color>7f00ffff</color>
        <width>4</width>
      </LineStyle>
      <PolyStyle>
        <color>7f00ff00</color>
      </PolyStyle>
    </Style>
    
    <Placemark>
      <name>BEGIN</name>
      <description>alan home</description>
      <Point>
        <coordinates>117.8250,24.1764</coordinates>
      </Point>
    </Placemark>
    

    <Placemark>
      <name>ROUTE</name>
      <description>route</description>
      <styleUrl>#yellowLineGreenPoly</styleUrl>
      <LineString>
        <extrude>1</extrude>
        <tessellate>1</tessellate>
        <altitudeMode>absolute</altitudeMode>
        <coordinates>
          117.8250, 24.1764, 0.0000
          117.8252, 24.1766, 0.0000
          117.8255, 24.1761, 0.0000
          117.8257, 24.1762, 0.0000
          117.8259, 24.1759, 0.0000
          117.8271, 24.1687, 0.0000
          117.8368, 24.1735, 0.0000
          117.8387, 24.1761, 0.0000
          117.8416, 24.1747, 0.0000
          117.8451, 24.1703, 0.0000
          117.8560, 24.1637, 0.0000
          117.8718, 24.1600, 0.0000
          117.8839, 24.1650, 0.0000
          117.8957, 24.1675, 0.0000
          117.9002, 24.1692, 0.0000
          117.9094, 24.1694, 0.0000
          117.9234, 24.1770, 0.0000
          117.9245, 24.1842, 0.0000
          117.9201, 24.1921, 0.0000
          117.9280, 24.2074, 0.0000
          117.9280, 24.2114, 0.0000
          117.9339, 24.2157, 0.0000
          117.9434, 24.2178, 0.0000
          117.9627, 24.2353, 0.0000
          117.9780, 24.2322, 0.0000
          117.9804, 24.2395, 0.0000
          117.9977, 24.2411, 0.0000
          118.0014, 24.2370, 0.0000
          118.0066, 24.2357, 0.0000
          118.0100, 24.2318, 0.0000
        </coordinates>
      </LineString>
    </Placemark>
    

    <Placemark>
      <name>END</name>
      <description>ayun home</description>
      <Point>
        <coordinates>118.0100,24.2318</coordinates>
      </Point>
    </Placemark>
    
  </Document>
</kml>

打開谷歌地圖 Google My Maps,點擊“創建新地圖” -> “導入”,導入生成kml文件即可看到軌跡

 

參考:

https://developers.google.com/kml/documentation/kml_tut?csw=1

https://developers.google.com/kml/documentation/kml_21tutorial?csw=1

https://developers.google.com/kml/documentation/kmlreference?csw=1

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章