-
Notifications
You must be signed in to change notification settings - Fork 2
/
ParseOpenAir.hs
348 lines (308 loc) · 9.17 KB
/
ParseOpenAir.hs
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import Text.ParserCombinators.Parsec hiding (newline)
import Text.ParserCombinators.Parsec.Number
import Arc
import GenNew hiding (St)
import qualified Data.Map as Map
import Control.Monad (liftM)
-- for testing
import Text.XML.HaXml hiding (x)
import Text.XML.HaXml.Pretty
import GenXml
import Control.Monad.State (runState)
import System.Environment
newline = try (string "\r\n") <|> try (string "\n")
data St = St { x :: LatLon
, curId :: Integer
, file :: File
, dir :: Bool
}
type OpenAir a = GenParser Char St a
parseFloat :: (Num a, Floating a) => OpenAir a
parseFloat = try floating <|> (liftM fromIntegral decimal)
parseNums :: OpenAir [Double]
parseNums = sepBy1 parseFloat (char ':')
parseLat = do
nums <- parseNums
optional space
ch <- oneOf "NSns"
let decimal = (nums !! 0) + (nums !! 1)/60 + (nums !! 2)/60^2
if ch == 'N' || ch == 'n'
then return decimal
else return (-decimal)
parseLon = do
nums <- parseNums
optional space
ch <- oneOf "EWew"
let decimal = (nums !! 0) + (nums !! 1)/60 + (nums !! 2)/60^2
if ch == 'E' || ch == 'e'
then return decimal
else return (-decimal)
parseLatLon :: OpenAir LatLon
parseLatLon = do
lat <- parseLat
space
lon <- parseLon
return (LatLon lat lon)
parseLenthUnit = try (string "FT")
parseHeight :: (Num a, Floating a) => OpenAir (a, String)
parseHeight = do
let gnd = ((string "GND" <|> string "MSL") >> return (0,"AGL"))
unl = (string "UNL" >> return (999, "FL"))
fl = do
string "FL"
num <- parseFloat
return (num, "FL")
r = do
num <- parseFloat
unit <- optional parseLenthUnit
space
ref <- (try (string "MSL" >> return "AMSL") <|> try (string "AGL") <|> (string "GND" >> return "AGL"))
return (num, ref)
ans <- try r <|> try fl <|> try gnd <|> unl
return ans
parseLowerHeight = do
string "AL "
h <- parseHeight
newline
return h
parseUpperHeight = do
string "AH "
h <- parseHeight
newline
return h
strings :: [String] -> OpenAir String
strings (s:ss) = foldl (\st v -> st <|> try (string v)) (try (string s)) ss
parseClass :: OpenAir String
parseClass = do
string "AC "
c <- strings ["CTR","TMZ","GP","R","Q","P","A","B","C","D","E","F","G","W"]
newline
return c
parseName :: OpenAir String
parseName = try (string "AN " >> manyTill anyChar newline) <|> try (string "TO " >> manyTill anyChar newline) <|> try (manyTill anyChar newline)
parseSet :: OpenAir [LatLon]
parseSet = do
string "V "
(try parseX) <|> parseDir
return []
parseDir = try parseDirV1 <|> try parseDirV2
parseDirV1 = do
char 'D'
optional space
char '='
optional space
c <- oneOf "+-"
newline
st <- getState
case c of
'-' -> setState (st {dir = False})
'+' -> setState (st {dir = True})
parseDirV2 = do
char '='
optional space
char 'D'
optional space
c <- oneOf "+-"
newline
st <- getState
case c of
'-' -> setState (st {dir = False})
'+' -> setState (st {dir = True})
parseX :: OpenAir ()
parseX = do
char 'X'
optional space
char '='
optional space
st <- getState
ll <- parseLatLon
newline
setState (st {x = ll})
newId :: OpenAir Integer
newId = do
st <- getState
let n = curId st
setState (st {curId = (curId st +1)})
return n
toNode :: LatLon -> OpenAir Node
toNode (LatLon lat lon) = do
i <- newId
return $ Node {nId=i, lat=lat, lon=lon, nTags=Map.empty}
addNodeToFile :: Node -> OpenAir ()
addNodeToFile n = do
st <- getState
let (File ns ws rs) = file st
setState (st {file =File (n:ns) ws rs})
addWayToFile :: Way -> OpenAir ()
addWayToFile w = do
st <- getState
let (File ns ws rs) = file st
setState (st {file =File ns (w:ws) rs})
createWay lls tags = do
ns <- mapM toNode lls
let membs = map nId ns
wid <- newId
let w = Way {wId = wid, wMembs = membs, wTags=tags}
mapM_ addNodeToFile ns
addWayToFile w
createArea lls tags = do
ns <- mapM toNode lls
let membs@(m:_) = map nId ns
wid <- newId
let w = Way {wId = wid, wMembs = (membs++[m]), wTags=tags}
mapM_ addNodeToFile ns
addWayToFile w
parseCircle :: OpenAir [[LatLon]]
parseCircle = do
string "DC "
d <- parseFloat
newline
st <- getState
return $ [genArc (x st) (1.852e3*d) (Deg 0) (Deg 346) True 25]
parseArc1 :: OpenAir [LatLon]
parseArc1 = do
string "DA "
r <- parseFloat
char ','
tc1 <- parseFloat
char ','
tc2 <- parseFloat
newline
st <- getState
return $ genArc (x st) (1.852e3*r) (Deg tc1) (Deg tc2) (dir st) 25
parseArc2 :: OpenAir [LatLon]
parseArc2 = do
string "DB "
ll1 <- parseLatLon
char ','
optional space
ll2 <- parseLatLon
newline
st <- getState
let (Deg tc1) = courceBetweenPoints (x st) ll1
(Deg tc2) = courceBetweenPoints (x st) ll2
r = distanceBetweenPoints (x st) ll1
return $ genArc (x st) r (Deg tc1) (Deg tc2) (dir st) 25
parseCoord :: OpenAir [LatLon]
parseCoord = do
string "DP "
ll <- parseLatLon
optional $ many space
try commentLine <|> try (newline >> return '')
return . return $ ll
parseAt :: OpenAir ()
parseAt = do
string "AT "
parseLatLon
return ()
parseSegment :: OpenAir [[LatLon]]
parseSegment = do
let arc1 = parseArc1
arc2 = parseArc2
coord = parseCoord
circle = parseCircle
set = parseSet
cm = try (commentLine >> return [])
optional cm
many (try set)
optional cm
(try circle) <|> (many1 (cm <|> try set <|> try arc1 <|> try arc2 <|> coord))
parseAirspace :: OpenAir ()
parseAirspace = do
st <- getState
--setState (st {dir = True})
let cm = optional commentLine
aClass <- parseClass
try cm
aName <- parseName -- parseName parses a newline
try cm
(lNum,lRef) <- parseLowerHeight
try cm
(uNum,uRef) <- parseUpperHeight
try cm
lls <- parseSegment
let tags = Map.fromList [("airspace","yes")
,("name",aName)
,("airspace:class","ft")
,("height:lower",show lNum)
,("height:lower:ref",lRef)
,("height:lower:unit","ft")
,("height:upper",show uNum)
,("height:upper:ref",uRef)
,("height:upper:unit","ft")]
createArea (concat lls) tags
commentLine = do
char '*'
manyTill anyChar newline
return ()
emptyLine = newline >> return ()
parseFile = do
manyTill (try emptyLine <|> try commentLine <|> try parseAirspace) eof
st <- getState
return (file st)
--testAs1 = unlines ["AC R",
-- "AN ED-R37B NORDHORN",
-- "AL GND",
-- "AH 2500MSL",
-- "V X=52:26:00 N 007:12:00 E",
-- "DB 52:29:31 N 007:17:48 E,52:26:00 N 007:03:50 E"]
--
--testAs = unlines ["AC R",
-- "AN ED-R37B NORDHORN",
-- "AL GND",
-- "AH 2500MSL",
-- "DP 52:43:05 N 007:03:49 E",
-- "DP 52:41:00 N 007:15:10 E",
-- "DP 52:29:31 N 007:17:48 E",
-- "V D=-",
-- "V X=52:26:00 N 007:12:00 E",
-- "DB 52:29:31 N 007:17:48 E,52:26:00 N 007:03:50 E",
-- "DP 52:26:00 N 007:03:39 E",
-- "DP 52:39:59 N 007:03:27 E",
-- "AC R",
-- "AN GRESSS!!!",
-- "AL GND",
-- "AH 2500MSL",
-- "DP 52:43:05 N 007:03:49 E",
-- "DP 52:41:00 N 007:15:10 E",
-- "DP 52:29:31 N 007:17:48 E",
-- "V D=-",
-- "V X=52:26:00 N 007:12:00 E",
-- "DB 52:29:31 N 007:17:48 E,52:26:00 N 007:03:50 E",
-- "DP 52:26:00 N 007:03:39 E",
-- "DP 52:39:59 N 007:03:27 E"]
--
--test = do
-- let st = St { x = LatLon 0 0
-- , file = File [] [] []
-- , curId = 0
-- , dir = True}
-- let ret = runParser parseFile st "test" testAs
-- case ret of
-- Right file -> do
-- let f = runState (genNewIds file) (0,Map.empty)
-- let out = render . document . genXml . fst $ f
-- putStrLn out
-- Left err -> putStrLn . show $ err
--
test f t = do
let st = St { x = LatLon 0 0
, file = File [] [] []
, curId = 0
, dir = True}
runParser f st "test" t
main = do
let st = St { x = LatLon 0 0
, file = File [] [] []
, curId = 0
, dir = True}
args <- getArgs
putStrLn . show $ args
input <- readFile (args !! 0)
let ret = runParser parseFile st (args !! 0) input
case ret of
Right file -> do
let f = runState (genNewIds file) (0,Map.empty)
let out = render . document . genXml . fst $ f
writeFile (args !! 1) out
Left err -> putStrLn . show $ err