Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
24 changes: 14 additions & 10 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import Pbf from 'pbf';
import {type GeoJSONOptions, type Feature, GeoJSONWrapper} from './lib/geojson_wrapper';
import {type Feature, GEOJSON_TILE_LAYER_NAME, type GeoJSONOptions, GeoJSONWrapper} from "./lib/geojson_wrapper";
import geojsonvt from 'geojson-vt';
import {type VectorTileLayer, type VectorTile, VectorTileFeature} from '@mapbox/vector-tile';
import type {VectorTileFeatureLike, VectorTileLike, VectorTileLayerLike} from './lib/types';

interface Context {
keys: string[];
values: (string | boolean | number)[];
keycache: Record<string, number>;
valuecache: Record<string, number>;
feature?: VectorTileFeature;
feature?: VectorTileFeatureLike;
}

/**
Expand All @@ -17,7 +17,7 @@ interface Context {
* @param tile
* @return uncompressed, pbf-serialized tile data
*/
export function fromVectorTileJs(tile: VectorTile): Uint8Array {
export function fromVectorTileJs(tile: VectorTileLike): Uint8Array {
const out = new Pbf();
writeTile(tile, out);
return out.finish();
Expand All @@ -31,7 +31,7 @@ export function fromVectorTileJs(tile: VectorTile): Uint8Array {
* @return uncompressed, pbf-serialized tile data
*/
export function fromGeojsonVt(layers: geojsonvt.Tile[], options?: GeoJSONOptions): Uint8Array {
const l: Record<string, VectorTileLayer> = {};
const l: Record<string, VectorTileLayerLike> = {};
// eslint-disable-next-line @typescript-eslint/no-for-in-array
for (const k in layers) {
l[k] = new GeoJSONWrapper(layers[k].features, options);
Expand All @@ -42,13 +42,13 @@ export function fromGeojsonVt(layers: geojsonvt.Tile[], options?: GeoJSONOptions
return fromVectorTileJs({ layers: l });
}

function writeTile(tile: VectorTile, pbf: Pbf) {
function writeTile(tile: VectorTileLike, pbf: Pbf) {
for (const key in tile.layers) {
pbf.writeMessage(3, writeLayer, tile.layers[key]);
}
}

function writeLayer(layer: VectorTileLayer, pbf: Pbf) {
function writeLayer(layer: VectorTileLayerLike, pbf: Pbf) {
pbf.writeVarintField(15, layer.version || 1);
pbf.writeStringField(1, layer.name || '');
pbf.writeVarintField(5, layer.extent || 4096);
Expand Down Expand Up @@ -128,7 +128,7 @@ function zigzag(num: number) {
return (num << 1) ^ (num >> 31);
}

function writeGeometry(feature: VectorTileFeature, pbf: Pbf) {
function writeGeometry(feature: VectorTileFeatureLike, pbf: Pbf) {
const geometry = feature.loadGeometry();
const type = feature.type;
let x = 0;
Expand Down Expand Up @@ -179,5 +179,9 @@ function writeValue(value: string | boolean | number, pbf: Pbf) {
export {
GeoJSONWrapper,
GeoJSONOptions,
Feature
}
Feature,
GEOJSON_TILE_LAYER_NAME,
VectorTileFeatureLike,
VectorTileLike,
VectorTileLayerLike,
};
38 changes: 24 additions & 14 deletions lib/geojson_wrapper.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import Point from '@mapbox/point-geometry';
import {VectorTileFeature, VectorTileLayer, type VectorTile} from '@mapbox/vector-tile';
import type {TileFeature, AnyProps} from 'supercluster';
import {type Feature as GeoJSONVTFeature, Geometry} from 'geojson-vt';
import Pbf from 'pbf';
import type {
VectorTileFeatureLike,
VectorTileLayerLike,
VectorTileLike,
} from "./types";

export { VectorTileFeatureLike, VectorTileLayerLike, VectorTileLike };

export type Feature = TileFeature<AnyProps, AnyProps> | GeoJSONVTFeature;

Expand All @@ -11,14 +16,18 @@ export interface GeoJSONOptions {
extent: number;
}

class FeatureWrapper extends VectorTileFeature {
class FeatureWrapper implements VectorTileFeatureLike {
feature: Feature;
type: VectorTileFeatureLike['type'];
properties: VectorTileFeatureLike['properties'];
id: VectorTileFeatureLike['id'];
extent: VectorTileFeatureLike['extent'];

constructor(feature: Feature, extent: number) {
super(new Pbf(), 0, 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
Expand Down Expand Up @@ -50,25 +59,26 @@ class FeatureWrapper extends VectorTileFeature {
}
}

export class GeoJSONWrapper extends VectorTileLayer implements VectorTile {
layers: Record<string, VectorTileLayer>;
name: string;
extent: number;
length: number;
version: number;
export const GEOJSON_TILE_LAYER_NAME = "_geojsonTileLayer";

export 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) {
super(new Pbf());
this.layers = {'_geojsonTileLayer': this};
this.name = '_geojsonTileLayer';
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: number): VectorTileFeature {
feature(i: number): VectorTileFeatureLike {
return new FeatureWrapper(this.features[i], this.extent);
}
}
21 changes: 21 additions & 0 deletions lib/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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>;
}
2 changes: 1 addition & 1 deletion test/geojson_wrapper-test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {describe, test, expect} from 'vitest';
import {GeoJSONWrapper} from '../index.ts';
import {GeoJSONWrapper} from '../index';

describe('geojsonwrapper', () => {
test('linestring', () => {
Expand Down
8 changes: 2 additions & 6 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,5 @@
"moduleResolution": "bundler",
"declarationDir": "dist/"
},
"files": [
"index.ts",
"lib/geojson_wrapper.ts",
"rollup.config.ts"
]
}
"include": ["**/*.ts"]
}