Skip to content

Commit 8faa6e2

Browse files
authored
Merge pull request #12 from CatalystCode/centroid-response-option
Add option to fetch feature centroid
2 parents ca9976d + d0f7d63 commit 8faa6e2

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

services/features.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,33 @@ function parseBoundingBox(bbox_geo_json) {
6464
];
6565
}
6666

67+
function parseCentroid(centroid_geo_json) {
68+
const feature = JSON.parse(centroid_geo_json);
69+
const coords = feature.coordinates;
70+
if (feature.type !== 'Point' || coords.length !== 2) {
71+
return null;
72+
}
73+
74+
const x = coords[0];
75+
const y = coords[1];
76+
77+
return [x, y];
78+
}
79+
6780
function rowToFeature(row, columns) {
6881
if (!row) return;
6982

7083
row['createdAt'] = row['created_at'];
7184
row['updatedAt'] = row['updated_at'];
7285
if (row['hull_geo_json']) row['hull'] = JSON.parse(row['hull_geo_json']);
7386
if (row['bbox_geo_json']) row['bbox'] = parseBoundingBox(row['bbox_geo_json']);
87+
if (row['centroid_geo_json']) row['centroid'] = parseCentroid(row['centroid_geo_json']);
7488

7589
delete row['created_at'];
7690
delete row['updated_at'];
7791
delete row['hull_geo_json'];
7892
delete row['bbox_geo_json'];
93+
delete row['centroid_geo_json'];
7994

8095
return row;
8196
}
@@ -99,6 +114,7 @@ function buildQueryColumns(query) {
99114
let includeArray = query.include.split(',');
100115

101116
if (includeArray.indexOf('bbox') !== -1) queryColumns += ',ST_AsGeoJSON(ST_Envelope(hull)) as bbox_geo_json';
117+
if (includeArray.indexOf('centroid') !== -1) queryColumns += ',ST_AsGeoJSON(ST_Centroid(hull)) as centroid_geo_json';
102118
if (includeArray.indexOf('hull') !== -1) queryColumns += ',ST_AsGeoJSON(hull) as hull_geo_json';
103119
if (includeArray.indexOf('properties') !== -1) queryColumns += ',properties';
104120
}

0 commit comments

Comments
 (0)