MEASUREMENT
COORDINATE MUTATION
TRANSFORMATION
FEATURE_CONVERSION
MISC
HELPER
RANDOM
GRIDS
AGGREGATION
META
ASSERTIONS
BOOLEANS
UNIT CONVERSION
DATA
JOINS
CLASSIFICATION

Turf.js开发手册

Turf.js是一个轻量级的JavaScript库,用于地理空间分析和操作。它提供了许多强大的函数和算法,用于处理地理空间数据,如点、线、多边形和网格等。Turf.js的API简单易用,可以轻松地与其他地理空间数据库和WebGIS平台集成,如Mapbox、Leaflet、OpenLayers等。本文将介绍如何使用Turf.js进行地理空间数据处理和分析。

安装

在使用Turf.js之前,需要先将其安装到您的项目中。Turf.js可以通过NPM或Yarn安装,也可以通过CDN引入。以下是NPM和CDN安装Turf.js的示例:

npm install turf

CDN

<script src="https://unpkg.com/turf@latest"></script>

使用

安装完Turf.js后,可以在JavaScript代码中使用它提供的函数和算法。以下是一些使用Turf.js的示例。

  1. 计算两个点之间的距离
const point1 = turf.point([-75.343, 39.984]);
const point2 = turf.point([-75.534, 39.123]);
const distance = turf.distance(point1, point2);
console.log(distance); // 97.15957804623796
  1. 将GeoJSON对象转换为WKT字符串
const geojson = {
  "type": "Polygon",
  "coordinates": [[
    [-122.801742, 45.48565],
    [-122.801742, 45.60491],
    [-122.584762, 45.60491],
    [-122.584762, 45.48565],
    [-122.801742, 45.48565]
  ]]
};
const wkt = turf.wktPolygon(geojson);
console.log(wkt); // POLYGON((-122.801742 45.48565,-122.801742 45.60491,-122.584762 45.60491,-122.584762 45.48565,-122.801742 45.48565))
  1. 计算两个多边形之间的交集
const poly1 = turf.polygon([[
  [-122.801742, 45.48565],
  [-122.801742, 45.60491],
  [-122.584762, 45.60491],
  [-122.584762, 45.48565],
  [-122.801742, 45.48565]
]]);
const poly2 = turf.polygon([[
  [-122.520217, 45.535693],
  [-122.64038, 45.553967],
  [-122.720031, 45.526554],
  [-122.669906, 45.507309],
  [-122.723464, 45.446643],
  [-122.532577, 45.408574],
  [-122.487258, 45.477466],
  [-122.520217, 45.535693]
]]);
const intersection = turf.intersect(poly1, poly2);
console.log(intersection);