Skip to content

Commit 49ce1de

Browse files
committed
Simplify parsing of hull for bbox response field
Previously we used `ST_AsText` to convert the geometry of the bounding box to WKT and then parsed that using regular expressions. We can simplify this by converting to GeoJSON instead which is much simpler to parse.
1 parent 327df67 commit 49ce1de

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

services/features.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,19 @@ function getById(query, callback) {
4242
}
4343

4444
function parseBoundingBox(bbox_geo_json) {
45-
const match = /^POLYGON\(\((.*)\)\)$/.exec(bbox_geo_json)
46-
if (!match || match.length !== 2) {
45+
const feature = JSON.parse(bbox_geo_json);
46+
const coords = feature.coordinates && feature.coordinates[0];
47+
if (feature.type !== 'Polygon' || coords.length !== 5) {
4748
return null;
4849
}
4950

50-
const coords = match[1].split(',').map(point => point.split(' '));
51-
if (coords.length !== 5 || coords.some(point => point.length !== 2)) {
52-
return null;
53-
}
51+
const xs = coords.map(point => point[0]);
52+
const ys = coords.map(point => point[1]);
5453

55-
const minX = parseFloat(coords[0][0]);
56-
const minY = parseFloat(coords[0][1]);
57-
const maxX = parseFloat(coords[2][0]);
58-
const maxY = parseFloat(coords[2][1]);
54+
const minX = Math.min(...xs);
55+
const minY = Math.min(...ys);
56+
const maxX = Math.max(...xs);
57+
const maxY = Math.max(...ys);
5958

6059
return [
6160
maxY, // north
@@ -99,7 +98,7 @@ function buildQueryColumns(query) {
9998
if (query.include) {
10099
let includeArray = query.include.split(',');
101100

102-
if (includeArray.indexOf('bbox') !== -1) queryColumns += ',ST_AsText(ST_Envelope(hull)) as bbox_geo_json';
101+
if (includeArray.indexOf('bbox') !== -1) queryColumns += ',ST_AsGeoJSON(ST_Envelope(hull)) as bbox_geo_json';
103102
if (includeArray.indexOf('hull') !== -1) queryColumns += ',ST_AsGeoJSON(hull) as hull_geo_json';
104103
if (includeArray.indexOf('properties') !== -1) queryColumns += ',properties';
105104
}

0 commit comments

Comments
 (0)