{"id":11191,"library":"jsts","title":"JSTS: JavaScript Topology Suite","description":"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.","status":"active","version":"2.12.1","language":"javascript","source_language":"en","source_url":"git://github.com/bjornharrtell/jsts","tags":["javascript","JSTS","JavaScript","JTS","Java","Topology","Geometry"],"install":[{"cmd":"npm install jsts","lang":"bash","label":"npm"},{"cmd":"yarn add jsts","lang":"bash","label":"yarn"},{"cmd":"pnpm add jsts","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"JSTS delivers as ES Modules for Node.js (v14+). Direct deep imports are required for specific classes from version 1.4.0+.","wrong":"const GeoJSONReader = require('jsts').io.GeoJSONReader;","symbol":"GeoJSONReader","correct":"import GeoJSONReader from 'jsts/org/locationtech/jts/io/GeoJSONReader.js';"},{"note":"Remember to import other geometry types (Point, Polygon, etc.) similarly if needed for the factory.","wrong":"const GeometryFactory = require('jsts/org/locationtech/jts/geom/GeometryFactory');","symbol":"GeometryFactory","correct":"import GeometryFactory from 'jsts/org/locationtech/jts/geom/GeometryFactory.js';"},{"note":"For browser environments, a global `jsts` object is exposed when including the UMD build via a `<script>` tag, allowing access like `jsts.org.locationtech.jts.io.WKTReader`.","symbol":"WKTReader","correct":"import WKTReader from 'jsts/org/locationtech/jts/io/WKTReader.js';"},{"note":"Most geometry types (Point, LineString, Polygon) are imported from the `jsts/org/locationtech/jts/geom/` path.","symbol":"Point","correct":"import Point from 'jsts/org/locationtech/jts/geom/Point.js';"}],"quickstart":{"code":"import GeoJSONReader from 'jsts/org/locationtech/jts/io/GeoJSONReader.js';\nimport GeoJSONWriter from 'jsts/org/locationtech/jts/io/GeoJSONWriter.js';\nimport GeometryFactory from 'jsts/org/locationtech/jts/geom/GeometryFactory.js';\nimport Point from 'jsts/org/locationtech/jts/geom/Point.js';\nimport BufferOp from 'jsts/org/locationtech/jts/operation/buffer/BufferOp.js';\n\n// 1. Read a GeoJSON string into a JSTS Geometry object\nconst geoJsonInput = {\n  type: 'Feature',\n  geometry: {\n    type: 'Point',\n    coordinates: [10, 20]\n  },\n  properties: {\n    name: 'Example Point'\n  }\n};\n\nconst reader = new GeoJSONReader();\nconst geometry = reader.read(geoJsonInput);\n\nconsole.log('Original Geometry (JSTS):', geometry.toString());\n// Expected: POINT (10 20)\n\n// 2. Perform a spatial operation, e.g., buffer\nconst distance = 5;\nconst bufferOp = new BufferOp(geometry, distance);\nconst bufferedGeometry = bufferOp.getResultGeometry();\n\nconsole.log('Buffered Geometry (JSTS):', bufferedGeometry.toString());\n// Expected: POLYGON ((...))\n\n// 3. Create a new geometry using GeometryFactory\nconst factory = new GeometryFactory();\n// JSTS Coordinate is expected by createPoint\nconst newPoint = factory.createPoint(new Point.Coordinate(15, 25));\n\nconsole.log('Newly Created Point (JSTS):', newPoint.toString());\n// Expected: POINT (15 25)\n\n// 4. Write a JSTS Geometry object back to GeoJSON\nconst writer = new GeoJSONWriter();\nconst bufferedGeoJson = writer.write(bufferedGeometry);\n\nconsole.log('Buffered Geometry (GeoJSON):', JSON.stringify(bufferedGeoJson, null, 2));\n\n/*\nExpected output example for bufferedGeoJson (simplified):\n{\n  \"type\": \"Polygon\",\n  \"coordinates\": [\n    [\n      [5.000..., 20.000...],\n      ...\n    ]\n  ]\n}\n*/","lang":"typescript","description":"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."},"warnings":[{"fix":"Refactor `require()` statements to `import` statements and use specific deep import paths, e.g., `import GeoJSONReader from 'jsts/org/locationtech/jts/io/GeoJSONReader.js'`.","message":"JSTS is delivered as ES Modules only for Node.js (since version 1.4.0, solidified for Node.js 14+). CommonJS `require()` statements will no longer work and will result in errors.","severity":"breaking","affected_versions":">=1.4.0"},{"fix":"Use dedicated processor classes from `jsts/org/locationtech/jts/operation/` (e.g., `BufferOp`, `IntersectionOp`) or `jsts/org/locationtech/jts/algorithm/` to perform operations.","message":"Direct shortcut methods like `.buffer()`, `.intersects()`, or `.union()` are not available on `Geometry` objects in the ES Modules build for Node.js. These methods are monkey-patched only into the bundled ES5 browser version.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Consult the JSTS API documentation or JTS Javadoc to determine the exact expected argument types for methods that have overloaded signatures in Java.","message":"Due to JavaScript's lack of function overloading, some JTS methods like `GeometryFactory.createMultiPoint` may only accept specific argument types (e.g., `Point[]`) unlike their Java counterparts.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure input geometries are valid (`geometry.isValid()`). Use `jsts/org/locationtech/jts/precision/GeometryPrecisionReducer.js` to reduce precision if needed, or validate geometries with `jsts/org/locationtech/jts/geom/Geometry.isValid()`.","message":"Calculations may throw a `TopologyException` (as a JavaScript `Error`) if precision issues arise or input geometries are invalid according to the OGC Simple Features specification.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Upgrade your Node.js environment to version 18 or later using `nvm` or your preferred Node.js version manager.","message":"JSTS requires Node.js version 18 or higher for execution as specified in its `engines` field. Older Node.js versions may encounter compatibility issues or fail to run.","severity":"breaking","affected_versions":">=1.4.0 (implicitly from ESM transition), >=2.0.0 (explicitly in package.json)"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Use ES `import` statements with specific deep paths, e.g., `import GeoJSONReader from 'jsts/org/locationtech/jts/io/GeoJSONReader.js'`.","cause":"Attempting to use CommonJS `require()` syntax in an ES module context or with JSTS versions that are ESM-only.","error":"TypeError: require is not a function"},{"fix":"Validate input geometries (`geometry.isValid()`). If valid, consider reducing precision using `new GeometryPrecisionReducer(precisionModel).reduce(geometry)`.","cause":"Geometric operations failing due to invalid input geometries or floating-point precision issues.","error":"TopologyException: Points of linear element are not distinct."},{"fix":"Use the dedicated operation classes, e.g., `import BufferOp from 'jsts/org/locationtech/jts/operation/buffer/BufferOp.js'; const buffered = new BufferOp(geometry, distance).getResultGeometry();`.","cause":"Trying to call a shortcut method (like `buffer`, `intersects`) directly on a `Geometry` object in the ES module build of JSTS, where these methods are not directly attached.","error":"TypeError: Cannot read properties of undefined (reading 'buffer')"},{"fix":"Ensure the JSTS UMD build (e.g., `jsts.min.js`) is correctly included via a `<script>` tag in your HTML before attempting to access `jsts`.","cause":"In a browser environment, the `jsts` global object is not available, typically because the UMD build script was not correctly included or executed.","error":"ReferenceError: jsts is not defined"}],"ecosystem":"npm"}