UNPKG

vector-tile-esm

Version:
54 lines (42 loc) 1.29 kB
import { classifyRings } from "./rings.js"; export const types = ["Unknown", "Point", "LineString", "Polygon"]; export function toGeoJSON(size, sx = 0, sy = 0) { // Input size is the side length of the (square) area over which the // coordinate space of this tile [0, this.extent] will be rendered. // Input sx, sy is the origin (top left corner) of the output coordinates // within the (size x size) rendered area of the full tile. size = size || this.extent; const scale = size / this.extent; let coords = this.loadGeometry(); let type = types[this.type]; function project(line) { return line.map(p => [p.x * scale - sx, p.y * scale - sy]); } switch (type) { case "Point": coords = project( coords.map(p => p[0]) ); break; case "LineString": coords = coords.map(project); break; case "Polygon": coords = classifyRings(coords); coords = coords.map(polygon => polygon.map(project)); break; } if (coords.length === 1) { coords = coords[0]; } else { type = "Multi" + type; } const result = { type: "Feature", geometry: { type: type, coordinates: coords }, properties: this.properties }; if ("id" in this) result.id = this.id; return result; }