mirror of
https://github.com/a-nyx/maputnik-with-pmtiles.git
synced 2024-12-28 18:31:17 +01:00
Support TileJSON sources for OL3
This commit is contained in:
parent
104d6311ec
commit
3a3e90c3dc
2 changed files with 76 additions and 26 deletions
|
@ -1,7 +1,7 @@
|
||||||
import React from 'react'
|
import React from 'react'
|
||||||
import style from '../../libs/style.js'
|
import style from '../../libs/style.js'
|
||||||
import isEqual from 'lodash.isequal'
|
import isEqual from 'lodash.isequal'
|
||||||
|
import { loadJSON } from '../../libs/urlopen'
|
||||||
|
|
||||||
function suitableVectorSource(mapStyle) {
|
function suitableVectorSource(mapStyle) {
|
||||||
const sources = Object.keys(mapStyle.sources)
|
const sources = Object.keys(mapStyle.sources)
|
||||||
|
@ -11,19 +11,40 @@ function suitableVectorSource(mapStyle) {
|
||||||
source: mapStyle.sources[sourceId]
|
source: mapStyle.sources[sourceId]
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.filter(({source}) => source.type === 'vector' && source.tiles && source.tiles.length > 0)
|
.filter(({source}) => source.type === 'vector')
|
||||||
.filter(({source}) => source.tiles.length > 0)
|
|
||||||
return sources[0]
|
return sources[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
function toVectorLayer(source, tilegrid) {
|
function toVectorLayer(source, tilegrid, cb) {
|
||||||
const ol = require('openlayers')
|
function newMVTLayer(tileUrl) {
|
||||||
return new ol.layer.VectorTile({
|
const ol = require('openlayers')
|
||||||
source: new ol.source.VectorTile({
|
return new ol.layer.VectorTile({
|
||||||
format: new ol.format.MVT(),
|
source: new ol.source.VectorTile({
|
||||||
tileGrid: tilegrid,
|
format: new ol.format.MVT(),
|
||||||
tilePixelRatio: 8,
|
tileGrid: tilegrid,
|
||||||
url: source.tiles[0]
|
tilePixelRatio: 8,
|
||||||
|
url: tileUrl
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!source.tiles) {
|
||||||
|
sourceFromTileJSON(source.url, tileSource => {
|
||||||
|
cb(newMVTLayer(tileSource.tiles[0]))
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
cb(newMVTLayer(source.tiles[0]))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function sourceFromTileJSON(url, cb) {
|
||||||
|
loadJSON(url, null, tilejson => {
|
||||||
|
if(!tilejson) return
|
||||||
|
cb({
|
||||||
|
type: 'vector',
|
||||||
|
tiles: tilejson.tiles,
|
||||||
|
minzoom: tilejson.minzoom,
|
||||||
|
maxzoom: tilejson.maxzoom,
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -49,26 +70,36 @@ class OpenLayers3Map extends React.Component {
|
||||||
}
|
}
|
||||||
|
|
||||||
updateStyle(newMapStyle) {
|
updateStyle(newMapStyle) {
|
||||||
const oldSource = suitableVectorSource(this.props.mapStyle)
|
const oldSource = suitableVectorSource(this.props.mapStyle)
|
||||||
const newSource = suitableVectorSource(newMapStyle)
|
const newSource = suitableVectorSource(newMapStyle)
|
||||||
|
const resolutions = this.resolutions
|
||||||
|
|
||||||
if(newSource) {
|
function setStyleFunc(map, layer) {
|
||||||
if(!this.layer) {
|
|
||||||
this.layer = toVectorLayer(newSource.source, this.tilegrid)
|
|
||||||
this.map.addLayer(this.layer)
|
|
||||||
} else if(!isEqual(oldSource, newSource)) {
|
|
||||||
this.map.removeLayer(this.layer)
|
|
||||||
this.layer = toVectorLayer(newSource.source, this.tilegrid)
|
|
||||||
this.map.addLayer(this.layer)
|
|
||||||
}
|
|
||||||
const olms = require('ol-mapbox-style')
|
const olms = require('ol-mapbox-style')
|
||||||
const styleFunc = olms.getStyleFunction(newMapStyle, newSource.id, this.resolutions)
|
const styleFunc = olms.getStyleFunction(newMapStyle, newSource.id, resolutions)
|
||||||
this.layer.setStyle(styleFunc)
|
layer.setStyle(styleFunc)
|
||||||
//NOTE: We need to mark the source as changed in order
|
//NOTE: We need to mark the source as changed in order
|
||||||
//to trigger a rerender
|
//to trigger a rerender
|
||||||
this.layer.getSource().changed()
|
layer.getSource().changed()
|
||||||
this.map.render()
|
map.render()
|
||||||
|
}
|
||||||
|
|
||||||
|
if(newSource) {
|
||||||
|
if(this.layer && !isEqual(oldSource, newSource)) {
|
||||||
|
this.map.removeLayer(this.layer)
|
||||||
|
this.layer = null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(!this.layer) {
|
||||||
|
toVectorLayer(newSource.source, this.tilegrid, vectorLayer => {
|
||||||
|
this.layer = vectorLayer
|
||||||
|
this.map.addLayer(this.layer)
|
||||||
|
setStyleFunc(this.map, this.layer)
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
setStyleFunc(this.map, this.layer)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
componentWillReceiveProps(nextProps) {
|
componentWillReceiveProps(nextProps) {
|
||||||
|
|
|
@ -21,3 +21,22 @@ export function loadStyleUrl(styleUrl, cb) {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function loadJSON(url, defaultValue, cb) {
|
||||||
|
request({
|
||||||
|
url: url,
|
||||||
|
withCredentials: false,
|
||||||
|
}, (error, response, body) => {
|
||||||
|
if (!error && body && response.statusCode == 200) {
|
||||||
|
try {
|
||||||
|
cb(JSON.parse(body))
|
||||||
|
} catch(err) {
|
||||||
|
console.error(err)
|
||||||
|
cb(defaultValue)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
console.error('Can not load JSON from ' + url)
|
||||||
|
cb(defaultValue)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue