This is on the frontend side. I'm using turf.js.
Scenario: Get all warnings and incidents within 10km of my current location.
I'm getting live geojson feed that contains warnings or incidents with lots of features some having their geometry.type='Point' || geometry.type='MultiPolygon' || geometry.type='GeometryCollection'
.
What I have done so far:
Create a buffer feature area with my current coordinates and comparing if an incident has occurred near me(10km).
If geometry.type='Point' I'm comparing using turf.inside(point, bufferPolygon), and this is working fine.
The challenge is when it's not a point, say a MultiPolygon or a GeometryCollection which has MultiPolygons in it.
The only other method that I can use to find that accepts polygon in both parameter is turf.intersect(polygon, bufferPolygon)
.
Here, I don't get geometry.type ='Polygon'
in my feed but it is a MultiPolygon
or GeometryCollection
(having MultiPolygons).
if(feature.geometry.type === 'Point') {
if(turf.inside(feature, bufferPolygon)) {
console.log("inside");
feature.properties.feedType === 'warning' ? warningsNearBy++ : incidentsNearBy++;
}
} else {
if(feature.geometry.type === 'GeometryCollection') {
$.each(feature.geometry.geometries, function(index, geo) {
if(geo.type === 'MultiPolygon') {
//console.log('MP:', geo.coordinates[0]);
var convertToPolygon = turf.polygon(geo.coordinates[0]);
console.log('convertToPolygon:',convertToPolygon);
//console.log('MP:intersect',turf.intersect(polygon, bufferPolygon));
var isIntersecting = turf.intersect(convertToPolygon, bufferPolygon);
if(isIntersecting) {
feature.properties.feedType === 'warning' ? warningsNearBy++ : incidentsNearBy++;
}
} else if(geo.type === 'GeometryCollection') {
console.log('GC:', geo);
}
});
} else {
console.log('not geo collection:', feature.geometry.type);
}
}
Also, tried to convert a multiPolygon to a Polygon, didn't work that well.
Can any one please suggest a way to compare a bufferFeature with a set of feature collection having point, multipolygon and GeometryCollection?