turfjs多边形切割多边形求交集

文章目录


前言

分割矢量多边形

一、安装turf

bash 复制代码
npm i @turf/turf

二、加载高德

bash 复制代码
AMapLoader.load({
    key: "你的key", // 申请好的Web端开发者Key,首次调用 load 时必填
    version: "2.0", // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
    plugins: ["AMap.PolygonEditor", "AMap.LngLat", "AMap.Polygon"], // 需要使用的的插件列表,如比例尺'AMap.Scale'等
  }).then((AMap) => {
  //此处处理后续操作
}

三、绘制图形

bash 复制代码
    map = new AMap.Map("container", {
      // 设置地图容器id
      viewMode: "3D", // 是否为3D地图模式
      zoom: 13, // 初始化地图级别
      center: [116.471354, 39.994257],
    });
   // 创建两个多边形
    const polygonPath1 = [
      [-73.982, 40.76],
      [-73.979, 40.749],
      [-73.979, 40.748],
      [-73.981, 40.748],
      [-73.982, 40.76],
    ];

    var polygon1 = new AMap.Polygon({
      path: polygonPath1,
      fillColor: "#FFC0CB",
      strokeColor: "#000000",
      strokeWeight: 2,
    });
    map.add(polygon1); // 添加至地图
 
    const polygonPath2 = [
      [-73.983, 40.751],
      [-73.981, 40.751],
      [-73.981, 40.749],
      [-73.983, 40.749],
      [-73.983, 40.751],
    ];
    var polygon2 = new AMap.Polygon({
      path: polygonPath2,
      fillColor: "#FFC0CB",
      strokeColor: "#000000",
      strokeWeight: 2,
    });
    map.add(polygon2); // 添加至地图

   
 

四、计算交集

bash 复制代码
  // 将图形转化格式
   var turfPolygon1 = turf.polygon([polygonPath1.map((p) => [p[0], p[1]])]);
   var turfPolygon2 = turf.polygon([polygonPath2.map((p) => [p[0], p[1]])]);
   //可以判断是否存在交集
   console.log(turf.booleanOverlap(turfPolygon1, turfPolygon2), 89); ///相交了
   // 计算两个多边形的交集
   var intersection = turf.intersect(
      turf.featureCollection([turfPolygon1, turfPolygon2])
    );
    console.log(intersection, 88); //交集数据

  //    `Tips:` 计算第二个多边形截取第一个多边形后 第一个多边形的数据
    var difference = turf.difference(
      turf.featureCollection([turfPolygon1, turfPolygon2])
    );
    console.log(difference, 88); //差异(原多边形剩余部分)
    var polygon3 = new AMap.Polygon({
      path: [difference.geometry.coordinates],
      fillColor: "red",
      strokeColor: "#000000",
      strokeWeight: 2,
    });
    map.add(polygon3); // 添加至地图

六、绘制图像

bash 复制代码
 var polygon3 = new AMap.Polygon({
      path: [intersection.geometry.coordinates],
      fillColor: "red",
      strokeColor: "#000000",
      strokeWeight: 2,
    });
    map.add(polygon3); // 添加至地图
    
   

七、效果

剩余部分效果:

在这里插入图片描述

相关推荐
Dxy12393102169 小时前
JS DES 加密详解
开发语言·javascript·arcgis
非科班Java出身GISer3 天前
ArcGIS JS 基础教程(46):编辑要素属性 FeatureForm
arcgis·arcgis js编辑要素·arcgis js 要素·arcgis js要素属性·arcgis js 编辑属性·arcgis js编辑要素属性
梅如你4 天前
ISPRS | 1990-2020年间中国人工林与天然林的空间分布数据
arcgis·gis·遥感
limit00757 天前
公开 WMTS / XYZ 瓦片接入 Cesium:6 类常见问题与排查顺序
chrome·低代码·arcgis·web3·旅游
limit00758 天前
三维模型悬空与切片偏移的排查顺序
低代码·3d·arcgis·web3·安卓·webgl
最强小杰8 天前
Continue + minimax-m3 接入教程:协议、模型 ID、404 与工具调用排错
arcgis·ai
2501_9343044012 天前
新增2026年! 2012-2026年全国逐月夜间灯光数据
arcgis·gis数据
梅如你13 天前
中国2026年路网数据
arcgis·gis·遥感
梦想的初衷~13 天前
GPT × ArcGIS 强强联合:AI助力数据处理、空间分析、可视化及多案例综合应用实战教程
人工智能·gpt·arcgis
时间的情敌14 天前
《WebGIS EPSG:4326 与 EPSG:3857 墨卡托投影转换》
arcgis·mars2d