Thank you Roger and Barry for your time, but I've unfortunately given up on
getting my GDAL set-up in order. Instead, I just wrote my own XML scraper
for this particular GPX brand of files (downloaded from Garmin's Orgeon
550). It's a long way before it reads files according to the GPX
Standard<http://www.topografix.com/GPX/1/1/>,
but it's a start. I'm sharing the code if anyone else finds it useful. It's
currently scraping out waypoints and tracks.

Cheers,
Roman

(ugh, ugly code folding - email me for an .R file)

#' Read and convert GPX files
#'
#' This function reads GPX files and outputs ways and tracks in a human
readable format.
#' Using external tools (Windows only, sorry), files can be directly
converted to .shp
#' format.
#'
#' For waypoints, all children tags should be exported (tested on ele, time,
name, cmt,
#' sym, desc and extensions). This is still a TODO for tracks.
#'
#' Conversion from gpx to shape files is aided by the gpx2shp (see section
See also).
#' gpx2shp.exe should be in your PATH
#' (My computer > Properties > Advanced system settings > Environmental
variables...).
#'
#' @param src A character vector of GPX filenames. Files must be located in
current
#' working directory, or, you must specify full paths.
#' @param cvt Logical. If TRUE, gpx is converted to .shp (see \code{gpx2shp
--help}).
#' @return A \\code{list} of waypoints and tracks.
#' @author Roman Luštrik \email{roman.lustrik@gmail.com}
#' @seealso http://gpx2shp.sourceforge.jp/
#roxygen()

readGPX <- function(src, cvt = FALSE) {
 # TODO: make this work for any gpx, not just those produced by garmin GPS
# the problem so far is with namespaces. i can't get namespaces to work with
# gpx files from "da net". they don't have the same default namespace
# ("http://www.topografix.com/GPX/1/1") and if i create one (i think this is
# the step that i do something wrong), parsing text via .//x:wpt/x: doesn't
work
# anymore.
# TODO: implement the possibility of using rgdal conversoin to shp instead
of gpx2shp
# TODO: a number of tags that need to be scraped await attention. see
topografix site (above)
 # Create XML tree
xtree <- xmlTreeParse(src, useInternalNodes = TRUE)
 # Check if there are tracks and waypoints
wpt <- FALSE
trk <- FALSE
if (length(xpathApply(doc = xtree, path = "//a:wpt",
namespaces = c(a = getDefaultNamespace(xtree)))) > 0) wpt <- TRUE
if (length(xpathApply(doc = xtree, path = "//a:trk",
namespaces = c(a = getDefaultNamespace(xtree)))) > 0) trk <- TRUE
 #####################
# Extract waypoints #
#####################
if (wpt) {
node.names <- unique(unlist(xpathSApply(doc = xtree,
path = "//*[local-name() = 'wpt']", fun = function(x) {
sapply(xmlChildren(x), xmlName)
})))
 # Construct a data.frame out of every node (node.names)
vals <- xmlToDataFrame(nodes = getNodeSet(xtree, "//x:gpx/x:wpt",
namespaces = c(x = getDefaultNamespace(xtree))))
 # Get coordinates of waypoints
coords <- xpathApply(doc = xtree, path = "//x:wpt", fun = function(gtc) {
matrix(as.numeric(xmlAttrs(gtc)), nrow = 1,
dimnames = list(NULL, c("lat", "long")))
}, namespaces = c(x = getDefaultNamespace(xtree)))
coords <- do.call("rbind", coords)
 wpt <- data.frame(coords, vals)
}
 ##################
# Extract tracks #
##################
if (trk) {
 # This function extracts <trk> tags. For each <trk>, name and segments are
extracted.
getTRK <- function(wg) {
 # Find <trk> nodes
trks <- getNodeSet(doc = wg, path = "//x:trk",
namespaces = c(x = getDefaultNamespace(wg)))
 out <- lapply(trks, function(x) {
 # Get names of tracks
trk.names.list <- names(xmlChildren(x))
 # If tracks were named, extract the name
if (any(trk.names.list == "name")) trk.name <- xmlValue(x[["name"]])
 # Find trksegs only
get.trkseg <- trk.names.list == "trkseg"
trk.segs <- x[get.trkseg]
 # extract track segments per track
trk.segs <- lapply(trk.segs, function(x) {
out <- do.call("rbind", lapply(xmlChildren(x), xmlAttrs))
row.names(out) <- NULL
 return(out)
})
 # find number of track segments before merging
seg.lengths <- unlist(lapply(trk.segs, nrow))
trk.num <- rep(seq_len(length(trk.segs)), times = seg.lengths)
trk.segs <- do.call("rbind", trk.segs)
trk.name <- rep(trk.name, nrow(trk.segs))
 x <- cbind(trk.segs, seg = trk.num, trk.name)
 return(x)
 })
 }
 trk <- getTRK(xtree)
 }
 get.out <- list()
get.out$wpt <- wpt
get.out$trk <- trk
 ###################################
# Conversion to shp using gpx2shp #
###################################
 if (cvt) system(paste("gpx2shp", src), show.output.on.console = FALSE)
 return(get.out)
 }




