zipscene-geo-utils
GeoJson utility library. Includes several classes for working with GeoJson entities, as well as a static simplification function.
simplifyPolygon
A simplification function for GeoJson geometries that removes vertices until specified tolerances are reached. It is capable of removing internal rings and individual MultiPolygon parts as part of the simplification process.
const simplifyPolygon = ; let geoJson = type: 'Polygon' // Can also be a MultiPolygon coordinates: // GeoJSON coordinate rings ; let options = // A strict maximum vertex count. Defaults to 200. maxVertices: 100 // Stopping point based on vertex count. Defaults to 30. minVertices: 20 // // Stopping point based on percent change to the polygon. Defaults to 0.05. maxError: 001 // If set to true, the algorithm will attempt to resolve self-intersections // that may occur as a result of simplification. Will fail for some complex polygons. fixIntersections: true; // Simplification occurs in place.;
GeoSimplifier
A class for performing simplification operations on GeoJson geometries. It is
used internally by simplifyPolygon
above.
const GeoSimplifier LinearRing = ; // See more about the LinearRing class below.let ring = 0 0 0 10 88 89 9 9 99 99 10 10 10 0 0 0 ; // Also accepts Polygon and MultiPolygon instances.let simplifier = ring; // Simplify by one vertex.simplifier; // Note that the ring is changed in-place.console;// [ [ 0, 0 ],// [ 0, 10 ],// [ 8.8, 8.9 ],// [ 9, 9 ],// [ 10, 10 ],// [ 10, 0 ],// [ 0, 0 ] ] // And again...simplifier; console;// [ [ 0, 0 ],// [ 0, 10 ],// [ 8.8, 8.9 ],// [ 10, 10 ],// [ 10, 0 ],// [ 0, 0 ] ] // Simplification can be undone.simplifier; console;// [ [ 0, 0 ],// [ 0, 10 ],// [ 8.8, 8.9 ],// [ 9, 9 ],// [ 10, 10 ],// [ 10, 0 ],// [ 0, 0 ] ]
Geometry
An abstract class for GeoJson geometries that can be simplified by the
GeoSimplifier
class above. Derived classes must override the following
methods:
listVertices()
: Returns a flat array of allVertex
objects remaining in the geometry.calculateArea()
': Returns the area of the geometry.toLineSegments()
: Returns a flat array of line segments remaining in the geometry in the form [ [x1, y1] , [ x2, y2 ] ].toGeoJson()
: Returns the vertices remaining geometry as a GeoJson coordinates array.
Vertex
The simplest GeoJson entity class. Represents a point in a GeoJson coordinate ring, with references to its neighboring points.
const Vertex = ; let vertex = 0 0 ;vertexprev = 1 0 ;vertexnext = 0 1 ; // Calculate the area of the triangle formed by the vertex and its neighbors.let area = vertex; console;// 0.5
LinearRing
A concrete subtype of Geometry
, this class represents a GeoJson coordinate
ring. Individual vertices can be accessed and manipulated through the vertices
property. Note that vertices are indexed by their original positions in the
ring, not by their current positions, which might change based on removal and
restoration.
const LinearRing = ; let ring = 0 0 0 4 4 4 4 0 0 0 // repeated start point is optional.; console;// 16 // Remove the top right corner.let corner = ringvertices2;corner; console;// 8 // Restore the top right corner.corner; console;// 16
Note: For efficiency, removed vertices maintain references to the neighbors they had at the time they were removed. These references are used in the restoration process. As a result, restored vertices must be restored in the same order they were removed.
Polygon
Another subtype of Geometry
, this class represents a collection of linear
rings, the first being the outer ring of the polygon, and the other being inner
holes.
const Polygon = ; let polygon = 0 0 0 4 4 4 4 0 0 0 // repeated start point is optional. 1 1 1 3 3 3 3 1 1 1 // repeated start point is optional. ; polygonrings0vertices2; console;// [// [// [0, 0],// [0, 4],// [4, 0],// [0, 0]// ],// [// [1, 1],// [1, 3],// [3, 3],// [3, 1],// [1, 1]// ]// ]
MultiPolygon
Yet another subtype of Geometry
, this class represents a collection of
disconnected polygons, each with a series of rings as described under
LinearRing
above.
const MultiPolygon = ; let multiPolygon = 0 0 0 4 4 4 4 0 0 0 // repeated start point is optional 1 1 1 3 3 3 3 1 1 1 // repeated start point is optional 15 15 15 25 25 25 25 15 15 15 // repeated start point is optional ; multiPolygonpolygons0rings1vertices2; console;// [// [// [// [0, 0],// [0, 4],// [4, 4],// [4, 0],// [0, 0]// ],// [// [1, 1],// [1, 3],// [3, 1],// [1, 1]// ]// ],// [// [// [1.5, 1.5],// [1.5, 2.5],// [2.5, 2.5],// [2.5, 1.5],// [1.5, 1.5]// ]// ]// ]