{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
module Data.Geospatial.Internal.GeoFeatureCollection (
GeoFeatureCollection(..)
, boundingbox
, geofeatures
) where
import Data.Geospatial.Internal.BasicTypes
import Data.Geospatial.Internal.GeoFeature
import Data.Geospatial.Internal.Geometry.Aeson
import Control.Applicative ((<$>), (<*>))
import Control.Lens (makeLenses)
import Control.Monad (mzero)
import Data.Aeson (FromJSON (..),
ToJSON (..),
Value (..), object,
(.:), (.:?), (.=))
import Data.List ((++))
import Data.Maybe (Maybe (..))
import qualified Data.Sequence as Sequence
import Data.Text (Text)
import Prelude (Eq (..), Show, ($))
data GeoFeatureCollection a = GeoFeatureCollection
{ _boundingbox :: Maybe BoundingBoxWithoutCRS
, _geofeatures :: Sequence.Seq (GeoFeature a)
} deriving (Show, Eq)
makeLenses ''GeoFeatureCollection
instance (FromJSON a) => FromJSON (GeoFeatureCollection a) where
parseJSON (Object obj) = do
objType <- obj .: ("type" :: Text)
if objType /= ("FeatureCollection" :: Text)
then
mzero
else
GeoFeatureCollection
<$> obj .:? ("bbox" :: Text)
<*> obj .: ("features" :: Text)
parseJSON _ = mzero
instance (ToJSON a) => ToJSON (GeoFeatureCollection a) where
toJSON (GeoFeatureCollection bbox' features) = object $ baseAttributes ++ optAttributes "bbox" bbox'
where
baseAttributes = ["type" .= ("FeatureCollection" :: Text), "features" .= features]