Lightweight utility for input validation and data extraction in Turf.js. Ensures GeoJSON inputs are in the correct format and extracts specific components like coordinates or geometries.
npm install @turf/invariantUnwrap a coordinate from a Point Feature, Geometry or a single coordinate.
* coord ([Array][1]<[number][2]> | [Geometry][3]<[Point][4]> | [Feature][5]<[Point][4]>) GeoJSON Point or an Array of numbers
``javascript
var pt = turf.point([10, 10]);
var coord = turf.getCoord(pt);
//= [10, 10]
`
Returns [Array][1]<[number][2]> coordinates
Unwrap coordinates from a Feature, Geometry Object or an Array
* coords ([Array][1]\
`javascript
var poly = turf.polygon([[[119.32, -8.7], [119.55, -8.69], [119.51, -8.54], [119.32, -8.7]]]);
var coords = turf.getCoords(poly);
//= [[[119.32, -8.7], [119.55, -8.69], [119.51, -8.54], [119.32, -8.7]]]
`
Returns [Array][1]\
Checks if coordinates contains a number
* coordinates [Array][1]\
Returns [boolean][6] true if Array contains a number
Enforce expectations about types of GeoJSON objects for Turf.
* value [GeoJSON][7] any GeoJSON objecttype
* [string][8] expected GeoJSON typename
* [string][8] name of calling function
* Throws [Error][9] if value is not the expected type.
Returns void
Enforce expectations about types of [Feature][5] inputs for Turf.
Internally this uses [geojsonType][10] to judge geometry types.
* feature [Feature][5] a feature with an expected geometry typetype
* [string][8] expected GeoJSON typename
* [string][8] name of calling function
* Throws [Error][9] error if value is not the expected type.
Returns void
Enforce expectations about types of [FeatureCollection][11] inputs for Turf.
Internally this uses [geojsonType][10] to judge geometry types.
* featureCollection [FeatureCollection][11] a FeatureCollection for which features will be judgedtype
* [string][8] expected GeoJSON typename
* [string][8] name of calling function
* Throws [Error][9] if value is not the expected type.
Get Geometry from Feature or Geometry Object
* geojson ([Feature][5] | [Geometry][3]) GeoJSON Feature or Geometry Object
`javascript`
var point = {
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [110, 40]
}
}
var geom = turf.getGeom(point)
//={"type": "Point", "coordinates": [110, 40]}
* Throws [Error][9] if geojson is not a Feature or Geometry Object
Returns ([Geometry][3] | null) GeoJSON Geometry Object
Get GeoJSON object's type, Geometry type is prioritize.
* geojson [GeoJSON][7] GeoJSON object_name
* [string][8]? name
* [string][8] name of the variable to display in error message (unused) (optional, default "geojson")
`javascript`
var point = {
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [110, 40]
}
}
var geom = turf.getType(point)
//="Point"
Returns [string][8] GeoJSON type
[1]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array
[2]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number
[3]: https://tools.ietf.org/html/rfc7946#section-3.1
[4]: https://tools.ietf.org/html/rfc7946#section-3.1.2
[5]: https://tools.ietf.org/html/rfc7946#section-3.2
[6]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean
[7]: https://tools.ietf.org/html/rfc7946#section-3
[8]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
[9]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error
[10]: #geojsontype
[11]: https://tools.ietf.org/html/rfc7946#section-3.3
---
This module is part of the Turfjs project, an open source module collection dedicated to geographic algorithms. It is maintained in the Turfjs/turf repository, where you can create PRs and issues.
Install this single module individually:
`sh`
$ npm install @turf/invariant
Or install the all-encompassing @turf/turf module that includes all modules as functions:
`sh``
$ npm install @turf/turf