/usr/share/tcltk/tcllib1.14/gpx/gpx.tcl is in tcllib 1.14-dfsg-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 | ##+##########################################################################
#
# gpx.tcl -- Parse gpx files
# by Keith Vetter, July 7, 2010
#
# gpx definition:
# http://www.topografix.com/gpx.asp
# http://www.topografix.com/GPX/1/1/
# GPX 1.0 => http://www.topografix.com/gpx_manual.asp
#
# code reference:
# http://wiki.tcl.tk/26635
# API
# set token [::gpx::Create gpxFilename]
# ::gpx::Cleanup $token
# ::gpx::GetGPXMetadata $token => dict of metadata
# ::gpx::GetWaypointCount $token => number of waypoints
# ::gpx::GetAllWaypoints $token => list of waypoint items
# ::gpx::GetTrackCount $token => number of tracks
# ::gpx::GetTrackMetadata $token $whichTrack => dict of metadata for this track
# ::gpx::GetTrackPoints $token $whichTrack => list of trkpts for this track
# ::gpx::GetRouteCount $token => number of routes
# ::gpx::GetRouteMetadata $token $whichRoute => dict of metadata for this route
# ::gpx::GetRoutePoints $token $whichRoute => list of rtepts for this route
#
# o metadata is a dictionary whose keys depends on the which optional elements
# are present and whose structure depends on the element's schema
#
# o a waypoint/trackpoint is a 3 element list consisting of latitude,
# longitude and a dictionary of metadata:
# e.g. 41.61716028 -70.61758477 {ele 35.706 time 2010-06-17T16:02:28Z}
#
package require Tcl 8.5
package require tdom
namespace eval gpx {
variable nameSpaces {
gpx "http://www.topografix.com/GPX/1/1"
xsi "http://www.w3.org/2001/XMLSchema-instance"
}
# gpx 1.0 was obsoleted August 9, 2004, but we handle it anyway
variable nameSpaces10 {
gpx "http://www.topografix.com/GPX/1/0"
topografix "http://www.topografix.com/GPX/Private/TopoGrafix/0/2"
}
variable gpx
set gpx(id) 0
# Cleanup any existing doms if we reload this module
::apply {{} {
foreach arr [array names ::gpx::gpx dom,*] {
catch {$::gpx::gpx($arr) delete}
unset ::gpx::gpx($arr)
}
}}
}
##+##########################################################################
#
# ::gpx::Create -- Creates a tdom object, returns opaque token to it
# parameters: gpxFilename
# returns: token for this tdom object
#
proc ::gpx::Create {gpxFilename {rawXML {}}} {
variable nameSpaces
variable gpx
if {$rawXML eq ""} {
set fin [open $gpxFilename r]
set rawXML [read $fin] ; list
close $fin
}
set token "gpx[incr gpx(id)]"
dom parse $rawXML gpx(dom,$token)
# Check version 1.0, 1.1 or fail
set version [[$gpx(dom,$token) documentElement] getAttribute version 0.0]
if {[package vcompare $version 1.1] >= 0} {
$gpx(dom,$token) selectNodesNamespaces $::gpx::nameSpaces
} elseif {[package vcompare $version 1.0] == 0} {
$gpx(dom,$token) selectNodesNamespaces $::gpx::nameSpaces10
} else {
$gpx(dom,$token) delete
error "$gpxFilename is version $version, need 1.0 or better"
}
set gpx(version,$token) $version
return $token
}
##+##########################################################################
#
# ::gpx::Cleanup -- Cleans up an instance of a tdom object
# parameter: token returned by ::gpx::Create
#
proc ::gpx::Cleanup {token} {
variable gpx
$gpx(dom,$token) delete
unset gpx(dom,$token)
}
##+##########################################################################
#
# ::gpx::GetGPXMetadata -- Return metadata dictionary for entire document
# parameter: token returned by ::gpx::Create
# returns: metadata dictionary for entire document
#
proc ::gpx::GetGPXMetadata {token} {
set gpxNode [$::gpx::gpx(dom,$token) documentElement]
set version $::gpx::gpx(version,$token)
set creator [$gpxNode getAttribute creator ?]
set attr [dict create version $version creator $creator]
if {[package vcompare $version 1.0] == 0} {
set result [::gpx::_ExtractNodeMetadata $token $gpxNode]
} else {
set meta [$::gpx::gpx(dom,$token) selectNodes /gpx:gpx/gpx:metadata]
set result [::gpx::_ExtractNodeMetadata $token $meta]
}
set result [dict merge $attr $result]
return $result
}
##+##########################################################################
#
# ::gpx::GetWaypointCount -- Return number of waypoints defined in gpx file
# parameter: token returned by ::gpx::Create
# returns: number of waypoints
#
proc ::gpx::GetWaypointCount {token} {
set wpts [$::gpx::gpx(dom,$token) selectNodes /gpx:gpx/gpx:wpt]
return [llength $wpts]
}
##+##########################################################################
#
# ::gpx::GetAllWaypoints -- Returns list of waypoints, each item consists
# of {lat lon <dictionary of metadata>}
# parameter: token returned by ::gpx::Create
# returns: list of waypoint items
#
proc ::gpx::GetAllWaypoints {token} {
set wpts [$::gpx::gpx(dom,$token) selectNodes /gpx:gpx/gpx:wpt]
set result {}
foreach wpt $wpts {
set lat [$wpt getAttribute "lat" ?]
set lon [$wpt getAttribute "lon" ?]
set meta [::gpx::_ExtractNodeMetadata $token $wpt]
lappend result [list $lat $lon $meta]
}
return $result
}
##+##########################################################################
#
# ::gpx::GetTrackCount -- returns how many tracks
# parameter: token returned by ::gpx::Create
# returns: number of tracks
#
proc ::gpx::GetTrackCount {token} {
set trks [$::gpx::gpx(dom,$token) selectNodes /gpx:gpx/gpx:trk]
return [llength $trks]
}
##+##########################################################################
#
# ::gpx::GetTrackMetadata -- Returns metadata dictionary for this track
# parameter: token returned by ::gpx::Create
# whichTrack: which track to get (1 based)
# returns: metadata dictionary for this track
#
proc ::gpx::GetTrackMetadata {token whichTrack} {
set trkNode [$::gpx::gpx(dom,$token) selectNodes \
/gpx:gpx/gpx:trk\[$whichTrack\]]
set meta [::gpx::_ExtractNodeMetadata $token $trkNode]
}
##+##########################################################################
#
# ::gpx::GetTrackPoints -- Returns track consisting of a list of track points,
# each of which consists of {lat lon <dictionary of metadata>}
# parameter: token returned by ::gpx::Create
# whichTrack: which track to get (1 based)
# returns: list of trackpoints for given track
#
proc ::gpx::GetTrackPoints {token whichTrack} {
set trkpts [$::gpx::gpx(dom,$token) selectNodes \
/gpx:gpx/gpx:trk\[$whichTrack\]//gpx:trkpt]
set result {}
foreach trkpt $trkpts {
set lat [$trkpt getAttribute "lat" ?]
set lon [$trkpt getAttribute "lon" ?]
set meta [::gpx::_ExtractNodeMetadata $token $trkpt]
lappend result [list $lat $lon $meta]
}
return $result
}
##+##########################################################################
#
# ::gpx::GetRouteCount -- returns how many routes
# parameter: token returned by ::gpx::Create
# returns: number of routes
#
proc ::gpx::GetRouteCount {token} {
set rtes [$::gpx::gpx(dom,$token) selectNodes /gpx:gpx/gpx:rte]
return [llength $rtes]
}
##+##########################################################################
#
# ::gpx::GetRouteMetadata -- Returns metadata dictionary for this route
# parameter: token returned by ::gpx::Create
# whichRoute: which route to get (1 based)
# returns: metadata dictionary for this route
#
proc ::gpx::GetRouteMetadata {token whichRoute} {
set rteNode [$::gpx::gpx(dom,$token) selectNodes \
/gpx:gpx/gpx:rte\[$whichRoute\]]
set meta [::gpx::_ExtractNodeMetadata $token $rteNode]
}
##+##########################################################################
#
# ::gpx::GetRoutePoints -- Returns route consisting of a list of route points,
# each of which consists of {lat lon <dictionary of metadata>}
# parameter: token returned by ::gpx::Create
# whichRoute: which route to get (1 based)
# returns: list of routepoints for given route
#
proc ::gpx::GetRoutePoints {token whichRoute} {
set rtepts [$::gpx::gpx(dom,$token) selectNodes \
/gpx:gpx/gpx:rte\[$whichRoute\]//gpx:rtept]
set result {}
foreach rtept $rtepts {
set lat [$rtept getAttribute "lat" ?]
set lon [$rtept getAttribute "lon" ?]
set meta [::gpx::_ExtractNodeMetadata $token $rtept]
lappend result [list $lat $lon $meta]
}
return $result
}
##+##########################################################################
#
# ::gpx::_ExtractNodeMetadata -- Internal routine to get all
# the optional data associated with an xml element. For most
# elements we just want element name and text value but some
# we want their attributes and some we want children metadata.
#
proc ::gpx::_ExtractNodeMetadata {token node} {
set result {}
if {$node eq ""} { return $result }
# author and email elements are different in version 1.0 and 1.1
set onlyAttributes [list "bounds" "email"]
set attributesAndElements [list "extension" "author" "link" "copyright"]
if {$::gpx::gpx(version,$token) == 1.0} {
set onlyAttributes [list "bounds"]
set attributesAndElements [list "extension" "link" "copyright"]
}
foreach child [$node childNodes] {
set nodeName [$child nodeName]
if {$nodeName in {"wpt" "trk" "trkseg" "trkpt" "rte" "rtept"}} continue
if {[string match "topografix:*" $nodeName]} continue
if {$nodeName in $onlyAttributes} {
set attr [::gpx::_GetAllAttributes $child]
lappend result $nodeName $attr
} elseif {$nodeName in $attributesAndElements} {
set attr [::gpx::_GetAllAttributes $child]
set meta [::gpx::_ExtractNodeMetadata $token $child]
set meta [concat $attr $meta]
lappend result $nodeName $meta
} else {
lappend result $nodeName [$child asText]
}
}
return $result
}
##+##########################################################################
#
# ::gpx::_GetAllAttributes -- Returns dictionary of attribute name and value
#
proc ::gpx::_GetAllAttributes {node} {
set result {}
foreach attr [$node attributes] {
lappend result $attr [$node getAttribute $attr]
}
return $result
}
################################################################
package provide gpx 1
return
|