geojson-polygon-aggregate

Aggregate properties of GeoJSON polygons, grouped by another set of polygons.

Usage no npm install needed!

<script type="module">
  import geojsonPolygonAggregate from 'https://cdn.skypack.dev/geojson-polygon-aggregate';
</script>

README

geojson-polygon-aggregate

Aggregate properties of GeoJSON polygons, grouped by another set of polygons

Install

npm install geojson-polygon-aggregate

Usage

var aggregate = require('geojson-polygon-aggregate')
var groups = { /* geojson FeatureCollection of polygons */ }
var data = { /* geojson FeatureCollection of polygons with some data */ } 

// assumes that the features in data have a numeric property called 'something'
var result = aggregate.groups(groups, data, {
  'something': aggregate.reducers.sum('something'),
  'something-area-weighted': aggregate.reducers.areaWeightedSum('something'),
  'area': aggregate.reducers.totalArea(),
  'count': aggregate.reducers.count(),
  'arbitraryProperty': function (memo, feature) {
    // the aggregations above are provided for convenience, but you can
    // do whatever you want here. `memo` is the previous return value
    // of this function, or groups.properties['arbitraryProperty'] on the
    // first iteration.
    if (memo) {
      return memo + ', ' + feature.properties['something']
    } else {
      return 'Values: ' + feature.properties['something']
    }
  }
})

API

all

index.js:21-38

Aggregate properties of GeoJSON polygon features.

Parameters

  • data (FeatureCollection.<Polygon>|Array) The polygons to aggregate.
  • features
  • aggregations Object The aggregations as key-value pairs, where the key is the name of the resulting property, and the value is the reducer function with signature (accumulator, clippedFeature, groupingFeature, additionalArgs[0], additionalArgs[1], ...) => accumulator
  • thisArg Object= Optional 'this' context with which to call reducer functions
  • additionalArgs Array= Optional array of additional args with which to call reducer functions

Returns Object A properties object with the aggregated property values

groups

index.js:54-90

Aggregate properties of GeoJSON polygon features, grouped by another set of polygons.

Parameters

  • groups (FeatureCollection.<Polygon>|Array) The polygons by which to group the aggregations.
  • data (FeatureCollection.<Polygon>|Array) The polygons to aggregate.
  • aggregations Object The aggregations as key-value pairs, where the key is the name of the resulting property, and the value is the reducer function with signature (accumulator, clippedFeature, groupingFeature, additionalArgs[0], additionalArgs[1], ...) => accumulator
  • thisArg Object= Optional 'this' context with which to call reducer functions
  • additionalArgs Array= Optional array of additional args with which to call reducer functions

Returns FeatureCollection.<Polygon> A set of polygons whose geometries are identical to groups, but with properties resulting from the aggregations. Existing properties on features in groups are copied (shallowly), but aggregation results will override if they have the same name.

stream

index.js:101-122

Aggregate properties of streaming GeoJSON polygon features.

Parameters

  • aggregations Object The aggregations as key-value pairs, where the key is the name of the resulting property, and the value is the reducer function with signature (accumulator, clippedFeature, groupingFeature, additionalArgs[0], additionalArgs[1], ...) => accumulator
  • thisArg Object= Optional 'this' context with which to call reducer functions
  • additionalArgs Array= Optional array of additional args with which to call reducer functions

Returns Object A transform stream reading GeoJSON feature objects and, writing, at the end, a properties object with the aggregated property values

Thanks

This module relies heavily on the fantastic Turf.js project. If it doesn't do what you need, something over there very likely does!