@barcia/maptools
v1.0.0-alpha.1
Published
A set of GeoJSON tools
Downloads
1
Readme
MapTools
A set of GeoJSON tools
Getting Started
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()
API
Table of Contents
Feature
Create a GeoJSON Feature.
Parameters
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.
Examples
const feature = new Feature('Point', [105.0, 4.0], { name: 'foo' });
addProperty
Add a new property to the Feature or update an existing one.
Parameters
key
string The property key.value
any The property value.
Examples
feature.addProperty('desc', 'bar');
toJSON
Return the Feature object.
Examples
feature.toJSON();
Returns Object The Feature object
fromJSON
Create a Feature instance from a GeoJSON feature object.
Parameters
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.
Examples
const feature = Feature.fromJSON({
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [105.0, 4.0]
},
properties: {
name: 'foo'
}
});
Returns Feature The Feature instance.
FeatureCollection
Create a FeatureCollection.
Parameters
Examples
const featureCollection = new FeatureCollection([
new Feature('Point', [100.0, 0.0]).toJSON(),
new Feature('Point', [101.0, 1.0]).toJSON()
], { name: 'foo' });
addFeatures
Add new features to the FeatureCollection.
Parameters
features
Array The array with Features to add.
Examples
featureCollection.addFeatures([
new Feature('Point', [103.0, 7.0]).toJSON(),
]);
removeFeatures
Remove features from the FeatureCollection.
Parameters
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
)
Examples
featureCollection.removeFeatures(feature => feature.properties.name === 'foo');
toJSON
Return the FeatureCollection object.
Examples
featureCollection.toJSON();
Returns Object The FeatureCollection object
fromJSON
Create a FeatureCollection instance from a GeoJSON featureCollection object.
Parameters
json
Object A valid GeoJSON featureCollection object.
Examples
const featureCollection = FeatureCollection.fromJSON({
type: 'FeatureCollection',
features: [
{
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [100.0, 0.0]
},
},
]
});
Returns FeatureCollection The FeatureCollection instance.
Geometry
Create a GeoJSON Geometry.
Parameters
type
("Point"
|"LineString"
|"Polygon"
|"MultiPoint"
|"MultiLineString"
|"MultiPolygon"
) The Geometry type.coords
Array The Geometry coordinates with the format[lon, lat, alt]
, whilealt
is optional.
Examples
const geometry = new Geometry('Point', [105.0, 4.0]);
toJSON
Return the Geometry object.
Examples
// { type: 'Point', coordinates: [105.0, 4.0] }
geometry.toJSON();
Returns Object The Geometry object
fromJSON
Create a Geometry instance from a GeoJSON geometry object.
Parameters
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.
Examples
const geometry = Geometry.fromJSON({
type: 'Point',
coordinates: [105.0, 4.0]
});
Returns Geometry The Geometry instance.
validate
Validate a Geometry.
Parameters
type
("Point"
|"LineString"
|"Polygon"
|"MultiPoint"
|"MultiLineString"
|"MultiPolygon"
) The Geometry type.coords
Array The Geometry coordinates with the format[lon, lat, alt]
, whilealt
is optional.
Examples
const isValidPoint = Geometry.validate({
type: 'Point',
coordinates: [105.0, 4.0]
});
Returns boolean True if the Geometry is valid, false otherwise.
GeometryCollection
Create a GeometryCollection.
Parameters
geometries
Array The GeometryCollection features array.
Examples
const geometryCollection = new GeometryCollection([
new Geometry('Point', [100.0, 0.0]).toJSON(),
new Geometry('Point', [101.0, 1.0]).toJSON()
]);
addGeometries
Add new geometries to the GeometryCollection.
Parameters
geometries
Array The array with geometries to add.
Examples
geometryCollection.addGeometries([
new Geometry('Point', [103.0, 7.0]).toJSON(),
]);
toJSON
Return the GeometryCollection object.
Examples
geometryCollection.toJSON();
Returns Object The GeometryCollection object
fromJSON
Create a GeometryCollection from a GeoJSON geometryCollection object.
Parameters
json
Object A valid GeoJSON geometryCollection object.json.type
("GeometryCollection"
) The geometryCollection type.json.geometries
Array An array of geometries.
Examples
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.