Skip to content

yymap-turf

yymap-turf 是一个空间数据计算工具库,内置坐标转换,空间数据计算等方法 base on turf

DANGER

yymap-turf仅仅提供简单的空间分析功能,如果想要更高要求或者数据量非常大,请使用专业的空间分析库 JTS , PostGIS

CDN

html
<script src="https://unpkg.com/yymap-turf/dist/yymap-turf.min.js"></script>

警告

unpkg 比较慢,如果你嫌慢的话,可以把这个资源下载下来

YY.Turf

YY.Turf 为导出的命名空间

creation

  • constructor()

example

js
const polygon = new YY.Polygon([
  [120.6114, 31.27683],
  [120.6666, 31.23761],
  [120.57162, 31.21344],
]);
console.log(YY.Turf.area(polygon)); //面积计算
vectorLayer.addOverlay(polygon);

const polygon = new YY.Polygon([
  [120.6114, 31.27683],
  [120.6666, 31.23761],
  [120.57162, 31.21344],
]);
vectorLayer.addOverlay(polygon);
const bboxPolygon = YY.Turf.bbox(polygon); //边界范围计算
vectorLayer.addOverlay(bboxPolygon);

const point1 = new YY.Point([117.35137542724613, 36.700332036478216]);
const point2 = new YY.Point([119.87823089599613, 38.35815013843134]);
vectorLayer.addOverlays([point1, point2]);
console.log(YY.Turf.bearing(point1, point2)); //两点之间角度计算

methods

TIP

static 表示静态方法, 即对象直接访问, 无需new

static area(polygon)

static bbox(overlay)

static bearing(point1, point2)

static center(overlay)

static centerOfMass(overlay)

static destination(point, radius, bearing)

  • 获取一个点特定距离特定角度对应的点 demo
  • 参数
    • {YY. Point} point
    • {Number} radius 距离
    • {Number} bearing 角度
  • 返回值: YY. Point

static distance(point1, point2)

static length(polyline)

  • 长度计算,一般用于计算线的长度
  • 参数
  • 返回值: Number

static pointOnFeature(overlay)

static polygonTangents(point, polygon)

static pointToLineDistance(point, polyline)

static bboxClip(overlay, bbox)

js
var polygon = new YY.Polygon([
    [120.6114, 31.27683],
    [120.6666, 31.23761],
    [120.57162, 31.21344],
]);
vectorLayer.addOverlay(polygon);
let bbox = [
    120.62635004014328, 31.221444396450096, 120.66102563828781, 31.266647891838005,
];
let clipPolygon = YY.Turf.bboxClip(polygon, bbox);
vectorLayer.addOverlay(
    clipPolygon.setStyle(YY.Style.formatFill({
        polygonFill: "green"
    }))
);

static bezierSpline(polyline)

static buffer(overlay, radius)

static circle(point, radius, steps)

  • 构造一个圆
  • 参数
    • {YY. Point} point :
    • {Number} radius : 缓冲的距离
    • {Number} steps : 步数,切割的值越大,圆的边界越圆
  • 返回值: YY. Polygon

static difference(polygon1, polygon2)

static intersect((polygon1, polygon2)

static tesselate(polygon)

static transformRotate(overlay, bearing)

static transformTranslate(overlay, radius, bearing)

  • 覆盖物按特定的角度平移一定的距离
  • 参数
    • {YY. Overlay} overlay :
    • {Number} radius :
    • {Number} bearing :
  • 返回值: YY. Overlay

static transformScale(overlay, scale)

static union(polygon1, polygon2)

static voronoi(points, bbox)

  • 泰森多边形
  • 参数
    • {Array[YY. Point]}:points
    • {Array} bbox : [120, 31, 122, 32]
  • 返回值: Array[YY. Polygon]

static explode(overlay)

static lineToPolygon(polyline)

static polygonToLine(polygon)

static kinks(overlay)

static lineArc(point, radius, startBearing, endBearing)

  • 求特定点,特定距离,开始角度和结束角度的圆弧
  • 参数
    • {YY. Point} point:
    • {Number} radius
    • {Number} startBearing
    • {Number} endBearing
  • 返回值: YY. Polyline

static lineChunk(polyline, length)

static lineIntersect(polyline1, polyline2)

static lineOverlap(polyline1, polyline2)

static lineSlice(startPoint, endPoint, polyline)

static mask(polygon1, polygon2)

static nearestPointOnLine(polyline, point)

static randomPoint(count, bbox)

  • 给定范围内随机生成点
  • 参数
    • {number} count
    • {Array} bbox : 边界范围 [120, 31, 122, 32];
  • 返回值: Array[YY. Point]

static pointsWithinPolygon(points, polygon)

static hexGrid(bbox, cellSize)

  • 给定范围内进行蜂窝切割
  • 参数
    • {Array} bbox : 边界范围 [120, 31, 122, 32];
    • {number} cellSize: 单个蜂窝的大小,越小生成的蜂窝个数越多
  • 返回值: Array[YY. Polygon]

static pointGrid(bbox, cellSize)

  • 给定范围内进行点的切割
  • 参数
    • {Array} bbox : 边界范围 [120, 31, 122, 32];
    • {number} cellSize: 点点之间的距离,越小生成的点个数越多
  • 返回值: Array[YY. Point]

static squareGrid(bbox, cellSize)

  • 给定范围内进行正方形切割
  • 参数
    • {Array} bbox : 边界范围 [120, 31, 122, 32];
    • {number} cellSize: 正方形大小
  • 返回值: Array[YY. Polygon]

static triangleGrid(bbox, celSize)

  • 给定范围内进行三角形切割
  • 参数
    • {Array} bbox : 边界范围 [120, 31, 122, 32];
    • {number} cellSize: 三角形大小
  • 返回值: Array[YY. Polygon]

static booleanContains(overlay1, overlay2)

static booleanCrosses(overlay1, overlay2)

static booleanDisjoint(overlay1, overlay2)

static booleanEqual(overlay1, overlay2)

static booleanPointInPolygon(point, polygon)

static booleanPointOnLine(point, polyline)

static booleanWithin(overlay1, overlay2)

static sliceByArea(polygon, params)

  • 多边形根据面积占比进行切割
  • 参数
  • 返回值: [YY.Polygon|YY.MultiPolygon]
js
//各个区域的面积占比,总和应该为1
const areas = [0.05, 0.11, 0.1, 0.055, 0.125, 0.05, 0.15, 0.25, 0.06, 0.05];

let params = [];
areas.forEach((element) => {
    params.push({
        percent: element, //必要字段
        id: YY.H.uuid(),
    });
});

let results = YY.Turf.sliceByArea(polygon, params);

static douglas(lnglats, d)

  • 对经纬度进行抽稀
  • 参数
    • {[lnglat]} lnglats,二维数组
    • {Number} d 抽稀的距离阈值
  • 返回值: [lnglat]
js
      let lnglats = [
          ...,
          ...,
          ....,
          ....
      ]
      let maxD = 50;
      //对点进行抽稀,道格拉斯算法
      let douglasLngLats = YY.Turf.douglas(lnglats, maxD);

This document is generated by vitepress and Edit by deyihu