-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransitLineFileRead.R
More file actions
159 lines (133 loc) · 6.57 KB
/
TransitLineFileRead.R
File metadata and controls
159 lines (133 loc) · 6.57 KB
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
# Raghu Sidharthan
#TODO
#1. Write a trnsit line write function which parses after a certain number of characters in each row. end with a comma
# Function to concat a set of lines
# Parameter allLines is a set of lines
# sttNum is the start index number and stpNum is the stop index number
concatLines <- function(allLines,sttNum,stpNum){
return(paste0(unlist(str_trim(allLines[sttNum:stpNum])),collapse = ''))
}
replaceLineNodesUntil <- function(lineNodesUntil,keywordStr,valStr){
indexHW <- gregexpr(keywordStr,lineNodesUntil)[[1]][1]
if(indexHW!=-1){
strInitial <- substr(lineNodesUntil,1,indexHW-1)
strRest <- substr(lineNodesUntil,indexHW+9,nchar(lineNodesUntil))
indexRest <- gregexpr(',',strRest)[[1]][1]
strFinal <- substr(strRest,indexRest+1,nchar(strRest))
if(valStr!=-1){
paste0(strInitial,gsub("\\", "", keywordStr, fixed=TRUE),'=',valStr,',',strFinal)
}
else
paste0(strInitial,strFinal)
}
else {
indexHW <- gregexpr('HEADWAY',lineNodesUntil)[[1]][1]
strInitial <- substr(lineNodesUntil,1,indexHW-1)
strRest <- substr(lineNodesUntil,indexHW,nchar(lineNodesUntil))
if(valStr!=-1){
paste0(strInitial,gsub("\\", "", keywordStr, fixed=TRUE),'=',valStr,',',strRest)
}
else{
lineNodesUntil
}
}
}
extractKeyword <- function(lineNodesUntilSample,keywordStr){
retval <- "-99"
if(gregexpr(keywordStr,lineNodesUntilSample)[[1]][1]!=-1){
retval <- gsub(',',' ',substr(lineNodesUntilSample,gregexpr(keywordStr,lineNodesUntilSample)[[1]][1]+nchar(keywordStr)+1,nchar(lineNodesUntilSample)))
retval <- (substr(retval,1,gregexpr(' ',retval)[[1]][1]-1))
}
retval
}
extractHeadWays <- function(lineNodesUntilSample){
# Getting the headways
h_1 <- -1
h_2 <- -1
h_3 <- -1
if(gregexpr('HEADWAY\\[1\\]',lineNodesUntilSample)[[1]][1]!=-1){
h_1 <- gsub(',',' ',substr(lineNodesUntilSample,gregexpr('HEADWAY\\[1\\]',lineNodesUntilSample)[[1]][1]+11,nchar(lineNodesUntilSample)))
h_1 <- as.integer(substr(h_1,1,gregexpr(' ',h_1)[[1]][1]-1))
}
if(gregexpr('HEADWAY=',lineNodesUntilSample)[[1]][1]!=-1){
h_1 <- gsub(',',' ',substr(lineNodesUntilSample,gregexpr('HEADWAY=',lineNodesUntilSample)[[1]][1]+8,nchar(lineNodesUntilSample)))
h_1 <- as.integer(substr(h_1,1,gregexpr(' ',h_1)[[1]][1]-1))
}
if(gregexpr('HEADWAY\\[2\\]',lineNodesUntilSample)[[1]][1]!=-1){
h_2 <- gsub(',',' ',substr(lineNodesUntilSample,gregexpr('HEADWAY\\[2\\]',lineNodesUntilSample)[[1]][1]+11,nchar(lineNodesUntilSample)))
h_2 <- as.integer(substr(h_2,1,gregexpr(' ',h_2)[[1]][1]-1))
}
if(gregexpr('HEADWAY\\[3\\]',lineNodesUntilSample)[[1]][1]!=-1){
h_3 <- gsub(',',' ',substr(lineNodesUntilSample,gregexpr('HEADWAY\\[3\\]',lineNodesUntilSample)[[1]][1]+11,nchar(lineNodesUntilSample)))
h_3 <- as.integer(substr(h_3,1,gregexpr(' ',h_3)[[1]][1]-1))
}
return(c(h_1,h_2,h_3))
}
extractNodesVector <- function(lineNodesSample){
# Return the nodes in a vector format
temp1 <- str_split(gsub(' ',',',gsub(' ',' ',gsub('NODES =',' ',gsub('NODES=',' ',gsub('N =',' ',gsub('N=',' ',gsub(',',' ',lineNodesSample))))))),',')
temp2 <- lapply(temp1,function(x) grep("^[-0123456789]", x))
nodeVector <- lapply(unlist(temp1)[unlist(temp2)],as.integer)
return(nodeVector)
}
extractEndOfString <- function(strArg){
strStartType <- substr(strArg,1,1)
strArgRest <- substr(strArg,2,nchar(strArg))
if(strStartType=='"'){
retval <- substr(strArg,2, regexpr('["]',strArgRest)[1])
}else if(strStartType=="'"){
retval <- substr(strArg,2, regexpr("[']",strArgRest)[1])
}else{
retval <- substr(strArg,1, regexpr('[ ,]',strArgRest)[1])
}
return(retval)
}
readTLFile <- function(TL_fname){
inpLines <- readLines(TL_fname)
#Remove Comments
linesThatAreComments <- unlist(lapply(inpLines,function(x) substr(x,1,1)==';'))
inpLines <- inpLines[!linesThatAreComments]
lineIdent <- unlist(lapply(inpLines,function(x) (substr(x,1,4)=='LINE')|(substr(x,2,5)=='LINE') ))
table(lineIdent)
sttPositions <- (1:length(lineIdent))[lineIdent]
stpPositions <- sttPositions[2:length(sttPositions)]-1
stpPositions[length(sttPositions)] <- length(lineIdent)
linesComplete <- lapply(1:length(sttPositions),function(x) concatLines(inpLines,sttPositions[x],stpPositions[x]) )
temp1 <- lapply(linesComplete,function(x) substr(x,regexpr('NAME=',x)[1]+5,nchar(x)) )
#temp2 <- lapply(temp1,function(x) substr(x,1,regexpr('[ ,]',x)[1]) )
#lineNames <- lapply(temp2,function(x) gsub('[ \"\',]','',x))
lineNames <- unlist(lapply(temp1,function(x) extractEndOfString(x) ))
temp1 <- lapply(linesComplete,function(x) substr(x,regexpr('LONGNAME=',x)[1]+9,nchar(x)) )
lineLongNames <- unlist(lapply(temp1,function(x) extractEndOfString(x) ))
lineNodesUntil <- lapply(linesComplete,function(x) substr(x,1,ifelse(gregexpr('N=',x)[[1]][1]>1,
gregexpr('N=',x)[[1]][1],
gregexpr('NODES=',x)[[1]][1])-1) )
lineNodes <- lapply(linesComplete,function(x) substr(x,ifelse(gregexpr('N=',x)[[1]][1]>1,
gregexpr('N=',x)[[1]][1],
gregexpr('NODES=',x)[[1]][1]),nchar(x)) )
nodeVector <- lapply(lineNodes,extractNodesVector)
lineHeadway <- lapply(lineNodesUntil,extractHeadWays)
lineSpeed <- lapply(linesComplete, function(x) ifelse(is.na(str_match(x, "SPEED=(.*?),")[,2]), "", str_match(x, "SPEED=(.*?),")[,2])) #for now, only get the first SPEED
lineMode <- lapply(lineNodesUntil,extractKeyword,'MODE')
retObject <- lapply(1:length(linesComplete),
function(x)
list(lineNames=unlist(lineNames[x]),
lineLongNames=unlist(lineLongNames[x]),
lineNodesUntil = unlist(lineNodesUntil[x]),
lineNodes = unlist(lineNodes[x]),
nodeVector=unlist(nodeVector[x]),
linesComplete=unlist(linesComplete[x]),
lineHeadway= unlist(lineHeadway[x]),
lineSpeed=unlist(lineSpeed[x]),
lineMode=unlist(lineMode[x]),
lineNumber= x
))
names(retObject) <- lineNames
return(retObject)
}
fillna = function(DT,val) {
DT_ret <- copy(DT)
for (j in seq_len(ncol(DT_ret)))
set(DT_ret,which(is.na(DT_ret[[j]])),j,val)
return(DT_ret)
}