{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE NoImplicitPrelude #-}
module Data.Geospatial.Internal.GeoFeatureCollection
(
GeoFeatureCollection (..),
boundingbox,
geofeatures,
)
where
import Control.Applicative ((<$>), (<*>))
import Control.Lens (makeLenses)
import Control.Monad (mzero)
import Data.Aeson
( FromJSON (..),
ToJSON (..),
Value (..),
object,
(.:),
(.:?),
(.=),
)
import Data.Geospatial.Internal.BasicTypes
import Data.Geospatial.Internal.GeoFeature
import Data.Geospatial.Internal.Geometry.Aeson
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
{ forall a. GeoFeatureCollection a -> Maybe BoundingBoxWithoutCRS
_boundingbox :: Maybe BoundingBoxWithoutCRS,
forall a. GeoFeatureCollection a -> Seq (GeoFeature a)
_geofeatures :: Sequence.Seq (GeoFeature a)
}
deriving (Int -> GeoFeatureCollection a -> ShowS
[GeoFeatureCollection a] -> ShowS
GeoFeatureCollection a -> String
(Int -> GeoFeatureCollection a -> ShowS)
-> (GeoFeatureCollection a -> String)
-> ([GeoFeatureCollection a] -> ShowS)
-> Show (GeoFeatureCollection a)
forall a. Show a => Int -> GeoFeatureCollection a -> ShowS
forall a. Show a => [GeoFeatureCollection a] -> ShowS
forall a. Show a => GeoFeatureCollection a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Show a => Int -> GeoFeatureCollection a -> ShowS
showsPrec :: Int -> GeoFeatureCollection a -> ShowS
$cshow :: forall a. Show a => GeoFeatureCollection a -> String
show :: GeoFeatureCollection a -> String
$cshowList :: forall a. Show a => [GeoFeatureCollection a] -> ShowS
showList :: [GeoFeatureCollection a] -> ShowS
Show, GeoFeatureCollection a -> GeoFeatureCollection a -> Bool
(GeoFeatureCollection a -> GeoFeatureCollection a -> Bool)
-> (GeoFeatureCollection a -> GeoFeatureCollection a -> Bool)
-> Eq (GeoFeatureCollection a)
forall a.
Eq a =>
GeoFeatureCollection a -> GeoFeatureCollection a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall a.
Eq a =>
GeoFeatureCollection a -> GeoFeatureCollection a -> Bool
== :: GeoFeatureCollection a -> GeoFeatureCollection a -> Bool
$c/= :: forall a.
Eq a =>
GeoFeatureCollection a -> GeoFeatureCollection a -> Bool
/= :: GeoFeatureCollection a -> GeoFeatureCollection a -> Bool
Eq)
makeLenses ''GeoFeatureCollection
instance (FromJSON a) => FromJSON (GeoFeatureCollection a) where
parseJSON :: Value -> Parser (GeoFeatureCollection a)
parseJSON (Object Object
obj) = do
objType <- Object
obj Object -> Key -> Parser Text
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"type"
if objType /= ("FeatureCollection" :: Text)
then mzero
else
GeoFeatureCollection
<$> obj .:? "bbox"
<*> obj .: "features"
parseJSON Value
_ = Parser (GeoFeatureCollection a)
forall a. Parser a
forall (m :: * -> *) a. MonadPlus m => m a
mzero
instance (ToJSON a) => ToJSON (GeoFeatureCollection a) where
toJSON :: GeoFeatureCollection a -> Value
toJSON (GeoFeatureCollection Maybe BoundingBoxWithoutCRS
bbox' Seq (GeoFeature a)
features) = [Pair] -> Value
object ([Pair] -> Value) -> [Pair] -> Value
forall a b. (a -> b) -> a -> b
$ [Pair]
baseAttributes [Pair] -> [Pair] -> [Pair]
forall a. [a] -> [a] -> [a]
++ Text -> Maybe BoundingBoxWithoutCRS -> [Pair]
forall a. ToJSON a => Text -> Maybe a -> [Pair]
optAttributes Text
"bbox" Maybe BoundingBoxWithoutCRS
bbox'
where
baseAttributes :: [Pair]
baseAttributes = [Key
"type" Key -> Text -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= (Text
"FeatureCollection" :: Text), Key
"features" Key -> Seq (GeoFeature a) -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Seq (GeoFeature a)
features]