UNPKG

@loaders.gl/mvt

Version:

Loader for Mapbox Vector Tiles

42 lines (41 loc) 1.53 kB
// loaders.gl // SPDX-License-Identifier: MIT // Copyright (c) vis.gl contributors // Forked from https://github.com/mapbox/geojson-vt under compatible ISC license /** * Transforms the coordinates of each protoFeature in the given protoTile from * mercator-projected space into (extent x extent) protoTile space. */ export function transformTile(protoTile, extent) { if (protoTile.transformed) { return protoTile; } const z2 = 1 << protoTile.z; const tx = protoTile.x; const ty = protoTile.y; for (const protoFeature of protoTile.protoFeatures) { const geom = protoFeature.geometry; const simplifiedType = protoFeature.simplifiedType; protoFeature.geometry = []; if (simplifiedType === 1) { for (let j = 0; j < geom.length; j += 2) { protoFeature.geometry.push(transformPoint(geom[j], geom[j + 1], extent, z2, tx, ty)); } } else { for (let j = 0; j < geom.length; j++) { const ring = []; for (let k = 0; k < geom[j].length; k += 2) { ring.push(transformPoint(geom[j][k], geom[j][k + 1], extent, z2, tx, ty)); } protoFeature.geometry.push(ring); } } } protoTile.transformed = true; return protoTile; } // eslint-disable-next-line max-params function transformPoint(x, y, extent, z2, tx, ty) { return [Math.round(extent * (x * z2 - tx)), Math.round(extent * (y * z2 - ty))]; }