1

I try to extract coordinates from gpx file and it returns empty list. I used xml.etree and have any idea why it is empty, because d.attrib return proper values.

import xml.etree.ElementTree as ET
f = ('St_Louis_Zoo_sample.gpx')
p = ET.parse(f)
root = p.getroot()
lats=[]
lons=[]

for d in root:
    if d.tag == 'wpt':       
        y = d.attrib['lat']
        x = d.attrib['lon']
        lats.append(y)
        lons.append(x)

Here it is a gpx file

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<gpx xmlns="http://www.topografix.com/GPX/1/1" creator="Geovative Solutions GeoTours" version="1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd">

<metadata>
  <name>St Louis Zoo sample</name>
  <desc>This self guided,GPS enabled tour of the world famous St. Louis Zoo, has 85 points of interest. Narratives in english,explaining each exhibit and provides guidance to zoo facilities.This audio tour guide can enhance your next visit.</desc>
  <author>
    <name>wizardone, using GeoTours</name>
    <link href="http://www.geovative.com/view?t=GEIF">
      <text>St Louis Zoo sample</text>
    </link>
  </author>
  <link href="http://www.geovative.com/view?t=GEIF">
    <text>St Louis Zoo sample</text>
  </link>
  <time>2008-02-26T19:49:13</time>
  <keywords>Audio tour guide
St.Louis Mo.
Zoo
Forest Park
Animals</keywords>
</metadata>


<wpt lat="38.63473" lon="-90.29408">
<name>Asian Elephant</name>
<desc>elephant</desc>
<link href="Data/Location3152-1"/>
<sym>Waypoint</sym>
<extensions>
        <gpxx:WaypointExtension xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensions/v3/GpxExtensionsv3.xsd">
            <gpxx:Proximity>15.24</gpxx:Proximity>
            <gpxx:DisplayMode>SymbolAndName</gpxx:DisplayMode>
        </gpxx:WaypointExtension>
</extensions>
</wpt>

<wpt lat="38.63368" lon="-90.28679">
<name>Bactrian Camel</name>
<desc>camel</desc>
<link href="Data/Location3152-2"/>
<sym>Waypoint</sym>
<extensions>
        <gpxx:WaypointExtension xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensions/v3/GpxExtensionsv3.xsd">
            <gpxx:Proximity>15.24</gpxx:Proximity>
            <gpxx:DisplayMode>SymbolAndName</gpxx:DisplayMode>
        </gpxx:WaypointExtension>
</extensions>
</wpt>

<wpt lat="38.63408" lon="-90.29323">
<name>Black Rhinoceros</name>
<desc>black rhino</desc>
<link href="Data/Location3152-3"/>
<sym>Waypoint</sym>
<extensions>
        <gpxx:WaypointExtension xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensions/v3/GpxExtensionsv3.xsd">
            <gpxx:Proximity>15.24</gpxx:Proximity>
            <gpxx:DisplayMode>SymbolAndName</gpxx:DisplayMode>
        </gpxx:WaypointExtension>
</extensions>
</wpt>

<wpt lat="38.63533" lon="-90.29019">
<name>Cafe Restroom</name>
<desc>cafe restroom</desc>
<link href="Data/Location3152-4"/>
<sym>Waypoint</sym>
<extensions>
        <gpxx:WaypointExtension xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensions/v3/GpxExtensionsv3.xsd">
            <gpxx:Proximity>15.24</gpxx:Proximity>
            <gpxx:DisplayMode>SymbolAndName</gpxx:DisplayMode>
        </gpxx:WaypointExtension>
</extensions>
</wpt>

<wpt lat="38.63677" lon="-90.28976">
<name>Polar Bear</name>
<desc>polar bear</desc>
<link href="Data/Location3152-5"/>
<sym>Waypoint</sym>
<extensions>
        <gpxx:WaypointExtension xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensions/v3/GpxExtensionsv3.xsd">
            <gpxx:Proximity>15.24</gpxx:Proximity>
            <gpxx:DisplayMode>SymbolAndName</gpxx:DisplayMode>
        </gpxx:WaypointExtension>
</extensions>
</wpt>

<wpt lat="38.63496" lon="-90.28948">
<name>California Sea Lion</name>
<desc>sea lion</desc>
<link href="Data/Location3152-6"/>
<sym>Waypoint</sym>
<extensions>
        <gpxx:WaypointExtension xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensions/v3/GpxExtensionsv3.xsd">
            <gpxx:Proximity>15.24</gpxx:Proximity>
            <gpxx:DisplayMode>SymbolAndName</gpxx:DisplayMode>
        </gpxx:WaypointExtension>
</extensions>
</wpt>

