extract_kmldata_outof_gpxfile.py
|
# ------------------------------------------------------------------------------#
import xml.etree.ElementTree as ET;
from typing import Dict,List,Tuple;
# ------------------------------------------------------------------------------#
TypeCoords=Tuple[str,str,str];
def getCoords(elem: ET.Element,ns: Dict) -> TypeCoords:
lon=elem.get('lon');
lat=elem.get('lat');
ele=elem.find('GPX:ele',ns).text;
return ((lon,lat,ele));
# end def getCoords
# ------------------------------------------------------------------------------#
TypeWptData=Dict[str,TypeCoords];
TypePtsData=Tuple[str,List[TypeCoords]];
def extractKmlData(inputFilePath: str) -> Tuple[TypeWptData,TypePtsData]:
#
with open(inputFilePath,mode='r',encoding='utf-8') as fReader:
etree=ET.parse(fReader);
root=etree.getroot();
namesSpace=root.tag[1:root.tag.index('}')];
# Should be: "http://www.topografix.com/GPX/1/1"
ns={'GPX':namesSpace};
# --------------------------------------------------------------------------#
# Waypoints
#
wptData={wpt.find('GPX:name',ns).text:getCoords(wpt,ns)
for wpt in root.findall('GPX:wpt',ns)};
# --------------------------------------------------------------------------#
# Points of track path
#
trk=root.find('GPX:trk',ns);
# Track name
trkName=trk.find('GPX:name',ns).text;
# Coordinates of track points
trkseg=trk.find('GPX:trkseg',ns);
ptsData=[getCoords(trkpt,ns) for trkpt in trkseg.findall('GPX:trkpt',ns)];
trkData=(trkName,ptsData);
# --------------------------------------------------------------------------#
return (wptData,trkData);
# end def extractKmlData
# ------------------------------------------------------------------------------#
# ------------------------------------------------------------------------------#
if __name__=='__main__':
print('\n# Beginning TEST extract_kmldata_outof_gpxfile.py ...\n');
# --------------------------------------------------------------------------#
inputFilePath="../gpxfiles/Mal_36_Tossals Verds_4122_17.gpx"
(wptData,trkData)=extractKmlData(inputFilePath);
# --------------------------------------------------------------------------#
print('\n# Finished TEST extract_kmldata_outof_gpxfile.py.\n');
# end if main
# ------------------------------------------------------------------------------#
# ------------------------------------------------------------------------------#