2

I am learning how to use writeOGR to write a GPX file. When I run the line of code I get an error message: Creation of output file failed.

Example set of data

 dput(head(location_information))
structure(list(Stratum = c(9, 9, 10, 10, 10, 10), Long = c(-74.7481648257133, 
-74.8131391902885, -74.8808092556099, -74.8653903776827, -74.7982608783084, 
-74.7124840980305), Lat = c(37.4333074571158, 37.2589604507964, 
37.0344661189436, 37.1094779835421, 36.9472164951635, 37.5385694318501
), Station = structure(c(449L, 450L, 1L, 2L, 3L, 4L), .Label = c("10001", 
"10002", "10003", "10004", "10005", "11001", "11002", "11003", 
"11004", "11005", "11006", "11007", "12001", "12002", "12003", 
"12004", "12005", "12006", "13001", "13002", "13003", "13004", 
"13005", "13006", "13007", "13008", "13009", "13010", "13011", 
"14001", "14002", "14003", "14004", "14005", "14006", "15001", 
"15002", "15003", "15004", "15005", "15006", "15007", "15008", 
"15009", "15010", "15011", "15012", "15013", "15014", "15015", 
"15016", "15017", "15018", "15019", "15020", "15021", "15022", 
"15023", "16001", "16002", "16003", "16004", "16005", "17001", 
"17002", "17003", "17004", "17005", "17006", "17007", "17008", 
"17009", "18001", "18002", "18003", "18004", "18005", "18006", 
"18007", "18008", "18009", "18010", "18011", "18012", "18013", 
"18014", "18015", "18016", "18017", "18018", "18019", "18020", 
"18021", "18022", "18023", "18024", "18025", "18026", "18027", 
"18028", "18029", "18030", "18031", "18032", "18033", "18034", 
"18035", "18036", "18037", "18038", "18039", "18040", "18041", 
"19001", "19002", "19003", "19004", "19005", "19006", "19007", 
"19008", "19009", "19010", "19011", "19012", "19013", "19014", 
"19015", "19016", "19017", "19018", "19019", "19020", "19021", 
"19022", "19023", "20001", "20002", "21001", "21002", "21003", 
"21004", "21005", "21006", "21007", "21008", "21009", "21010", 
"21011", "21012", "21013", "21014", "21015", "21016", "21017", 
"21018", "21019", "21020", "21021", "22001", "22002", "22003", 
"22004", "22005", "22006", "22007", "22008", "22009", "22010", 
"22011", "22012", "22013", "22014", "22015", "22016", "22017", 
"22018", "22019", "22020", "22021", "22022", "22023", "22024", 
"22025", "22026", "22027", "22028", "23001", "23002", "23003", 
"23004", "23005", "23006", "23007", "23008", "23009", "23010", 
"23011", "23012", "23013", "23014", "23015", "23016", "23017", 
"23018", "23019", "23020", "23021", "23022", "23023", "23024", 
"23025", "23026", "23027", "23028", "23029", "23030", "23031", 
"23032", "23033", "23034", "23035", "23036", "23037", "23038", 
"23039", "23040", "23041", "23042", "23043", "23044", "23045", 
"23046", "23047", "23048", "23049", "23050", "23051", "23052", 
"23053", "23054", "23055", "23056", "23057", "23058", "23059", 
"23060", "23061", "23062", "23063", "23064", "23065", "23066", 
"23067", "23068", "23069", "23070", "23071", "23072", "23073", 
"23074", "23075", "23076", "23077", "23078", "23079", "23080", 
"23081", "23082", "23083", "23084", "23085", "23086", "23087", 
"23088", "23089", "23090", "23091", "23092", "23093", "23094", 
"23095", "23096", "23097", "23098", "23099", "23100", "23101", 
"23102", "23103", "24001", "24002", "24003", "24004", "24005", 
"24006", "24007", "24008", "24009", "25001", "25002", "25003", 
"25004", "25005", "25006", "25007", "25008", "25009", "25010", 
"25011", "25012", "25013", "26001", "26002", "26003", "26004", 
"26005", "27001", "27002", "27003", "27004", "27005", "27006", 
"27007", "27008", "27009", "27010", "27011", "27012", "27013", 
"27014", "27015", "27016", "27017", "27018", "27019", "28001", 
"28002", "28003", "29001", "29002", "29003", "29004", "29005", 
"29006", "29007", "29008", "29009", "29010", "29011", "29012", 
"29013", "29014", "29015", "29016", "29017", "29018", "29019", 
"29020", "29021", "29022", "29023", "29024", "30001", "30002", 
"30003", "30004", "30005", "30006", "30007", "30008", "30009", 
"30010", "30011", "30012", "30013", "30014", "30015", "30016", 
"30017", "30018", "30019", "31001", "31002", "31003", "31004", 
"31005", "31006", "31007", "31008", "31009", "31010", "31011", 
"31012", "31013", "31014", "31015", "31016", "31017", "31018", 
"31019", "31020", "31021", "31022", "31023", "31024", "31025", 
"31026", "31027", "31028", "31029", "31030", "31031", "31032", 
"31033", "31034", "31035", "31036", "33001", "33002", "33003", 
"33004", "33005", "33006", "33007", "34001", "34002", "34003", 
"34004", "34005", "35001", "35002", "35003", "35004", "35005", 
"35006", "35007", "35008", "35009", "35010", "35011", "35012", 
"35013", "37001", "37002", "37003", "38001", "38002", "9001", 
"9002"), class = "factor")), .Names = c("Stratum", "Long", "Lat", 
"Station"), row.names = c(NA, 6L), class = "data.frame")

