Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## main
### ✨ Features and improvements
- Add `geoJSONToTile` function to generate a single tile directly from GeoJSON without building the full tile index. (by [montzkie18](https://github.com/montzkie18) and [lucaswoj](https://github.com/lucaswoj)).
Comment thread
lucaswoj marked this conversation as resolved.
- _...Add new stuff here..._

### 🐞 Bug fixes
Expand Down
12 changes: 12 additions & 0 deletions src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ export type GeoJSONVTOptions = {
debug?: number;
};

export type GeoJSONToTileOptions = GeoJSONVTOptions & {
/**
* Whether to wrap features around the antimeridian
* @default false
*/
wrap?: boolean;
/**
* Whether to clip features to the tile boundary
* @default false
*/
clip?: boolean;
};

export type StartEndSizeArray = number[] & { start?: number; end?: number; size?: number };

Expand Down
58 changes: 58 additions & 0 deletions src/geoJSONToTile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import {clip} from './clip';
import {convert} from './convert';
import type {GeoJSONToTileOptions} from './definitions';
import {defaultOptions} from './geojsonvt';
import {createTile} from './tile';
import {transformTile, type GeoJSONVTTile} from './transform';
import {wrap} from './wrap';

/**
* Converts GeoJSON data directly to a single vector tile without building a tile index.
*
* Unlike the {@link GeoJSONVT} class which builds a hierarchical tile index for efficient
* repeated tile access, this function generates a single tile on-demand. This is useful when:
* - You only need one specific tile and don't need to query multiple tiles
* - The source data is already spatially filtered to the tile's bounding box
* - You want to avoid the overhead of building a full tile index
*
* @example
* ```ts
* import {geoJSONToTile} from '@maplibre/geojson-vt';
*
* const geojson = {
* type: 'FeatureCollection',
* features: [{
* type: 'Feature',
* geometry: { type: 'Point', coordinates: [-77.03, 38.90] },
* properties: { name: 'Washington, D.C.' }
* }]
* };
*
* const tile = geoJSONToTile(geojson, 10, 292, 391, { extent: 4096 });
* ```
*
* @param data - GeoJSON data (Feature, FeatureCollection, or Geometry)
* @param z - Tile zoom level
* @param x - Tile x coordinate
* @param y - Tile y coordinate
* @param options - Optional configuration for tile generation
* @returns The generated tile with geometries in tile coordinates, or null if no features
*/

export function geoJSONToTile(data: GeoJSON.GeoJSON, z: number, x: number, y: number, options: GeoJSONToTileOptions = {}): GeoJSONVTTile {
options = {...defaultOptions, ...options};
const {wrap: shouldWrap = false, clip: shouldClip = false} = options;

let features = convert(data, options);
if (shouldWrap) {
features = wrap(features, options);
}
if (shouldClip || options.lineMetrics) {
const pow2 = 1 << z;
const buffer = options.buffer / options.extent;
const left = clip(features, pow2, (x - buffer), (x + 1 + buffer), 0, -1, 2, options);
features = clip(left || [], pow2, (y - buffer), (y + 1 + buffer), 1, -1, 2, options);
}

return transformTile(createTile(features ?? [], z, x, y, options), options.extent);
}
2 changes: 1 addition & 1 deletion src/geojsonvt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {createTile, type GeoJSONVTInternalTile} from './tile';
import {applySourceDiff, type GeoJSONVTSourceDiff} from './difference';
import type { GeoJSONVTInternalFeature, GeoJSONVTOptions } from './definitions';

const defaultOptions: GeoJSONVTOptions = {
export const defaultOptions: GeoJSONVTOptions = {
maxZoom: 14,
indexMaxZoom: 5,
indexMaxPoints: 100000,
Expand Down
11 changes: 7 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@
import type {GeoJSONVTFeature, GeoJSONVTFeatureNonPoint, GeoJSONVTFeaturePoint, GeoJSONVTTile} from './transform';
import type {GeoJSONVTInternalTile, GeoJSONVTInternalTileFeature, GeoJSONVTInternalTileFeaturePoint, GeoJSONVTInternalTileFeatureNonPoint} from './tile';
import type {GeoJSONVTFeatureDiff, GeoJSONVTSourceDiff} from './difference';
import type {GeoJSONVTInternalFeature, GeoJSONVTInternalLineStringFeature, GeoJSONVTInternalMultiLineStringFeature, GeoJSONVTInternalMultiPointFeature, GeoJSONVTInternalMultiPolygonFeature, GeoJSONVTInternalPointFeature, GeoJSONVTInternalPolygonFeature, GeoJSONVTOptions, PartialGeoJSONVTFeature, StartEndSizeArray} from './definitions';
import type {GeoJSONVTInternalFeature, GeoJSONVTInternalLineStringFeature, GeoJSONVTInternalMultiLineStringFeature, GeoJSONVTInternalMultiPointFeature, GeoJSONVTInternalMultiPolygonFeature, GeoJSONVTInternalPointFeature, GeoJSONVTInternalPolygonFeature, GeoJSONVTOptions, GeoJSONToTileOptions, PartialGeoJSONVTFeature, StartEndSizeArray} from './definitions';
import {GeoJSONVT} from './geojsonvt';

export default function geojsonvt(data: GeoJSON.GeoJSON, options?: GeoJSONVTOptions) {
return new GeoJSONVT(data, options);
}

export type {
GeoJSONVTInternalFeature,
GeoJSONVTOptions,
export {geoJSONToTile} from './geoJSONToTile';

export type {
GeoJSONVTInternalFeature,
GeoJSONVTOptions,
GeoJSONToTileOptions,
GeoJSONVTInternalTile,
GeoJSONVTInternalTileFeature,
PartialGeoJSONVTFeature,
Expand Down
Loading