admin管理员组文章数量:1420110
I want to integrate a feature using Openlayers 4.x is that I want to get all points inside a polygon on map. Currently I can get all coordinates of polygon itself.
But I want all coordinates or points inside of the polygon. If I explain it more that means I want all points or coordinates on the map surrounded by the polygon area.
I want to integrate a feature using Openlayers 4.x is that I want to get all points inside a polygon on map. Currently I can get all coordinates of polygon itself.
But I want all coordinates or points inside of the polygon. If I explain it more that means I want all points or coordinates on the map surrounded by the polygon area.
Share Improve this question edited Oct 30, 2020 at 13:15 mkrieger1 23.5k7 gold badges64 silver badges82 bronze badges asked Sep 25, 2018 at 13:08 Shahriat HossainShahriat Hossain 3404 silver badges12 bronze badges3 Answers
Reset to default 3Building on @willsters answer, if having identified the candidates and you are only looking for points (where the geometry is the extent) forEachFeatureIntersectingExtent could then be used in the reverse direction to see if the points intersect the polygon's geometry.
var candidates = [];
source.forEachFeatureIntersectingExtent(myPolygon.getGeometry().getExtent(),function(feature){
if (feature.getGeometry().get('type') == 'Point') {
candidates.push(feature);
}
});
var selected = [];
candidates.forEach(function(candidate){
source.forEachFeatureIntersectingExtent(candidate.getGeometry().getExtent(),function(feature){
if (feature === myPolygon) {
selected.push(candidate);
}
});
});
Also for single coordinate points I think it could be done in a single step:
var selected = [];
source.forEachFeatureIntersectingExtent(myPolygon.getGeometry().getExtent(),function(feature){
if (feature.getGeometry().get('type') == 'Point' &&
myPolygon.getGeometry().intersectsCoordinate(feature.getGeometry().get('coordinates')) {
candidates.push(selected);
}
});
Regarding dividing into cells something like this would generate pinpush points for each cell of a 10x10 grid which contained the polygon. If only part of a cell intersected the polygon a pinpush at the center of the cell might be outside the geometry.
var extent = myPolygon.getGeometry().getExtent();
for (var i=extent[0]; i<extent[2]; i+=(extent[2]-extent[0])/10) {
for (var j=extent[1]; j<extent[3]; j+=(extent[3]-extent[1])/10) {
var cellExtent = [i,j,i+(extent[2]-extent[0])/10),j+(extent[3]-extent[1])/10];
source.forEachFeatureIntersectingExtent(cellExtent,function(feature){
if (feature === myPolygon) {
var pinPush = new ol.feature(new ol.geom.Point(ol.extent.getCenter(cellExtent)));
source.addFeature(pinPush);
}
});
}
}
I often include the turf.js library alongside of openlayers specifically for tasks like this. Most of my geometries are nativley in geojson so turf.js is a perfect fit. If you have a geojson FeatureCollection. you can iterate the .features array (or even just any array of [x, y]
points) and check each one to see if it's inside your polygon. I can make a working fiddle if it helps.
// In this example I'm looking for all features
// that have AT LEAST ONE point within
// the world extent (WGS84)
const polygon = turf.bboxPolygon([-180, -90, 180, 90])
myGeoJson.features.forEach(function(feature){
const points = turf.explode(feature);
const pointsWithin = turf.pointsWithinPolygon(points, polygon);
if(pointsWithin.features && pointsWithin.features.length){
// feature has AT LEAST ONE point inside the polygon
// I can see what points by iterating the
// pointsWithin.features array
}else{
// feature has ZERO points inside the polgyon
}
});
http://turfjs/docs#pointsWithinPolygon
Use vectorSource.forEachFeatureIntersectingExtent()
with polygonGeom.getExtent()
. That will get you the extent shortcut for most of them. After that you will need to either implement point-in-poly yourself (there are numerous sources online for this) or use a library like https://github./bjornharrtell/jsts. OpenLayers only provides geometry intersection with extents.
本文标签: javascriptHow to get all points inside a polygon in openlayers 4xStack Overflow
版权声明:本文标题:javascript - How to get all points inside a polygon in openlayers 4.x? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744745026a2622823.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论