gjtk
v2.1.0
Published
GeoJSON ToolKit
Downloads
116
Readme
GeoJSON ToolKit
gjtk
is a library for working with GeoJSON.
Installation
gjtk
is available on npm.
npm install gjtk
Usage
var gjtk = require('gjtk');
Validation Methods
All validation methods take a single argument.
isGeoJSON
returns true
when passed a valid GeoJSON object, otherwise false
GeoJSON always consists of a single object. This object (referred to as the GeoJSON object [above]) represents a geometry, feature, or collection of features.
isGeometry
returns true
when passed a valid GeoJSON Geometry, otherwise false
A geometry is a GeoJSON object where the type member's value is one of the following strings: "Point", "MultiPoint", "LineString", "MultiLineString", "Polygon", "MultiPolygon", or "GeometryCollection".
isPosition
returns true
when passed a valid GeoJSON Position, otherwise false
A position is represented by an array of numbers. There must be at least two elements, and may be more. The order of elements must follow x, y, z order (easting, northing, altitude for coordinates in a projected coordinate reference system, or longitude, latitude, altitude for coordinates in a geographic coordinate reference system). Any number of additional elements are allowed -- interpretation and meaning of additional elements is beyond the scope of this specification.
isPointCoordinates
returns true
when passed valid GeoJSON Point coordinates, otherwise false
example
[100.0, 0.0]
isMultiPointCoordinates
returns true
when passed valid GeoJSON MultiPoint coordinates, otherwise false
example
[ [100.0, 0.0], [101.0, 1.0], [102.0, 2.0] ]
isLineStringCoordinates
returns true
when passed valid GeoJSON LineString coordinates, otherwise false
example
[ [100.0, 0.0], [101.0, 1.0] ]
isLinearRingCoordinates
returns true
when passed valid GeoJSON LinearRing coordinates, otherwise false
A LinearRing is closed LineString with 4 or more positions. The first and last positions are equivalent (they represent equivalent points). Though a LinearRing is not explicitly represented as a GeoJSON geometry type, it is referred to in the Polygon geometry type definition.
example
[ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ]
isMultiLineStringCoordinates
returns true
when passed valid GeoJSON MultiLineString coordinates, otherwise false
example
[
[ [100.0, 0.0], [101.0, 1.0] ],
[ [102.0, 2.0], [103.0, 3.0] ]
]
isPolygonCoordinates
returns true
when passed valid GeoJSON Polygon coordinates, otherwise false
example
- 0 holes
[
[ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ]
]
- 1 hole
[
[ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ],
[ [100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2] ]
]
- etc.
isMultiPolygonCoordinates
returns true
when passed valid GeoJSON MultiPolygon coordinates, otherwise false
example
[
[
[ [102.0, 2.0], [103.0, 2.0], [103.0, 3.0], [102.0, 3.0], [102.0, 2.0] ]
],
[
[ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ],
[ [100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2] ]
]
]
isPoint
returns true
when passed a valid GeoJSON Point, otherwise false
example
{ "type": "Point", "coordinates": [100.0, 0.0] }
isMultiPoint
returns true
when passed a valid GeoJSON MultiPoint, otherwise false
example
{ "type": "MultiPoint", "coordinates": [ [100.0, 0.0], [101.0, 1.0] ] }
isLineString
returns true
when passed a valid GeoJSON LineString, otherwise false
example
{
"type": "LineString",
"coordinates": [ [100.0, 0.0], [101.0, 1.0] ]
}
isMultiLineString
returns true
when passed a valid GeoJSON MultiLineString, otherwise false
example
{
"type": "MultiLineString",
"coordinates": [
[ [100.0, 0.0], [101.0, 1.0] ],
[ [102.0, 2.0], [103.0, 3.0] ]
]
}
isPolygon
returns true
when passed a valid GeoJSON Polygon, otherwise false
example
- 0 holes
{
"type": "Polygon",
"coordinates": [
[ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ]
]
}
- 1 hole
{
"type": "Polygon",
"coordinates": [
[ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ],
[ [100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2] ]
]
}
- etc.
isMultiPolygon
returns true
when passed a valid GeoJSON MultiPolygon, otherwise false
example
{
"type": "MultiPolygon",
"coordinates": [
[
[ [102.0, 2.0], [103.0, 2.0], [103.0, 3.0], [102.0, 3.0], [102.0, 2.0] ]
],
[
[ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ],
[ [100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2] ]
]
]
}
isGeometryCollection
returns true
when passed a valid GeoJSON Geometry Collection, otherwise false
example
{
"type": "GeometryCollection",
"geometries": [
{
"type": "Point",
"coordinates": [100.0, 0.0]
},
{
"type": "LineString",
"coordinates": [ [101.0, 0.0], [102.0, 1.0] ]
}
]
}
isFeature
returns true
when passed a valid GeoJSON Feature, otherwise false
example
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [125.6, 10.1]
},
"properties": {
"name": "Dinagat Islands"
}
}
isFeatureCollection
returns true
when passed a valid GeoJSON Feature Collection, otherwise false
example
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [102.0, 0.5]
},
"properties": {
"prop0": "value0"
}
},
{
"type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": [ [102.0, 0.0], [103.0, 1.0], [104.0, 0.0], [105.0, 1.0] ]
},
"properties": {
"prop0": "value0",
"prop1": 0.0
}
},
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ]
]
},
"properties": {
"prop0": "value0",
"prop1": {
"this": "that"
}
}
}
]
}
isCRS
returns true
when passed a valid GeoJSON Coordinate Reference System, otherwise false
example
{
"type": "name",
"properties": {
"name": "urn:ogc:def:crs:OGC:1.3:CRS84"
}
}
{
"type": "link",
"properties": {
"href": "http://example.com/crs/42",
"type": "proj4"
}
}
hasCRS
returns true
when passed an object that validly specifies a GeoJSON Coordinate Reference System, otherwise false
The coordinate reference system (CRS) of a GeoJSON object is determined by its "crs" member (referred to as the CRS object below). If an object has no crs member, then its parent or grandparent object's crs member may be acquired. If no crs member can be so acquired, the default CRS shall apply to the GeoJSON object.
isLink
returns true
when passed a valid GeoJSON Link, otherwise false
example
{
"type": "link",
"properties": {
"href": "data.crs",
"type": "ogcwkt"
}
}
isBbox (not implemented)
returns true
when passed a valid GeoJSON Bounding Box, otherwise false
example
[-180.0, -90.0, 180.0, 90.0]
hasBbox (partially implemented)
returns true
when passed an object that validly specifies a GeoJSON Bounding Box, otherwise false
example
{
"type": "Feature",
"bbox": [-180.0, -90.0, 180.0, 90.0],
"geometry": {
"type": "Polygon",
"coordinates": [
[ [-180.0, 10.0], [20.0, 90.0], [180.0, -5.0], [-30.0, -90.0] ]
]
}
}
Utility Methods
Comparison
equalPositions(position1, position2)
returns true
when all parameters are identical GeoJSON Positions, otherwise false
containedPolygon(innerLinearRing, outerLinearRing)
returns true
when one GeoJSON LinearRing contains another, otherwise false
Templates
Point(Position)
returns a GeoJSON Point object
Feature(Geometry, properties)
returns a GeoJSON Feature object
FeatureCollection(Features)
returns a GeoJSON FeatureCollection object
GeometryCollection(Geometries)
returns a GeoJSON GeometryCollection object
Extraction
These methods all take a single argument: a valid GeoJSON object.
positionsOf
returns all the Positions in a valid GeoJSON object
featuresOf
returns all the Features in a valid GeoJSON object
geometriesOf
returns all the Geometries in a valid GeoJSON object