gpx文件解析 Demo

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_24452475/article/details/81024836

GPX (the GPS Exchange Format)

  • WHAT IS GPX?
    • GPX (the GPS Exchange Format) is a light-weight XML data format for the interchange of GPS data (waypoints, routes, and tracks) between applications and Web services on the Internet.
    • 即,一种用于存储坐标数据的轻量级 XML 文件格式,它可以储存在一条路上的路点,轨迹,路线,且易于处理和转换到其他格式
    • GPX (the GPS Exchange Format)
    • 路点、轨迹、路线3者之间的关系如下图所示:
      • 路点、路线、轨迹

Demo

<gpx version="1.0" creator="..." xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.topografix.com/GPX/1/0" xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd">
    <name>kngines's demo</name>
    <desc>gpx parser example</desc>
    <author>kngines</author>
    <wpt lat="12.3" lon="45.6">
        <ele>75.1</ele>
        <magvar>1.1</magvar>
        <name>example one</name>
    </wpt>
    <wpt lat="13.4" lon="46.7">
        <ele>35.1</ele>
        <magvar>1.1</magvar>
        <name>example two</name>
    </wpt>
    <rte>
        <rtept lat="45.2787641494" lon="13.726695478">
        <name>#001</name></rtept>
        <rtept lat="45.2785961743" lon="13.7286695838">
        <name>#002</name></rtept>
        <rtept lat="45.2786546825" lon="13.7290772796">
        <name>#003</name></rtept>
    </rte>
    <trk>
        <trkseg>
            <trkpt lat="10.1" lon="-20.2">
                <ele>11.1</ele>
                <time>2013-01-01T12:00:04</time>
                <geoidheight>3</geoidheight>
            </trkpt>
        </trkseg>
        <trkseg>
            <trkpt lat="12.1" lon="-22.2">
                <ele>12.2</ele>
                <time>2013-01-01T12:00:04</time>
                <geoidheight>13</geoidheight>
            </trkpt>
        </trkseg>
    </trk>
</gpx>
  • 解析代码

# coding: utf-8

# ### 1 读文件并完成解析
import gpxpy.parser as parser

gpx_file = open( './gpx1.3.2_demo.gpx', 'r' )  # 读取文件
gpx_parser = parser.GPXParser( gpx_file )
gpx = gpx_parser.parse()  # 文件解析
gpx_file.close()

# ### 2 读文件并完成解析
print ('======================')
print (gpx.name)
print (gpx.description)
print (gpx.author_name)
print ('======================')

# ### 3 打印解析的轨迹数据
for track in gpx.tracks:
    for segment in track.segments:
        for point in segment.points:
            print 'Point at ({0},{1}) -> {2},{3}'.format( point.latitude, point.longitude, point.elevation, point.geoid_height)


# ### 4 打印解析的路线
for waypoint in gpx.waypoints:
    print 'waypoint {0} -> ({1},{2},{3})'.format( waypoint.name, waypoint.latitude, waypoint.longitude, waypoint.elevation)


# ### 5 打印解析的路上各个点数据
for route in gpx.routes:
    for point in route.points:
        print 'Point at ({0},{1}) -> {2}'.format( point.latitude, point.longitude, point.name )


# ### 6 其他方法
print 'GPX:', gpx.to_xml()
  • 效果图
    gpx_pic

参考/引用

阅读更多
换一批

没有更多推荐了,返回首页