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.coords
Array The Feature coordinates with the format[lon, lat, alt]
, whilealt
is optional.props
Object? 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.
key
string The property key.value
any 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.
-
json
Object A valid GeoJSON feature object.-
json.type
("Feature"
) The feature type. -
json.geometry
Object A valid GeoJSON geometry.json.geometry.type
("Point"
|"LineString"
|"Polygon"
|"MultiPoint"
|"MultiLineString"
|"MultiPolygon"
) The Feature geometry type.json.geometry.coordinates
Array The Feature coordinates with the format[lon, lat, alt]
, whilealt
is optional.
-
json.properties
Object? 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.
features
Array The FeatureCollection features array.props
Object? 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.
features
Array The array with Features to add.
featureCollection.addFeatures([
new Feature('Point', [103.0, 7.0]).toJSON(),
]);
Remove features from the FeatureCollection.
func
Function The function to filter the features to remove. Uses native JSArray.filter()
method.dryrun
boolean 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.
-
json
Object 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.coords
Array The Geometry coordinates with the format[lon, lat, alt]
, whilealt
is 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.
-
json
Object A valid GeoJSON geometry object.json.type
("Point"
|"LineString"
|"Polygon"
|"MultiPoint"
|"MultiLineString"
|"MultiPolygon"
) The Geometry type.json.coordinates
Array The Geometry coordinates with the format[lon, lat, alt]
, whilealt
is 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.coords
Array The Geometry coordinates with the format[lon, lat, alt]
, whilealt
is 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.
geometries
Array 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.
geometries
Array 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.
-
json
Object A valid GeoJSON geometryCollection object.json.type
("GeometryCollection"
) The geometryCollection type.json.geometries
Array 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.