module ArcGrid
( ArcGrid
, ncols
, nrows
, xllcorner
, yllcorner
, xllcenter
, yllcenter
, cellsize
, nodata_value
, vat
, arcGridFromFile
, asciiGridParser
)
where
import Data.Char
import Text.Parsec
import Text.Parsec.ByteString
import Text.ParserCombinators.Parsec.Numeric
data ArcGrid = ArcGrid
{ ncols :: Int
, nrows :: Int
, xllcorner :: Maybe Float
, yllcorner :: Maybe Float
, xllcenter :: Maybe Float
, yllcenter :: Maybe Float
, cellsize :: Float
, nodata_value :: Maybe Int
, vat :: [Int]
} deriving (Show)
arcGridFromFile :: String -> IO ArcGrid
arcGridFromFile fname = do
e <- parseFromFile asciiGridParser fname
case e of
Left err -> error $ show err
Right ag -> return ag
wordParser :: String -> Parser ()
wordParser str =
let ucstr = map toUpper str
lcstr = map toLower str
in
do
string str <|> string ucstr <|> string lcstr
return ()
pvDelimParser :: Parser ()
pvDelimParser = do
many1 (char ' ' <|> char '\t')
return ()
pvLineParser :: String -> Parser a -> Parser a
pvLineParser param vparser = do
wordParser param
pvDelimParser
val <- vparser
endOfLine
return val
cllParser :: Char -> Parser (Maybe Float, Maybe Float)
cllParser c =
let cllcorner = c : "llcorner"
cllcenter = c : "llcenter"
in
do
mbcor <- optionMaybe $ try $ pvLineParser cllcorner floating
case mbcor of
Just val -> return (Just val, Nothing)
Nothing -> do
val <- pvLineParser cllcenter floating
return (Nothing, Just val)
vatParser :: Int -> Parser [Int]
vatParser n = do
as <- count n $ do
spaces
a <- int
space <|> newline
return a
return as
asciiGridParser :: Parser ArcGrid
asciiGridParser = do
_ncols <- pvLineParser "ncols" decimal
_nrows <- pvLineParser "nrows" decimal
(_xllcorner, _xllcenter) <- cllParser 'x'
(_yllcorner, _yllcenter) <- cllParser 'y'
_cellsize <- pvLineParser "cellsize" floating
_nodata_value <- optionMaybe $ pvLineParser "NODATA_value" int
_vat <- vatParser (_ncols * _nrows)
return $ ArcGrid
{ ncols = _ncols
, nrows = _nrows
, xllcorner = _xllcorner
, yllcorner = _yllcorner
, xllcenter = _xllcenter
, yllcenter = _yllcenter
, cellsize = _cellsize
, nodata_value = _nodata_value
, vat = _vat
}