Code to write to GPX

#write location_information to GPX format for use with navigation software 

#make location_information a spatial points shapefile
#get lat/long from location_information
xy2<-location_information[,c(2,3)]

ll_points <- SpatialPointsDataFrame(coords=xy2,data=location_information,proj4string =CRS("+proj=longlat + ellps=WGS84"))
class(ll_points)
summary(ll_points)

writeOGR(ll_points, dsn="N:/2015 Projects/Surveys/Station Allocation/gpxfile.gpx",layer="waypoints3",driver="GPX")

R information R version 3.2.1 (2015-06-18) Platform: i386-w64-mingw32/i386 (32-bit) Running under: Windows 7 x64 (build 7601) Service Pack 1

locale: [1] LC_COLLATE=English_United States.1252 [2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252 [4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252

attached base packages: [1] grid datasets utils stats graphics grDevices methods
[8] base

other attached packages: [1] spatialEco_0.1-3 spatstat_1.42-2 rgdal_1.0-6
[4] maptools_0.8-36 mapdata_2.2-5 maps_2.3-11
[7] sp_1.1-1 car_2.0-26 xlsx_0.5.7
[10] xlsxjars_0.6.1 rJava_0.9-7 plyr_1.8.3
[13] MASS_7.3-40 RODBC_1.3-12 latticeExtra_0.6-26 [16] RColorBrewer_1.1-2 lattice_0.20-31

loaded via a namespace (and not attached): [1] minqa_1.2.4 splines_3.2.1 MatrixModels_0.4-1 [4] goftest_1.0-3 abind_1.4-3 lme4_1.1-10
[7] deldir_0.1-9 nnet_7.3-9 mgcv_1.8-6
[10] tensor_1.5 Matrix_1.2-1 pbkrtest_0.4-2
[13] tools_3.2.1 foreign_0.8-63 parallel_3.2.1
[16] nloptr_1.0.4 nlme_3.1-120 quantreg_5.18
[19] polyclip_1.3-2 Rcpp_0.11.6 SparseM_1.7

4

Yeah, this is a tricky one. The "dsn" argument is the output file and the "layer" argument is the GPX feature type (eg., waypoints). This is how ESRI file geodatabases work as well. There is no clear direction in this anywhere but you also have to use the "dataset_options" argument with "GPX_USE_EXTENSIONS=yes". I got this example to work.

library(rgdal)
setwd("D:/TMP") # set your output directory here

( dat <- data.frame(Stratum=c(9,9,10,10,10,10),
  Long=c(-74.74816,-74.81314,-74.88081,-74.86539,-74.79826,-74.71248),
  Lat=c(37.43331,37.25896,37.03447,37.10948,36.94722,37.53857),
  Station=c(9001,9002,10001,10002,10003,10004)) )

coordinates(dat) <- ~Lat+Long
  proj4string(dat) <- "+proj=longlat +datum=WGS84" 

writeOGR(dat, dsn = "waypoints3.gpx", layer = "waypoints", driver = "GPX", 
         dataset_options = "GPX_USE_EXTENSIONS=yes")
  • Thank you. I was able to get my code working from your example. – user41509 Apr 1 '16 at 19:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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