-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use the pmtiles-protocol package for the pmtiles example
- Loading branch information
Showing
3 changed files
with
19 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,7 @@ | ||
import 'ol/ol.css'; | ||
import {PMTiles} from 'pmtiles'; | ||
import {apply} from 'ol-mapbox-style'; | ||
import {register as registerPMTiles} from 'pmtiles-protocol'; | ||
|
||
// Generic transformRequest function that can be used with any style | ||
// that uses pmtiles:// URLs for sources and tiles | ||
let styleUrl; | ||
const pmtilesByUrl = {}; | ||
const tileUrlRegex = /^pmtiles:\/\/(.+)\/([0-9]+)\/([0-9]+)\/([0-9]+).mvt$/; | ||
const tileCoordRegex = /\/([0-9]+)\/([0-9]+)\/([0-9]+).mvt$/; | ||
const transformRequest = async (url, type) => { | ||
// Workaround for broken URL handling in Safari | ||
url = url.replace(/^pmtiles:\/\/http(s?)\/\//, 'pmtiles://http$1://'); | ||
registerPMTiles(); | ||
|
||
if (type === 'Style') { | ||
styleUrl = url; | ||
} | ||
/** @type {PMTiles} */ | ||
let pmtiles; | ||
if (url.startsWith('pmtiles://')) { | ||
const baseUrl = url.slice(10).replace(tileCoordRegex, ''); | ||
if (!pmtilesByUrl[baseUrl]) { | ||
pmtilesByUrl[baseUrl] = new PMTiles( | ||
new URL(url.slice(10), styleUrl).href, | ||
); | ||
} | ||
pmtiles = pmtilesByUrl[baseUrl]; | ||
} | ||
if (!pmtiles) { | ||
return url; | ||
} | ||
if (type === 'Source') { | ||
const tileJson = await pmtiles.getTileJson(url); | ||
return `data:application/json,${encodeURIComponent( | ||
JSON.stringify(tileJson), | ||
)}`; | ||
} | ||
if (type === 'Tiles') { | ||
const [, baseUrl, z, x, y] = url.match(tileUrlRegex); | ||
const tileResult = await pmtilesByUrl[baseUrl].getZxy( | ||
Number(z), | ||
Number(x), | ||
Number(y), | ||
); | ||
const data = tileResult?.data ?? new ArrayBuffer(0); | ||
const objectUrl = URL.createObjectURL(new Blob([data])); | ||
window.setTimeout(() => URL.revokeObjectURL(objectUrl), 10000); | ||
return objectUrl; | ||
} | ||
return url; | ||
}; | ||
|
||
apply('map', 'data/protomaps-dark-style.json', { | ||
transformRequest, | ||
}); | ||
apply('map', 'data/protomaps-dark-style.json'); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters