@svizzle/geo/geojson

Methods

(static) getOrMakeBBox(geojson) → {array}

Source:
Since:
  • 0.1.0

Return or create the bbox of the provided geojson

Example
> getOrMakeBBox({
	type: 'FeatureCollection',
	features: [{
		type: 'Feature',
		geometry: {
			type: 'Polygon',
			coordinates: [
				[[1, -1], [1, 1], [-1, 1], [-1, -1], [1, -1]]
			]
		}
	}, {
		type: 'Feature',
		geometry: {
			type: 'Polygon',
			coordinates: [
				[[2, -1], [2, 1], [0, 1], [0, -1], [2, -1]]
			]
		}
	}]
})
[-1, -1, 2, 1]

> // no calculation involved here
> getOrMakeBBox({
	type: 'FeatureCollection',
	bbox: [-10.0, -10.0, 10.0, 10.0],
	geometry: {
	type: 'Polygon',
	coordinates: [
		[
			[-10.0, -10.0],
			[10.0, -10.0],
			[10.0, 10.0],
			[-10.0, -10.0]
		]
	]
	}
})
[-10.0, -10.0, 10.0, 10.0]
Parameters:
Name Type Description
geojson object

Geojson object

Returns:
Type
array

(static) makeCentroids(features) → {object}

Source:
Since:
  • 0.1.0

Return the a collection of centroids of the provided features, each having the correspondent feature properties.

Example
> makeCentroids([
	{
		type: 'Feature',
		properties: {foo: 'a'},
		geometry: {type: 'LineString', coordinates: [
			[[1, -1], [1, 1], [-1, 1], [-1, -1], [1, -1]]
		]}
	},
	{
		type: 'Feature',
		properties: {foo: 'b'},
		geometry: {type: 'LineString', coordinates: [
			[[2, -1], [2, 1], [0, 1], [0, -1], [2, -1]]
		]}
	}
])
{
	type: 'FeatureCollection',
	features: [{
		type: 'Feature',
		geometry: {type: 'Point', coordinates: [0.2, -0.2]},
		properties: {foo: 'a'}
	}, {
		type: 'Feature',
		geometry: {type: 'Point', coordinates: [1.2, -0.2]},
		properties: {foo: 'b'}
	}]
}
Parameters:
Name Type Description
features array

Array of features

Returns:

collection - FeatureCollection of Point features

Type
object

(static) makeToGeoPoints(coordPicker, propsTransformer) → {object}

Source:
Since:
  • 0.1.0

Return a function expecting an array of objects and returning them as a FeatureCollection of Point features. You can define a coordPicker using makeKeysGetter: const getCoordinates = makeKeysGetter(['lng', 'lat'])

Example
> coordPicker = _.collect([_.getKey('lng'), _.getKey('lat')])
> toGeoPoints = makeToGeoPoints(coordPicker)
> toGeoPoints([
	{foo: 'a', lng: 0.1, lat: 0.1},
	{foo: 'b', lng: 0.2, lat: 0.2}
])
{
	type: 'FeatureCollection',
	features: [{
		type: 'Feature',
		geometry: {type: 'Point', coordinates: [0.1, 0.1]},
		properties: {foo: 'a', lng: 0.1, lat: 0.1}
	}, {
		type: 'Feature',
		geometry: {type: 'Point', coordinates: [0.2, 0.2]},
		properties: {foo: 'b', lng: 0.2, lat: 0.2}
	}]
}

> propsTransformer = applyFnMap({name: _.getKey('foo')})
> toGeoPoints = makeToGeoPoints(coordPicker, propsTransformer)
> toGeoPoints([
	{foo: 'a', lng: 0.1, lat: 0.1},
	{foo: 'b', lng: 0.2, lat: 0.2}
])
{
	type: 'FeatureCollection',
	features: [{
		type: 'Feature',
		geometry: {type: 'Point', coordinates: [0.1, 0.1]},
		properties: {name: 'a'}
	}, {
		type: 'Feature',
		geometry: {type: 'Point', coordinates: [0.2, 0.2]},
		properties: {name: 'b'}
	}]
}
Parameters:
Name Type Description
coordPicker function

The function to create the point coordinates ([longitude, latitude]) from the provided features

propsTransformer function

The function to create the properties of the resulting points from the provided features

Returns:

collection - FeatureCollection of Point features

Type
object

(static) makeToPointFeature(coordPicker, propsTransformer) → {object}

Source:
Since:
  • 0.1.0

Return a function expecting an object and returning it as a Point feature. You can define a coordPicker using makeKeysGetter: const getCoordinates = makeKeysGetter(['lng', 'lat'])

Example
> coordPicker = _.collect([_.getKey('lng'), _.getKey('lat')])
> toPointFeature = makeToPointFeature(coordPicker)
> toPointFeature({foo: 'a', lng: 0.1, lat: 0.1})
{
	type: 'Feature',
	geometry: {type: 'Point', coordinates: [0.1, 0.1]},
	properties: {foo: 'a', lng: 0.1, lat: 0.1}
}

> const propsTransformer = applyFnMap({name: _.getKey('foo')})
> const toPointFeature = makeToPointFeature(coordPicker, propsTransformer)
> toPointFeature({foo: 'a', lng: 0.1, lat: 0.1})
{
	type: 'Feature',
	geometry: {type: 'Point', coordinates: [0.1, 0.1]},
	properties: {name: 'a'}
}
Parameters:
Name Type Description
coordPicker function

The function to create the point coordinates ([longitude, latitude]) from the provided feature

propsTransformer function

The function to create the properties of the resulting point from the provided feature

Returns:

point - Geojson Point feature.

Type
object

(static) makeUpdateFeaturesProperty(args) → {function}

Source:
Since:
  • 0.5.0

Return a function expecting a geojson and creating or updating the provided property of all features using the provided map. Note that you can pass a key or an alternative key key_alt` e.g. when you use ISO Alpha 2 codes and you need to identify unrecognized territories with another key.

Example
> geojson = {
	type: 'FeatureCollection',
	features: [{
		type: 'Feature',
		geometry: {
			type: 'Polygon',
			coordinates: [
				[[1, -1], [1, 1], [-1, 1], [-1, -1], [1, -1]]
			]
		},
		properties: {iso_a2: 'BF'}
	}, {
		type: 'Feature',
		geometry: {
			type: 'Polygon',
			coordinates: [
				[[2, -1], [2, 1], [0, 1], [0, -1], [2, -1]]
			]
		},
		properties: {name: 'Kosovo'}
	}, {
		type: 'Feature',
		geometry: {
			type: 'Polygon',
			coordinates: [
				[[4, -1], [2, 7], [0, 5], [0, -4], [4, -1]]
			]
		},
		properties: {iso_a2: 'FR'}
	}]
}
> keyToColor = {BF: 'red', Kosovo: 'yellow'}
> addColor = makeAddFeaturesProperty({
	propName: 'color',
	map: keyToColor,
	key: 'iso_a2',
	key_alt: 'name'
})
> coloredFeatures = addColor(geojson)
{
	type: 'FeatureCollection',
	features: [{
		type: 'Feature',
		geometry: {
			type: 'Polygon',
			coordinates: [
				[[1, -1], [1, 1], [-1, 1], [-1, -1], [1, -1]]
			]
		},
		properties: {iso_a2: 'BF', color: 'red'}
	}, {
		type: 'Feature',
		geometry: {
			type: 'Polygon',
			coordinates: [
				[[2, -1], [2, 1], [0, 1], [0, -1], [2, -1]]
			]
		},
		properties: {name: 'Kosovo', color: 'yellow'}
	}, {
		type: 'Feature',
		geometry: {
			type: 'Polygon',
			coordinates: [
				[[4, -1], [2, 7], [0, 5], [0, -4], [4, -1]]
			]
		},
		properties: {iso_a2: 'FR', color: undefined}
	}]
}
Parameters:
Name Type Description
args object

Geojson object

Properties
Name Type Description
key_alt string

Alternative key to be found in properties in key is not found.

key string

Key to be found in properties

map object

Mapping key (string) -> string

mapFn function

Function key (string) -> string

propName string

Name of the property to be added to properties

Returns:
  • Object -> Object
Type
function

(static) setGeometryPrecision(precision) → {function}

Source:
Since:
  • 0.1.0

Return a function returning a copy of the provided geojson having the geometry coordinates rounded to the given precision.

Example
> truncateGeometry = setGeometryPrecision(4)
> point = {
	type: 'Feature',
	geometry: {type: 'Point', coordinates: [0.1234567, 0.12341]},
	properties: {name: 'a'}
}
> truncateGeometry(point)
{
	type: 'Feature',
	geometry: {type: 'Point', coordinates: [0.1234, 0.1234]},
	properties: {name: 'a'}
}
Parameters:
Name Type Description
precision number

coordinate decimal precision

Returns:
  • Geojson -> Geojson
Type
function