TurfJs 交叉口问题

问题描述 投票:0回答:1

嗨,我正在尝试实现地图对象(街道区域),如果与其他街道区域相交,它们就会弯曲。我已经实现了下一个逻辑:当区域绘制完成后,我尝试使用 turfjs 中的

intersection
函数找到它与其他区域的所有交集,然后我将所有交集与
union
结合起来并使用
 difference
与原始对象

获取并集交集fn(截断用于补偿边界之间的距离)

    const format = new GeoJSON();
    const feaPoly = format.writeFeatureObject(feature) as GeoJSONPolygon;
    
    const intersections = featuresToGetIntersectionsWith
      .filter(existingFea =>
        existingFea.get('type') !== feature.get('type') ||
        existingFea.getId() !== feature.getId())
      .map((existingFea) => {
        const existingFeaturePoly = format.writeFeatureObject(existingFea) as GeoJSONPolygon;
        return intersect(feaPoly, existingFeaturePoly);
      })
      .filter(intersection => intersection);


    if (!intersections.length) { return null; }

    return intersections
      .reduce((prev, curr) => union(truncate(prev), truncate(curr)));
  } 

弯曲功能

        try {
      const format = new GeoJSON();
      const feaPoly = format.writeFeatureObject(feature) as GeoJSONPolygon;
      const unionIntersection = this.getUnionIntersection(feature, featuresToBend);
      if (!unionIntersection) { return; }
      const diff = difference(feaPoly, unionIntersection) as GeoJSONPolygon | GeoJSONMultiPolygon;

      if (!diff) { throw Error('Inner fea'); }
      if (diff.geometry.type === 'MultiPolygon' && !ignoreMultiPolygon) { throw Error('Many shapes'); }
      
      return { difference: diff, intersection: unionIntersection, bendedFeatures: featuresToBend };
        } catch (e) {
            this.handleBendError(objType, e);
            throw Error(e.message);
        }
    }

正如您所看到的,在我的例子中,唯一适合差异结果的情况是多边形。创建前两个街道区域时一切正常,当绘制第三个街道区域时问题开始出现

第一个多边形的坐标(屏幕截图中的蓝色)

[
    [
        9227079.586445518,
        7361106.72521227
    ],
    [
        9226950.666712714,
        7358240.1570358025
    ],
    [
        9229240.8878484,
        7358528.330556187
    ],
    [
        9227079.586445518,
        7361106.72521227
    ]
]

第二个多边形的坐标(屏幕截图中的紫色)

[
    [
        9227891.022410812,
        7361228.061431374
    ],
    [
        9227789.82178895,
        7360259.4269078225
    ],
    [
        9229240.8878484,
        7358528.330556187
    ],
    [
        9229230.813561888,
        7358527.062930729
    ],
    [
        9229976.488676753,
        7358452.495419242
    ],
    [
        9227891.022410812,
        7361228.061431374
    ]
]

第一个(蓝色)和新多边形(红色)的交点坐标

[
    [
        9227440.736314,
        7359644.350225
    ],
    [
        9228159.754835,
        7359818.103274
    ],
    [
        9227491.252183,
        7360615.615209
    ],
    [
        9227440.736314,
        7359644.350225
    ]
]

第二个多边形(紫色)和新多边形(红色)的交点坐标

[
    [
        9227789.821789,
        7360259.426908
    ],
    [
        9228159.754835,
        7359818.103274
    ],
    [
        9228476.375107,
        7359894.615543
    ],
    [
        9227821.439394,
        7360562.052554
    ],
    [
        9227789.821789,
        7360259.426908
    ]
]

截断交点几何图形的并集结果是其中存在两个交点的多多边形。我只需要一个统一的多边形。而且,因为

truncate
difference
返回多边形,其点留在交叉点的边界上

红色多边形是我正在画的,沙拉部分是

difference
结果后需要留下的 enter image description here

geometry openlayers geo turfjs
1个回答
0
投票

首先,如果您使用 Turf,您应该将坐标转换为经度、纬度(-180 到 180、-90 到 90)。

之后,如果您想要的是绿色多边形 - 红色多边形中未被蓝色或紫色多边形的任何部分覆盖的部分 - 可以使用 Turf 只需几个步骤即可完成。

简单来说,首先得到蓝色和紫色多边形的并集。然后得到红色多边形和联合多边形之间的差异。

const bluePoly = turf.polygon([
  [
    [
      41.76534176121291,
      55.30020075177271
    ],
    [
      41.53667178476189,
      54.93371491220336
    ],
    [
      42.030982332657516,
      54.950231895745446
    ],
    [
      41.76534176121291,
      55.30020075177271
    ]
  ]
]);

const purplePoly = turf.polygon([
  [
    [
      42.23500146948683,
      54.926542661401385
    ],
    [
      41.99537724266625,
      55.33439338563056
    ],
    [
      41.84338701879824,
      54.97293854904663
    ],
    [
      42.23500146948683,
      54.926542661401385
    ]
  ]
]);

const redPoly = turf.polygon([
  [
    [
      41.87898833249693,
      55.25019065857009
    ],
    [
      41.79546217343503,
      55.0475312977143
    ],
    [
      42.056994900991924,
      55.0781134256396
    ],
    [
      41.87898833249693,
      55.25019065857009
    ]
  ]
]);

const blueAndPurplePoly = turf.union(turf.featureCollection([bluePoly, purplePoly]));
const greenPoly = turf.difference(turf.featureCollection([redPoly, blueAndPurplePoly]));

console.log(greenPoly);
<script src="https://cdn.jsdelivr.net/npm/@turf/[email protected]/turf.min.js"></script>

Four polygons

© www.soinside.com 2019 - 2024. All rights reserved.