module Geography.VectorTile.Raw
(
RawVectorTile(..)
, RawLayer(..)
, RawVal(..)
, RawFeature(..)
, GeomType(..)
, Geom(..)
, decode
, encode
, decodeIO
, encodeIO
) where
import Control.DeepSeq (NFData)
import qualified Data.ByteString as BS
import Data.Int
import Data.ProtocolBuffers hiding (decode, encode)
import Data.Serialize.Get
import Data.Serialize.Put
import Data.Text (Text, pack)
import Data.Word
import GHC.Generics (Generic)
import qualified Geography.VectorTile.Geometry as G
data RawVectorTile = RawVectorTile { layers :: Repeated 3 (Message RawLayer) }
deriving (Generic,Show,Eq)
instance Encode RawVectorTile
instance Decode RawVectorTile
instance NFData RawVectorTile
data RawLayer = RawLayer { version :: Required 15 (Value Word32)
, name :: Required 1 (Value Text)
, features :: Repeated 2 (Message RawFeature)
, keys :: Repeated 3 (Value Text)
, values :: Repeated 4 (Message RawVal)
, extent :: Optional 5 (Value Word32)
} deriving (Generic,Show,Eq)
instance Encode RawLayer
instance Decode RawLayer
instance NFData RawLayer
data RawVal = RawVal { string :: Optional 1 (Value Text)
, float :: Optional 2 (Value Float)
, double :: Optional 3 (Value Double)
, int64 :: Optional 4 (Value Int64)
, uint64 :: Optional 5 (Value Word64)
, sint :: Optional 6 (Value (Signed Int64))
, bool :: Optional 7 (Value Bool)
} deriving (Generic,Show,Eq)
instance Encode RawVal
instance Decode RawVal
instance NFData RawVal
data RawFeature = RawFeature { featureId :: Optional 1 (Value Word64)
, tags :: Packed 2 (Value Word32)
, geom :: Optional 3 (Enumeration GeomType)
, geometries :: Packed 4 (Value Word32)
} deriving (Generic,Show,Eq)
instance Encode RawFeature
instance Decode RawFeature
instance NFData RawFeature
data GeomType = Unknown | Point | LineString | Polygon
deriving (Generic,Enum,Show,Eq)
instance Encode GeomType
instance Decode GeomType
instance NFData GeomType
class G.Geometry g => Geom g where
geomType :: g -> GeomType
instance Geom G.Point where
geomType _ = Point
instance Geom G.LineString where
geomType _ = LineString
instance Geom G.Polygon where
geomType _ = Polygon
decode :: BS.ByteString -> Either Text RawVectorTile
decode bs = case runGet decodeMessage bs of
Left e -> Left $ pack e
Right vt -> Right vt
encode :: RawVectorTile -> BS.ByteString
encode = runPut . encodeMessage
decodeIO :: FilePath -> IO (Either Text RawVectorTile)
decodeIO = fmap decode . BS.readFile
encodeIO :: RawVectorTile -> FilePath -> IO ()
encodeIO vt fp = BS.writeFile fp $ encode vt