arcgrid (empty) → 0.1.0.0
raw patch · 6 files changed
+269/−0 lines, 6 filesdep +arcgriddep +basedep +parsecsetup-changed
Dependencies added: arcgrid, base, parsec, parsec-numeric
Files
- LICENSE +30/−0
- README.md +40/−0
- Setup.hs +2/−0
- app/Main.hs +20/−0
- arcgrid.cabal +35/−0
- src/ArcGrid.hs +142/−0
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Author name here (c) 2017++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of Author name here nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ README.md view
@@ -0,0 +1,40 @@+## Description+This is a parser for ESRI/ArcInfo (ArcGrid) files. These are raster GIS files+widely used by many geographics-related software to represent elevations or+terrain features.++Only ASCII GRID (.asc) files are supported at the moment.++In `app/valley.asc` there is a test elevation model of a small valley area. It+is parsed and printed by the `arcgrid-exe` if you do `stack exec arcgrid-exe`.++## Usage+The library exports the `ArcGrid` datatype and its accessors:++``` haskell+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]+ }+```++The main interface for reading of grid files is `arcGridFromFile`, but the+bytestring parser (to be used with Parsec) is also exported.++``` haskell+arcGridFromFile :: String -> IO ArcGrid++asciiGridParser :: Parser ArcGrid+```++## TODO:+- unparsing+- optimizations+- support of proprietary binary formats?
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ app/Main.hs view
@@ -0,0 +1,20 @@+module Main where++import System.Environment++import ArcGrid++main :: IO ()+main = do+ args <- getArgs+ case args of+ [] -> usage+ fnames -> mapM_ printFile fnames++usage :: IO ()+usage = do+ pname <- getProgName+ putStrLn $ "Usage: " ++ pname ++ " <file ...>"++printFile :: String -> IO ()+printFile fn = (arcGridFromFile fn >>= putStrLn . show)
+ arcgrid.cabal view
@@ -0,0 +1,35 @@+name: arcgrid+version: 0.1.0.0+synopsis: Parse ESRI/ArcInfo (ArcGrid) raster GIS files+description: A parser for ESRI/ArcInfo (ArcGrid) files. These are raster+ GIS files widely used by many geographics-related software+ to represent elevations or terrain features.+homepage: https://github.com/nbrk/arcgrid+license: BSD3+license-file: LICENSE+author: Nikolay Burkov+maintainer: nbrk@linklevel.net+category: Geo+build-type: Simple+extra-source-files: README.md+cabal-version: >=1.10++library+ hs-source-dirs: src+ exposed-modules: ArcGrid+ build-depends: base >= 4.7 && < 5+ , parsec+ , parsec-numeric+ default-language: Haskell2010++executable arcgrid-dump+ hs-source-dirs: app+ main-is: Main.hs+ ghc-options: -threaded -rtsopts -with-rtsopts=-N+ build-depends: base+ , arcgrid+ default-language: Haskell2010++source-repository head+ type: git+ location: https://github.com/nbrk/arcgrid
+ src/ArcGrid.hs view
@@ -0,0 +1,142 @@+-- | ESRI/ArcInfo ASCII Grid file format+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++{-+ - The ESRI ArcInfo ASCII Grid spec:+ - http://resources.esri.com/help/9.3/arcgisdesktop/com/gp_toolref/spatial_analyst_tools/esri_ascii_raster_format.htm+ -}+++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] -- value attribute table+ } 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+++-- | Both upper- and lower-case strings+wordParser :: String -> Parser ()+wordParser str =+ let ucstr = map toUpper str+ lcstr = map toLower str+ in+ do+ string str <|> string ucstr <|> string lcstr+ return ()++-- | Spaces and tabs+pvDelimParser :: Parser ()+pvDelimParser = do+ many1 (char ' ' <|> char '\t')+ return ()+++-- | Parameter-value line+pvLineParser :: String -> Parser a -> Parser a+pvLineParser param vparser = do+ wordParser param+ pvDelimParser+ val <- vparser+ endOfLine+ return val+++-- | Parse (x|y)llcorner xor (x|y)llcenter block. Returns (corner, center)+cllParser :: Char -> Parser (Maybe Float, Maybe Float)+cllParser c =+ let cllcorner = c : "llcorner"+ cllcenter = c : "llcenter"+ in+ do+ -- try "?llcorner" first without consuming the output+ mbcor <- optionMaybe $ try $ pvLineParser cllcorner floating+ case mbcor of+ Just val -> return (Just val, Nothing)+ Nothing -> do+ -- read "?llcenter" then+ val <- pvLineParser cllcenter floating+ return (Nothing, Just val)+++-- -- | Parse VAT row+-- vatLineParser :: Int -> Parser [Int]+-- vatLineParser ncols = do+-- skipMany space+-- as <- count ncols $ do+-- a <- int+-- space <|> newline+-- return a++-- return as++vatParser :: Int -> Parser [Int]+vatParser n = do+ as <- count n $ do+ spaces+ a <- int+ space <|> newline+ return a+ return as+++-- | The parser+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+ }+