On Sun, Jul 24, 2011 at 8:49 AM, Roger Bivand <Roger.Bivand@nhh.no> wrote:

> On Sat, 23 Jul 2011, Roman Luštrik wrote:
>
>  I've installed a build of GDAL from http://www.gisinternals.com/**sdk/<http://www.gisinternals.com/sdk/>.
>> The
>> information for the build I'm using (GDAL 1.8.1, 64 bit) says that
>> expat-2.0.1-VS10 has been used to compile GDAL. Still no dice.
>>
>> FWIW, this is the output I get when I load rgdal.
>>
>>  library(rgdal)
>>>
>> Loading required package: sp
>> Geospatial Data Abstraction Library extensions to R successfully loaded
>> Loaded GDAL runtime: GDAL 1.7.2, released 2010/04/23
>>
>
> You have multiple installed GDAL libraries. The one found when installin
> rgdal is the one you see with gdal-config --version without any extra steps.
> Disambiguate your GDAL installations, or, better, install expat (perhaps
> binary) then GDAL from source. Many binary builds on Linux are badly made
> and confusing.
>
> Roger
>
>
>  Path to GDAL shared files:
>> C:/Users/romunov/Documents/R/**win-library/2.13/rgdal/gdal
>> Loaded PROJ.4 runtime: Rel. 4.7.1, 23 September 2009, [PJ_VERSION: 470]
>> Path to PROJ.4 shared files:
>> C:/Users/romunov/Documents/R/**win-library/2.13/rgdal/proj
>>
>>
>> Cheers,
>> Roman
>>
>>
>> On Sat, Jul 23, 2011 at 7:59 PM, Roger Bivand <Roger.Bivand@nhh.no>
>> wrote:
>>
>>  On Sat, 23 Jul 2011, Roman Luštrik wrote:
>>>
>>>  Thank you Roger for taking interest in this. Your time is very
>>>
>>>> appreciated.
>>>>
>>>>
>>> Unfortunately, the GPX driver may be built write-only if GDAL was not
>>> built
>>> with expat - the same applies to KML. This appears to be your situation.
>>> I
>>> suggest upgrading your GDAL, and making sure that it is built with expat.
>>>
>>> Roger
>>>
>>>
>>>  I do have a "standalone" gdal version installed (from a while back) but
>>>> I
>>>> think it may be "dead". I can't access ogrinfo through a command prompt
>>>> (only if I run a special .bat that sets up the SDK shell) even though I
>>>> have
>>>> the /gdal/bin in my PATH. If I run ogrinfo in the aforementioned SDK
>>>> shell,
>>>> I get this output.
>>>>
>>>> C:\gdal>ogrinfo --formats
>>>> Supported Formats:
>>>>  -> "ESRI Shapefile" (read/write)
>>>>  -> "MapInfo File" (read/write)
>>>>  -> "UK .NTF" (readonly)
>>>>  -> "SDTS" (readonly)
>>>>  -> "TIGER" (read/write)
>>>>  -> "S57" (read/write)
>>>>  -> "DGN" (read/write)
>>>>  -> "VRT" (readonly)
>>>>  -> "REC" (readonly)
>>>>  -> "Memory" (read/write)
>>>>  -> "BNA" (read/write)
>>>>  -> "CSV" (read/write)
>>>>  -> "NAS" (readonly)
>>>>  -> "GML" (read/write)
>>>> *  -> "GPX" (read/write)*
>>>>  -> "KML" (read/write)
>>>>  -> "GeoJSON" (read/write)
>>>>  -> "GMT" (read/write)
>>>>  -> "SQLite" (read/write)
>>>>  -> "ODBC" (read/write)
>>>>  -> "PGeo" (readonly)
>>>>  -> "MSSQLSpatial" (read/write)
>>>>  -> "PostgreSQL" (read/write)
>>>>  -> "MySQL" (read/write)
>>>>  -> "PCIDSK" (read/write)
>>>>  -> "XPlane" (readonly)
>>>>  -> "AVCBin" (readonly)
>>>>  -> "AVCE00" (readonly)
>>>>  -> "DXF" (read/write)
>>>>  -> "Geoconcept" (read/write)
>>>>  -> "GeoRSS" (read/write)
>>>>  -> "GPSTrackMaker" (read/write)
>>>>  -> "VFK" (readonly)
>>>>  -> "PGDump" (read/write)
>>>>  -> "GPSBabel" (read/write)
>>>>  -> "SUA" (readonly)
>>>>  -> "OpenAir" (readonly)
>>>>  -> "PDS" (readonly)
>>>>  -> "WFS" (readonly)
>>>>  -> "HTF" (readonly)
>>>>  -> "AeronavFAA" (readonly)
>>>>
>>>> ogrDrivers() gives out this:
>>>>
>>>>  ogrDrivers()
>>>>
>>>>>
>>>>>            name write
>>>> 1          AVCBin FALSE
>>>> 2          AVCE00 FALSE
>>>> 3             BNA  TRUE
>>>> 4             CSV  TRUE
>>>> 5             DGN  TRUE
>>>> 6             DXF  TRUE
>>>> 7  ESRI Shapefile  TRUE
>>>> 8      Geoconcept  TRUE
>>>> 9         GeoJSON  TRUE
>>>> 10         GeoRSS  TRUE
>>>> 11            GML  TRUE
>>>> 12            GMT  TRUE
>>>> 13  GPSTrackMaker  TRUE
>>>> *14            GPX  TRUE*
>>>> 15            KML  TRUE
>>>> 16   MapInfo File  TRUE
>>>> 17         Memory  TRUE
>>>> 18         PCIDSK FALSE
>>>> 19            REC FALSE
>>>> 20            S57  TRUE
>>>> 21           SDTS FALSE
>>>> 22          TIGER  TRUE
>>>> 23        UK .NTF FALSE
>>>> 24            VFK FALSE
>>>> 25            VRT FALSE
>>>> 26         XPlane FALSE
>>>>
>>>> Cheers,
>>>> Roman
>>>>
>>>>
>>>>
>>>> On Sat, Jul 23, 2011 at 10:51 AM, Roger Bivand <Roger.Bivand@nhh.no>
>>>> wrote:
>>>>
>>>>  On Sat, 23 Jul 2011, Roman Luštrik wrote:
>>>>
>>>>>
>>>>>  Dear list,
>>>>>
>>>>>
>>>>>> I'm trying to read a gpx file using rgdal::readOGR function. I get the
>>>>>> following error, as demonstrated by using the example from the help
>>>>>> file
>>>>>> of
>>>>>> readOGR.
>>>>>>
>>>>>>
>>>>>>  You have installed GDAL yourself. What does ogrinfo --formats or
>>>>> ogrDrivers() show? Do you have a GDAL built with expat to include the
>>>>> GPX
>>>>> driver?
>>>>>
>>>>> Roger
>>>>>
>>>>>
>>>>>  dsn <- system.file("vectors/test_******trk2.gpx", package =
>>>>> "rgdal")[1]
>>>>>
>>>>>> dsn
>>>>>>  [1]
>>>>>> "C:/Users/romunov/Documents/R/******win-library/2.13/rgdal/**
>>>>>> vectors/test_trk2.gpx"
>>>>>> test_trk2 <- try(readOGR(dsn=dsn, layer="tracks"))
>>>>>>  Error in ogrInfo(dsn = dsn, layer = layer, input_field_name_encoding
>>>>>> =
>>>>>> input_field_name_encoding) :
>>>>>>  Cannot open file
>>>>>>
>>>>>>
>>>>>> I've traced the ogrInfo and it stops at the line where it makes a
>>>>>> .Call
>>>>>> to
>>>>>> "ogrInfo" function.
>>>>>>
>>>>>> debug: ogrinfo <- .Call("ogrInfo", as.character(dsn),
>>>>>> as.character(layer),
>>>>>>  PACKAGE = "rgdal")
>>>>>> Browse[3]> n
>>>>>> Error in ogrInfo(dsn = dsn, layer = layer, input_field_name_encoding =
>>>>>> input_field_name_encoding) :
>>>>>>  Cannot open file
>>>>>>
>>>>>>
>>>>>> What am I missing?
>>>>>>
>>>>>> Here's my sessionInfo.
>>>>>>
>>>>>>  sessionInfo()
>>>>>>
>>>>>>
>>>>>>>  R version 2.13.1 (2011-07-08)
>>>>>>>
>>>>>> Platform: x86_64-pc-mingw32/x64 (64-bit)
>>>>>> locale:
>>>>>> [1] LC_COLLATE=Slovenian_Slovenia.******1250
>>>>>>  LC_CTYPE=Slovenian_Slovenia.**
>>>>>> 1250
>>>>>> [3] LC_MONETARY=Slovenian_******Slovenia.1250 LC_NUMERIC=C
>>>>>> [5] LC_TIME=Slovenian_Slovenia.******1250
>>>>>> attached base packages:
>>>>>> [1] stats     graphics  grDevices utils     datasets  methods   base
>>>>>> other attached packages:
>>>>>> [1] rgdal_0.7-1 sp_0.9-79
>>>>>> loaded via a namespace (and not attached):
>>>>>> [1] grid_2.13.1     lattice_0.19-30 tools_2.13.1
>>>>>>
>>>>>>
>>>>>> Any tips on how to solve this will be much appreciated.
>>>>>>
>>>>>>
>>>>>> Cheers,
>>>>>> Roman
>>>>>>
>>>>>>
>>>>>>
>>>>>>  --
>>>>>> In God we trust, all others bring data.
>>>>>>
>>>>>>      [[alternative HTML version deleted]]
>>>>>>
>>>>>> ______________________________******_________________
>>>>>> R-sig-Geo mailing list
>>>>>> R-sig-Geo@r-project.org
>>>>>> https://stat.ethz.ch/mailman/******listinfo/r-sig-geo<https://stat.ethz.ch/mailman/****listinfo/r-sig-geo>
>>>>>> <https://**stat.ethz.ch/mailman/****listinfo/r-sig-geo<https://stat.ethz.ch/mailman/**listinfo/r-sig-geo>
>>>>>> >
>>>>>> <https://**stat.ethz.ch/**mailman/listinfo/**r-sig-geo<http://stat.ethz.ch/mailman/listinfo/**r-sig-geo>
>>>>>> <h**ttps://stat.ethz.ch/mailman/**listinfo/r-sig-geo<https://stat.ethz.ch/mailman/listinfo/r-sig-geo>
>>>>>> >
>>>>>>
>>>>>>>
>>>>>>>
>>>>>>
>>>>>>  --
>>>>>>
>>>>> Roger Bivand
>>>>> Department of Economics, NHH Norwegian School of Economics,
>>>>> Helleveien 30, N-5045 Bergen, Norway.
>>>>> voice: +47 55 95 93 55; fax +47 55 95 95 43
>>>>> e-mail: Roger.Bivand@nhh.no
>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>>>
>>>>  --
>>> Roger Bivand
>>> Department of Economics, NHH Norwegian School of Economics,
>>> Helleveien 30, N-5045 Bergen, Norway.
>>> voice: +47 55 95 93 55; fax +47 55 95 95 43
>>> e-mail: Roger.Bivand@nhh.no
>>>
>>>
>>
>>
>>
>>
> --
> Roger Bivand
> Department of Economics, NHH Norwegian School of Economics,
> Helleveien 30, N-5045 Bergen, Norway.
> voice: +47 55 95 93 55; fax +47 55 95 95 43
> e-mail: Roger.Bivand@nhh.no
>



-- 
In God we trust, all others bring data.

	[[alternative HTML version deleted]]