<wpt lat="38.63421" lon="-90.29458">
<name>Cheetah</name>
<desc>cheetah</desc>
<link href="Data/Location3152-7"/>
<sym>Waypoint</sym>
<extensions>
        <gpxx:WaypointExtension xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensions/v3/GpxExtensionsv3.xsd">
            <gpxx:Proximity>15.24</gpxx:Proximity>
            <gpxx:DisplayMode>SymbolAndName</gpxx:DisplayMode>
        </gpxx:WaypointExtension>
</extensions>
</wpt>

<wpt lat="38.63633" lon="-90.29083">
<name>Grizzley Bear</name>
<desc>grizzly bear</desc>
<link href="Data/Location3152-8"/>
<sym>Waypoint</sym>
<extensions>
        <gpxx:WaypointExtension xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensions/v3/GpxExtensionsv3.xsd">
            <gpxx:Proximity>15.24</gpxx:Proximity>
            <gpxx:DisplayMode>SymbolAndName</gpxx:DisplayMode>
        </gpxx:WaypointExtension>
</extensions>
</wpt>

<wpt lat="38.63395" lon="-90.28715">
<name>Jaguar</name>
<desc>jaguar</desc>
<link href="Data/Location3152-9"/>
<sym>Waypoint</sym>
<extensions>
        <gpxx:WaypointExtension xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensions/v3/GpxExtensionsv3.xsd">
            <gpxx:Proximity>15.24</gpxx:Proximity>
            <gpxx:DisplayMode>SymbolAndName</gpxx:DisplayMode>
        </gpxx:WaypointExtension>
</extensions>
</wpt>

<wpt lat="38.63347" lon="-90.28769">
<name>Lion</name>
<desc>African lions live in a number of different habitats: grassy plains, open woodlands, semi-desert areas, even high mountains.</desc>
<link href="Data/Location3152-10"/>
<sym>Waypoint</sym>
<extensions>
        <gpxx:WaypointExtension xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensions/v3/GpxExtensionsv3.xsd">
            <gpxx:Proximity>15.24</gpxx:Proximity>
            <gpxx:DisplayMode>SymbolAndName</gpxx:DisplayMode>
        </gpxx:WaypointExtension>
</extensions>
</wpt>
</gpx>
4

GPX files use XML namespaces (look at Parsing XML with namespace in Python via 'ElementTree' or Read GPX using Python ElementTree.register_namespace? for example).

The namespace is {http://www.topografix.com/GPX/1/1}

import xml.etree.ElementTree as ET
tree = ET.parse("St_Louis_Zoo_sample.gpx")
for elem in tree.findall("{http://www.topografix.com/GPX/1/1}wpt"):
    print elem, elem.attrib['lon'], elem.attrib['lat']

<Element {http://www.topografix.com/GPX/1/1}wpt at 0x104076ea8> -90.29408 38.63473
<Element {http://www.topografix.com/GPX/1/1}wpt at 0x104076bd8> -90.28679 38.63368
<Element {http://www.topografix.com/GPX/1/1}wpt at 0x104076d88> -90.29323 38.63408
<Element {http://www.topografix.com/GPX/1/1}wpt at 0x104076758> -90.29019 38.63533
<Element {http://www.topografix.com/GPX/1/1}wpt at 0x1040766c8> -90.28976 38.63677
<Element {http://www.topografix.com/GPX/1/1}wpt at 0x1040767e8> -90.28948 38.63496
<Element {http://www.topografix.com/GPX/1/1}wpt at 0x104076680> -90.29458 38.63421
<Element {http://www.topografix.com/GPX/1/1}wpt at 0x104076830> -90.29083 38.63633
<Element {http://www.topografix.com/GPX/1/1}wpt at 0x1040765f0> -90.28715 38.63395
<Element {http://www.topografix.com/GPX/1/1}wpt at 0x103a9f2d8> -90.28769 38.63347

With ElementTree (xml.etree), you can define the namespace before

 namespace = {"gpx": "http://www.topografix.com/GPX/1/1"} 
 for elem in tree.findall('gpx:wpt', namespace):
        print elem.attrib['lon'],  elem.attrib['lat']
 -90.29408 38.63473
 -90.28679 38.63368
 -90.29323 38.63408
 -90.29019 38.63533
 -90.28976 38.63677
 -90.28948 38.63496
 -90.29458 38.63421
 -90.29083 38.63633
 -90.28715 38.63395
 -90.28769 38.63347

As with lxml

from lxml import etree
NSMAP = {"gpx": "http://www.topografix.com/GPX/1/1"}
tree = etree.parse("St_Louis_Zoo_sample.gpx")
for elem in tree.findall("gpx:wpt", namespaces=NSMAP):
     print elem.attrib['lon'], elem.attrib['lat']
-90.29408 38.63473
-90.28679 38.63368
-90.29323 38.63408
-90.29019 38.63533
....
| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.