JSTS: JavaScript Topology Suite

2.12.1 · active · verified Sun Apr 19

JSTS (JavaScript Topology Suite) is a comprehensive JavaScript library providing spatial predicates and functions for processing geometric data. It strictly adheres to the Simple Features Specification for SQL by the Open Geospatial Consortium (OGC). As a direct port of the well-established Java Topology Suite (JTS), JSTS offers a robust set of tools for geometric operations like buffering, intersection, union, and validity checks. The library is currently stable at version 2.12.1, with an active development cycle evidenced by consistent minor and patch releases. Key differentiators include its fidelity to the JTS API, support for common I/O formats such as WKT and GeoJSON, and integration capabilities with web mapping libraries like OpenLayers. For Node.js environments (version 18+), JSTS is distributed exclusively as ES Modules, moving away from CommonJS.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates reading GeoJSON input, performing a buffer operation on a point geometry, creating a new point with `GeometryFactory`, and serializing the buffered geometry back into GeoJSON using JSTS ES modules.

import GeoJSONReader from 'jsts/org/locationtech/jts/io/GeoJSONReader.js';
import GeoJSONWriter from 'jsts/org/locationtech/jts/io/GeoJSONWriter.js';
import GeometryFactory from 'jsts/org/locationtech/jts/geom/GeometryFactory.js';
import Point from 'jsts/org/locationtech/jts/geom/Point.js';
import BufferOp from 'jsts/org/locationtech/jts/operation/buffer/BufferOp.js';

// 1. Read a GeoJSON string into a JSTS Geometry object
const geoJsonInput = {
  type: 'Feature',
  geometry: {
    type: 'Point',
    coordinates: [10, 20]
  },
  properties: {
    name: 'Example Point'
  }
};

const reader = new GeoJSONReader();
const geometry = reader.read(geoJsonInput);

console.log('Original Geometry (JSTS):', geometry.toString());
// Expected: POINT (10 20)

// 2. Perform a spatial operation, e.g., buffer
const distance = 5;
const bufferOp = new BufferOp(geometry, distance);
const bufferedGeometry = bufferOp.getResultGeometry();

console.log('Buffered Geometry (JSTS):', bufferedGeometry.toString());
// Expected: POLYGON ((...))

// 3. Create a new geometry using GeometryFactory
const factory = new GeometryFactory();
// JSTS Coordinate is expected by createPoint
const newPoint = factory.createPoint(new Point.Coordinate(15, 25));

console.log('Newly Created Point (JSTS):', newPoint.toString());
// Expected: POINT (15 25)

// 4. Write a JSTS Geometry object back to GeoJSON
const writer = new GeoJSONWriter();
const bufferedGeoJson = writer.write(bufferedGeometry);

console.log('Buffered Geometry (GeoJSON):', JSON.stringify(bufferedGeoJson, null, 2));

/*
Expected output example for bufferedGeoJson (simplified):
{
  "type": "Polygon",
  "coordinates": [
    [
      [5.000..., 20.000...],
      ...
    ]
  ]
}
*/

view raw JSON →