node-polygon
v1.1.3
Published
A JavaScript/TypeScript library for handling Polygon geometry according to OGC standards with advanced geometric operations
Maintainers
Readme
node-polygon
A JavaScript/TypeScript library for handling Polygon geometry according to OGC (Open Geospatial Consortium) standards with advanced geometric operations.
Features
- Create and manipulate Polygon geometries
- Support for WKT (Well-Known Text) format
- Geometric transformations including translation, scaling, and rotation
- Matrix-based transformations
- Polygon simplification using Douglas-Peucker algorithm
- Polygon smoothing using Catmull-Rom spline interpolation
- TypeScript support with full type definitions
Installation
npm install node-polygonUsage
Basic Usage
import { Polygon, create, clone } from 'node-polygon';
// Create a new Polygon
const polygon = create();
polygon.coordinates = [[
[-100, 40],
[-105, 45],
[-110, 55],
[-100, 40]
]];
// Clone a polygon
const clonedPolygon = clone(polygon);Working with WKT
import { Polygon, fromWKT } from 'node-polygon';
// Create polygon from WKT format
const polygon = new Polygon();
fromWKT(polygon, "POLYGON((116.36 39.90, 116.46 39.90, 116.56 39.90, 116.36 39.90))");
console.log(polygon.coordinates);Geometric Transformations
import { Polygon, translate, scale, rotate, transformMat3 } from 'node-polygon';
const polygon = new Polygon();
polygon.coordinates = [[
[10, 10],
[20, 10],
[20, 20],
[10, 20],
[10, 10]
]];
// Translation
translate(polygon, polygon, [5, 5]);
// Scaling
scale(polygon, polygon, [2, 2], [0, 0]);
// Rotation
rotate(polygon, polygon, Math.PI / 2, [0, 0]);
// Matrix transformation
const matrix = new Float32Array([1, 0, 0, 0, 1, 0, 10, 20, 1]);
transformMat3(polygon, polygon, matrix);Polygon Simplification
import { Polygon, simplify } from 'node-polygon';
const complexPolygon = new Polygon();
// ... set complex coordinates ...
// Simplify polygon with tolerance value
const simplifiedPolygon = new Polygon();
simplify(simplifiedPolygon, complexPolygon, 0.01);Polygon Smoothing
import { Polygon, smooth } from 'node-polygon';
const polygon = new Polygon();
// ... set coordinates ...
// Smooth polygon with default parameters
const smoothedPolygon = new Polygon();
smooth(smoothedPolygon, polygon);
// Smooth with custom tension and segment count
smooth(smoothedPolygon, polygon, 0.3, 15);API Reference
Polygon Class
The main Polygon class representing a polygon geometry with the following properties:
- type: String - Always 'Polygon'
coordinates: Array - Array of linear rings, each ring is an array of [x, y] coordinates
Functions
create(): Polygon
Creates a new empty Polygon object.
clone(a: Polygon): Polygon
Clones a Polygon object.
fromWKT(out: Polygon, wkt: string): void
Parses a Polygon from WKT format string (e.g., "POLYGON((116.36 39.90, 116.46 39.90, 116.56 39.90, 116.36 39.90))").
transformMat3(out: Polygon, a: Polygon, mat3: Float32Array): Polygon
Transforms a Polygon using a 3x3 matrix transformation.
translate(out: Polygon, a: Polygon, dxdy: number[]): Polygon
Translates a Polygon by dx and dy distances provided as an array.
scale(out: Polygon, a: Polygon, scalexy: number[], origin: number[] = [0, 0]): Polygon
Scales a Polygon by scale factors around an origin point.
rotate(out: Polygon, a: Polygon, rad: number, origin: number[] = [0, 0]): Polygon
Rotates a Polygon by radians around an origin point.
simplify(out: Polygon, a: Polygon, tolerance: number): Polygon
Simplifies a Polygon using the Douglas-Peucker algorithm with the given tolerance.
smooth(out: Polygon, a: Polygon, tension: number = 0.5, numOfSegments: number = 10): Polygon
Smooths a Polygon using Catmull-Rom spline interpolation with configurable tension and segment count.
Data Format
Polygon Structure
{
"type": "Polygon",
"coordinates": [
[
[-100, 40],
[-105, 45],
[-110, 55],
[-100, 40]
]
]
}WKT Format Support
Supports parsing POLYGON type WKT format:
POLYGON((116.36 39.90, 116.46 39.90, 116.56 39.90, 116.36 39.90))License
MIT
