-
Notifications
You must be signed in to change notification settings - Fork 0
/
Map.hs
265 lines (218 loc) · 7.37 KB
/
Map.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
{-# LANGUAGE ForeignFunctionInterface #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE JavaScriptFFI #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE DataKinds #-}
module Reflex.OpenLayers.Map (
MapConfig
, Map
, View
, Property
, HasAttributes(..)
, HasView (..)
, HasResolution (..)
, HasCenter (..)
, HasRotation (..)
, HasLayers (..)
, HasReady (..)
, HasInitialValue (..)
, HasSetValue (..)
, HasInteractions (..)
, HasZoomBounds (..)
, olMap
, constProperty
, Center (..)
, HasX (..)
, HasY (..)
, mkCenter
, module Reflex.OpenLayers.Collection
, module Reflex.OpenLayers.Projection
-- Re-exports
) where
import Reflex.OpenLayers.Layer
import Reflex.OpenLayers.Collection
import Reflex.OpenLayers.Projection
import Reflex.OpenLayers.Util
import Reflex
import Reflex.Dom
import Control.Lens (Lens', lens, to, makeFields, (^.), (^?!))
import Control.Concurrent
import Control.Monad
import Control.Monad.IO.Class (MonadIO(liftIO))
import Data.Default (Default)
import Data.Proxy
import Data.Text (Text)
import qualified Data.Map as M
import GHCJS.DOM.HTMLDivElement (castToHTMLDivElement)
import GHCJS.DOM.Element (toElement)
import GHCJS.DOM.Types (unElement)
import GHCJS.Marshal (ToJSVal(toJSVal))
import GHCJS.Marshal.Pure (PToJSVal(pToJSVal), PFromJSVal(pFromJSVal))
import GHCJS.Types (JSVal)
import GHCJS.Foreign.QQ
import Sigym4.Geometry
--
-- View
--
class HasPoint o a | o -> a where
point :: Lens' o a
class HasX o a | o -> a where
x :: Lens' o a
class HasY o a | o -> a where
y :: Lens' o a
newtype Center crs = Center { unCenter :: Point V2 crs}
deriving (Eq, Ord, Show)
mkCenter :: Projection -> Double -> Double -> WithSomeCrs Center
mkCenter (Projection crs) i j =
reifyCrs crs $ \(Proxy :: Proxy crs) ->
WithSomeCrs $ Center (Point (V2 i j) :: Point V2 crs)
instance PToJSVal (Center crs) where
pToJSVal (Center (Point (V2 a b))) = [jsu'|[`a, `b]|]
instance PFromJSVal (Center crs) where
pFromJSVal l = Center $ Point $ V2 [js'|`l[0]|] [js'|`l[1]|]
instance Default (Center crs) where
def = Center $ Point 0
instance HasPoint (Center crs) (Point V2 crs) where
point = lens unCenter (const Center)
instance HasX (Center crs) Double where
x = point.vertex._x
instance HasY (Center crs) Double where
y = point.vertex._y
instance Default (WithSomeCrs Center) where
def = WithSomeCrs (def :: Center SphericalMercator)
instance HasPoint (WithSomeCrs Center) (WithSomeCrs (Point V2)) where
point = lens (\(WithSomeCrs (Center p)) -> WithSomeCrs p)
(\_ (WithSomeCrs p) -> WithSomeCrs (Center p))
instance HasX (WithSomeCrs Center) Double where
x = point.vertex._x
instance HasY (WithSomeCrs Center) Double where
y = point.vertex._y
type Rotation = Double
data View t p
= View {
_viewCenter :: !(p t (WithSomeCrs Center))
, _viewResolution :: !(p t Double)
, _viewRotation :: !(p t Rotation)
, _viewZoomBounds :: Maybe (Int,Int) -- ^ Used instead of min/maxZoom since
-- both need to be specified
}
makeFields ''View
instance Reflex t => Default (View t Property) where
def = View {
_viewCenter = constProperty def
, _viewResolution = constProperty 100000
, _viewRotation = constProperty 0
, _viewZoomBounds = Nothing
}
--
-- MapConfig
--
data MapConfig t
= MapConfig {
_mapConfig_attributes :: Dynamic t (M.Map Text Text)
, _mapConfigView :: View t Property
, _mapConfigLayers :: Dynamic t (LayerSet (Layer t Property))
, _mapConfigInteractions :: M.Map Text Bool
}
makeFields ''MapConfig
instance HasCenter (MapConfig t) (Property t (WithSomeCrs Center)) where
center = view . center
instance HasResolution (MapConfig t) (Property t Double) where
resolution = view . resolution
instance HasRotation (MapConfig t) (Property t Rotation) where
rotation = view . rotation
instance HasZoomBounds (MapConfig t) (Maybe (Int,Int)) where
zoomBounds = view . zoomBounds
instance HasAttributes (MapConfig t) where
type Attrs (MapConfig t) = Dynamic t (M.Map Text Text)
attributes = lens _mapConfig_attributes (\o v -> o {_mapConfig_attributes=v})
instance Reflex t => Default (MapConfig t) where
def = MapConfig {
_mapConfig_attributes = constDyn mempty
, _mapConfigView = def
, _mapConfigLayers = constDyn def
, _mapConfigInteractions = mempty
}
data Map t
= Map {
_mapView :: View t Dynamic
, _mapLayers :: Dynamic t (LayerSet (Layer t Dynamic))
, mapJsVal :: JSVal
, _mapReady :: Event t ()
}
makeFields ''Map
instance PToJSVal (Map t) where
pToJSVal = mapJsVal
instance ToJSVal (Map t) where
toJSVal = return . pToJSVal
instance HasCenter (Map t) (Dynamic t (WithSomeCrs Center)) where
center = view . center
instance HasResolution (Map t) (Dynamic t Double) where
resolution = view . resolution
instance HasRotation (Map t) (Dynamic t Rotation) where
rotation = view . rotation
olMap
:: ( MonadWidget t m
, Attributes m (Dynamic t (M.Map Text Text)) t
) => MapConfig t -> m (Map t)
olMap cfg = do
(jv, v) <- mkView (cfg^.view)
(jg, g) <- mkLayer (group (cfg^?!layers))
let ics = map_toJSVal (cfg^.interactions)
target <- (unElement . toElement . castToHTMLDivElement)
<$> buildEmptyElement "div" (cfg^.attributes)
m <- liftIO $ [jsu|
$r = new ol.Map({
view:`jv
, layers:`jg
, target: `target
, interactions : ol.interaction.defaults(`ics)
});
|]
-- FIXME: This is a hack because when postBuild fires the element hasn't been
-- added to the DOM yet so it hasn't got a size for openlayers to update to.
-- There must be a better way to do this...
pb <- getPostBuild
eReady <- performEventAsync $ ffor pb $ const $ \cb ->
let loop = do
inDom <- [jsu|$r=document.body.contains(`target);|]
if not inDom then threadDelay 50000 >> loop
else [jsu_|`m.updateSize();|] >> cb ()
in void $ liftIO $ forkIO $ loop
return $ Map v (g^?!layers) m eReady
mkView
:: forall t m. MonadWidget t m
=> View t Property -> m (JSVal, View t Dynamic)
mkView v = do
let crs = getCrs (v^.center.initialValue)
jsProj <- toJSVal_projection (Projection crs)
let build :: WithSomeCrs Center -> m JSVal
build wc@(WithSomeCrs c) = do
jsSrc <- toJSVal_projection (Projection (getCrs wc))
liftIO [js|$r=ol.proj.transform(`c, `jsSrc, `jsProj);|]
unBuild c = reifyCrs crs $ \(Proxy :: Proxy crs) ->
return $ WithSomeCrs (pFromJSVal c :: Center crs)
minz = v^.zoomBounds.to (fmap fst)
maxz = v^.zoomBounds.to (fmap snd)
j <- liftIO [jsu|
$r = new ol.View(
{ projection: `jsProj
, minZoom: `minz || undefined
, maxZoom: `maxz || undefined
});
|]
_viewCenter <- initPropertyWith (Just unBuild) build "center" j (v^.center)
_viewResolution <- initProperty "resolution" j (v^.resolution)
_viewRotation <- initProperty "rotation" j (v^.rotation)
return (j, View { _viewZoomBounds=v^.zoomBounds
, ..
})