检查两个 GeoJSON 是否重叠

Checking if two GeoJSONs overlap

我有两个有效的 GeoJSON。我试图了解它们是否重叠。我一直在尝试使用 Turf.js 的线相交,如下所示:

import { lineIntersect } from '@turf/turf';
if (lineIntersect(e.layer.toGeoJSON(), realGeoJSON)) 
    {
         console.log (lineIntersect(e.layer.toGeoJSON(), realGeoJSON));
    }

不幸的是,这只会带回一个空的特征集合。

例如,这里有两个重叠的特征(一个包含另一个),但是当我对它们进行 运行 lineIntersect 时,我得到一个空结果:

{"type":"Feature","properties":{},"geometry":{"type":"Polygon","coordinates":[[[34.38151665119666,31.47890462548891,0],[34.24131053682105,31.39519970105964,0],[34.20391229566884,31.31046528984165,0],[34.31023972371427,31.12192885120737,0],[34.32238143660881,31.09608541854309,0],[34.32845010662197,31.08316227875504,0],[34.3399878573332,31.05111274029189,0],[34.3819736618827,30.87941536483305,0],[34.4625794738603,30.65469000345236,0],[34.46079897849552,30.63605651330755,0],[34.48093837332151,30.54064393834374,0],[34.48701144862344,30.52765539371588,0],[34.55491673915576,30.3971320487581,0],[34.57859864255086,30.33884544466566,0],[34.5841010709255,30.3195774836368,0],[34.59568048671346,30.28726986771069,0],[34.59452479274348,30.27479275329208,0],[34.62488304800757,30.20958854031441,0],[34.6387603290821,30.20222082678984,0],[34.71436347629015,30.07682031495306,0],[34.71259779426985,30.05809037881291,0],[34.71628915470217,30.02005342200006,0],[34.75076906620446,29.92290870131085,0],[34.75680680602306,29.90983473619655,0],[34.86200494951397,29.78080306661147,0],[34.86038824538134,29.76186588722682,0],[34.85985465965802,29.75554744508348,0],[34.87458404107667,29.67157102811895,0],[34.87967097237944,29.64566203932595,0],[34.88079268646108,29.57563973494665,0],[34.90538890070649,29.52945734421175,0],[34.98109436237601,29.5542870425272,0],[35.03949367509274,29.63847028614006,0],[35.09890268380539,29.73532624342861,0],[35.10662487673798,29.74108861079644,0],[35.15838643590403,29.83204001172806,0],[35.17581417200116,29.86220648447516,0],[35.17642201553415,29.86846832766622,0],[35.22752559603887,29.94641218538877,0],[35.23716690705381,29.97087168207232,0],[35.26672235396222,30.05048288346658,0],[35.27039417463298,30.08801137042606,0],[35.28897082403768,30.13071126439134,0],[35.18979861092051,30.25551467908188,0],[35.38491270168139,30.93027973988132,0],[35.45082126925127,31.06594264974105,0],[35.44722368690986,31.24990699939903,0],[35.39159459673273,31.49811901942546,0],[35.30663360486346,31.50514564876487,0],[35.22682220736229,31.51219288891959,0],[35.20182280376471,31.50399372918324,0],[35.20257232954722,31.51072084692123,0],[35.17845425556429,31.50456774949482,0],[35.17715019191054,31.5080912969423,0],[35.17584130839815,31.51161346815049,0],[35.17615545360398,31.51499006142148,0],[35.17634008732453,31.51701656989114,0],[35.19462720371881,31.51275211889307,0],[35.17373380395745,31.50634219008566,0],[35.17211642460078,31.50648424243521,0],[35.13663682917868,31.50923658211334,0],[35.10997231069795,31.50448491651967,0],[35.11045603614101,31.50175523199227,0],[35.10608104898849,31.49803734440621,0],[35.09319760456682,31.49631331040035,0],[35.0640867946593,31.4910299229135,0],[35.04236314932221,31.49608863302037,0],[35.02071025749389,31.50178829489378,0],[34.99360687527013,31.50914407573474,0],[34.98304638712193,31.51529139391714,0],[34.9607716570927,31.50874723841895,0],[34.96284549594341,31.51335157521662,0],[34.96554955203035,31.51652570530491,0],[34.96821565782273,31.51905477026719,0],[34.96915140126914,31.5203480011361,0],[34.95586297893289,31.50774103465133,0],[34.96594857172293,31.52806484995884,0],[34.96852953236886,31.53740852679276,0],[34.96525541695491,31.54445456073684,0],[34.96827302996868,31.55035702098375,0],[34.97157587431909,31.55896103778682,0],[34.97479844343971,31.56688530825338,0],[34.97513871614513,31.57026095463069,0],[34.97520672211002,31.57093623752663,0],[34.96206021732661,31.5910034931354,0],[34.96133836855449,31.591739276496,0],[34.94070502436983,31.60148403071715,0],[34.92241820431752,31.61106005296995,0],[34.89434374334633,31.60392503100067,0],[34.80687543628677,31.61045864537442,0],[34.79957082732486,31.61099992786523,0],[34.73003921185517,31.64144693398919,0],[34.63718286917748,31.65305512858577,0],[34.54700252433163,31.66989760698388,0],[34.52042121323742,31.61883522021243,0],[34.38151665119666,31.47890462548891,0]]]}}
,{"type":"Feature","properties":{},"geometry":{"type":"Polygon","coordinates":[[[34.928146,30.991737],[35.103928,30.982319],[35.005051,30.897511],[34.928146,30.991737]]]}}  ]

事实证明,使用 Turf 的 booleanWithin 函数是有效的。对于那些边缘情况,我也使用 booleanOverlaps。像这样:

if (
    booleanWithin(e.layer.toGeoJSON(), realGeoJSON) || booleanOverlaps(e.layer.toGeoJSON(), realGeoJSON)) 
      {this.props.setNewPlotRegion(a.RegionName);} 

您可以使用 intersect() (@turf/intersect) 其中 returns 代表覆盖两个 GeoJSON 交集的所有形状的特征。

如果你只想知道,无论是真还是假,如果两者之间有任何交集,来自 API 的任何 non-null 响应都将指示交集。

另一方面,如果您想检查重叠而不仅仅是相交的情况(相交的形状有面积,而不仅仅是共享的边或点),则需要使用类似的算法来解析响应这个:

* If feature.geometry.type is "Polygon" or "MultiPolygon" then return true (it overlaps) * If feature.geometry.type is "GeometryCollection" then iterate through all members in the collection and return true if a "Polygon" or "MultiPolygon" is found. * Otherwise, return false. (no overlap)