A set of GeoJSON tools
-
Install the library:
npm install @barcia/maptools
-
Import some utility in your JS code
import { Feature } from 'maptools'
-
Create a new GeoJSON feature
const feature = new Feature('Point', [103.32, 34.21], { name: "Interesting point" }) const geojsonFeature = feature.toJSON()
Create a GeoJSON Feature.
type("Point"|"LineString"|"Polygon"|"MultiPoint"|"MultiLineString"|"MultiPolygon") The Feature type.coordsArray The Feature coordinates with the format[lon, lat, alt], whilealtis optional.propsObject? The Feature properties.
const feature = new Feature('Point', [105.0, 4.0], { name: 'foo' });Add a new property to the Feature or update an existing one.
keystring The property key.valueany The property value.
feature.addProperty('desc', 'bar');Return the Feature object.
feature.toJSON();Returns Object The Feature object
Create a Feature instance from a GeoJSON feature object.
-
jsonObject A valid GeoJSON feature object.-
json.type("Feature") The feature type. -
json.geometryObject A valid GeoJSON geometry.json.geometry.type("Point"|"LineString"|"Polygon"|"MultiPoint"|"MultiLineString"|"MultiPolygon") The Feature geometry type.json.geometry.coordinatesArray The Feature coordinates with the format[lon, lat, alt], whilealtis optional.
-
json.propertiesObject? The Feature properties.
-
const feature = Feature.fromJSON({
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [105.0, 4.0]
},
properties: {
name: 'foo'
}
});Returns Feature The Feature instance.
Create a FeatureCollection.
featuresArray The FeatureCollection features array.propsObject? The FeatureCollection properties.
const featureCollection = new FeatureCollection([
new Feature('Point', [100.0, 0.0]).toJSON(),
new Feature('Point', [101.0, 1.0]).toJSON()
], { name: 'foo' });Add new features to the FeatureCollection.
featuresArray The array with Features to add.
featureCollection.addFeatures([
new Feature('Point', [103.0, 7.0]).toJSON(),
]);Remove features from the FeatureCollection.
funcFunction The function to filter the features to remove. Uses native JSArray.filter()method.dryrunboolean IF true, return the features to remove but don't remove them. (optional, defaultfalse)
featureCollection.removeFeatures(feature => feature.properties.name === 'foo');Return the FeatureCollection object.
featureCollection.toJSON();Returns Object The FeatureCollection object
Create a FeatureCollection instance from a GeoJSON featureCollection object.
-
jsonObject A valid GeoJSON featureCollection object.
const featureCollection = FeatureCollection.fromJSON({
type: 'FeatureCollection',
features: [
{
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [100.0, 0.0]
},
},
]
});Returns FeatureCollection The FeatureCollection instance.
Create a GeoJSON Geometry.
type("Point"|"LineString"|"Polygon"|"MultiPoint"|"MultiLineString"|"MultiPolygon") The Geometry type.coordsArray The Geometry coordinates with the format[lon, lat, alt], whilealtis optional.
const geometry = new Geometry('Point', [105.0, 4.0]);Return the Geometry object.
// { type: 'Point', coordinates: [105.0, 4.0] }
geometry.toJSON();Returns Object The Geometry object
Create a Geometry instance from a GeoJSON geometry object.
-
jsonObject A valid GeoJSON geometry object.json.type("Point"|"LineString"|"Polygon"|"MultiPoint"|"MultiLineString"|"MultiPolygon") The Geometry type.json.coordinatesArray The Geometry coordinates with the format[lon, lat, alt], whilealtis optional.
const geometry = Geometry.fromJSON({
type: 'Point',
coordinates: [105.0, 4.0]
});Returns Geometry The Geometry instance.
Validate a Geometry.
type("Point"|"LineString"|"Polygon"|"MultiPoint"|"MultiLineString"|"MultiPolygon") The Geometry type.coordsArray The Geometry coordinates with the format[lon, lat, alt], whilealtis optional.
const isValidPoint = Geometry.validate({
type: 'Point',
coordinates: [105.0, 4.0]
});Returns boolean True if the Geometry is valid, false otherwise.
Create a GeometryCollection.
geometriesArray The GeometryCollection features array.
const geometryCollection = new GeometryCollection([
new Geometry('Point', [100.0, 0.0]).toJSON(),
new Geometry('Point', [101.0, 1.0]).toJSON()
]);Add new geometries to the GeometryCollection.
geometriesArray The array with geometries to add.
geometryCollection.addGeometries([
new Geometry('Point', [103.0, 7.0]).toJSON(),
]);Return the GeometryCollection object.
geometryCollection.toJSON();Returns Object The GeometryCollection object
Create a GeometryCollection from a GeoJSON geometryCollection object.
-
jsonObject A valid GeoJSON geometryCollection object.json.type("GeometryCollection") The geometryCollection type.json.geometriesArray An array of geometries.
const geometryCollection = GeometryCollection.fromJSON({
type: 'GeometryCollection',
geometries: [
{
type: 'Point',
coordinates: [100.0, 0.0]
},
{
type: 'Point',
coordinates: [101.0, 1.0]
}
]
});Returns GeometryCollection The GeometryCollection instance.