-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathMapOptionsStore.ts
More file actions
226 lines (213 loc) · 8.01 KB
/
MapOptionsStore.ts
File metadata and controls
226 lines (213 loc) · 8.01 KB
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
import Store from '@/stores/Store'
import { Action } from '@/stores/Dispatcher'
import {
MapIsLoaded,
SelectMapLayer,
ToggleExternalMVTLayer,
ToggleRoutingGraph,
ToggleUrbanDensityLayer,
UpdateSettings,
} from '@/actions/Actions'
import config from 'config'
const osApiKey = config.keys.omniscale
const mapTilerKey = config.keys.maptiler
const thunderforestApiKey = config.keys.thunderforest
const kurvigerApiKey = config.keys.kurviger
const osmAttribution =
'© <a href="https://www.openstreetmap.org/copyright" target="_blank">OpenStreetMap</a> contributors'
export interface MapOptionsStoreState {
styleOptions: StyleOption[]
selectedStyle: StyleOption
isMapLoaded: boolean
routingGraphEnabled: boolean
urbanDensityEnabled: boolean
externalMVTEnabled: boolean
}
export interface StyleOption {
name: string
type: 'raster' | 'vector'
url: string[] | string
attribution: string
maxZoom?: number
}
export interface RasterStyle extends StyleOption {
type: 'raster'
url: string[]
tilePixelRatio?: number
}
export interface VectorStyle extends StyleOption {
type: 'vector'
url: string
}
const mediaQuery =
'(-webkit-min-device-pixel-ratio: 1.5),(min--moz-device-pixel-ratio: 1.5),(-o-min-device-pixel-ratio: 3/2),(min-resolution: 1.5dppx)'
const isRetina = window.devicePixelRatio > 1 || (window.matchMedia && window.matchMedia(mediaQuery).matches)
const tilePixelRatio = isRetina ? 2 : 1
const retina2x = isRetina ? '@2x' : ''
const mapTilerSatellite: VectorStyle = {
name: 'MapTiler Satellite',
type: 'vector',
url: 'https://api.maptiler.com/maps/hybrid/style.json?key=' + mapTilerKey,
attribution: osmAttribution + ', © <a href="https://www.maptiler.com/copyright/" target="_blank">MapTiler</a>',
}
const osmOrg: RasterStyle = {
name: 'OpenStreetMap',
type: 'raster',
url: ['https://tile.openstreetmap.org/{z}/{x}/{y}.png'],
attribution: osmAttribution,
maxZoom: 19,
}
const osmCycl: RasterStyle = {
name: 'Cyclosm',
type: 'raster',
url: [
'https://a.tile-cyclosm.openstreetmap.fr/cyclosm/{z}/{x}/{y}.png',
'https://b.tile-cyclosm.openstreetmap.fr/cyclosm/{z}/{x}/{y}.png',
'https://c.tile-cyclosm.openstreetmap.fr/cyclosm/{z}/{x}/{y}.png',
],
attribution:
osmAttribution +
', © <a href="https://github.com/cyclosm/cyclosm-cartocss-style/releases" target="_blank">CyclOSM</a>',
maxZoom: 19,
}
const omniscale: RasterStyle = {
name: 'Omniscale',
type: 'raster',
url: [
'https://maps.omniscale.net/v2/' + osApiKey + '/style.default/{z}/{x}/{y}.png' + (isRetina ? '?hq=true' : ''),
],
attribution: osmAttribution + ', © <a href="https://maps.omniscale.com/" target="_blank">Omniscale</a>',
tilePixelRatio: tilePixelRatio,
}
const esriSatellite: RasterStyle = {
name: 'Esri Satellite',
type: 'raster',
url: ['https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}'],
attribution:
'© <a href="http://www.esri.com/" target="_blank">Esri</a>' +
' i-cubed, USDA, USGS, AEX, GeoEye, Getmapping, Aerogrid, IGN, IGP, UPR-EGP, and the GIS User Community',
maxZoom: 18,
}
const tfTransport: RasterStyle = {
name: 'TF Transport',
type: 'raster',
url: [
'https://a.tile.thunderforest.com/transport/{z}/{x}/{y}' + retina2x + '.png?apikey=' + thunderforestApiKey,
'https://b.tile.thunderforest.com/transport/{z}/{x}/{y}' + retina2x + '.png?apikey=' + thunderforestApiKey,
'https://c.tile.thunderforest.com/transport/{z}/{x}/{y}' + retina2x + '.png?apikey=' + thunderforestApiKey,
],
attribution:
osmAttribution +
', <a href="https://www.thunderforest.com/maps/transport/" target="_blank">Thunderforest Transport</a>',
tilePixelRatio: tilePixelRatio,
}
const tfCycle: RasterStyle = {
name: 'TF Cycle',
type: 'raster',
url: [
'https://a.tile.thunderforest.com/cycle/{z}/{x}/{y}' + retina2x + '.png?apikey=' + thunderforestApiKey,
'https://b.tile.thunderforest.com/cycle/{z}/{x}/{y}' + retina2x + '.png?apikey=' + thunderforestApiKey,
'https://c.tile.thunderforest.com/cycle/{z}/{x}/{y}' + retina2x + '.png?apikey=' + thunderforestApiKey,
],
attribution:
osmAttribution +
', <a href="https://www.thunderforest.com/maps/opencyclemap/" target="_blank">Thunderforest Cycle</a>',
tilePixelRatio: tilePixelRatio,
}
const tfOutdoors: RasterStyle = {
name: 'TF Outdoors',
type: 'raster',
url: [
'https://a.tile.thunderforest.com/outdoors/{z}/{x}/{y}' + retina2x + '.png?apikey=' + thunderforestApiKey,
'https://b.tile.thunderforest.com/outdoors/{z}/{x}/{y}' + retina2x + '.png?apikey=' + thunderforestApiKey,
'https://c.tile.thunderforest.com/outdoors/{z}/{x}/{y}' + retina2x + '.png?apikey=' + thunderforestApiKey,
],
attribution:
osmAttribution +
', <a href="https://www.thunderforest.com/maps/outdoors/" target="_blank">Thunderforest Outdoors</a>',
tilePixelRatio: tilePixelRatio,
}
const mapillion: VectorStyle = {
name: 'Mapilion',
type: 'vector',
url: 'https://tiles.mapilion.com/assets/osm-bright/style.json?key=' + kurvigerApiKey,
attribution:
osmAttribution +
', © <a href="https://mapilion.com/attribution" target="_blank">Mapilion</a> <a href="http://www.openmaptiles.org/" target="_blank">© OpenMapTiles</a>',
}
const wanderreitkarte: RasterStyle = {
name: 'WanderReitKarte',
type: 'raster',
url: [
'https://topo.wanderreitkarte.de/topo/{z}/{x}/{y}.png',
'https://topo2.wanderreitkarte.de/topo/{z}/{x}/{y}.png',
'https://topo3.wanderreitkarte.de/topo/{z}/{x}/{y}.png',
'https://topo4.wanderreitkarte.de/topo/{z}/{x}/{y}.png',
],
attribution: osmAttribution + ', <a href="https://wanderreitkarte.de" target="_blank">WanderReitKarte</a>',
maxZoom: 18,
}
const styleOptions: StyleOption[] = [
omniscale,
osmOrg,
osmCycl,
esriSatellite,
mapTilerSatellite,
tfTransport,
tfCycle,
tfOutdoors,
mapillion,
wanderreitkarte,
]
export default class MapOptionsStore extends Store<MapOptionsStoreState> {
constructor() {
super(MapOptionsStore.getInitialState())
}
private static getInitialState(): MapOptionsStoreState {
const selectedStyle = styleOptions.find(s => s.name === config.defaultTiles)
if (!selectedStyle)
console.warn(
`Could not find tile layer specified in config: '${config.defaultTiles}', using default instead`
)
return {
selectedStyle: selectedStyle ? selectedStyle : omniscale,
styleOptions,
routingGraphEnabled: false,
urbanDensityEnabled: false,
externalMVTEnabled: false,
isMapLoaded: false,
}
}
reduce(state: MapOptionsStoreState, action: Action): MapOptionsStoreState {
if (action instanceof SelectMapLayer) {
const styleOption = state.styleOptions.find(o => o.name === action.layer)
if (styleOption)
return {
...state,
selectedStyle: styleOption,
}
} else if (action instanceof UpdateSettings) {
} else if (action instanceof ToggleRoutingGraph) {
return {
...state,
routingGraphEnabled: action.routingGraphEnabled,
}
} else if (action instanceof ToggleUrbanDensityLayer) {
return {
...state,
urbanDensityEnabled: action.urbanDensityEnabled,
}
} else if (action instanceof ToggleExternalMVTLayer) {
return {
...state,
externalMVTEnabled: action.externalMVTLayerEnabled,
}
} else if (action instanceof MapIsLoaded) {
return {
...state,
isMapLoaded: true,
}
}
return state
}
}