UNPKG

913 BJavaScriptView Raw
1const area = require('geojson-area');
2const Constants = require('../constants');
3
4const FEATURE_SORT_RANKS = {
5 Point: 0,
6 LineString: 1,
7 Polygon: 2
8};
9
10function comparator(a, b) {
11 const score = FEATURE_SORT_RANKS[a.geometry.type] - FEATURE_SORT_RANKS[b.geometry.type];
12
13 if (score === 0 && a.geometry.type === Constants.geojsonTypes.POLYGON) {
14 return a.area - b.area;
15 }
16
17 return score;
18}
19
20// Sort in the order above, then sort polygons by area ascending.
21function sortFeatures(features) {
22 return features.map(feature => {
23 if (feature.geometry.type === Constants.geojsonTypes.POLYGON) {
24 feature.area = area.geometry({
25 type: Constants.geojsonTypes.FEATURE,
26 property: {},
27 geometry: feature.geometry
28 });
29 }
30 return feature;
31 })
32 .sort(comparator)
33 .map(feature => {
34 delete feature.area;
35 return feature;
36 });
37}
38
39module.exports = sortFeatures;