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

Fix #1612. Extent calculation for GeoJson supported #1615

Merged
merged 3 commits into from
Mar 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 35 additions & 22 deletions web/client/utils/CoordinatesUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,35 +234,48 @@ const CoordinatesUtils = {
}
return newExtent;
},
/**
* Calculates the extent for the geoJSON passed. It used a small buffer for points.
* Like turf/bbox but works only with simple geometries.
* @deprecated We may replace it with turf/bbox + turf/buffer in the future, so using it with geometry is discouraged
* @param {geoJSON|geometry} GeoJSON or geometry
* @return {array} extent of the geoJSON
*/
getGeoJSONExtent: function(geoJSON) {
let newExtent = [Infinity, Infinity, -Infinity, -Infinity];

const reduceCollectionExtent = (extent, collectionElement) => {
let ext = this.getGeoJSONExtent(collectionElement);
if (this.isValidExtent(ext)) {
return this.extendExtent(ext, extent);
}
};
if (geoJSON.coordinates) {
if (geoJSON.type !== "Point" && geoJSON.type !== "GeometryCollection") {
const flatCoordinates = chunk(flattenDeep(geoJSON.coordinates), 2);
flatCoordinates.reduce((extent, point) => {
extent[0] = (point[0] < newExtent[0]) ? point[0] : newExtent[0];
extent[1] = (point[1] < newExtent[1]) ? point[1] : newExtent[1];
extent[2] = (point[0] > newExtent[2]) ? point[0] : newExtent[2];
extent[3] = (point[1] > newExtent[3]) ? point[1] : newExtent[3];
return extent;
}, newExtent);
}else if (geoJSON.type === "Point") {
if (geoJSON.type === "Point") {
let point = geoJSON.coordinates;
newExtent[0] = point[0] - point[0] * 0.01;
newExtent[1] = point[1] - point[1] * 0.01;
newExtent[2] = point[0] + point[0] * 0.01;
newExtent[3] = point[1] + point[1] * 0.01;
}else if (geoJSON.type === "GeometryCollection") {
geoJSON.geometies.reduce((extent, geometry) => {
let ext = this.getGeoJSONExtent(geometry);
if (this.isValidExtent(ext)) {
extent[0] = (ext[0] < newExtent[0]) ? ext[0] : newExtent[0];
extent[1] = (ext[1] < newExtent[1]) ? ext[1] : newExtent[1];
extent[2] = (ext[2] > newExtent[2]) ? ext[2] : newExtent[2];
extent[3] = (ext[3] > newExtent[3]) ? ext[3] : newExtent[3];
}
}, newExtent);
}
// other kinds of geometry
const flatCoordinates = chunk(flattenDeep(geoJSON.coordinates), 2);
return flatCoordinates.reduce((extent, point) => {
return [
(point[0] < extent[0]) ? point[0] : extent[0],
(point[1] < extent[1]) ? point[1] : extent[1],
(point[0] > extent[2]) ? point[0] : extent[2],
(point[1] > extent[3]) ? point[1] : extent[3]
];
}, newExtent);

} else if (geoJSON.type === "GeometryCollection") {
let geometries = geoJSON.geometries;
return geometries.reduce(reduceCollectionExtent, newExtent);
} else if (geoJSON.type) {
if (geoJSON.type === "FeatureCollection") {
return geoJSON.features.reduce(reduceCollectionExtent, newExtent);
} else if (geoJSON.type === "Feature" && geoJSON.geometry) {
return this.getGeoJSONExtent(geoJSON.geometry);
}
}

Expand All @@ -278,7 +291,7 @@ const CoordinatesUtils = {
isValidExtent: function(extent) {
return !(
extent.indexOf(Infinity) !== -1 || extent.indexOf(-Infinity) !== -1 ||
extent[1] >= extent[2] || extent[1] >= extent[3]
extent[0] > extent[2] || extent[1] > extent[3]
);
},
calculateCircleCoordinates: function(center, radius, sides, rotation) {
Expand Down
61 changes: 61 additions & 0 deletions web/client/utils/__tests__/CoordinatesUtils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,65 @@ describe('CoordinatesUtils', () => {
expect(reprojectedTestPoint.features[0].geometry.coordinates[0].toFixed(4)).toBe((-12523490.492568726).toFixed(4));
expect(reprojectedTestPoint.features[0].geometry.coordinates[1].toFixed(4)).toBe((5195238.005360028).toFixed(4));
});

it('test geojson extent', () => {
let geojsonPoint = {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [125.6, 10.1]
},
"properties": {
"name": "Dinagat Islands"
}
};
expect(CoordinatesUtils.getGeoJSONExtent(geojsonPoint)[0] <= 125.6).toBe(true);
expect(CoordinatesUtils.getGeoJSONExtent(geojsonPoint)[1] <= 10.1).toBe(true);
expect(CoordinatesUtils.getGeoJSONExtent(geojsonPoint)[2] >= 125.6).toBe(true);
expect(CoordinatesUtils.getGeoJSONExtent(geojsonPoint)[3] >= 10.1).toBe(true);
let featureCollection = { "type": "FeatureCollection",
"features": [
{ "type": "Feature",
"geometry": {"type": "Point", "coordinates": [102.0, 0.5]},
"properties": {"prop0": "value0"}
},
{ "type": "Feature",
"geometry": {
"type": "GeometryCollection",
"geometries": [{"type": "Point", "coordinates": [102.0, 0.5]}]
},
"properties": {"prop0": "value0"}
},
{ "type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": [
[102.0, 0.0], [103.0, 1.0], [104.0, 0.0], [105.0, 1.0]
]
},
"properties": {
"prop0": "value0",
"prop1": 0.0
}
},
{ "type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0],
[100.0, 1.0], [100.0, 0.0] ]
]
},
"properties": {
"prop0": "value0",
"prop1": {"this": "that"}
}
}
]
};
expect(CoordinatesUtils.getGeoJSONExtent(featureCollection)[0]).toBe(100.0);
expect(CoordinatesUtils.getGeoJSONExtent(featureCollection)[1]).toBe(0.0);
expect(CoordinatesUtils.getGeoJSONExtent(featureCollection)[2]).toBe(105.0);
expect(CoordinatesUtils.getGeoJSONExtent(featureCollection)[3]).toBe(1.0);
});
});