Monday 29 October 2018

How to properly get coordinates from gpx file in python



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






St Louis Zoo sample

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.

wizardone, using GeoTours

St Louis Zoo sample



St Louis Zoo sample



Audio tour guide
St.Louis Mo.
Zoo
Forest Park
Animals






Asian Elephant
elephant

Waypoint


15.24
SymbolAndName






Bactrian Camel
camel

Waypoint


15.24

SymbolAndName





Black Rhinoceros
black rhino

Waypoint



15.24
SymbolAndName





Cafe Restroom

cafe restroom

Waypoint


15.24
SymbolAndName






Polar Bear
polar bear

Waypoint


15.24
SymbolAndName






California Sea Lion
sea lion

Waypoint



15.24
SymbolAndName





Cheetah
cheetah


Waypoint


15.24
SymbolAndName






Grizzley Bear
grizzly bear

Waypoint


15.24
SymbolAndName






Jaguar
jaguar

Waypoint



15.24
SymbolAndName





Lion
African lions live in a number of different habitats: grassy plains, open woodlands, semi-desert areas, even high mountains.


Waypoint


15.24
SymbolAndName





Answer




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']

-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

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

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...