Initial commit

This commit is contained in:
2026-04-15 17:08:39 +02:00
parent ae164c47a8
commit 47fd1c2b7a
1819 changed files with 685388 additions and 0 deletions

56
node_modules/@maplibre/vt-pbf/LICENSE generated vendored Normal file
View File

@@ -0,0 +1,56 @@
The MIT License (MIT)
Copyright (c) 2025 MapLibre contributors
Copyright (c) 2015 Anand Thakker
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
--------------------------------------------------------------------------------
Contains geojson_wrapper.js from https://github.com/mapbox/mapbox-gl-js
Copyright (c) 2014, Mapbox
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of Mapbox GL JS nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

54
node_modules/@maplibre/vt-pbf/README.md generated vendored Normal file
View File

@@ -0,0 +1,54 @@
[![MapLibre Logo](https://maplibre.org/img/maplibre-logo-big.svg)](https://maplibre.org/)
# vt-pbf
[![NPM Version](https://img.shields.io/npm/v/@maplibre/vt-pbf.svg)](https://www.npmjs.com/package/@maplibre/vt-pbf)
Serialize [Mapbox vector tiles](https://github.com/mapbox/vector-tile-spec) to binary protobufs in javascript.
## Installation
Using NPM: `npm install @maplibre/vt-pbf`
Or `npm run build` and find build artifacts in `dist/`
## Usage
As far as I know, the two places you might get a JS representation of a vector
tile are [geojson-vt](https://github.com/mapbox/geojson-vt) and
[vector-tile-js](https://github.com/mapbox/vector-tile-js). These both use
slightly different internal representations, so serializing each looks slightly
different:
## From vector-tile-js
```javascript
import {fromVectorTileJs} from 'vt-pbf'
import {VectorTile} = from '@mapbox/vector-tile'
import Protobuf from 'pbf'
var data = fs.readFileSync(__dirname + '/fixtures/rectangle-1.0.0.pbf')
var tile = new VectorTile(new Protobuf(data))
var orig = tile.layers['geojsonLayer'].feature(0).toGeoJSON(0, 0, 1)
var buff = fromVectorTileJs(tile)
fs.writeFileSync('my-tile.pbf', buff)
```
## From geojson-vt
```javascript
import {fromGeojsonVt} from 'vt-pbf'
import geojsonVt from '@maplibre/geojson-vt'
var orig = JSON.parse(fs.readFileSync(__dirname + '/fixtures/rectangle.geojson'))
var tileindex = geojsonVt(orig)
var tile = tileindex.getTile(1, 0, 0)
// pass in an object mapping layername -> tile object
var buff = fromGeojsonVt({ 'geojsonLayer': tile })
fs.writeFileSync('my-tile.pbf', buff)
```
`fromGeojsonVt` takes two arguments:
- `layerMap` is an object where keys are layer names and values are a geojson-vt tile,
- `options` is an object (optional argument). There are 2 supported keys: `version` to define the version of the mvt spec used and `extent` to define the extent of the tile. `version` defaults to 1 and `extent` to 4096.

20
node_modules/@maplibre/vt-pbf/dist/index.d.ts generated vendored Normal file
View File

@@ -0,0 +1,20 @@
import { type Feature, GEOJSON_TILE_LAYER_NAME, type GeoJSONOptions, GeoJSONWrapper } from "./lib/geojson_wrapper";
import type { GeoJSONVTTile } from '@maplibre/geojson-vt';
import type { VectorTileFeatureLike, VectorTileLike, VectorTileLayerLike } from './lib/types';
/**
* Serialize a vector-tile-js-created tile to pbf
*
* @param tile - the tile to serialize
* @param jsonPrefix - a string prefix to prepend to JSON-stringified non-primitive property values, used to distinguish them from regular string values when parsing the tile later. Default is "".
* @return uncompressed, pbf-serialized tile data
*/
export declare function fromVectorTileJs(tile: VectorTileLike, jsonPrefix?: string): Uint8Array;
/**
* Serialized a geojson-vt-created tile to pbf.
*
* @param layers - An object mapping layer names to geojson-vt-created vector tile objects
* @param options - An object specifying the vector-tile specification version and extent that were used to create `layers`.
* @return uncompressed, pbf-serialized tile data
*/
export declare function fromGeojsonVt(layers: Record<string, GeoJSONVTTile>, options?: GeoJSONOptions): Uint8Array;
export { GeoJSONWrapper, GeoJSONOptions, Feature, GEOJSON_TILE_LAYER_NAME, VectorTileFeatureLike, VectorTileLike, VectorTileLayerLike, };

206
node_modules/@maplibre/vt-pbf/dist/index.es.js generated vendored Normal file
View File

@@ -0,0 +1,206 @@
import Pbf from 'pbf';
import Point from '@mapbox/point-geometry';
class FeatureWrapper {
constructor(feature, extent) {
this.feature = feature;
this.type = feature.type;
this.properties = feature.tags ? feature.tags : {};
this.extent = extent;
// If the feature has a top-level `id` property, copy it over, but only
// if it can be coerced to an integer, because this wrapper is used for
// serializing geojson feature data into vector tile PBF data, and the
// vector tile spec only supports integer values for feature ids --
// allowing non-integer values here results in a non-compliant PBF
// that causes an exception when it is parsed with vector-tile-js
if ('id' in feature) {
if (typeof feature.id === 'string') {
this.id = parseInt(feature.id, 10);
}
else if (typeof feature.id === 'number' && !isNaN(feature.id)) {
this.id = feature.id;
}
}
}
loadGeometry() {
const geometry = [];
const rawGeo = this.feature.type === 1 ? [this.feature.geometry] : this.feature.geometry;
for (const ring of rawGeo) {
const newRing = [];
for (const point of ring) {
newRing.push(new Point(point[0], point[1]));
}
geometry.push(newRing);
}
return geometry;
}
}
const GEOJSON_TILE_LAYER_NAME = "_geojsonTileLayer";
class GeoJSONWrapper {
constructor(features, options) {
this.layers = { [GEOJSON_TILE_LAYER_NAME]: this };
this.name = GEOJSON_TILE_LAYER_NAME;
this.version = options ? options.version : 1;
this.extent = options ? options.extent : 4096;
this.length = features.length;
this.features = features;
}
feature(i) {
return new FeatureWrapper(this.features[i], this.extent);
}
}
/**
* Serialize a vector-tile-js-created tile to pbf
*
* @param tile - the tile to serialize
* @param jsonPrefix - a string prefix to prepend to JSON-stringified non-primitive property values, used to distinguish them from regular string values when parsing the tile later. Default is "".
* @return uncompressed, pbf-serialized tile data
*/
function fromVectorTileJs(tile, jsonPrefix = "") {
const out = new Pbf();
writeTile(tile, out, jsonPrefix);
return out.finish();
}
/**
* Serialized a geojson-vt-created tile to pbf.
*
* @param layers - An object mapping layer names to geojson-vt-created vector tile objects
* @param options - An object specifying the vector-tile specification version and extent that were used to create `layers`.
* @return uncompressed, pbf-serialized tile data
*/
function fromGeojsonVt(layers, options) {
const l = {};
// eslint-disable-next-line @typescript-eslint/no-for-in-array
for (const k in layers) {
l[k] = new GeoJSONWrapper(layers[k].features, options);
l[k].name = k;
l[k].version = options ? options.version : 1;
l[k].extent = options ? options.extent : 4096;
}
return fromVectorTileJs({ layers: l });
}
function writeTile(tile, pbf, jsonPrefix = "") {
for (const key in tile.layers) {
pbf.writeMessage(3, (layer, pbf) => writeLayer(layer, pbf, jsonPrefix), tile.layers[key]);
}
}
function writeLayer(layer, pbf, jsonPrefix = "") {
pbf.writeVarintField(15, layer.version || 1);
pbf.writeStringField(1, layer.name || '');
pbf.writeVarintField(5, layer.extent || 4096);
const context = {
jsonPrefix,
keys: [],
values: [],
keycache: {},
valuecache: {}
};
for (let i = 0; i < layer.length; i++) {
context.feature = layer.feature(i);
pbf.writeMessage(2, writeFeature, context);
}
const keys = context.keys;
for (const key of keys) {
pbf.writeStringField(3, key);
}
const values = context.values;
for (const value of values) {
pbf.writeMessage(4, writeValue, value);
}
}
function writeFeature(context, pbf) {
if (!context.feature) {
return;
}
const feature = context.feature;
if (feature.id !== undefined) {
pbf.writeVarintField(1, feature.id);
}
pbf.writeMessage(2, writeProperties, context);
pbf.writeVarintField(3, feature.type);
pbf.writeMessage(4, writeGeometry, feature);
}
function writeProperties(context, pbf) {
for (const key in context.feature?.properties) {
let value = context.feature.properties[key];
let keyIndex = context.keycache[key];
if (value == null)
continue; // don't encode null/undefined value properties
if (typeof keyIndex === 'undefined') {
context.keys.push(key);
keyIndex = context.keys.length - 1;
context.keycache[key] = keyIndex;
}
pbf.writeVarint(keyIndex);
if (typeof value !== 'string' && typeof value !== 'boolean' && typeof value !== 'number') {
value = context.jsonPrefix + JSON.stringify(value);
}
const valueKey = typeof value + ':' + value;
let valueIndex = context.valuecache[valueKey];
if (typeof valueIndex === 'undefined') {
context.values.push(value);
valueIndex = context.values.length - 1;
context.valuecache[valueKey] = valueIndex;
}
pbf.writeVarint(valueIndex);
}
}
function command(cmd, length) {
return (length << 3) + (cmd & 0x7);
}
function zigzag(num) {
return (num << 1) ^ (num >> 31);
}
function writeGeometry(feature, pbf) {
const geometry = feature.loadGeometry();
const type = feature.type;
let x = 0;
let y = 0;
for (const ring of geometry) {
let count = 1;
if (type === 1) {
count = ring.length;
}
pbf.writeVarint(command(1, count)); // moveto
// do not write polygon closing path as lineto
const lineCount = type === 3 ? ring.length - 1 : ring.length;
for (let i = 0; i < lineCount; i++) {
if (i === 1 && type !== 1) {
pbf.writeVarint(command(2, lineCount - 1)); // lineto
}
const dx = ring[i].x - x;
const dy = ring[i].y - y;
pbf.writeVarint(zigzag(dx));
pbf.writeVarint(zigzag(dy));
x += dx;
y += dy;
}
if (feature.type === 3) {
pbf.writeVarint(command(7, 1)); // closepath
}
}
}
function writeValue(value, pbf) {
const type = typeof value;
if (type === 'string') {
pbf.writeStringField(1, value);
}
else if (type === 'boolean') {
pbf.writeBooleanField(7, value);
}
else if (type === 'number') {
if (value % 1 !== 0) {
pbf.writeDoubleField(3, value);
}
else if (value < 0) {
pbf.writeSVarintField(6, value);
}
else {
pbf.writeVarintField(5, value);
}
}
}
export { GEOJSON_TILE_LAYER_NAME, GeoJSONWrapper, fromGeojsonVt, fromVectorTileJs };
//# sourceMappingURL=index.es.js.map

1
node_modules/@maplibre/vt-pbf/dist/index.es.js.map generated vendored Normal file

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,20 @@
import type { TileFeature, AnyProps } from 'supercluster';
import type { GeoJSONVTFeature } from '@maplibre/geojson-vt';
import type { VectorTileFeatureLike, VectorTileLayerLike, VectorTileLike } from "./types";
export { VectorTileFeatureLike, VectorTileLayerLike, VectorTileLike };
export type Feature = TileFeature<AnyProps, AnyProps> | GeoJSONVTFeature;
export interface GeoJSONOptions {
version: number;
extent: number;
}
export declare const GEOJSON_TILE_LAYER_NAME = "_geojsonTileLayer";
export declare class GeoJSONWrapper implements VectorTileLayerLike {
layers: Record<string, VectorTileLayerLike>;
features: Feature[];
version: VectorTileLayerLike['version'];
name: VectorTileLayerLike['name'];
extent: VectorTileLayerLike['extent'];
length: VectorTileLayerLike['length'];
constructor(features: Feature[], options?: GeoJSONOptions);
feature(i: number): VectorTileFeatureLike;
}

18
node_modules/@maplibre/vt-pbf/dist/lib/types.d.ts generated vendored Normal file
View File

@@ -0,0 +1,18 @@
import type Point from '@mapbox/point-geometry';
export interface VectorTileFeatureLike {
type: 0 | 1 | 2 | 3;
properties: Record<string, number | string | boolean>;
id: number | undefined;
extent: number;
loadGeometry(): Point[][];
}
export interface VectorTileLayerLike {
version: number;
name: string;
extent: number;
length: number;
feature(i: number): VectorTileFeatureLike;
}
export interface VectorTileLike {
layers: Record<string, VectorTileLayerLike>;
}

View File

@@ -0,0 +1 @@
export {};

View File

@@ -0,0 +1 @@
export {};

View File

@@ -0,0 +1 @@
export {};

View File

@@ -0,0 +1,2 @@
declare const _default: import("vite").UserConfig;
export default _default;

View File

@@ -0,0 +1,39 @@
The MIT License (MIT)
Copyright (c) 2025 MapLibre contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
--------------------------------------------------------------------------------
ISC License
Copyright (c) 2024, Mapbox
Permission to use, copy, modify, and/or distribute this software for any purpose
with or without fee is hereby granted, provided that the above copyright notice
and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
THIS SOFTWARE.

View File

@@ -0,0 +1,128 @@
<p align="center">
<picture>
<source media="(prefers-color-scheme: dark)" srcset="https://maplibre.org/img/maplibre-logos/maplibre-logo-for-dark-bg.svg">
<source media="(prefers-color-scheme: light)" srcset="https://maplibre.org/img/maplibre-logos/maplibre-logo-for-light-bg.svg">
<img alt="MapLibre Logo" src="https://maplibre.org/img/maplibre-logos/maplibre-logo-for-light-bg.svg" width="200">
</picture>
</p>
## geojson-vt &mdash; GeoJSON Vector Tiles
A highly efficient JavaScript library for **slicing GeoJSON data into vector tiles on the fly**,
primarily designed to enable rendering and interacting with large geospatial datasets
on the browser side (without a server).
Created to power GeoJSON in [MapLibre GL JS](https://github.com/maplibre/maplibre-gl-js),
but can be useful in other visualization platforms
like [Leaflet](https://github.com/Leaflet/Leaflet), [OpenLayers](https://openlayers.org/) and [d3](https://github.com/mbostock/d3),
as well as Node.js server applications.
Resulting tiles conform to the JSON equivalent
of the [vector tile specification](https://github.com/mapbox/vector-tile-spec/).
To make data rendering and interaction fast, the tiles are simplified,
retaining the minimum level of detail appropriate for each zoom level
(simplifying shapes, filtering out tiny polygons and polylines).
### Demo
Here's **geojson-vt** action in [MapLibre GL JS](https://github.com/maplibre/maplibre-gl-js),
dynamically loading a 100Mb US zip codes GeoJSON with 5.4 million points:
![](https://cloud.githubusercontent.com/assets/25395/5360312/86028d8e-7f91-11e4-811f-87f24acb09ca.gif)
There's a convenient [debug page](http://maplibre.org/geojson-vt/debug/) to test out **geojson-vt** on different data.
Just drag any GeoJSON on the page, watching the console.
![](https://cloud.githubusercontent.com/assets/25395/5363235/41955c6e-7fa8-11e4-9575-a66ef54cb6d9.gif)
### Usage
```js
// build an initial index of tiles
var tileIndex = geojsonvt(geoJSON);
// request a particular tile
var features = tileIndex.getTile(z, x, y).features;
// show an array of tile coordinates created so far
console.log(tileIndex.tileCoords); // [{z: 0, x: 0, y: 0}, ...]
```
### Options
You can fine-tune the results with an options object,
although the defaults are sensible and work well for most use cases.
```js
var tileIndex = geojsonvt(data, {
maxZoom: 14, // max zoom to preserve detail on; can't be higher than 24
tolerance: 3, // simplification tolerance (higher means simpler)
extent: 4096, // tile extent (both width and height)
buffer: 64, // tile buffer on each side
debug: 0, // logging level (0 to disable, 1 or 2)
lineMetrics: false, // whether to enable line metrics tracking for LineString/MultiLineString features
promoteId: null, // name of a feature property to promote to feature.id. Cannot be used with `generateId`
generateId: false, // whether to generate feature ids. Cannot be used with `promoteId`
updateable: false, // whether the tile index can be updated (with the caveat of a stored simplified copy)
indexMaxZoom: 5, // max zoom in the initial tile index
indexMaxPoints: 100000 // max number of points per tile in the index
});
```
By default, tiles at zoom levels above `indexMaxZoom` are generated on the fly, but you can pre-generate all possible tiles for `data` by setting `indexMaxZoom` and `maxZoom` to the same value, setting `indexMaxPoints` to `0`, and then accessing the resulting tile coordinates from the `tileCoords` property of `tileIndex`.
The `promoteId` and `generateId` options ignore existing `id` values on the feature objects.
GeoJSON-VT only operates on zoom levels up to 24.
### Update
For incremental updates to the tile index, you can use `updateData` to change features without having to recreate a fresh index. `updateData` takes a diff object as a parameter with the following properties:
```js
var diff = {
removeAll: false, // set to true to clear all features
remove: ['id1', 'id2'], // array of feature ids to remove
add: [feature1, feature2], // array of GeoJSON features to add
update: [ // array of feature update objects
{
id: 'feature1', // required - id of feature to update
newGeometry: {type: 'Point', ...}, // new geometry for the feature
removeAllProperties: false, // remove all properties
removeProperties: ['prop1', 'prop2'], // array of property keys to remove
addOrUpdateProperties: [ // array of properties to add/update
{key: 'name', value: 'New Name'},
{key: 'population', value: 5000}
]
}
]
};
tileIndex.updateData(diff);
```
All properties in the diff are optional, but at least one operation should be specified. Remove operations are applied before add/update operations.
To use `updateData`, the index must be created with the `updateable: true` option.
### Install
Install using NPM (`npm install @maplibre/geojson-vt`), then:
```js
// import as a ES module
import geojsonvt from '@maplibre/geojson-vt';
// import from a CDN in the browser:
import geojsonvt from 'https://esm.run/@maplibre/geojson-vt';
```
Or use a browser build directly:
```html
<script src="https://unpkg.com/@maplibre/geojson-vt/geojson-vt.js"></script>
```
### Getting Involved
Join the #maplibre slack channel at OSMUS: get an invite at https://slack.openstreetmap.us/

View File

@@ -0,0 +1,3 @@
import type { GeoJSONVTInternalFeature, GeoJSONVTOptions } from './definitions';
export declare function clip(features: GeoJSONVTInternalFeature[], scale: number, k1: number, k2: number, axis: number, minAll: number, maxAll: number, options: GeoJSONVTOptions): GeoJSONVTInternalFeature[] | null;
//# sourceMappingURL=clip.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"clip.d.ts","sourceRoot":"","sources":["../src/clip.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,wBAAwB,EAAE,gBAAgB,EAAqB,MAAM,eAAe,CAAC;AAYnG,wBAAgB,IAAI,CAAC,QAAQ,EAAE,wBAAwB,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,gBAAgB,GAAG,wBAAwB,EAAE,GAAG,IAAI,CAqG5M"}

View File

@@ -0,0 +1,2 @@
export {};
//# sourceMappingURL=clip.test.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"clip.test.d.ts","sourceRoot":"","sources":["../src/clip.test.ts"],"names":[],"mappings":""}

View File

@@ -0,0 +1,9 @@
import type { GeoJSONVTInternalFeature, GeoJSONVTOptions } from './definitions';
/**
* converts GeoJSON feature into an intermediate projected JSON vector format with simplification data
* @param data
* @param options
* @returns
*/
export declare function convert(data: GeoJSON.GeoJSON, options: GeoJSONVTOptions): GeoJSONVTInternalFeature[];
//# sourceMappingURL=convert.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"convert.d.ts","sourceRoot":"","sources":["../src/convert.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,wBAAwB,EAAE,gBAAgB,EAAqB,MAAM,eAAe,CAAC;AAEnG;;;;;GAKG;AACH,wBAAgB,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,OAAO,EAAE,OAAO,EAAE,gBAAgB,GAAG,wBAAwB,EAAE,CAiBpG"}

View File

@@ -0,0 +1,85 @@
export type GeoJSONVTOptions = {
/**
* Max zoom to preserve detail on
* @default 14
*/
maxZoom?: number;
/**
* Max zoom in the tile index
* @default 5
*/
indexMaxZoom?: number;
/**
* Max number of points per tile in the tile index
* @default 100000
*/
indexMaxPoints?: number;
/**
* Simplification tolerance (higher means simpler)
* @default 3
*/
tolerance?: number;
/**
* Tile extent
* @default 4096
*/
extent?: number;
/**
* Tile buffer on each side
* @default 64
*/
buffer?: number;
/**
* Whether to calculate line metrics
* @default false
*/
lineMetrics?: boolean;
/**
* Name of a feature property to be promoted to feature.id
*/
promoteId?: string | null;
/**
* Whether to generate feature ids. Cannot be used with promoteId
* @default false
*/
generateId?: boolean;
/**
* Whether geojson can be updated (with caveat of a stored simplified copy)
* @default false
*/
updateable?: boolean;
/**
* Logging level (0, 1 or 2)
* @default 0
*/
debug?: number;
};
export type StartEndSizeArray = number[] & {
start?: number;
end?: number;
size?: number;
};
export type PartialGeoJSONVTFeature = {
id?: number | string | undefined;
tags: GeoJSON.GeoJsonProperties;
minX: number;
minY: number;
maxX: number;
maxY: number;
};
export type GeometryTypeMap = {
Point: number[];
MultiPoint: number[];
LineString: StartEndSizeArray;
MultiLineString: StartEndSizeArray[];
Polygon: StartEndSizeArray[];
MultiPolygon: StartEndSizeArray[][];
};
export type GeometryType = "Point" | "MultiPoint" | "LineString" | "MultiLineString" | "Polygon" | "MultiPolygon";
export type GeoJSONVTInternalFeature = {
[K in GeometryType]: PartialGeoJSONVTFeature & {
type: K;
geometry: GeometryTypeMap[K];
};
}[GeometryType];
//# sourceMappingURL=definitions.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"definitions.d.ts","sourceRoot":"","sources":["../src/definitions.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,gBAAgB,GAAG;IAC3B;;;OAGG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB;;;OAGG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB;;;OAGG;IACH,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB;;;OAGG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB;;;OAGG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB;;;OAGG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB;;;OAGG;IACH,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB;;OAEG;IACH,SAAS,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B;;;OAGG;IACH,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB;;;OAGG;IACH,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB;;;OAGG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC;CAClB,CAAC;AAGF,MAAM,MAAM,iBAAiB,GAAG,MAAM,EAAE,GAAG;IAAE,KAAK,CAAC,EAAE,MAAM,CAAC;IAAC,GAAG,CAAC,EAAE,MAAM,CAAC;IAAC,IAAI,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC;AAE3F,MAAM,MAAM,uBAAuB,GAAG;IAClC,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAAC;IACjC,IAAI,EAAE,OAAO,CAAC,iBAAiB,CAAC;IAChC,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;CAChB,CAAA;AAED,MAAM,MAAM,eAAe,GAAG;IAC1B,KAAK,EAAE,MAAM,EAAE,CAAC;IAChB,UAAU,EAAE,MAAM,EAAE,CAAC;IACrB,UAAU,EAAE,iBAAiB,CAAC;IAC9B,eAAe,EAAE,iBAAiB,EAAE,CAAC;IACrC,OAAO,EAAE,iBAAiB,EAAE,CAAC;IAC7B,YAAY,EAAE,iBAAiB,EAAE,EAAE,CAAC;CACvC,CAAA;AAED,MAAM,MAAM,YAAY,GAAG,OAAO,GAAG,YAAY,GAAG,YAAY,GAAG,iBAAiB,GAAG,SAAS,GAAG,cAAc,CAAC;AAElH,MAAM,MAAM,wBAAwB,GAAG;KAClC,CAAC,IAAI,YAAY,GAAG,uBAAuB,GAAG;QAC3C,IAAI,EAAE,CAAC,CAAC;QACR,QAAQ,EAAE,eAAe,CAAC,CAAC,CAAC,CAAC;KAChC;CACJ,CAAC,YAAY,CAAC,CAAC"}

View File

@@ -0,0 +1,67 @@
import type { GeoJSONVTInternalFeature, GeoJSONVTOptions } from './definitions';
export type GeoJSONVTSourceDiff = {
/**
* If true, clear all existing features
*/
removeAll?: boolean;
/**
* Array of feature IDs to remove
*/
remove?: (string | number)[];
/**
* Array of GeoJSON features to add
*/
add?: GeoJSON.Feature[];
/**
* Array of per-feature updates
*/
update?: GeoJSONVTFeatureDiff[];
};
export type GeoJSONVTFeatureDiff = {
/**
* ID of the feature being updated
*/
id: string | number;
/**
* Optional new geometry
*/
newGeometry?: GeoJSON.Geometry;
/**
* Remove all properties if true
*/
removeAllProperties?: boolean;
/**
* Specific properties to delete
*/
removeProperties?: string[];
/**
* Properties to add or update
*/
addOrUpdateProperties?: {
key: string;
value: unknown;
}[];
};
type HashedGeoJSONVTSourceDiff = {
removeAll?: boolean | undefined;
remove: Set<string | number>;
add: Map<string | number | undefined, GeoJSON.Feature>;
update: Map<string | number, GeoJSONVTFeatureDiff>;
};
/**
* Applies a GeoJSON Source Diff to an existing set of simplified features
* @param source
* @param dataDiff
* @param options
* @returns
*/
export declare function applySourceDiff(source: GeoJSONVTInternalFeature[], dataDiff: GeoJSONVTSourceDiff, options: GeoJSONVTOptions): {
affected: GeoJSONVTInternalFeature[];
source: GeoJSONVTInternalFeature[];
};
/**
* Convert a GeoJSON Source Diff to an idempotent hashed representation using Sets and Maps
*/
export declare function diffToHashed(diff: GeoJSONVTSourceDiff): HashedGeoJSONVTSourceDiff;
export {};
//# sourceMappingURL=difference.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"difference.d.ts","sourceRoot":"","sources":["../src/difference.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,wBAAwB,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AAEhF,MAAM,MAAM,mBAAmB,GAAG;IAC9B;;OAEG;IACH,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB;;OAEG;IACH,MAAM,CAAC,EAAE,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,CAAC;IAC7B;;OAEG;IACH,GAAG,CAAC,EAAE,OAAO,CAAC,OAAO,EAAE,CAAC;IACxB;;OAEG;IACH,MAAM,CAAC,EAAE,oBAAoB,EAAE,CAAC;CACnC,CAAC;AAEF,MAAM,MAAM,oBAAoB,GAAG;IAC/B;;OAEG;IACH,EAAE,EAAE,MAAM,GAAG,MAAM,CAAC;IACpB;;OAEG;IACH,WAAW,CAAC,EAAE,OAAO,CAAC,QAAQ,CAAC;IAC/B;;OAEG;IACH,mBAAmB,CAAC,EAAE,OAAO,CAAC;IAC9B;;OAEG;IACH,gBAAgB,CAAC,EAAE,MAAM,EAAE,CAAC;IAC5B;;OAEG;IACH,qBAAqB,CAAC,EAAE;QACpB,GAAG,EAAE,MAAM,CAAC;QACZ,KAAK,EAAE,OAAO,CAAC;KAClB,EAAE,CAAC;CACP,CAAC;AAEF,KAAK,yBAAyB,GAAG;IAC7B,SAAS,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;IAChC,MAAM,EAAE,GAAG,CAAC,MAAM,GAAG,MAAM,CAAC,CAAC;IAC7B,GAAG,EAAE,GAAG,CAAC,MAAM,GAAG,MAAM,GAAG,SAAS,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC;IACvD,MAAM,EAAE,GAAG,CAAC,MAAM,GAAG,MAAM,EAAE,oBAAoB,CAAC,CAAC;CACtD,CAAC;AAEF;;;;;;GAMG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,wBAAwB,EAAE,EAAE,QAAQ,EAAE,mBAAmB,EAAE,OAAO,EAAE,gBAAgB;;;EAwE3H;AAgED;;GAEG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,mBAAmB,GAAG,yBAAyB,CAejF"}

View File

@@ -0,0 +1,2 @@
export {};
//# sourceMappingURL=difference.test.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"difference.test.d.ts","sourceRoot":"","sources":["../src/difference.test.ts"],"names":[],"mappings":""}

View File

@@ -0,0 +1,12 @@
import type { GeoJSONVTInternalFeature, GeometryType, GeometryTypeMap } from "./definitions";
export type SupportedGeometries = GeoJSON.Point | GeoJSON.MultiPoint | GeoJSON.LineString | GeoJSON.MultiLineString | GeoJSON.Polygon | GeoJSON.MultiPolygon;
/**
*
* @param id - the feature's ID
* @param type - the feature's type
* @param geom - the feature's geometry
* @param tags - the feature's properties
* @returns the created feature
*/
export declare function createFeature<T extends GeometryType>(id: number | string | undefined, type: T, geom: GeometryTypeMap[T], tags: GeoJSON.GeoJsonProperties): GeoJSONVTInternalFeature;
//# sourceMappingURL=feature.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"feature.d.ts","sourceRoot":"","sources":["../src/feature.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,wBAAwB,EAAE,YAAY,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAE7F,MAAM,MAAM,mBAAmB,GAAG,OAAO,CAAC,KAAK,GAAG,OAAO,CAAC,UAAU,GAAG,OAAO,CAAC,UAAU,GAAG,OAAO,CAAC,eAAe,GAAG,OAAO,CAAC,OAAO,GAAG,OAAO,CAAC,YAAY,CAAC;AAE7J;;;;;;;GAOG;AACH,wBAAgB,aAAa,CAAC,CAAC,SAAS,YAAY,EAAE,EAAE,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,EAAE,IAAI,EAAE,CAAC,EAAE,IAAI,EAAE,eAAe,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,OAAO,CAAC,iBAAiB,GAAG,wBAAwB,CA0CnL"}

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,63 @@
import { type GeoJSONVTFeature, type GeoJSONVTFeatureNonPoint, type GeoJSONVTFeaturePoint, type GeoJSONVTTile } from './transform';
import { type GeoJSONVTInternalTile, type GeoJSONVTInternalTileFeature, type GeoJSONVTInternalTileFeaturePoint, type GeoJSONVTInternalTileFeaturNonPoint } from './tile';
import { type GeoJSONVTFeatureDiff, type GeoJSONVTSourceDiff } from './difference';
import type { GeoJSONVTInternalFeature, GeoJSONVTOptions, GeometryType, GeometryTypeMap, PartialGeoJSONVTFeature, StartEndSizeArray } from './definitions';
/**
* Main class for creating and managing a vector tile index from GeoJSON data.
*/
declare class GeoJSONVT {
private options;
/** @internal */
tiles: {
[key: string]: GeoJSONVTInternalTile;
};
private tileCoords;
/** @internal */
stats: {
[key: string]: number;
};
/** @internal */
total: number;
private source?;
constructor(data: GeoJSON.GeoJSON, options: GeoJSONVTOptions);
/**
* splits features from a parent tile to sub-tiles.
* z, x, and y are the coordinates of the parent tile
* cz, cx, and cy are the coordinates of the target tile
*
* If no target tile is specified, splitting stops when we reach the maximum
* zoom or the number of points is low as specified in the options.
* @internal
* @param features - features to split
* @param z - tile zoom level
* @param x - tile x coordinate
* @param y - tile y coordinate
* @param cz - target tile zoom level
* @param cx - target tile x coordinate
* @param cy - target tile y coordinate
*/
splitTile(features: GeoJSONVTInternalFeature[], z: number, x: number, y: number, cz?: number, cx?: number, cy?: number): void;
/**
* Given z, x, and y tile coordinates, returns the corresponding tile with geometries in tile coordinates, much like MVT data is stored.
* @param z - tile zoom level
* @param x - tile x coordinate
* @param y - tile y coordinate
* @returns the transformed tile or null if not found
*/
getTile(z: number | string, x: number | string, y: number | string): GeoJSONVTTile | null;
/**
* Invalidates (removes) tiles affected by the provided features
* @internal
* @param features
*/
invalidateTiles(features: GeoJSONVTInternalFeature[]): void;
/**
* Updates the tile index by adding and/or removing geojson features
* invalidates tiles that are affected by the update for regeneration on next getTile call.
* @param diff - the source diff object
*/
updateData(diff: GeoJSONVTSourceDiff): void;
}
export default function geojsonvt(data: GeoJSON.GeoJSON, options?: GeoJSONVTOptions): GeoJSONVT;
export type { GeoJSONVTInternalFeature, GeoJSONVTOptions, GeoJSONVTInternalTile, GeoJSONVTInternalTileFeature, GeometryType, PartialGeoJSONVTFeature, GeoJSONVT, GeometryTypeMap, StartEndSizeArray, GeoJSONVTTile, GeoJSONVTFeature, GeoJSONVTSourceDiff, GeoJSONVTFeatureDiff, GeoJSONVTFeaturePoint, GeoJSONVTFeatureNonPoint, GeoJSONVTInternalTileFeaturePoint, GeoJSONVTInternalTileFeaturNonPoint };
//# sourceMappingURL=index.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAIA,OAAO,EAAgB,KAAK,gBAAgB,EAAE,KAAK,wBAAwB,EAAE,KAAK,qBAAqB,EAAE,KAAK,aAAa,EAAC,MAAM,aAAa,CAAC;AAChJ,OAAO,EAAa,KAAK,qBAAqB,EAAE,KAAK,4BAA4B,EAAE,KAAK,iCAAiC,EAAE,KAAK,mCAAmC,EAAC,MAAM,QAAQ,CAAC;AACnL,OAAO,EAAkB,KAAK,oBAAoB,EAAE,KAAK,mBAAmB,EAAC,MAAM,cAAc,CAAC;AAClG,OAAO,KAAK,EAAE,wBAAwB,EAAE,gBAAgB,EAAE,YAAY,EAAE,eAAe,EAAE,uBAAuB,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAgB3J;;GAEG;AACH,cAAM,SAAS;IACX,OAAO,CAAC,OAAO,CAAmB;IAClC,gBAAgB;IACT,KAAK,EAAE;QAAC,CAAC,GAAG,EAAE,MAAM,GAAG,qBAAqB,CAAA;KAAC,CAAC;IACrD,OAAO,CAAC,UAAU,CAAkD;IACpE,gBAAgB;IACT,KAAK,EAAE;QAAC,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAA;KAAC,CAAM;IAC3C,gBAAgB;IACT,KAAK,EAAE,MAAM,CAAK;IACzB,OAAO,CAAC,MAAM,CAAC,CAA6B;gBAEhC,IAAI,EAAE,OAAO,CAAC,OAAO,EAAE,OAAO,EAAE,gBAAgB;IA6C5D;;;;;;;;;;;;;;;OAeG;IACH,SAAS,CAAC,QAAQ,EAAE,wBAAwB,EAAE,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,EAAE,CAAC,EAAE,MAAM,EAAE,EAAE,CAAC,EAAE,MAAM,EAAE,EAAE,CAAC,EAAE,MAAM;IA4FtH;;;;;;OAMG;IACH,OAAO,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,aAAa,GAAG,IAAI;IA+CzF;;;;OAIG;IACH,eAAe,CAAC,QAAQ,EAAE,wBAAwB,EAAE;IAuEpD;;;;OAIG;IACH,UAAU,CAAC,IAAI,EAAE,mBAAmB;CAwCvC;AAMD,MAAM,CAAC,OAAO,UAAU,SAAS,CAAC,IAAI,EAAE,OAAO,CAAC,OAAO,EAAE,OAAO,CAAC,EAAE,gBAAgB,aAElF;AAED,YAAY,EACR,wBAAwB,EACxB,gBAAgB,EAChB,qBAAqB,EACrB,4BAA4B,EAC5B,YAAY,EACZ,uBAAuB,EACvB,SAAS,EACT,eAAe,EACf,iBAAiB,EACjB,aAAa,EACb,gBAAgB,EAChB,mBAAmB,EACnB,oBAAoB,EACpB,qBAAqB,EACrB,wBAAwB,EACxB,iCAAiC,EACjC,mCAAmC,EACtC,CAAC"}

View File

@@ -0,0 +1,9 @@
/**
* calculate simplification data using optimized Douglas-Peucker algorithm
* @param coords - flat array of coordinates
* @param first - index of the first coordinate in the segment
* @param last - index of the last coordinate in the segment
* @param sqTolerance - square tolerance value
*/
export declare function simplify(coords: number[], first: number, last: number, sqTolerance: number): void;
//# sourceMappingURL=simplify.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"simplify.d.ts","sourceRoot":"","sources":["../src/simplify.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,wBAAgB,QAAQ,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,QAqC1F"}

View File

@@ -0,0 +1,2 @@
export {};
//# sourceMappingURL=simplify.test.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"simplify.test.d.ts","sourceRoot":"","sources":["../src/simplify.test.ts"],"names":[],"mappings":""}

View File

@@ -0,0 +1,40 @@
import type { GeoJSONVTInternalFeature, GeoJSONVTOptions } from "./definitions";
export type GeoJSONVTInternalTileFeaturePoint = {
id?: number | string | undefined;
type: 1;
tags: GeoJSON.GeoJsonProperties | null;
geometry: number[];
};
export type GeoJSONVTInternalTileFeaturNonPoint = {
id?: number | string | undefined;
type: 2 | 3;
tags: GeoJSON.GeoJsonProperties | null;
geometry: number[][];
};
export type GeoJSONVTInternalTileFeature = GeoJSONVTInternalTileFeaturePoint | GeoJSONVTInternalTileFeaturNonPoint;
export type GeoJSONVTInternalTile = {
features: GeoJSONVTInternalTileFeature[];
numPoints: number;
numSimplified: number;
numFeatures: number;
x: number;
y: number;
z: number;
transformed: boolean;
minX: number;
minY: number;
maxX: number;
maxY: number;
source: GeoJSONVTInternalFeature[] | null;
};
/**
* Creates a tile object from the given features
* @param features - the features to include in the tile
* @param z
* @param tx
* @param ty
* @param options - the options object
* @returns the created tile
*/
export declare function createTile(features: GeoJSONVTInternalFeature[], z: number, tx: number, ty: number, options: GeoJSONVTOptions): GeoJSONVTInternalTile;
//# sourceMappingURL=tile.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"tile.d.ts","sourceRoot":"","sources":["../src/tile.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,wBAAwB,EAAE,gBAAgB,EAAqB,MAAM,eAAe,CAAC;AAEnG,MAAM,MAAM,iCAAiC,GAAG;IAC5C,EAAE,CAAC,EAAG,MAAM,GAAG,MAAM,GAAG,SAAS,CAAC;IAClC,IAAI,EAAE,CAAC,CAAC;IACR,IAAI,EAAE,OAAO,CAAC,iBAAiB,GAAG,IAAI,CAAC;IACvC,QAAQ,EAAE,MAAM,EAAE,CAAC;CACtB,CAAA;AAED,MAAM,MAAM,mCAAmC,GAAG;IAC9C,EAAE,CAAC,EAAG,MAAM,GAAG,MAAM,GAAG,SAAS,CAAC;IAClC,IAAI,EAAE,CAAC,GAAG,CAAC,CAAC;IACZ,IAAI,EAAE,OAAO,CAAC,iBAAiB,GAAG,IAAI,CAAC;IACvC,QAAQ,EAAE,MAAM,EAAE,EAAE,CAAC;CACxB,CAAA;AACD,MAAM,MAAM,4BAA4B,GAAG,iCAAiC,GAAG,mCAAmC,CAAC;AAEnH,MAAM,MAAM,qBAAqB,GAAG;IAChC,QAAQ,EAAE,4BAA4B,EAAE,CAAC;IACzC,SAAS,EAAE,MAAM,CAAC;IAClB,aAAa,EAAE,MAAM,CAAC;IACtB,WAAW,EAAE,MAAM,CAAC;IACpB,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;IACV,WAAW,EAAE,OAAO,CAAC;IACrB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,wBAAwB,EAAE,GAAG,IAAI,CAAC;CAC7C,CAAA;AAED;;;;;;;;GAQG;AACH,wBAAgB,UAAU,CAAC,QAAQ,EAAE,wBAAwB,EAAE,EAAE,CAAC,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,gBAAgB,GAAG,qBAAqB,CAwBpJ"}

View File

@@ -0,0 +1,27 @@
import type { GeoJSONVTInternalTile } from "./tile";
export type GeoJSONVTFeaturePoint = {
id?: number | string | undefined;
type: 1;
tags: GeoJSON.GeoJsonProperties | null;
geometry: [number, number][];
};
export type GeoJSONVTFeatureNonPoint = {
id?: number | string | undefined;
type: 2 | 3;
tags: GeoJSON.GeoJsonProperties | null;
geometry: [number, number][][];
};
export type GeoJSONVTFeature = GeoJSONVTFeaturePoint | GeoJSONVTFeatureNonPoint;
export type GeoJSONVTTile = GeoJSONVTInternalTile & {
transformed: true;
features: GeoJSONVTFeature[];
};
/**
* Transforms the coordinates of each feature in the given tile from
* mercator-projected space into (extent x extent) tile space.
* @param tile - the tile to transform, this gets modified in place
* @param extent - the tile extent (usually 4096)
* @returns the transformed tile
*/
export declare function transformTile(tile: GeoJSONVTInternalTile, extent: number): GeoJSONVTTile;
//# sourceMappingURL=transform.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"transform.d.ts","sourceRoot":"","sources":["../src/transform.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,QAAQ,CAAC;AAEpD,MAAM,MAAM,qBAAqB,GAAG;IAChC,EAAE,CAAC,EAAG,MAAM,GAAG,MAAM,GAAG,SAAS,CAAC;IAClC,IAAI,EAAE,CAAC,CAAC;IACR,IAAI,EAAE,OAAO,CAAC,iBAAiB,GAAG,IAAI,CAAC;IACvC,QAAQ,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,CAAA;CAC/B,CAAA;AAED,MAAM,MAAM,wBAAwB,GAAG;IACnC,EAAE,CAAC,EAAG,MAAM,GAAG,MAAM,GAAG,SAAS,CAAC;IAClC,IAAI,EAAE,CAAC,GAAG,CAAC,CAAC;IACZ,IAAI,EAAE,OAAO,CAAC,iBAAiB,GAAG,IAAI,CAAC;IACvC,QAAQ,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,EAAE,CAAA;CACjC,CAAA;AAED,MAAM,MAAM,gBAAgB,GAAG,qBAAqB,GAAG,wBAAwB,CAAC;AAEhF,MAAM,MAAM,aAAa,GAAG,qBAAqB,GAAG;IAChD,WAAW,EAAE,IAAI,CAAC;IAClB,QAAQ,EAAE,gBAAgB,EAAE,CAAA;CAC/B,CAAA;AAED;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,qBAAqB,EAAE,MAAM,EAAE,MAAM,GAAG,aAAa,CAgCxF"}

View File

@@ -0,0 +1,3 @@
import type { GeoJSONVTInternalFeature, GeoJSONVTOptions } from './definitions';
export declare function wrap(features: GeoJSONVTInternalFeature[], options: GeoJSONVTOptions): GeoJSONVTInternalFeature[];
//# sourceMappingURL=wrap.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"wrap.d.ts","sourceRoot":"","sources":["../src/wrap.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,wBAAwB,EAAE,gBAAgB,EAAqB,MAAM,eAAe,CAAC;AAGnG,wBAAgB,IAAI,CAAC,QAAQ,EAAE,wBAAwB,EAAE,EAAE,OAAO,EAAE,gBAAgB,GAAG,wBAAwB,EAAE,CAehH"}

View File

@@ -0,0 +1,60 @@
{
"name": "@maplibre/geojson-vt",
"version": "5.0.4",
"description": "Slice GeoJSON data into vector tiles efficiently",
"type": "module",
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/geojson-vt.mjs",
"default": "./dist/geojson-vt.js"
}
},
"sideEffects": false,
"keywords": [
"spatial",
"geojson",
"tiles",
"geometry"
],
"author": "Vladimir Agafonkin",
"module": "dist/geojson-vt.mjs",
"main": "dist/geojson-vt.js",
"typings": "dist/index.d.ts",
"jsdelivr": "dist/geojson-vt.js",
"unpkg": "dist/geojson-vt.js",
"devDependencies": {
"@eslint/js": "^9.39.2",
"@rollup/plugin-terser": "^0.4.4",
"@rollup/plugin-typescript": "^12.3.0",
"@types/geojson": "^7946.0.16",
"@types/node": "^25.0.9",
"@vitest/coverage-v8": "^4.0.17",
"benchmark": "^2.1.4",
"eslint": "^9.39.2",
"rollup": "^4.55.2",
"tslib": "^2.8.1",
"typedoc": "^0.28.16",
"typescript": "^5.9.3",
"typescript-eslint": "^8.53.1",
"vitest": "^4.0.17"
},
"license": "ISC",
"scripts": {
"lint": "eslint **/*.ts",
"test": "vitest",
"build": "rollup -c && tsc -p tsconfig.declaration.json",
"watch": "rollup -cw",
"bench": "node bench/benchmark.js",
"docs": "typedoc && mkdir -p docs/debug && cp -r debug/* docs/debug && find docs/debug -name '*.html' -exec sed -i 's|../dist/geojson-vt-dev.js|https://unpkg.com/@maplibre/geojson-vt@latest/dist/geojson-vt.js|g' {} +",
"prepublishOnly": "npm run test && npm run build"
},
"files": [
"dist",
"src"
],
"repository": {
"type": "git",
"url": "https://github.com/maplibre/geojson-vt"
}
}

View File

@@ -0,0 +1,79 @@
import {test, expect} from 'vitest';
import {clip} from './clip';
import type { StartEndSizeArray } from './definitions';
const geom1 = [0,0,0,50,0,0,50,10,0,20,10,0,20,20,0,30,20,0,30,30,0,50,30,0,50,40,0,25,40,0,25,50,0,0,50,0,0,60,0,25,60,0];
const geom2 = [0,0,0,50,0,0,50,10,0,0,10,0];
test('clips polylines', () => {
const clipped = clip([
{geometry: geom1, type: 'LineString', tags: { "1": 1}, minX: 0, minY: 0, maxX: 50, maxY: 60},
{geometry: geom2, type: 'LineString', tags: { "2": 2}, minX: 0, minY: 0, maxX: 50, maxY: 10}
], 1, 10, 40, 0, -Infinity, Infinity, {});
const expected = [
{id: null as string, type: 'MultiLineString', geometry: [
[10,0,1,40,0,1],
[40,10,1,20,10,0,20,20,0,30,20,0,30,30,0,40,30,1],
[40,40,1,25,40,0,25,50,0,10,50,1],
[10,60,1,25,60,0]], tags: {"1": 1}, minX: 10, minY: 0, maxX: 40, maxY: 60},
{id: null as string, type: 'MultiLineString', geometry: [
[10,0,1,40,0,1],
[40,10,1,10,10,1]], tags: {"2": 2}, minX: 10, minY: 0, maxX: 40, maxY: 10}
];
expect(JSON.stringify(clipped)).toEqual(JSON.stringify(expected));
});
test('clips lines with line metrics on', () => {
const geom = geom1.slice() as StartEndSizeArray;
geom.size = 0;
for (let i = 0; i < geom.length - 3; i += 3) {
const dx = geom[i + 3] - geom[i];
const dy = geom[i + 4] - geom[i + 1];
geom.size += Math.sqrt(dx * dx + dy * dy);
}
geom.start = 0;
geom.end = geom.size;
const clipped = clip([{id: 1, geometry: geom, type: 'LineString', tags: {}, minX: 0, minY: 0, maxX: 50, maxY: 60}],
1, 10, 40, 0, -Infinity, Infinity, {lineMetrics: true});
expect(
clipped.map(f => [(f.geometry as StartEndSizeArray).start, (f.geometry as StartEndSizeArray).end])).toEqual(
[[10, 40], [70, 130], [160, 200], [230, 245]]
);
});
function closed(geometry: number[]): number[][] {
return [geometry.concat(geometry.slice(0, 3))];
}
test('clips polygons', () => {
const clipped = clip([
{geometry: closed(geom1), type: 'Polygon', tags: {"1": 1}, minX: 0, minY: 0, maxX: 50, maxY: 60},
{geometry: closed(geom2), type: 'Polygon', tags: {"2": 2}, minX: 0, minY: 0, maxX: 50, maxY: 10}
], 1, 10, 40, 0, -Infinity, Infinity, {});
const expected = [
{id: null as string, type: 'Polygon', geometry: [[10,0,1,40,0,1,40,10,1,20,10,0,20,20,0,30,20,0,30,30,0,40,30,1,40,40,1,25,40,0,25,50,0,10,50,1,10,60,1,25,60,0,10,24,1,10,0,1]], tags: {"1": 1}, minX: 10, minY: 0, maxX: 40, maxY: 60},
{id: null as string, type: 'Polygon', geometry: [[10,0,1,40,0,1,40,10,1,10,10,1,10,0,1]], tags: {"2": 2}, minX: 10, minY: 0, maxX: 40, maxY: 10}
];
expect(JSON.stringify(clipped)).toEqual(JSON.stringify(expected));
});
test('clips points', () => {
const clipped = clip([
{geometry: geom1, type: 'MultiPoint', tags: {"1": 1}, minX: 0, minY: 0, maxX: 50, maxY: 60},
{geometry: geom2, type: 'MultiPoint', tags: {"2": 2}, minX: 0, minY: 0, maxX: 50, maxY: 10}
], 1, 10, 40, 0, -Infinity, Infinity, {});
expect(clipped).toEqual([{id: null, type: 'MultiPoint',
geometry: [20,10,0,20,20,0,30,20,0,30,30,0,25,40,0,25,50,0,25,60,0], tags: {"1": 1}, minX: 20, minY: 10, maxX: 30, maxY: 60}]);
});

View File

@@ -0,0 +1,232 @@
import {createFeature} from './feature';
import type { GeoJSONVTInternalFeature, GeoJSONVTOptions, StartEndSizeArray } from './definitions';
/* clip features between two vertical or horizontal axis-parallel lines:
* | |
* ___|___ | /
* / | \____|____/
* | |
*
* k1 and k2 are the line coordinates
* axis: 0 for x, 1 for y
* minAll and maxAll: minimum and maximum coordinate value for all features
*/
export function clip(features: GeoJSONVTInternalFeature[], scale: number, k1: number, k2: number, axis: number, minAll: number, maxAll: number, options: GeoJSONVTOptions): GeoJSONVTInternalFeature[] | null {
k1 /= scale;
k2 /= scale;
if (minAll >= k1 && maxAll < k2) { // trivial accept
return features;
}
if (maxAll < k1 || minAll >= k2) { // trivial reject
return null;
}
const clipped: GeoJSONVTInternalFeature[] = [];
for (const feature of features) {
const min = axis === 0 ? feature.minX : feature.minY;
const max = axis === 0 ? feature.maxX : feature.maxY;
if (min >= k1 && max < k2) { // trivial accept
clipped.push(feature);
continue;
}
if (max < k1 || min >= k2) { // trivial reject
continue;
}
switch (feature.type) {
case 'Point':
case 'MultiPoint': {
const pointGeometry: number[] = [];
clipPoints(feature.geometry, pointGeometry, k1, k2, axis);
if (!pointGeometry.length) continue;
const type = pointGeometry.length === 3 ? 'Point' : 'MultiPoint';
clipped.push(createFeature(feature.id, type, pointGeometry, feature.tags));
continue;
}
case 'LineString': {
const lineGeometry: StartEndSizeArray[] = [];
clipLine(feature.geometry, lineGeometry, k1, k2, axis, false, options.lineMetrics);
if (!lineGeometry.length) continue;
if (options.lineMetrics) {
for (const line of lineGeometry) {
clipped.push(createFeature(feature.id, feature.type, line, feature.tags));
}
continue;
}
if (lineGeometry.length > 1) {
clipped.push(createFeature(feature.id, "MultiLineString", lineGeometry, feature.tags));
continue;
}
clipped.push(createFeature(feature.id, feature.type, lineGeometry[0], feature.tags));
continue;
}
case 'MultiLineString': {
const multiLineGeometry: StartEndSizeArray[] = [];
clipLines(feature.geometry, multiLineGeometry, k1, k2, axis, false);
if (!multiLineGeometry.length) continue;
if (multiLineGeometry.length === 1) {
clipped.push(createFeature(feature.id, "LineString", multiLineGeometry[0], feature.tags));
continue;
}
clipped.push(createFeature(feature.id, feature.type, multiLineGeometry, feature.tags));
continue;
}
case 'Polygon': {
const polygonGeometry: StartEndSizeArray[] = [];
clipLines(feature.geometry, polygonGeometry, k1, k2, axis, true);
if (!polygonGeometry.length) continue;
clipped.push(createFeature(feature.id, feature.type, polygonGeometry, feature.tags));
continue;
}
case 'MultiPolygon': {
const multiPolygonGeometry: StartEndSizeArray[][] = [];
for (const polygon of feature.geometry) {
const newPolygon: StartEndSizeArray[] = [];
clipLines(polygon, newPolygon, k1, k2, axis, true);
if (newPolygon.length) multiPolygonGeometry.push(newPolygon);
}
if (!multiPolygonGeometry.length) continue;
clipped.push(createFeature(feature.id, feature.type, multiPolygonGeometry, feature.tags));
continue;
}
}
}
if (!clipped.length) return null;
return clipped;
}
function clipPoints(geom: number[], newGeom: number[], k1: number, k2: number, axis: number) {
for (let i = 0; i < geom.length; i += 3) {
const a = geom[i + axis];
if (a >= k1 && a <= k2) {
addPoint(newGeom, geom[i], geom[i + 1], geom[i + 2]);
}
}
}
function clipLine(geom: StartEndSizeArray, newGeom: StartEndSizeArray[], k1: number, k2: number, axis: number, isPolygon: boolean, trackMetrics: boolean) {
let slice = newSlice(geom);
const intersect = axis === 0 ? intersectX : intersectY;
let len = geom.start;
let segLen, t;
for (let i = 0; i < geom.length - 3; i += 3) {
const ax = geom[i];
const ay = geom[i + 1];
const az = geom[i + 2];
const bx = geom[i + 3];
const by = geom[i + 4];
const a = axis === 0 ? ax : ay;
const b = axis === 0 ? bx : by;
let exited = false;
if (trackMetrics) segLen = Math.sqrt(Math.pow(ax - bx, 2) + Math.pow(ay - by, 2));
if (a < k1) {
// ---|--> | (line enters the clip region from the left)
if (b > k1) {
t = intersect(slice, ax, ay, bx, by, k1);
if (trackMetrics) slice.start = len + segLen * t;
}
} else if (a > k2) {
// | <--|--- (line enters the clip region from the right)
if (b < k2) {
t = intersect(slice, ax, ay, bx, by, k2);
if (trackMetrics) slice.start = len + segLen * t;
}
} else {
addPoint(slice, ax, ay, az);
}
if (b < k1 && a >= k1) {
// <--|--- | or <--|-----|--- (line exits the clip region on the left)
t = intersect(slice, ax, ay, bx, by, k1);
exited = true;
}
if (b > k2 && a <= k2) {
// | ---|--> or ---|-----|--> (line exits the clip region on the right)
t = intersect(slice, ax, ay, bx, by, k2);
exited = true;
}
if (!isPolygon && exited) {
if (trackMetrics) slice.end = len + segLen * t;
newGeom.push(slice);
slice = newSlice(geom);
}
if (trackMetrics) len += segLen;
}
// add the last point
let last = geom.length - 3;
const ax = geom[last];
const ay = geom[last + 1];
const az = geom[last + 2];
const a = axis === 0 ? ax : ay;
if (a >= k1 && a <= k2) addPoint(slice, ax, ay, az);
// close the polygon if its endpoints are not the same after clipping
last = slice.length - 3;
if (isPolygon && last >= 3 && (slice[last] !== slice[0] || slice[last + 1] !== slice[1])) {
addPoint(slice, slice[0], slice[1], slice[2]);
}
// add the final slice
if (slice.length) {
newGeom.push(slice);
}
}
function newSlice(line: StartEndSizeArray): StartEndSizeArray {
const slice: StartEndSizeArray = [];
slice.size = line.size;
slice.start = line.start;
slice.end = line.end;
return slice;
}
function clipLines(geom: StartEndSizeArray[], newGeom: StartEndSizeArray[], k1: number, k2: number, axis: number, isPolygon: boolean) {
for (const line of geom) {
clipLine(line, newGeom, k1, k2, axis, isPolygon, false);
}
}
function addPoint(out: number[], x: number, y: number, z: number) {
out.push(x, y, z);
}
function intersectX(out: StartEndSizeArray, ax: number, ay: number, bx: number, by: number, x: number) {
const t = (x - ax) / (bx - ax);
addPoint(out, x, ay + (by - ay) * t, 1);
return t;
}
function intersectY(out: StartEndSizeArray, ax: number, ay: number, bx: number, by: number, y: number) {
const t = (y - ay) / (by - ay);
addPoint(out, ax + (bx - ax) * t, y, 1);
return t;
}

View File

@@ -0,0 +1,178 @@
import {simplify} from './simplify';
import {createFeature} from './feature';
import type { GeoJSONVTInternalFeature, GeoJSONVTOptions, StartEndSizeArray } from './definitions';
/**
* converts GeoJSON feature into an intermediate projected JSON vector format with simplification data
* @param data
* @param options
* @returns
*/
export function convert(data: GeoJSON.GeoJSON, options: GeoJSONVTOptions): GeoJSONVTInternalFeature[] {
const features: GeoJSONVTInternalFeature[] = [];
switch (data.type) {
case 'FeatureCollection':
for (let i = 0; i < data.features.length; i++) {
convertFeature(features, data.features[i], options, i);
}
break;
case 'Feature':
convertFeature(features, data, options);
break;
default:
convertFeature(features, {type: "Feature" as const, geometry: data, properties: undefined}, options);
}
return features;
}
function convertFeature(features: GeoJSONVTInternalFeature[], geojson: GeoJSON.Feature, options: GeoJSONVTOptions, index?: number) {
if (!geojson.geometry) return;
if (geojson.geometry.type === 'GeometryCollection') {
for (const singleGeometry of geojson.geometry.geometries) {
convertFeature(features, {
id: geojson.id,
type: 'Feature',
geometry: singleGeometry,
properties: geojson.properties
}, options, index);
}
return;
}
const coords = geojson.geometry.coordinates;
if (!coords?.length) return;
const tolerance = Math.pow(options.tolerance / ((1 << options.maxZoom) * options.extent), 2);
let id = geojson.id;
if (options.promoteId) {
id = geojson.properties?.[options.promoteId];
} else if (options.generateId) {
id = index || 0;
}
switch (geojson.geometry.type) {
case 'Point': {
const pointGeometry: StartEndSizeArray = [];
convertPoint(geojson.geometry.coordinates, pointGeometry);
features.push(createFeature(id, geojson.geometry.type, pointGeometry, geojson.properties));
return;
}
case 'MultiPoint': {
const multiPointGeometry: StartEndSizeArray = [];
for (const p of geojson.geometry.coordinates) {
convertPoint(p, multiPointGeometry);
}
features.push(createFeature(id, geojson.geometry.type, multiPointGeometry, geojson.properties));
return;
}
case 'LineString': {
const lineGeometry: StartEndSizeArray = [];
convertLine(geojson.geometry.coordinates, lineGeometry, tolerance, false);
features.push(createFeature(id, geojson.geometry.type, lineGeometry, geojson.properties));
return;
}
case 'MultiLineString': {
if (options.lineMetrics) {
// explode into linestrings in order to track metrics
for (const line of geojson.geometry.coordinates) {
const lineGeometry: StartEndSizeArray = [];
convertLine(line, lineGeometry, tolerance, false);
features.push(createFeature(id, 'LineString', lineGeometry, geojson.properties));
}
return;
}
const multiLineGeometry: StartEndSizeArray[] = [];
convertLines(geojson.geometry.coordinates, multiLineGeometry, tolerance, false);
features.push(createFeature(id, geojson.geometry.type, multiLineGeometry, geojson.properties));
return;
}
case 'Polygon': {
const polygonGeometry: StartEndSizeArray[] = [];
convertLines(geojson.geometry.coordinates, polygonGeometry, tolerance, true);
features.push(createFeature(id, geojson.geometry.type, polygonGeometry, geojson.properties));
return;
}
case 'MultiPolygon': {
const multiPolygonGeometry: StartEndSizeArray[][] = [];
for (const polygon of geojson.geometry.coordinates) {
const newPolygon: StartEndSizeArray[] = [];
convertLines(polygon, newPolygon, tolerance, true);
multiPolygonGeometry.push(newPolygon);
}
features.push(createFeature(id, geojson.geometry.type, multiPolygonGeometry, geojson.properties));
return;
}
default:
throw new Error('Input data is not a valid GeoJSON object.');
}
}
function convertPoint(coords: GeoJSON.Position, out: number[]) {
out.push(projectX(coords[0]), projectY(coords[1]), 0);
}
function convertLine(ring: GeoJSON.Position[], out: StartEndSizeArray, tolerance: number, isPolygon: boolean) {
let x0, y0;
let size = 0;
for (let j = 0; j < ring.length; j++) {
const x = projectX(ring[j][0]);
const y = projectY(ring[j][1]);
out.push(x, y, 0);
if (j > 0) {
if (isPolygon) {
size += (x0 * y - x * y0) / 2; // area
} else {
size += Math.sqrt(Math.pow(x - x0, 2) + Math.pow(y - y0, 2)); // length
}
}
x0 = x;
y0 = y;
}
const last = out.length - 3;
out[2] = 1;
if (tolerance > 0) simplify(out, 0, last, tolerance);
out[last + 2] = 1;
out.size = Math.abs(size);
out.start = 0;
out.end = out.size;
}
function convertLines(rings: GeoJSON.Position[][], out: StartEndSizeArray[], tolerance: number, isPolygon: boolean) {
for (let i = 0; i < rings.length; i++) {
const geom: StartEndSizeArray = [];
convertLine(rings[i], geom, tolerance, isPolygon);
out.push(geom);
}
}
function projectX(x: number) {
return x / 360 + 0.5;
}
function projectY(y: number) {
const sin = Math.sin(y * Math.PI / 180);
const y2 = 0.5 - 0.25 * Math.log((1 + sin) / (1 - sin)) / Math.PI;
return y2 < 0 ? 0 : y2 > 1 ? 1 : y2;
}

View File

@@ -0,0 +1,86 @@
export type GeoJSONVTOptions = {
/**
* Max zoom to preserve detail on
* @default 14
*/
maxZoom?: number;
/**
* Max zoom in the tile index
* @default 5
*/
indexMaxZoom?: number;
/**
* Max number of points per tile in the tile index
* @default 100000
*/
indexMaxPoints?: number;
/**
* Simplification tolerance (higher means simpler)
* @default 3
*/
tolerance?: number;
/**
* Tile extent
* @default 4096
*/
extent?: number;
/**
* Tile buffer on each side
* @default 64
*/
buffer?: number;
/**
* Whether to calculate line metrics
* @default false
*/
lineMetrics?: boolean;
/**
* Name of a feature property to be promoted to feature.id
*/
promoteId?: string | null;
/**
* Whether to generate feature ids. Cannot be used with promoteId
* @default false
*/
generateId?: boolean;
/**
* Whether geojson can be updated (with caveat of a stored simplified copy)
* @default false
*/
updateable?: boolean;
/**
* Logging level (0, 1 or 2)
* @default 0
*/
debug?: number;
};
export type StartEndSizeArray = number[] & { start?: number; end?: number; size?: number };
export type PartialGeoJSONVTFeature = {
id?: number | string | undefined;
tags: GeoJSON.GeoJsonProperties;
minX: number;
minY: number;
maxX: number;
maxY: number;
}
export type GeometryTypeMap = {
Point: number[];
MultiPoint: number[];
LineString: StartEndSizeArray;
MultiLineString: StartEndSizeArray[];
Polygon: StartEndSizeArray[];
MultiPolygon: StartEndSizeArray[][];
}
export type GeometryType = "Point" | "MultiPoint" | "LineString" | "MultiLineString" | "Polygon" | "MultiPolygon";
export type GeoJSONVTInternalFeature = {
[K in GeometryType]: PartialGeoJSONVTFeature & {
type: K;
geometry: GeometryTypeMap[K];
}
}[GeometryType];

View File

@@ -0,0 +1,270 @@
import {test, expect} from 'vitest';
import {applySourceDiff} from './difference';
const options = {
maxZoom: 14,
indexMaxZoom: 5,
indexMaxPoints: 100000,
tolerance: 3,
extent: 4096,
buffer: 64,
updateable: true
};
test('applySourceDiff: adds a feature using the feature id', () => {
const point = {
type: 'Feature' as const,
id: 'point',
geometry: {
type: 'Point' as const,
coordinates: [0, 0]
},
properties: {},
};
const {source} = applySourceDiff([], {
add: [point]
}, options);
expect(source.length).toBe(1);
expect(source[0].id).toBe('point');
});
test('applySourceDiff: adds a feature using the promoteId', () => {
const point2 = {
type: 'Feature' as const,
geometry: {
type: 'Point' as const,
coordinates: [0, 0],
},
properties: {
promoteId: 'point2'
},
};
const {source} = applySourceDiff([], {
add: [point2]
}, {promoteId: 'promoteId'});
expect(source.length).toBe(1);
expect(source[0].id).toBe('point2');
});
test('applySourceDiff: removes a feature by its id', () => {
const point = {
type: 'Point' as const,
id: 'point',
geometry: [0, 0],
tags: {},
minX: 0,
minY: 0,
maxX: 0,
maxY: 0
};
const point2 = {
type: 'Point' as const,
id: 'point2',
geometry: [0, 0],
tags: {},
minX: 0,
minY: 0,
maxX: 0,
maxY: 0
};
const {source} = applySourceDiff([point, point2], {
remove: ['point2'],
}, options);
expect(source.length).toBe(1);
expect(source[0].id).toBe('point');
});
test('applySourceDiff: removeAll clears all features', () => {
const point = {
type: 'Point' as const,
id: 'point',
geometry: [0, 0],
tags: {},
minX: 0,
minY: 0,
maxX: 0,
maxY: 0
};
const point2 = {
type: 'Point' as const,
id: 'point2',
geometry: [0, 0],
tags: {},
minX: 0,
minY: 0,
maxX: 0,
maxY: 0
};
const source = [point, point2];
const result = applySourceDiff(source, {
removeAll: true
}, options);
expect(source).toEqual(result.affected);
expect(result.source).toEqual([]);
});
test('applySourceDiff: updates a feature geometry', () => {
const point = {
type: 'Point' as const,
id: 'point',
geometry: [0, 0],
tags: {},
minX: 0,
minY: 0,
maxX: 0,
maxY: 0
};
const {source} = applySourceDiff([point], {
update: [{
id: 'point',
newGeometry: {
type: 'Point',
coordinates: [1, 0]
}
}]
}, options);
expect(source.length).toBe(1);
expect(source[0].id).toBe('point');
expect(source[0].geometry[0]).toBe(0.5027777777777778);
expect(source[0].geometry[1]).toBe(0.5);
});
test('applySourceDiff: adds properties', () => {
const point = {
type: 'Point' as const,
id: 'point',
geometry: [0, 0],
tags: {},
minX: 0,
minY: 0,
maxX: 0,
maxY: 0
};
const {source} = applySourceDiff([point], {
update: [{
id: 'point',
addOrUpdateProperties: [
{key: 'prop', value: 'value'},
{key: 'prop2', value: 'value2'}
]
}]
}, options);
expect(source.length).toBe(1);
const tags = source[0].tags;
expect(Object.keys(tags).length).toBe(2);
expect(tags.prop).toBe('value');
expect(tags.prop2).toBe('value2');
});
test('applySourceDiff: updates properties', () => {
const point = {
type: 'Point' as const,
id: 'point',
geometry: [0, 0],
tags: {prop: 'value', prop2: 'value2'},
minX: 0,
minY: 0,
maxX: 0,
maxY: 0
};
const {source} = applySourceDiff([point], {
update: [{
id: 'point',
addOrUpdateProperties: [
{key: 'prop2', value: 'value3'}
]
}]
}, options);
expect(source.length).toBe(1);
const tags2 = source[0].tags;
expect(Object.keys(tags2).length).toBe(2);
expect(tags2.prop).toBe('value');
expect(tags2.prop2).toBe('value3');
});
test('applySourceDiff: removes properties', () => {
const point = {
type: 'Point' as const,
id: 'point',
geometry: [0, 0],
tags: {prop: 'value', prop2: 'value2'},
minX: 0,
minY: 0,
maxX: 0,
maxY: 0
};
const {source} = applySourceDiff([point], {
update: [{
id: 'point',
removeProperties: ['prop2']
}]
}, options);
expect(source.length).toBe(1);
const tags3 = source[0].tags;
expect(Object.keys(tags3).length).toBe(1);
expect(tags3.prop).toBe('value');
});
test('applySourceDiff: removes all properties', () => {
const point = {
type: 'Point' as const,
id: 'point',
geometry: [0, 0],
tags: {prop: 'value', prop2: 'value2'},
minX: 0,
minY: 0,
maxX: 0,
maxY: 0
};
const {source} = applySourceDiff([point], {
update: [{
id: 'point',
removeAllProperties: true,
}]
}, options);
expect(source.length).toBe(1);
expect(Object.keys(source[0].tags).length).toBe(0);
});
test('applySourceDiff: empty update preserves properties', () => {
const point = {
type: 'Point' as const,
id: 'point',
geometry: [0, 0],
tags: {prop: 'value', prop2: 'value2'},
minX: 0,
minY: 0,
maxX: 0,
maxY: 0
};
const {source} = applySourceDiff([point], {
update: [{id: 'point'}]
}, options);
expect(source.length).toBe(1);
const tags2 = source[0].tags;
expect(Object.keys(tags2).length).toBe(2);
expect(tags2.prop).toBe('value');
expect(tags2.prop2).toBe('value2');
});

View File

@@ -0,0 +1,218 @@
import {convert} from './convert';
import {wrap} from './wrap';
import type { GeoJSONVTInternalFeature, GeoJSONVTOptions } from './definitions';
export type GeoJSONVTSourceDiff = {
/**
* If true, clear all existing features
*/
removeAll?: boolean;
/**
* Array of feature IDs to remove
*/
remove?: (string | number)[];
/**
* Array of GeoJSON features to add
*/
add?: GeoJSON.Feature[];
/**
* Array of per-feature updates
*/
update?: GeoJSONVTFeatureDiff[];
};
export type GeoJSONVTFeatureDiff = {
/**
* ID of the feature being updated
*/
id: string | number;
/**
* Optional new geometry
*/
newGeometry?: GeoJSON.Geometry;
/**
* Remove all properties if true
*/
removeAllProperties?: boolean;
/**
* Specific properties to delete
*/
removeProperties?: string[];
/**
* Properties to add or update
*/
addOrUpdateProperties?: {
key: string;
value: unknown;
}[];
};
type HashedGeoJSONVTSourceDiff = {
removeAll?: boolean | undefined;
remove: Set<string | number>;
add: Map<string | number | undefined, GeoJSON.Feature>;
update: Map<string | number, GeoJSONVTFeatureDiff>;
};
/**
* Applies a GeoJSON Source Diff to an existing set of simplified features
* @param source
* @param dataDiff
* @param options
* @returns
*/
export function applySourceDiff(source: GeoJSONVTInternalFeature[], dataDiff: GeoJSONVTSourceDiff, options: GeoJSONVTOptions) {
// convert diff to sets/maps for o(1) lookups
const diff = diffToHashed(dataDiff);
// collection for features that will be affected by this update
let affected: GeoJSONVTInternalFeature[] = [];
// full removal - clear everything before applying diff
if (diff.removeAll) {
affected = source;
source = [];
}
// remove/add features and collect affected ones
if (diff.remove.size || diff.add.size) {
const removeFeatures = [];
// collect source features to be removed
for (const feature of source) {
const {id} = feature;
// explicit feature removal
if (diff.remove.has(id)) {
removeFeatures.push(feature);
// feature with duplicate id being added
} else if (diff.add.has(id)) {
removeFeatures.push(feature);
}
}
// collect affected and remove from source
if (removeFeatures.length) {
affected.push(...removeFeatures);
const removeIds = new Set(removeFeatures.map(f => f.id));
source = source.filter(f => !removeIds.has(f.id));
}
// convert and add new features
if (diff.add.size) {
// projects and adds simplification info
let addFeatures = convert({type: 'FeatureCollection', features: Array.from(diff.add.values())}, options);
// wraps features (ie extreme west and extreme east)
addFeatures = wrap(addFeatures, options);
affected.push(...addFeatures);
source.push(...addFeatures);
}
}
if (diff.update.size) {
for (const [id, update] of diff.update) {
const featureIndex = source.findIndex(f => f.id === id);
if (featureIndex === -1) continue;
const feature = source[featureIndex];
// get updated geojsonvt simplified feature
const updatedFeature = getUpdatedFeature(feature, update, options);
if (!updatedFeature) continue;
// track both features for invalidation
affected.push(feature, updatedFeature);
// replace old feature with updated feature
source[featureIndex] = updatedFeature;
}
}
return {affected, source};
}
// return an updated geojsonvt simplified feature
function getUpdatedFeature(vtFeature: GeoJSONVTInternalFeature, update: GeoJSONVTFeatureDiff, options: GeoJSONVTOptions): GeoJSONVTInternalFeature | null {
const changeGeometry = !!update.newGeometry;
const changeProps =
update.removeAllProperties ||
update.removeProperties?.length > 0 ||
update.addOrUpdateProperties?.length > 0;
// if geometry changed, need to create new geojson feature and convert to simplified format
if (changeGeometry) {
const geojsonFeature = {
type: 'Feature' as const,
id: vtFeature.id,
geometry: update.newGeometry,
properties: changeProps ? applyPropertyUpdates(vtFeature.tags, update) : vtFeature.tags
};
// projects and adds simplification info
let features = convert({type: 'FeatureCollection', features: [geojsonFeature]}, options);
// wraps features (ie extreme west and extreme east)
features = wrap(features, options);
return features[0];
}
// only properties changed - update tags directly
if (changeProps) {
const feature = {...vtFeature};
feature.tags = applyPropertyUpdates(feature.tags, update);
return feature;
}
return null;
}
/**
* helper to apply property updates from a diff update object to a properties object
*/
function applyPropertyUpdates(tags: GeoJSON.GeoJsonProperties, update: GeoJSONVTFeatureDiff): GeoJSON.GeoJsonProperties {
if (update.removeAllProperties) {
return {};
}
const properties = {...tags || {}};
if (update.removeProperties) {
for (const key of update.removeProperties) {
delete properties[key];
}
}
if (update.addOrUpdateProperties) {
for (const {key, value} of update.addOrUpdateProperties) {
properties[key] = value;
}
}
return properties;
}
/**
* Convert a GeoJSON Source Diff to an idempotent hashed representation using Sets and Maps
*/
export function diffToHashed(diff: GeoJSONVTSourceDiff): HashedGeoJSONVTSourceDiff {
if (!diff) return {
remove: new Set(),
add: new Map(),
update: new Map()
};
const hashed: HashedGeoJSONVTSourceDiff = {
removeAll: diff.removeAll,
remove: new Set(diff.remove || []),
add: new Map(diff.add?.map(feature => [feature.id, feature])),
update: new Map(diff.update?.map(update => [update.id, update]))
};
return hashed;
}

View File

@@ -0,0 +1,64 @@
import type { GeoJSONVTInternalFeature, GeometryType, GeometryTypeMap } from "./definitions";
export type SupportedGeometries = GeoJSON.Point | GeoJSON.MultiPoint | GeoJSON.LineString | GeoJSON.MultiLineString | GeoJSON.Polygon | GeoJSON.MultiPolygon;
/**
*
* @param id - the feature's ID
* @param type - the feature's type
* @param geom - the feature's geometry
* @param tags - the feature's properties
* @returns the created feature
*/
export function createFeature<T extends GeometryType>(id: number | string | undefined, type: T, geom: GeometryTypeMap[T], tags: GeoJSON.GeoJsonProperties): GeoJSONVTInternalFeature {
// This is mostly for TypeScript type narrowing
const data = { type, geom } as { [K in GeometryType]: { type: K, geom: GeometryTypeMap[K] } }[GeometryType];
const feature = {
id: id == null ? null : id,
type: data.type,
geometry: data.geom,
tags,
minX: Infinity,
minY: Infinity,
maxX: -Infinity,
maxY: -Infinity
} as GeoJSONVTInternalFeature;
switch (data.type) {
case 'Point':
case 'MultiPoint':
case 'LineString':
calcLineBBox(feature, data.geom);
break;
case 'Polygon':
// the outer ring (ie [0]) contains all inner rings
calcLineBBox(feature, data.geom[0]);
break;
case 'MultiLineString':
for (const line of data.geom) {
calcLineBBox(feature, line);
}
break;
case 'MultiPolygon':
for (const polygon of data.geom) {
// the outer ring (ie [0]) contains all inner rings
calcLineBBox(feature, polygon[0]);
}
break;
}
return feature;
}
function calcLineBBox(feature: GeoJSONVTInternalFeature, geom: number[]) {
for (let i = 0; i < geom.length; i += 3) {
feature.minX = Math.min(feature.minX, geom[i]);
feature.minY = Math.min(feature.minY, geom[i + 1]);
feature.maxX = Math.max(feature.maxX, geom[i]);
feature.maxY = Math.max(feature.maxY, geom[i + 1]);
}
}

View File

@@ -0,0 +1,394 @@
import {convert} from './convert';
import {clip} from './clip';
import {wrap} from './wrap';
import {transformTile, type GeoJSONVTFeature, type GeoJSONVTFeatureNonPoint, type GeoJSONVTFeaturePoint, type GeoJSONVTTile} from './transform';
import {createTile, type GeoJSONVTInternalTile, type GeoJSONVTInternalTileFeature, type GeoJSONVTInternalTileFeaturePoint, type GeoJSONVTInternalTileFeaturNonPoint} from './tile';
import {applySourceDiff, type GeoJSONVTFeatureDiff, type GeoJSONVTSourceDiff} from './difference';
import type { GeoJSONVTInternalFeature, GeoJSONVTOptions, GeometryType, GeometryTypeMap, PartialGeoJSONVTFeature, StartEndSizeArray } from './definitions';
const defaultOptions: GeoJSONVTOptions = {
maxZoom: 14,
indexMaxZoom: 5,
indexMaxPoints: 100000,
tolerance: 3,
extent: 4096,
buffer: 64,
lineMetrics: false,
promoteId: null,
generateId: false,
updateable: false,
debug: 0
};
/**
* Main class for creating and managing a vector tile index from GeoJSON data.
*/
class GeoJSONVT {
private options: GeoJSONVTOptions;
/** @internal */
public tiles: {[key: string]: GeoJSONVTInternalTile};
private tileCoords: {z: number, x: number, y: number, id: number}[];
/** @internal */
public stats: {[key: string]: number} = {};
/** @internal */
public total: number = 0;
private source?: GeoJSONVTInternalFeature[];
constructor(data: GeoJSON.GeoJSON, options: GeoJSONVTOptions) {
options = this.options = Object.assign({}, defaultOptions, options);
const debug = options.debug;
if (debug) console.time('preprocess data');
if (options.maxZoom < 0 || options.maxZoom > 24) throw new Error('maxZoom should be in the 0-24 range');
if (options.promoteId && options.generateId) throw new Error('promoteId and generateId cannot be used together.');
// projects and adds simplification info
let features = convert(data, options);
// tiles and tileCoords are part of the public API
this.tiles = {};
this.tileCoords = [];
if (debug) {
console.timeEnd('preprocess data');
console.log('index: maxZoom: %d, maxPoints: %d', options.indexMaxZoom, options.indexMaxPoints);
console.time('generate tiles');
this.stats = {};
this.total = 0;
}
// wraps features (ie extreme west and extreme east)
features = wrap(features, options);
// start slicing from the top tile down
if (features.length) {
this.splitTile(features, 0, 0, 0);
}
// for updateable indexes, store a copy of the original simplified features
if (options.updateable) {
this.source = features;
}
if (debug) {
if (features.length) console.log('features: %d, points: %d', this.tiles[0].numFeatures, this.tiles[0].numPoints);
console.timeEnd('generate tiles');
console.log('tiles generated:', this.total, JSON.stringify(this.stats));
}
}
/**
* splits features from a parent tile to sub-tiles.
* z, x, and y are the coordinates of the parent tile
* cz, cx, and cy are the coordinates of the target tile
*
* If no target tile is specified, splitting stops when we reach the maximum
* zoom or the number of points is low as specified in the options.
* @internal
* @param features - features to split
* @param z - tile zoom level
* @param x - tile x coordinate
* @param y - tile y coordinate
* @param cz - target tile zoom level
* @param cx - target tile x coordinate
* @param cy - target tile y coordinate
*/
splitTile(features: GeoJSONVTInternalFeature[], z: number, x: number, y: number, cz?: number, cx?: number, cy?: number) {
const stack = [features, z, x, y];
const options = this.options;
const debug = options.debug;
// avoid recursion by using a processing queue
while (stack.length) {
y = stack.pop() as number;
x = stack.pop() as number;
z = stack.pop() as number;
features = stack.pop() as GeoJSONVTInternalFeature[];
const z2 = 1 << z;
const id = toID(z, x, y);
let tile = this.tiles[id];
if (!tile) {
if (debug > 1) console.time('creation');
tile = this.tiles[id] = createTile(features, z, x, y, options);
this.tileCoords.push({z, x, y, id});
if (debug) {
if (debug > 1) {
console.log('tile z%d-%d-%d (features: %d, points: %d, simplified: %d)',
z, x, y, tile.numFeatures, tile.numPoints, tile.numSimplified);
console.timeEnd('creation');
}
const key = `z${ z}`;
this.stats[key] = (this.stats[key] || 0) + 1;
this.total++;
}
}
// save reference to original geometry in tile so that we can drill down later if we stop now
tile.source = features;
// if it's the first-pass tiling
if (cz == null) {
// stop tiling if we reached max zoom, or if the tile is too simple
if (z === options.indexMaxZoom || tile.numPoints <= options.indexMaxPoints) continue;
// if a drilldown to a specific tile
} else if (z === options.maxZoom || z === cz) {
// stop tiling if we reached base zoom or our target tile zoom
continue;
} else if (cz != null) {
// stop tiling if it's not an ancestor of the target tile
const zoomSteps = cz - z;
if (x !== cx >> zoomSteps || y !== cy >> zoomSteps) continue;
}
// if we slice further down, no need to keep source geometry
tile.source = null;
if (!features.length) continue;
if (debug > 1) console.time('clipping');
// values we'll use for clipping
const k1 = 0.5 * options.buffer / options.extent;
const k2 = 0.5 - k1;
const k3 = 0.5 + k1;
const k4 = 1 + k1;
let tl = null;
let bl = null;
let tr = null;
let br = null;
const left = clip(features, z2, x - k1, x + k3, 0, tile.minX, tile.maxX, options);
const right = clip(features, z2, x + k2, x + k4, 0, tile.minX, tile.maxX, options);
if (left) {
tl = clip(left, z2, y - k1, y + k3, 1, tile.minY, tile.maxY, options);
bl = clip(left, z2, y + k2, y + k4, 1, tile.minY, tile.maxY, options);
}
if (right) {
tr = clip(right, z2, y - k1, y + k3, 1, tile.minY, tile.maxY, options);
br = clip(right, z2, y + k2, y + k4, 1, tile.minY, tile.maxY, options);
}
if (debug > 1) console.timeEnd('clipping');
stack.push(tl || [], z + 1, x * 2, y * 2);
stack.push(bl || [], z + 1, x * 2, y * 2 + 1);
stack.push(tr || [], z + 1, x * 2 + 1, y * 2);
stack.push(br || [], z + 1, x * 2 + 1, y * 2 + 1);
}
}
/**
* Given z, x, and y tile coordinates, returns the corresponding tile with geometries in tile coordinates, much like MVT data is stored.
* @param z - tile zoom level
* @param x - tile x coordinate
* @param y - tile y coordinate
* @returns the transformed tile or null if not found
*/
getTile(z: number | string, x: number | string, y: number | string): GeoJSONVTTile | null {
z = +z;
x = +x;
y = +y;
const options = this.options;
const {extent, debug} = options;
if (z < 0 || z > 24) return null;
const z2 = 1 << z;
x = (x + z2) & (z2 - 1); // wrap tile x coordinate
const id = toID(z, x, y);
if (this.tiles[id]) {
return transformTile(this.tiles[id], extent);
}
if (debug > 1) console.log('drilling down to z%d-%d-%d', z, x, y);
let z0 = z;
let x0 = x;
let y0 = y;
let parent;
while (!parent && z0 > 0) {
z0--;
x0 = x0 >> 1;
y0 = y0 >> 1;
parent = this.tiles[toID(z0, x0, y0)];
}
if (!parent?.source) return null;
// if we found a parent tile containing the original geometry, we can drill down from it
if (debug > 1) {
console.log('found parent tile z%d-%d-%d', z0, x0, y0);
console.time('drilling down');
}
this.splitTile(parent.source, z0, x0, y0, z, x, y);
if (debug > 1) console.timeEnd('drilling down');
if (!this.tiles[id]) return null;
return transformTile(this.tiles[id], extent);
}
/**
* Invalidates (removes) tiles affected by the provided features
* @internal
* @param features
*/
invalidateTiles(features: GeoJSONVTInternalFeature[]) {
const options = this.options;
const {debug} = options;
// calculate bounding box of all features for trivial reject
let minX = Infinity;
let maxX = -Infinity;
let minY = Infinity;
let maxY = -Infinity;
for (const feature of features) {
minX = Math.min(minX, feature.minX);
maxX = Math.max(maxX, feature.maxX);
minY = Math.min(minY, feature.minY);
maxY = Math.max(maxY, feature.maxY);
}
// tile buffer clipping value - not halved as in splitTile above because checking against tile's own extent
const k1 = options.buffer / options.extent;
// track removed tile ids for o(1) lookup
const removedLookup = new Set();
// iterate through existing tiles and remove ones that are affected by features
for (const id in this.tiles) {
const tile = this.tiles[id];
// calculate tile bounds including buffer
const z2 = 1 << tile.z;
const tileMinX = (tile.x - k1) / z2;
const tileMaxX = (tile.x + 1 + k1) / z2;
const tileMinY = (tile.y - k1) / z2;
const tileMaxY = (tile.y + 1 + k1) / z2;
// trivial reject if feature bounds don't intersect tile
if (maxX < tileMinX || minX >= tileMaxX ||
maxY < tileMinY || minY >= tileMaxY) {
continue;
}
// check if any feature intersects with the tile
let intersects = false;
for (const feature of features) {
if (feature.maxX >= tileMinX && feature.minX < tileMaxX &&
feature.maxY >= tileMinY && feature.minY < tileMaxY) {
intersects = true;
break;
}
}
if (!intersects) continue;
if (debug) {
if (debug > 1) {
console.log('invalidate tile z%d-%d-%d (features: %d, points: %d, simplified: %d)',
tile.z, tile.x, tile.y, tile.numFeatures, tile.numPoints, tile.numSimplified);
}
const key = `z${ tile.z}`;
this.stats[key] = (this.stats[key] || 0) - 1;
this.total--;
}
delete this.tiles[id];
removedLookup.add(id);
}
// remove tile coords that are no longer in the index
if (removedLookup.size) {
this.tileCoords = this.tileCoords.filter(c => !removedLookup.has(c.id));
}
}
/**
* Updates the tile index by adding and/or removing geojson features
* invalidates tiles that are affected by the update for regeneration on next getTile call.
* @param diff - the source diff object
*/
updateData(diff: GeoJSONVTSourceDiff) {
const options = this.options;
const debug = options.debug;
if (!options.updateable) throw new Error('to update tile geojson `updateable` option must be set to true');
// apply diff and collect affected features and updated source that will be used to invalidate tiles
const {affected, source} = applySourceDiff(this.source, diff, options);
// nothing has changed
if (!affected.length) return;
// update source with new simplified feature set
this.source = source;
if (debug > 1) {
console.log('invalidating tiles');
console.time('invalidating');
}
this.invalidateTiles(affected);
if (debug > 1) console.timeEnd('invalidating');
// re-generate root tile with updated feature set
const [z, x, y] = [0, 0, 0];
const rootTile = createTile(this.source, z, x, y, this.options);
rootTile.source = this.source;
// update tile index with new root tile - ready for getTile calls
const id = toID(z, x, y);
this.tiles[id] = rootTile;
this.tileCoords.push({z, x, y, id});
if (debug) {
const key = `z${ z}`;
this.stats[key] = (this.stats[key] || 0) + 1;
this.total++;
}
}
}
function toID(z: number, x: number, y: number): number {
return (((1 << z) * y + x) * 32) + z;
}
export default function geojsonvt(data: GeoJSON.GeoJSON, options?: GeoJSONVTOptions) {
return new GeoJSONVT(data, options);
}
export type {
GeoJSONVTInternalFeature,
GeoJSONVTOptions,
GeoJSONVTInternalTile,
GeoJSONVTInternalTileFeature,
GeometryType,
PartialGeoJSONVTFeature,
GeoJSONVT,
GeometryTypeMap,
StartEndSizeArray,
GeoJSONVTTile,
GeoJSONVTFeature,
GeoJSONVTSourceDiff,
GeoJSONVTFeatureDiff,
GeoJSONVTFeaturePoint,
GeoJSONVTFeatureNonPoint,
GeoJSONVTInternalTileFeaturePoint,
GeoJSONVTInternalTileFeaturNonPoint
};

View File

@@ -0,0 +1,73 @@
import {test, expect} from 'vitest';
import {simplify} from './simplify';
const points = [
[0.22455,0.25015],[0.22691,0.24419],[0.23331,0.24145],[0.23498,0.23606],
[0.24421,0.23276],[0.26259,0.21531],[0.26776,0.21381],[0.27357,0.20184],
[0.27312,0.19216],[0.27762,0.18903],[0.28036,0.18141],[0.28651,0.17774],
[0.29241,0.15937],[0.29691,0.15564],[0.31495,0.15137],[0.31975,0.14516],
[0.33033,0.13757],[0.34148,0.13996],[0.36998,0.13789],[0.38739,0.14251],
[0.39128,0.13939],[0.40952,0.14114],[0.41482,0.13975],[0.42772,0.12730],
[0.43960,0.11974],[0.47493,0.10787],[0.48651,0.10675],[0.48920,0.10945],
[0.49379,0.10863],[0.50474,0.11966],[0.51296,0.12235],[0.51863,0.12089],
[0.52409,0.12688],[0.52957,0.12786],[0.53421,0.14093],[0.53927,0.14724],
[0.56769,0.14891],[0.57525,0.15726],[0.58062,0.15815],[0.60153,0.15685],
[0.61774,0.15986],[0.62200,0.16704],[0.62955,0.19460],[0.63890,0.19561],
[0.64126,0.20081],[0.65177,0.20456],[0.67155,0.22255],[0.68368,0.21745],
[0.69525,0.21915],[0.70064,0.21798],[0.70312,0.21436],[0.71226,0.21587],
[0.72149,0.21281],[0.72781,0.21336],[0.72998,0.20873],[0.73532,0.20820],
[0.73994,0.20477],[0.76998,0.20842],[0.77960,0.21687],[0.78420,0.21816],
[0.80024,0.21462],[0.81053,0.21973],[0.81719,0.22682],[0.82077,0.23617],
[0.82723,0.23616],[0.82989,0.23989],[0.85100,0.24894],[0.85988,0.25549],
[0.86521,0.26853],[0.85795,0.28030],[0.86548,0.29145],[0.86681,0.29866],
[0.86468,0.30271],[0.86779,0.30617],[0.85987,0.31137],[0.86008,0.31435],
[0.85829,0.31494],[0.85810,0.32760],[0.85454,0.33540],[0.86092,0.34300],
[0.85643,0.35015],[0.85142,0.35296],[0.84984,0.35959],[0.85456,0.36553],
[0.84974,0.37038],[0.84409,0.37189],[0.84475,0.38044],[0.84152,0.38367],
[0.83957,0.39040],[0.84559,0.39905],[0.84840,0.40755],[0.84371,0.41130],
[0.84409,0.41988],[0.83951,0.43276],[0.84133,0.44104],[0.84762,0.44922],
[0.84716,0.45844],[0.85138,0.46279],[0.85397,0.47115],[0.86636,0.48077]
];
const simplified = [
[0.22455,0.25015],[0.26776,0.21381],[0.29691,0.15564],[0.33033,0.13757],
[0.40952,0.14114],[0.4396,0.11974],[0.48651,0.10675],[0.52957,0.12786],
[0.53927,0.14724],[0.56769,0.14891],[0.61774,0.15986],[0.62955,0.1946],
[0.67155,0.22255],[0.72781,0.21336],[0.73994,0.20477],[0.76998,0.20842],
[0.7842,0.21816],[0.80024,0.21462],[0.82077,0.23617],[0.85988,0.25549],
[0.86521,0.26853],[0.85795,0.2803],[0.86779,0.30617],[0.85829,0.31494],
[0.85454,0.3354],[0.86092,0.343],[0.84984,0.35959],[0.85456,0.36553],
[0.84409,0.37189],[0.83957,0.3904],[0.8484,0.40755],[0.83951,0.43276],
[0.85397,0.47115],[0.86636,0.48077]
];
test('simplifies points correctly with the given tolerance', () => {
const coords = [];
for (let i = 0; i < points.length; i++) {
coords.push(points[i][0], points[i][1], 0);
}
coords[2] = 1;
coords[coords.length - 1] = 1;
simplify(coords, 0, coords.length - 3, 0.001 * 0.001);
const result = [];
for (let i = 0; i < coords.length; i += 3) {
if (coords[i + 2] > 0.005 * 0.005) {
result.push([coords[i], coords[i + 1]]);
}
}
expect(result).toEqual(simplified);
});
test('does not throw max call stack error on bad long input', () => {
const coords: number[] = [];
for (let i = 0; i < 1400; i++) {
coords.push(0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0);
}
expect(() => {
simplify(coords, 0, coords.length, 2e-15);
}).not.toThrow();
});

View File

@@ -0,0 +1,78 @@
/**
* calculate simplification data using optimized Douglas-Peucker algorithm
* @param coords - flat array of coordinates
* @param first - index of the first coordinate in the segment
* @param last - index of the last coordinate in the segment
* @param sqTolerance - square tolerance value
*/
export function simplify(coords: number[], first: number, last: number, sqTolerance: number) {
let maxSqDist = sqTolerance;
const mid = first + ((last - first) >> 1);
let minPosToMid = last - first;
let index;
const ax = coords[first];
const ay = coords[first + 1];
const bx = coords[last];
const by = coords[last + 1];
for (let i = first + 3; i < last; i += 3) {
const d = getSqSegDist(coords[i], coords[i + 1], ax, ay, bx, by);
if (d > maxSqDist) {
index = i;
maxSqDist = d;
continue;
}
if (d === maxSqDist) {
// a workaround to ensure we choose a pivot close to the middle of the list,
// reducing recursion depth, for certain degenerate inputs
// https://github.com/mapbox/geojson-vt/issues/104
const posToMid = Math.abs(i - mid);
if (posToMid < minPosToMid) {
index = i;
minPosToMid = posToMid;
}
}
}
if (maxSqDist > sqTolerance) {
if (index - first > 3) simplify(coords, first, index, sqTolerance);
coords[index + 2] = maxSqDist;
if (last - index > 3) simplify(coords, index, last, sqTolerance);
}
}
/**
* Claculates the square distance from a point to a segment
* @param px - x coordinate of the point
* @param py - y coordinate of the point
* @param x - x coordinate of the first segment endpoint
* @param y - y coordinate of the first segment endpoint
* @param bx - x coordinate of the second segment endpoint
* @param by - y coordinate of the second segment endpoint
* @returns square distance from a point to a segment
*/
function getSqSegDist(px: number, py: number, x: number, y: number, bx: number, by: number): number {
let dx = bx - x;
let dy = by - y;
if (dx !== 0 || dy !== 0) {
const t = ((px - x) * dx + (py - y) * dy) / (dx * dx + dy * dy);
if (t > 1) {
x = bx;
y = by;
} else if (t > 0) {
x += dx * t;
y += dy * t;
}
}
dx = px - x;
dy = py - y;
return dx * dx + dy * dy;
}

View File

@@ -0,0 +1,194 @@
import type { GeoJSONVTInternalFeature, GeoJSONVTOptions, StartEndSizeArray } from "./definitions";
export type GeoJSONVTInternalTileFeaturePoint = {
id? : number | string | undefined;
type: 1;
tags: GeoJSON.GeoJsonProperties | null;
geometry: number[];
}
export type GeoJSONVTInternalTileFeaturNonPoint = {
id? : number | string | undefined;
type: 2 | 3;
tags: GeoJSON.GeoJsonProperties | null;
geometry: number[][];
}
export type GeoJSONVTInternalTileFeature = GeoJSONVTInternalTileFeaturePoint | GeoJSONVTInternalTileFeaturNonPoint;
export type GeoJSONVTInternalTile = {
features: GeoJSONVTInternalTileFeature[];
numPoints: number;
numSimplified: number;
numFeatures: number;
x: number;
y: number;
z: number;
transformed: boolean;
minX: number;
minY: number;
maxX: number;
maxY: number;
source: GeoJSONVTInternalFeature[] | null;
}
/**
* Creates a tile object from the given features
* @param features - the features to include in the tile
* @param z
* @param tx
* @param ty
* @param options - the options object
* @returns the created tile
*/
export function createTile(features: GeoJSONVTInternalFeature[], z: number, tx: number, ty: number, options: GeoJSONVTOptions): GeoJSONVTInternalTile {
const tolerance = z === options.maxZoom ? 0 : options.tolerance / ((1 << z) * options.extent);
const tile = {
features: [] as GeoJSONVTInternalTileFeature[],
numPoints: 0,
numSimplified: 0,
numFeatures: features.length,
source: null as GeoJSONVTInternalFeature[] | null,
x: tx,
y: ty,
z,
transformed: false,
minX: 2,
minY: 1,
maxX: -1,
maxY: 0
};
for (const feature of features) {
addFeature(tile, feature, tolerance, options);
}
return tile;
}
function addFeature(tile: GeoJSONVTInternalTile, feature: GeoJSONVTInternalFeature, tolerance: number, options: GeoJSONVTOptions) {
tile.minX = Math.min(tile.minX, feature.minX);
tile.minY = Math.min(tile.minY, feature.minY);
tile.maxX = Math.max(tile.maxX, feature.maxX);
tile.maxY = Math.max(tile.maxY, feature.maxY);
let tags = feature.tags || null;
let tileFeature: GeoJSONVTInternalTileFeature;
switch (feature.type) {
case 'Point':
case 'MultiPoint': {
const geometry: number[] = [];
for (let i = 0; i < feature.geometry.length; i += 3) {
geometry.push(feature.geometry[i] , feature.geometry[i + 1]);
tile.numPoints++;
tile.numSimplified++;
}
if (!geometry.length) return;
tileFeature = {
type: 1,
tags: tags,
geometry: geometry
}
break;
}
case 'LineString': {
const geometry: number[][] = [];
addLine(geometry, feature.geometry, tile, tolerance, false, false);
if (!geometry.length) return;
if (options.lineMetrics) {
tags = {};
for (const key in feature.tags) tags[key] = feature.tags[key];
// HM TODO: replace with geojsonvt
tags['mapbox_clip_start'] = feature.geometry.start / feature.geometry.size;
tags['mapbox_clip_end'] = feature.geometry.end / feature.geometry.size;
}
tileFeature = {
type: 2,
tags: tags,
geometry: geometry
}
break;
}
case 'MultiLineString':
case 'Polygon': {
const geometry: number[][] = [];
for (let i = 0; i < feature.geometry.length; i++) {
addLine(geometry, feature.geometry[i], tile, tolerance, feature.type === 'Polygon', i === 0);
}
if (!geometry.length) return;
tileFeature = {
type: feature.type === 'Polygon' ? 3 : 2,
tags: tags,
geometry: geometry
}
break;
}
case 'MultiPolygon': {
const geometry: number[][] = [];
for (let k = 0; k < feature.geometry.length; k++) {
const polygon = feature.geometry[k];
for (let i = 0; i < polygon.length; i++) {
addLine(geometry, polygon[i], tile, tolerance, true, i === 0);
}
}
if (!geometry.length) return;
tileFeature = {
type: 3,
tags: tags,
geometry: geometry
}
break;
}
}
if (feature.id !== null) {
tileFeature.id = feature.id;
}
tile.features.push(tileFeature);
}
function addLine(result: number[][], geom: StartEndSizeArray, tile: GeoJSONVTInternalTile, tolerance: number, isPolygon: boolean, isOuter: boolean) {
const sqTolerance = tolerance * tolerance;
if (tolerance > 0 && (geom.size < (isPolygon ? sqTolerance : tolerance))) {
tile.numPoints += geom.length / 3;
return;
}
const ring = [];
for (let i = 0; i < geom.length; i += 3) {
if (tolerance === 0 || geom[i + 2] > sqTolerance) {
tile.numSimplified++;
ring.push(geom[i], geom[i + 1]);
}
tile.numPoints++;
}
if (isPolygon) rewind(ring, isOuter);
result.push(ring);
}
function rewind(ring: number[], clockwise: boolean) {
let area = 0;
for (let i = 0, len = ring.length, j = len - 2; i < len; j = i, i += 2) {
area += (ring[i] - ring[j]) * (ring[i + 1] + ring[j + 1]);
}
if (area > 0 !== clockwise) return;
for (let i = 0, len = ring.length; i < len / 2; i += 2) {
const x = ring[i];
const y = ring[i + 1];
ring[i] = ring[len - 2 - i];
ring[i + 1] = ring[len - 1 - i];
ring[len - 2 - i] = x;
ring[len - 1 - i] = y;
}
}

View File

@@ -0,0 +1,70 @@
import type { GeoJSONVTInternalTile } from "./tile";
export type GeoJSONVTFeaturePoint = {
id? : number | string | undefined;
type: 1;
tags: GeoJSON.GeoJsonProperties | null;
geometry: [number, number][]
}
export type GeoJSONVTFeatureNonPoint = {
id? : number | string | undefined;
type: 2 | 3;
tags: GeoJSON.GeoJsonProperties | null;
geometry: [number, number][][]
}
export type GeoJSONVTFeature = GeoJSONVTFeaturePoint | GeoJSONVTFeatureNonPoint;
export type GeoJSONVTTile = GeoJSONVTInternalTile & {
transformed: true;
features: GeoJSONVTFeature[]
}
/**
* Transforms the coordinates of each feature in the given tile from
* mercator-projected space into (extent x extent) tile space.
* @param tile - the tile to transform, this gets modified in place
* @param extent - the tile extent (usually 4096)
* @returns the transformed tile
*/
export function transformTile(tile: GeoJSONVTInternalTile, extent: number): GeoJSONVTTile {
if (tile.transformed) {
return tile as GeoJSONVTTile;
}
const z2 = 1 << tile.z;
const tx = tile.x;
const ty = tile.y;
for (const feature of tile.features) {
if (feature.type === 1) {
const pointGeometry: [number, number][] = []
for (let j = 0; j < feature.geometry.length; j += 2) {
pointGeometry.push(transformPoint(feature.geometry[j], feature.geometry[j + 1], extent, z2, tx, ty));
}
(feature as unknown as GeoJSONVTFeaturePoint).geometry = pointGeometry;
continue;
}
const geometry: [number, number][][] = [];
for (const singleGeom of feature.geometry) {
const ring: [number, number][] = [];
for (let k = 0; k < singleGeom.length; k += 2) {
ring.push(transformPoint(singleGeom[k], singleGeom[k + 1], extent, z2, tx, ty));
}
geometry.push(ring);
}
(feature as unknown as GeoJSONVTFeatureNonPoint).geometry = geometry;
}
tile.transformed = true;
return tile as GeoJSONVTTile;
}
function transformPoint(x: number, y: number, extent: number, z2: number, tx: number, ty: number): [number, number] {
return [
Math.round(extent * (x * z2 - tx)),
Math.round(extent * (y * z2 - ty))
];
}

View File

@@ -0,0 +1,81 @@
import {clip} from './clip';
import type { GeoJSONVTInternalFeature, GeoJSONVTOptions, StartEndSizeArray } from './definitions';
import {createFeature} from './feature';
export function wrap(features: GeoJSONVTInternalFeature[], options: GeoJSONVTOptions): GeoJSONVTInternalFeature[] {
const buffer = options.buffer / options.extent;
let merged = features;
const left = clip(features, 1, -1 - buffer, buffer, 0, -1, 2, options); // left world copy
const right = clip(features, 1, 1 - buffer, 2 + buffer, 0, -1, 2, options); // right world copy
if (!left && !right) return merged;
merged = clip(features, 1, -buffer, 1 + buffer, 0, -1, 2, options) || []; // center world copy
if (left) merged = shiftFeatureCoords(left, 1).concat(merged); // merge left into center
if (right) merged = merged.concat(shiftFeatureCoords(right, -1)); // merge right into center
return merged;
}
function shiftFeatureCoords(features: GeoJSONVTInternalFeature[], offset: number): GeoJSONVTInternalFeature[] {
const newFeatures = [];
for (const feature of features) {
switch (feature.type) {
case 'Point':
case 'MultiPoint':
case 'LineString': {
const newGeometry = shiftCoords(feature.geometry, offset);
newFeatures.push(createFeature(feature.id, feature.type, newGeometry, feature.tags));
continue;
}
case 'MultiLineString':
case 'Polygon': {
const newGeometry = [];
for (const line of feature.geometry) {
newGeometry.push(shiftCoords(line, offset));
}
newFeatures.push(createFeature(feature.id, feature.type, newGeometry, feature.tags));
continue;
}
case 'MultiPolygon': {
const newGeometry = [];
for (const polygon of feature.geometry) {
const newPolygon = [];
for (const line of polygon) {
newPolygon.push(shiftCoords(line, offset));
}
newGeometry.push(newPolygon);
}
newFeatures.push(createFeature(feature.id, feature.type, newGeometry, feature.tags));
continue;
}
}
}
return newFeatures;
}
function shiftCoords(points: StartEndSizeArray, offset: number): number[] | StartEndSizeArray {
const newPoints: StartEndSizeArray = [];
newPoints.size = points.size;
if (points.start !== undefined) {
newPoints.start = points.start;
newPoints.end = points.end;
}
for (let i = 0; i < points.length; i += 3) {
newPoints.push(points[i] + offset, points[i + 1], points[i + 2]);
}
return newPoints;
}

62
node_modules/@maplibre/vt-pbf/package.json generated vendored Normal file
View File

@@ -0,0 +1,62 @@
{
"name": "@maplibre/vt-pbf",
"version": "4.3.0",
"description": "Serialize mapbox vector tiles to binary protobufs in javascript.",
"main": "dist/index.es.js",
"types": "dist/index.d.ts",
"files": [
"dist"
],
"scripts": {
"lint": "eslint index.ts lib/**/*.ts test/**/*.ts",
"test-unit": "vitest run --config vitest.config.unit.ts",
"test-unit-ci": "vitest run --config vitest.config.unit.ts --coverage",
"build": "rollup --config rollup.config.ts --configPlugin typescript && tsc",
"docs": "typedoc --entryPoints index.ts --out docs/API"
},
"author": "Anand Thakker <vestibule@anandthakker.net> (http://anandthakker.net/)",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/maplibre/vt-pbf"
},
"type": "module",
"dependencies": {
"@mapbox/point-geometry": "^1.1.0",
"@mapbox/vector-tile": "^2.0.4",
"@maplibre/geojson-vt": "^5.0.4",
"@types/geojson": "^7946.0.16",
"@types/supercluster": "^7.1.3",
"pbf": "^4.0.1",
"supercluster": "^8.0.1"
},
"devDependencies": {
"@eslint/js": "^9.30.0",
"@mapbox/geojson-fixtures": "^1.0.0",
"@mapbox/mvt-fixtures": "^3.10.0",
"@maplibre/vtvalidate": "^0.4.2",
"@rollup/plugin-node-resolve": "^16.0.1",
"@rollup/plugin-replace": "^6.0.2",
"@rollup/plugin-strip": "^3.0.4",
"@rollup/plugin-typescript": "^12.1.4",
"@types/geojson-equality": "^0.2.2",
"@types/node": "^24.0.3",
"@vitest/coverage-v8": "^3.2.4",
"benchmark": "^2.1.4",
"eslint": "^8.57.1",
"eslint-config-standard": "^17.1.0",
"eslint-plugin-import": "^2.32.0",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^6.0.0",
"geojson-equality": "^0.2.1",
"geojson-vt": "^4.0.2",
"rollup": "^4.44.1",
"standard": "^17.1.2",
"tslib": "^2.8.1",
"typedoc": "^0.28.16",
"typescript": "^5.8.3",
"typescript-eslint": "^8.35.1",
"vite-tsconfig-paths": "^5.1.4",
"vitest": "^3.2.4"
}
}