Skip to main content
Version: 7.2.0

立即开始

¥Getting started

如何将 Turf 添加到你的项目中取决于你的环境和工具,但以下是一些入门指南。

¥How you add Turf to your project will dependend on your environment and tooling but here are some guidelines to get you started.

Turf 已发布版本的可信来源是 NPM。欢迎你使用重新发布这些软件包的其他提供商。

¥The source of truth for published versions of Turf is NPM. You are welcome to use other providers that republish these packages.

安装

¥Installation

在 Node.js 中

¥In Node.js

# get all of turf
npm install @turf/turf

# or get individual packages
npm install @turf/helpers
npm install @turf/buffer

作为 v7 版本包含 CommonJS 和 ESM 软件包。

¥As of v7, both CommonJS and ESM bundles are included.

在浏览器中

¥In browser

无论是本地下载,还是直接包含第三方版本的 Turf,都有多个 CDN 可供选择,每个 CDN 都有一个 URL 方案,允许你灵活地指定所需的版本。根据你的需求构建合适的 URL:

¥Whether downloading locally, or including a 3rd party version of turf directly, there are multiple CDN's to choose from and each has a URL scheme that allows you to specify what version you want, with some flexibility. Structure your URL as appropriate for your needs:

例如,下载 最新压缩版本 7,并将其包含在脚本标签中。这将公开一个名为 turf 的全局变量。

¥For example, download the latest minified version 7, and include it in a script tag. This will expose a global variable named turf.

<script src="turf.min.js" charset="utf-8"></script>

你也可以直接从 CDN 中获取。此示例指定了 v7 中的最新版本。

¥You can also include it directly from a CDN. This example specifies the latest version within v7.

<script src="https://cdn.jsdelivr.net/npm/@turf/turf@7/turf.min.js"></script>

不建议使用提供 Turf latest 最新版的 CDN URL,尤其是在生产应用中。由于 Turf 函数始终向浏览器用户提供最新版本,因此在主要版本之间,Turf 函数存在重大变更,这可能会导致你的应用处于崩溃状态。

¥It is not recommended to use a CDN URL that gives you the latest bleeding edge version of Turf, especially in a production app. There are breaking changes to turf functions between major versions that can leave your app in a broken state because it always gives your browser users the latest version.

TypeScript

每个 Turf 模块都包含并导出 TypeScript 定义,但 GeoJSON 类型定义(例如 PolygonFeatureCollection)除外,这些定义由 @types/geojson 软件包提供。Turf 不会重新导出这些类型定义。如果你需要它们,你可以直接导入并使用它们,例如 import { Polygon, FeatureCollection } from 'geojson'。你可能需要先安装 @types/geojson 软件包。

¥TypeScript definitions are included and exported by each Turf module, except for GeoJSON type definitions (e.g. Polygon, FeatureCollection) which are provided by the @types/geojson package. Turf does not re-export these type definitionas. If you need them, you can import and use them directly, e.g. import { Polygon, FeatureCollection } from 'geojson'. You may need to install the @types/geojson package first.

其他语言

¥Other languages

Turf.js 的端口可用于:

¥Ports of Turf.js are available in:

  • Java 语言(Android, Java SE)

    ¥Java (Android, Java SE)

  • Swift(iOS、macOS、tvOS、watchOS、Linux)

    • Turf for Swift 处于实验阶段,其公共 API 可能会有所变更。请谨慎使用。

      ¥Turf for Swift is experimental and its public API is subject to change. Please use with care.

  • Dart/Flutter 算法(Dart Web、Dart Native;Flutter for iOS、Android、macOS、Windows、Linux、Web)

    ¥Dart/Flutter (Dart Web, Dart Native; Flutter for iOS, Android, macOS, Windows, Linux, Web)

    • Turf for Dart 移植仍在进行中,实现状态可在 README 中找到。

      ¥The Turf for Dart port is still in progress, the implementation status can be found in the README.


Turf 中的数据

¥Data in Turf

Turf 对所有地理数据都使用 GeoJSON。Turf 要求数据为标准 WGS84 经度、纬度坐标。查看 geojson.io 以获取轻松创建此数据的工具。

¥Turf uses GeoJSON for all geographic data. Turf expects the data to be standard WGS84 longitude, latitude coordinates. Check out geojson.io for a tool to easily create this data.

注意:Turf 要求数据按照 GeoJSON 标准按(经度,纬度)顺序排列。

¥NOTE: Turf expects data in (longitude, latitude) order per the GeoJSON standard.

大多数 Turf 函数可与 GeoJSON 要素配合使用。这些数据片段代表一组属性(例如:人口、海拔、邮政编码等)以及几何图形。GeoJSON 有几种几何类型,例如:

¥Most Turf functions work with GeoJSON features. These are pieces of data that represent a collection of properties (ie: population, elevation, zipcode, etc.) along with a geometry. GeoJSON has several geometry types such as:

  • ¥Point

  • LineString

  • 多边形

    ¥Polygon

Turf 提供了一些自身的几何函数。这些文件只不过是一些输出普通 GeoJSON 格式的简单(可选)封装器。例如,以下两种创建点的方法功能相同:

¥Turf provides a few geometry functions of its own. These are nothing more than simple (and optional) wrappers that output plain old GeoJSON. For example, these two methods of creating a point are functionally equivalent:

// Note order: longitude, latitude.
var point1 = turf.point([-73.988214, 40.749128]);

var point2 = {
type: "Feature",
geometry: {
type: "Point",
// Note order: longitude, latitude.
coordinates: [-73.988214, 40.749128],
},
properties: {},
};

浏览器支持

¥Browser support

Turf 软件包编译为 ES2017 目标语言。但是,@turf/turf 的浏览器版本已编译为支持 IE11。如果你正在使用这些软件包并且需要以 IE11 为目标平台,请在构建过程中编译以下软件包:

¥Turf packages are compiled to target ES2017. However, the browser version of @turf/turf is transpiled to also include support for IE11. If you are using these packages and need to target IE11, please transpile the following packages as part of your build:

@turf/*
robust-predicates
rbush
tinyqueue