立即开始
🌐 Getting started
如何将 Turf 添加到你的项 目中取决于你的环境和工具,但以下是一些入门指南。
🌐 How you add Turf to your project will depend 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,并将其包含在 script 标签中。这将暴露一个名为 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>
不推荐在生产应用中使用提供 latest 最新版本 Turf 的 CDN URL。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 类型定义(例如 Polygon、FeatureCollection)由 @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(安卓,Java SE)
- Swift(iOS、macOS、tvOS、watchOS、Linux)
-
Turf for Swift 是实验性的,其公共 API 可能会发生变化。请小心使用。
-
- Dart/Flutter(Dart 网页版,Dart 原生版;Flutter 适用于 iOS、Android、macOS、Windows、Linux、网页)
-
Dart 版本的 Turf 仍在进行中,具体实现情况可以在 README 中查看。
-
Turf 中的数据
🌐 Data in Turf
Turf 使用 GeoJSON 来处理所有地理数据。Turf 期望数据为标准的 WGS84 经纬度坐标。查看 geojson.io ,这是一个可以轻松创建这些数据的工具。
注意: Turf 根据 GeoJSON 标准预期数据顺序为(经度,纬度)。
大多数 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:
- 点
- LineString
- 多边形
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