如何在 OL3 中检查一个多边形是否包含另一个多边形?

How to check if a polygon contains another polygon in OL3?

在 OL3 中是否可以检查一个多边形是否包含在另一个多边形中?

我注意到 ol.extent 中有几个方法,例如ol.extent.containsExtent(extent1, extent2)。但这只检查范围而不是整个多边形。所以在某些情况下我得到 "true" 虽然多边形在另一个多边形之外,但仍在范围内...... 我需要的是一个函数或算法来检查一个多边形是否完全包含另一个多边形,就像我通常在任何 GIS 中所做的那样。

我没有使用 OL3 的经验,但是:

如果你有两个多边形 A 和 B,并且 A 的所有顶点都位于 B 的区域内,则 B 包含 A。运行 ol.extent.containsCoordinate 在 A 的每个坐标上应该做诀窍。

旁注:如果 B 是凹的(不确定这是这里使用的单词的正确形式),这可能行不通...

OpenLayers 3 不包含检查一个多边形是否在另一个多边形内部的功能。但是您可以使用 JSTS, the JavaScript port of JTS Topology Suite 进行检查:

    var feature1 = new ol.Feature(new ol.geom.Polygon(...));
    var feature2 = new ol.Feature(new ol.geom.Polygon(...));

    var format = new ol.format.GeoJSON();
    var geojsonReader = new jsts.io.GeoJSONReader();

    var polygon1Jsts = geojsonReader.read(
        format.writeFeatureObject(feature1)).geometry;
    var polygon2Jsts = geojsonReader.read(
        format.writeFeatureObject(feature2)).geometry;

    console.log(polygon1Jsts.contains(polygon2Jsts));

http://jsfiddle.net/d6o81vc7/1/

另请参阅:jsts.geom.Geometry.html#contains