0%

geojson-multiply: a simple package to pack single tyle geojson features

Some PostGIS functions only accept multi type geometry, so I write a simple geojson utility package to help me aggregate geojson features.

The basic purpose of geojson-multiply is to generate a MutliPoint/MultiLineString/MultiPolygon geojson feature from many Point/LineString/Polygon geojson features. So this package provides a function

1
multiply(geojsons[, options])

Where the geojsons could be a geojson feature, an array of geojson features, or a geojson feature collection

Not just the coordinates, the multiply() also supports the aggregation of properties. Its options parameter accepts two input:

  • properties - the default properties of result geojson

  • onEachFeature - a function to aggregate properties. It has four parameters:

    • properties - the result geojson’s properties

    • featureProp - input feature geojson’s properties

    • index - input feature geojson’s index in the array

    • geojsons - geojson array.

It takes the form of Array.reduce() and make the aggregation pretty straightforward:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
var multiply = require('geojson-multiply');

var geojsonA = {
type: 'Feature',
geometry: { type: 'Point', coordinates: [12, 43] },
properties: { count: 5 }
};

var geojsonB = {
type: 'Feature',
geometry: { type: 'Point', coordinates: [13, 34] },
properties: { count: 5 }
};

var onEachFeature = function(properties, featureProp) {
properties.count += featureProp.count;
return properties;
};

var result = multiply([geojsonA, geojsonB], {
properties: { count: 0 },
onEachFeature: onEachFeature
});

/**
The reuslt geojson should be
{
type: 'Feature'
geometry: {
type: 'MultiPoint',
coordinates: [[12, 43], [14, 34]]
},
properties: {
count: 10
}
}
*/

This package has been published at npm. If you think it’s helpful, just install and try!

1
npm install geojson-multiply --save