mapalgebra: Efficient, polymorphic Map Algebra.

[ bsd3, geography, library ] [ Propose Tags ] [ Report a vulnerability ]

Efficient, polymorphic Map Algebra.

This library is an implementation of Map Algebra as described in the book GIS and Cartographic Modeling by Dana Tomlin. The fundamental type, the Raster, is typesafe. Rasters of different size and projection are considered different types, and so cannot be combined mistakenly in any way.

Also featured are op fusion (i.e. "lazy Rasters"), extremely fast Focal Operations, and typesafe NoData handling. Please see the main module for a more detailed introduction.


[Skip to Readme]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.1.0, 0.1.1, 0.1.2, 0.2.0, 0.2.1
Change log CHANGELOG.md
Dependencies base (>=4.10 && <4.14), bytestring, containers, deepseq, hmatrix (>=0.18 && <0.21), massiv (>=0.4.3 && <0.5), massiv-io (>=0.1 && <0.2), vector (>=0.11 && <0.13) [details]
License BSD-3-Clause
Copyright 2018 - 2020 Colin Woodbury
Author Colin Woodbury
Maintainer colin@fosskers.ca
Category Geography
Home page https://github.com/fosskers/mapalgebra
Uploaded by fosskers at 2020-02-02T22:17:26Z
Distributions
Reverse Dependencies 1 direct, 0 indirect [details]
Downloads 2258 total (17 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2020-02-03 [all 1 reports]

Readme for mapalgebra-0.2.1

[back to package description]

mapalgebra

Efficient, polymorphic Map Algebra for Haskell.

This library is an implementation of Map Algebra as described in the book GIS and Cartographic Modeling by Dana Tomlin. The fundamental primitive is the Raster, a rectangular grid of data that usually describes some area on the earth.

mapalgebra is built on top of massiv, a powerful Parallel Array library by Alexey Kuleshevich.

Usage

Always compile with -threaded, -O2, and -with-rtsopts=-N for best performance.

The Raster Type

This library provides Rasters which are lazy, polymorphic, and typesafe. They can hold any kind of data, and are aware of their projection and dimensions at the type level. This means that imagery of different size or projection are considered completely different types, which prevents an entire class of bugs.

Rasters have types signatures like this:

-- | A Raster of Ints backed by efficient byte-packed arrays, encoded
-- via the `Storable` typeclass. `P` (Prim), `U` (Unbox) and `B` (boxed) are also available.
--
-- This is either a freshly read image, or the result of evaluating a "delayed"
-- (`D` or `DW`) Raster.
Raster S LatLng 256 256 Int

-- | A "delayed" Raster of bytes. Likely the result of some Local Operation.
-- Waiting to be evaluated by the `strict` function.
Raster D WebMercator 512 512 Word8

-- | A "windowed" Raster of an ADT, the result of some Focal Operation.
-- Waiting to be evaluated by the `strict` function.
--
-- A generic `p` means we don't care about Projection here.
Raster DW p 1024 1024 (Maybe Double)

Reading Imagery

mapalgebra can currently read any image file of any value type, so long as it is grayscale (singleband) or RGBA. True multiband rasters (like from LandSat) are not yet supported.

To read a Raster:

-- | You must know the image dimensions ahead of time. If you don't care
-- about the projection, then `p` can be left generic.
getRaster :: IO (Raster S p 512 512 Word8)
getRaster = do
  erast <- fromGray "path/to/image.tif"
  case erast of
    Left err -> ... -- deal with the error.
    Right r  -> pure r

Colouring and Viewing Imagery

To quickly view a Raster you're working on, use the display function:

-- | Simplified type signature.
display :: Raster D p r c a -> IO ()

This will automatically colour gray, evaluate, and display your Raster using your OS's default image viewer.

To colour a Raster gray yourself, use grayscale:

grayscale :: Functor (Raster u p r c) => Raster u p r c a -> Raster u p r c (Pixel Y a)

True colouring is done with the classify function and colour ramps inspired by Gretchen N. Peterson's book Cartographer's Toolkit.

-- | Both `Raster D` and `Raster DW` are Functors, so this function works on
-- either of them. `Raster S`, etc., do not form Functors by design.
classify :: (Ord a, Functor f) => b -> Map a b -> f a -> f b

-- | An invisible pixel (alpha channel set to 0) to be passed
-- to `classify` as a default.
invisible :: Pixel RGBA Word8

-- | Given a list of "breaks", forms a colour ramp to be passed
-- to `classify`.
spectrum :: Ord k => [k] -> Map k (Pixel RGBA Word8)

We can generate the breaks via histogram and breaks (currently supports Word8 only):

-- | Input here is `Raster S`, meaning this image was freshly read,
-- or has already been fully computed with `strict`.
colourIt :: Raster S p r c Word8 -> Raster S p r c (Pixel RGBA Word8)
colourIt r = strict S . classify invisible cm $ lazy r
  where cm = spectrum . breaks $ histogram r

Before-and-after:

original image colour via spectrum ramp

Local Operations

All Local Operations defined in GIS and Cartographic Modeling are available. For the usual math ops, Raster D has a Num instance:

rast :: Raster D p 512 512 Int

squared :: Raster D p 512 512 Int
squared = rast * rast  -- Element-wise multiplication.

Focal Operations

Except for Focal Ranking and Focal Insularity, all Focal Operations of immedatiate neighbourhoods are provided:

rast :: Raster S p 512 512 Double

-- | `Raster DW` forms a Functor, so we can do simple unary transformations
-- (like colouring!) to it after Focal Ops.
averagedPlusAbit :: Raster S p 512 512 Double
averagedPlusAbit = strict S . fmap (+1) $ fmean rast

Typesafe NoData Handling

If it's known that your images have large areas of NoData, consider that Maybe has a Monoid instance:

import Data.Monoid (Sum(..))

nodatafsum :: Raster S p r c Word8 -> Raster DW p r c 512 Word8
nodatafsum = fmap (maybe 0 getSum) . fmonoid . strict B . fmap check . lazy
  where check 0 = Nothing
        check n = Just $ Sum n

In theory, one could construct special newtype wrappers with Monoid instances that handle any Focal scenario imaginable.

Future Work

  • Projection handling at IO time
  • Histogram generation for more data types
  • Reprojections
  • Extended neighbourhoods for Focal Ops
  • Upsampling and Downsampling
  • Improved NoData handling