module Data.Geometry.LegacyVTK.Util (
vtkFromGeojson
) where
import Data.Scientific (Scientific)
import qualified Data.Geography.GeoJSON as J (FeatureCollection, Geometry(..), MultiPolygonGeometry(..), PolygonGeometry(..), coordinates, features, geometry)
import qualified Data.Geometry.LegacyVTK as V (Geometry(..), Point(..))
vtkFromGeojson
:: J.FeatureCollection
-> V.Geometry
vtkFromGeojson geojson =
V.PolyData
{
V.points = []
, V.vertices = []
, V.lines = []
, V.polygons = concatMap (map (map pointFromScientific) . outerBoundaries . J.geometry)
$ J.features geojson
, V.triangleStrips = []
}
pointFromScientific :: [Scientific] -> V.Point
pointFromScientific (x : y : z : _) =
V.Point
{
V.x = x
, V.y = y
, V.z = z
}
pointFromScientific (x : y : _) =
V.Point
{
V.x = x
, V.y = y
, V.z = 0
}
pointFromScientific _ = undefined
outerBoundaries :: J.Geometry -> [[[Scientific]]]
outerBoundaries (J.Polygon g) = [map J.coordinates $ J.exterior g]
outerBoundaries (J.MultiPolygon g) = map (map J.coordinates . J.exterior) $ J.polygons g
outerBoundaries _ = []