Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to fetch feature centroid #12

Merged
merged 1 commit into from Sep 4, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions services/features.js
Expand Up @@ -64,18 +64,33 @@ function parseBoundingBox(bbox_geo_json) {
];
}

function parseCentroid(centroid_geo_json) {
const feature = JSON.parse(centroid_geo_json);
const coords = feature.coordinates;
if (feature.type !== 'Point' || coords.length !== 2) {
return null;
}

const x = coords[0];
const y = coords[1];

return [x, y];
}

function rowToFeature(row, columns) {
if (!row) return;

row['createdAt'] = row['created_at'];
row['updatedAt'] = row['updated_at'];
if (row['hull_geo_json']) row['hull'] = JSON.parse(row['hull_geo_json']);
if (row['bbox_geo_json']) row['bbox'] = parseBoundingBox(row['bbox_geo_json']);
if (row['centroid_geo_json']) row['centroid'] = parseCentroid(row['centroid_geo_json']);

delete row['created_at'];
delete row['updated_at'];
delete row['hull_geo_json'];
delete row['bbox_geo_json'];
delete row['centroid_geo_json'];

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

if (includeArray.indexOf('bbox') !== -1) queryColumns += ',ST_AsGeoJSON(ST_Envelope(hull)) as bbox_geo_json';
if (includeArray.indexOf('centroid') !== -1) queryColumns += ',ST_AsGeoJSON(ST_Centroid(hull)) as centroid_geo_json';
if (includeArray.indexOf('hull') !== -1) queryColumns += ',ST_AsGeoJSON(hull) as hull_geo_json';
if (includeArray.indexOf('properties') !== -1) queryColumns += ',properties';
}
Expand Down