1 | const area = require('geojson-area');
|
2 | const Constants = require('../constants');
|
3 |
|
4 | const FEATURE_SORT_RANKS = {
|
5 | Point: 0,
|
6 | LineString: 1,
|
7 | Polygon: 2
|
8 | };
|
9 |
|
10 | function 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 |
|
21 | function 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 |
|
39 | module.exports = sortFeatures;
|