-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patholexToGpx.R
332 lines (296 loc) · 9.29 KB
/
olexToGpx.R
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
library(data.table)
library(XML)
#' @noRd
pop <- function(lines){
if (length(lines)<=1){
return(c())
}
else{
return(lines[2:length(lines)])
}
}
#' @noRd
parseWaypoint <- function(lines){
waypoint <- list()
waypoint$lat <- as.numeric(NA)
waypoint$lon <- as.numeric(NA)
waypoint$time <- as.POSIXct(NA)
waypoint$symbol <- as.character(NA)
waypoint$name <- as.character(NA)
waypoint$descr <- as.character(NA)
waypoint$comment <- as.character(NA)
coordstring <- lines[1]
coordlist <- strsplit(coordstring, " ")[[1]]
lat <- as.numeric(coordlist[1])/60
lon <- as.numeric(coordlist[2])/60
if (!is.na(coordlist[3])){
time <- as.POSIXct(as.integer(coordlist[3]), origin="1970-01-01")
}
else{
time <- NA
}
if (!is.na(coordlist[4])){
symbol <- coordlist[4]
}
else{
symbol <- NA
}
waypoint$lat <- lat
waypoint$lon <- lon
waypoint$time <- time
waypoint$symbol <- symbol
lines <- pop(lines)
if (substr(lines[1], 1,4)=="Navn"){
waypoint$name <- trimws(substr(lines[1],5, nchar(lines[1])))
lines <- pop(lines)
}
while (length(lines) > 0 && substr(lines[1], 1,6)=="MTekst"){
if (is.na(waypoint$comment)){
waypoint$comment <- ""
}
waypoint$comment <- paste(waypoint$comment, substr(lines[1],7, nchar(lines[1])), " ")
lines <- pop(lines)
}
output <- list()
output$waypoint <- waypoint
output$lines <- lines
return(output)
}
#' @noRd
parseOlexRoute <- function(lines){
route <- list()
stopifnot(substr(lines[1],1,4)=="Rute")
route$routename <- trimws(substr(lines[1],5,nchar(lines[1])))
lines <- pop(lines)
if(substr(lines[1],1,8)=="Rutetype"){
route$routetype <- trimws(substr(lines[1],9,nchar(lines[1])))
lines <- pop(lines)
}
else{
route$routetype <- NA
}
if(substr(lines[1],1,10)=="Linjefarge"){
route$linecolor <- trimws(substr(lines[1],11,nchar(lines[1])))
lines <- pop(lines)
}
else{
route$linecolor <- NA
}
if(substr(lines[1],1,9)=="Plottsett"){
route$plotsett <- trimws(substr(lines[1],10,nchar(lines[1])))
lines <- pop(lines)
}
else{
route$plotsett <- NA
}
if(substr(lines[1],1,7)=="Fikspos"){
route$fixpos <- trimws(substr(lines[1],8,nchar(lines[1])))
lines <- pop(lines)
}
else{
route$fixpos <- NA
}
wpn <- 1
while(length(lines) > 0 && trimws(lines[1])!=""){
pp <- parseWaypoint(lines)
lines <- pp$lines
route$waypoints[[wpn]] <- pp$waypoint
wpn <- wpn + 1
}
stopifnot(trimws(lines[1])=="")
lines <- pop(lines)
output <- list()
output$lines <- lines
output$route <- route
return(output)
}
#' waypoint
#'
#' @description list with members
#' \describe{
#' \item{lat}{latitude in decimal degrees (WGS84)}
#' \item{lon}{longitude in decimal degrees (WGS84)}
#' \item{time}{PSOIXct time}
#' \item{symbol}{code for symbol, specific to plotters}
#' \item{name}{name of waypoint}
#' \item{descr}{description of waypoint}
#' \item{comment}{comments for waypoint}
#' }
#'
#'
#' @name waypoint
#'
"waypoint"
#' route
#'
#' @description
#' a list with members:
#' \descibe{
#' \item{routename}{name of route}
#' \item{routetype}{type of route, used for visualization, derived from Olex, not supperted by gpx}
#' \item{linecolor}{linecolor, used for visualization, derived from Olex, not supperted by gpx}
#' \item{plotsett}{dont know, probably used for visualization, derived from Olex, not supperted by gpx}
#' \item{fixpos}{dont know, probably used for visualization, derived from Olex, not supperted by gpx}
#' \item{waypoints}{a list of \code{\link{waypoint}}}
#' }
#'
#' nestedRoutes
#'
#' @description
#' a list of \code{\link{route}}
#' @name nestedRoutes
"nestedRoutes"
#' plotterData
#'
#' @description
#' list of plotterdata contents. May have a member 'routes' which is an \code{\link{nestedRoutes}} object
#'
#' @name plotterData
"plotterData"
#' Read routes from OLEX - file
#'
#' @details
#' Implementation is not based on proper format description. May crash if untested Olex-elements are introduced.
#' @param olexfile file to be read. May be .gz compressed.
#' @param encoding encoding used in file
#' @return \code{\link{plotterData}} with routes from the file.
parseOlex <- function(olexfile, encoding="latin1"){
lines <- readLines(olexfile, encoding=encoding)
output <- list()
output$routes <- list()
while(nchar(lines[1])==0){
lines <- pop(lines)
}
routenr <- 1
while (length(lines) > 0){
head <- lines[1]
if (substr(head,1,4) == "Rute"){
pp <- parseOlexRoute(lines)
lines <- pp$lines
output$routes[[routenr]] <- pp$route
routenr <- routenr + 1
}
else if (is.na(lines[1])){
lines <- pop(lines)
}
else{
stop("Did not recognize: ", head)
}
}
return(output)
}
#' extract routes as a date table
#' @param routes nestedRoute
#' @return \code{\link[data.table]{data.table}} of waypoints.
flattenRoute <- function(routes){
flatr <- NULL
for (r in routes){
rframe <- data.frame(r[1:4])
for (wp in r$waypoints){
wpframe <- data.frame(wp, stringsAsFactors = F)
flatr <- rbind(flatr, cbind(rframe, wpframe))
}
}
return(data.table::as.data.table(flatr))
}
#' Writes route as gpx
#' @param filename filename to write to
#' @param nestedRoute \code{\link{nestedRoute}} object to write
#' @param symbolMap list mapping names of waypoint symbols in 'nestedRoute' to replacement names understood by target plotter
#' @param creator value for the 'creatur' attribute in the gpx file. Defaults to openCPN.
writeGpxRoute <- function(filename, nestedRoute, symbolMap=list(Brunsirkel="Circle", Rødramme="Square", Empty="empty"), creator="OpenCPN"){
t <- XML::xmlOutputDOM("gpx", attrs = c(creator=creator))
for (r in nestedRoute){
t$addTag("rte", close=F)
t$addTag("name", r$routename)
for (wp in r$waypoints){
t$addTag("rtept", attrs = c(lat=wp$lat, lon=wp$lon), close=F)
if (!is.na(wp$time)){
t$addTag("time", wp$time)
}
if (!is.na(wp$name)){
t$addTag("name", wp$name)
}
if (!is.na(wp$comment)){
t$addTag("cmt", wp$comment)
}
if (!is.na(wp$descr)){
t$addTag("desc", wp$descr)
}
if (!is.na(wp$symbol)){
t$addTag("sym", symbolMap[[wp$symbol]])
}
t$addTag("type", "WPT")
t$closeTag() #wpt
}
t$closeTag() #rte
}
saveXML(t$value(), file=filename)
}
#' Changes symbols for nameless waypoints
#'
#' @details
#' Useful for instance for distinguising transect endpoints from planned fishing stations.
#'
#' @param nestedRoute \code{\link{nestedRoute}} object to modify
#' @param symbol name for symbol that should be set on unnamed waypoints.
#' @param \code{\link{nestedRoute}} modified nestedRoute.
set_symbols_nonames <- function(nestedRoute, symbol="Empty"){
for (i in 1:length(nestedRoute)){
for (j in 1:length(nestedRoute[[i]]$waypoints)){
if (is.na(nestedRoute[[i]]$waypoints[[j]]$name)){
nestedRoute[[i]]$waypoints[[j]]$symbol <- symbol
}
}
}
return(nestedRoute)
}
#' Write waypoints as gpx
#' @param filename filename to write
#' @param nestedRoute \code{\link{nestedRoute}} object to extract waypoints from
#' @param symbolMap list mapping names of waypoint symbols in 'nestedRoute' to replacement names understood by target plotter
writeGpxWaypoints <- function(filename, waypoints, symbolMap=list(Brunsirkel="circle", Rødramme="square", Empty="empty")){
t <- XML::xmlOutputDOM("gpx")
for (route in waypoints){
for (wp in route$waypoints){
t$addTag("wpt", attrs = c(lat=wp$lat, lon=wp$lon), close=F)
t$addTag("time", wp$time)
t$addTag("name", wp$name)
t$addTag("cmt", wp$comment)
t$addTag("desc", wp$descr)
t$addTag("sym", symbolMap[[wp$symbol]])
t$addTag("type", "WPT")
t$closeTag() #wpt
}
}
saveXML(t$value(), file=filename)
}
# example of use conversion
ex_conversion <- function(olexfile="~/hi_sync/tokt_og_felt/2021_kystokt/toktinfo/kysttokt21-kb.Ruter", outfile="~/hi_sync/tokt_og_felt/2021_kystokt/toktinfo/kysttokt21-kb.gpx"){
cont<-parseOlex(olexfile)
cont$routes <- set_symbols_nonames(cont$routes)
writeGpxRoute(outfile, cont$routes)
}
#example fo use importing routes
ex_import <- function(){
cont <- parseOlex("~/hi_sync/tokt_og_felt/2021_kystokt/toktinfo/kysttokt21-kb.Ruter")
dt <- flattenRoute(cont$routes)
stations <- dt[!is.na(dt$name),]
return(stations)
}
#' Eksporterer data til format for planlagt stasjoner (toktlogger)
#' aktivitetstype blir satt likt 'acttype' for alle stasjoner
ex_toktlogger <- function(olexfile="~/hi_sync/tokt_og_felt/2021_kystokt/toktinfo/kysttokt21-kb.Ruter", targetfile="~/hi_sync/tokt_og_felt/2021_kystokt/toktinfo/kysttokt21-kb.csv", acttype="Bunntrål"){
cont <- parseOlex(olexfile)
dt <- flattenRoute(cont$routes)
stations <- dt[!is.na(dt$name),]
stations[["P-nummer"]] <- 1:nrow(stations)
stations$Navn <- stations$name
stations$Aktivitetstype <- acttype
stations$Breddegrad <- stations$lat
stations$Lengdegrad <- stations$lon
stations$Comments <- stations$comment
stations <- stations[,c("P-nummer", "Navn", "Aktivitetstype", "Breddegrad", "Lengdegrad", "Comments")]
write.table(stations, file=targetfile, sep=";", row.names = F, quote = F, na = " ")
return(stations)
}