1.0.0-alpha.1 • Published 2 years ago

@barcia/maptools v1.0.0-alpha.1

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

MapTools

A set of GeoJSON tools

Read GeoJSON Specification

Getting Started

  1. Install the library:

    npm install @barcia/maptools
  2. Import some utility in your JS code

    import { Feature } from 'maptools'
  3. 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

  1. MapTools
    1. Getting Started
    2. API
      1. Table of Contents
      2. Feature
        1. Parameters
        2. Examples
        3. addProperty
          1. Parameters
          2. Examples
        4. toJSON
          1. Examples
        5. fromJSON
          1. Parameters
          2. Examples
      3. FeatureCollection
        1. Parameters
        2. Examples
        3. addFeatures
          1. Parameters
          2. Examples
        4. removeFeatures
          1. Parameters
          2. Examples
        5. toJSON
          1. Examples
        6. fromJSON
          1. Parameters
          2. Examples
      4. Geometry
        1. Parameters
        2. Examples
        3. toJSON
          1. Examples
        4. fromJSON
          1. Parameters
          2. Examples
        5. validate
          1. Parameters
          2. Examples
      5. GeometryCollection
        1. Parameters
        2. Examples
        3. addGeometries
          1. Parameters
          2. Examples
        4. toJSON
          1. Examples
        5. fromJSON
          1. Parameters
          2. Examples

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], while alt 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], while alt 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

  • features Array The FeatureCollection features array.
  • props Object? The FeatureCollection properties.

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 JS Array.filter() method.
  • dryrun boolean IF true, return the features to remove but don't remove them. (optional, default false)
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.

    • json.type ("FeatureCollection") The featureCollection type.
    • json.features Array An array of features.
    • json.properties Object? The FeatureCollection properties.
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], while alt 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], while alt 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], while alt 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.