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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值