本文轉自 Turf.js—讓你在瀏覽器上實現地理分析 請支持原創!
我們在地圖相關系統中必不可少的就是空間分析操作相關需求,例如緩沖區、計算等高線等。憑借簡單的js我們無法將點線面進行結合分析,而Turf.js
的出現幫我們解決了這一難題,讓我們在瀏覽器中也可以輕松的使用以前只屬于桌面GIS的分析功能。
Turf.js是MapBox公司研發的基于瀏覽器端的空間分析庫,它使用JavaScript進行編寫,通過npm進行包管理。值得一提的是,良好的模塊化設計使其不僅能夠作用于瀏覽器端、還可通過Node.js在服務端使用。Turf 原生支持 GeoJSON 矢量數據。GeoJSON 的優點是結構簡單,并且得到了所有網頁地圖API的支持;但 GeoJSON 不支持空間索引,這個缺點可能會限制 Turf 處理大型文件的能力效率。其適用于輕量級(數據輕量而非功能輕量)的WebGIS應用。
瀏覽器端支持空間分析的意義在于,通過網頁地圖的不僅可提供地名搜索與路徑查詢(目前 Google Maps 的功能其實與十年前并沒有太大區別),而且可以在瀏覽器中分享空間分析模型。以前的 WebGIS 功能當然也支持空間分析,但是分析過程需要在服務器端進行,本地能夠進行的設置有限,現在使用 Turf.js 可以將分析過程完全移到本地,如果頁面中提供了參數設置的話,可以在本地對模型進行修改并立即看到分析結果。這樣的直接好處有兩個方面:更渲的數據展示,以及更加復雜的用戶交互(復雜交互本身需要空間分析作為基礎)。
引入全部功能
// 下載$ npm install @turf/turf
// 引入import * as turf from '@turf/turf'import { lineString, along } from '@turf/turf'
如果想引用指定模塊,可以下載功能名稱對應的npm包(功能名稱對應其包的名稱)
$ npm install @turf/collect
import collect from '@turf/collect';
Turf 有著質量極高的官方文檔,詳細介紹了每個功能模塊的使用,并有在線示例可以直接上手試用。
Turf的功能分為幾大類,我們列舉幾個常用類并抽出一兩個常用方法做展示。
獲取一個或多個
feature
,并返回其面積平方米。
參數
參數 | 類型 | 描述 |
---|---|---|
geojson | GeoJSON | input GeoJSON feature(s) |
返回
number - area in square meters
示例
var polygon = turf.polygon([[ [108.09876, 37.200787], [106.398901, 33.648651], [114.972103, 33.340483], [113.715685, 37.845557], [108.09876, 37.200787] ]]);var area = turf.area(polygon);
npm install @turf/area
取任何
Feature
或FeatureCollection
,并利用這個公式返回其質心:多邊形質心。
參數
參數 | 類型 | 描述 |
---|---|---|
geojson | GeoJSON | GeoJSON to be centered |
properties | Object | an Object that is used as the Feature 's properties |
返回
Feature - the center of mass
示例
var polygon = turf.polygon([[[-81, 41], [-88, 36], [-84, 31], [-80, 33], [-77, 39], [-81, 41]]]);var center = turf.centerOfMass(polygon);
npm install @turf/center-of-mass
為給定半徑的
Feature
計算一個緩沖區。支持的單位是英里、公里和度數。
參數
參數 | 類型 | 描述 |
---|---|---|
geojson | (FeatureCollection|Geometry|Feature ) | input to be buffered |
radius | number | distance to draw the buffer (negative values are allowed) |
options | Object | Optional parameters: see below |
options選項
屬性 | 類型 | 默認值 | 描述 |
---|---|---|---|
units | string | kilometers | any of the options supported by turf units |
steps | number | 64 | number of steps |
返回
(FeatureCollection|Feature <(Polygon|MultiPolygon)>|undefined) - buffered features
示例
var point = turf.point([-90.548630, 14.616599]);var buffered = turf.buffer(point, 500, {units: 'miles'});
npm install @turf/buffer
在給定的方向角上沿沿恒向線移動指定距離的任何geojson
Feature
或幾何圖形。
參數
參數 | 類型 | 描述 |
---|---|---|
geojson | GeoJSON | object to be translated |
distance | number | length of the motion; negative values determine motion in opposite direction |
direction | number | of the motion; angle from North in decimal degrees, positive clockwise |
options | Object | Optional parameters: see below |
options選項
屬性 | 類型 | 默認值 | 描述 |
---|---|---|---|
units | string | kilometers | in which |
zTranslation | number | 0 | length of the vertical motion, same unit of distance |
mutate | boolean | false | allows GeoJSON input to be mutated (significant performance increase if true) |
返回
GeoJSON - the translated GeoJSON object
示例
var poly = turf.polygon([[[0,29],[3.5,29],[2.5,32],[0,29]]]);var translatedPoly = turf.transformTranslate(poly, 100, 35);
npm install @turf/transform-translate
獲取任何
LineString
或Polygon
GeoJSON,并返回相交點。
參數
參數 | 類型 | 描述 |
---|---|---|
line1 | (Geometry|FeatureCollection|Feature <(LineString|MultiLineString|Polygon|MultiPolygon)>) | any LineString or Polygon |
line2 | (Geometry|FeatureCollection|Feature <(LineString|MultiLineString|Polygon|MultiPolygon)>) | any LineString or Polygon |
返回
FeatureCollection - point(s) that intersect both
示例
var line1 = turf.lineString([[126, -11], [129, -21]]);var line2 = turf.lineString([[123, -18], [131, -14]]);var intersects = turf.lineIntersect(line1, line2);
npm install @turf/line-intersect
獲取任意類型的多邊形和一個可選的遮罩,并返回一個帶孔的多邊形外部環。
參數
參數 | 類型 | 描述 |
---|---|---|
polygon | (FeatureCollection|Feature <(Polygon|MultiPolygon)>) | GeoJSON Polygon used as interior rings or holes. |
mask | (Feature ) | GeoJSON Polygon used as the exterior ring (if undefined, the world extent is used) |
返回
Feature - Masked Polygon (exterior ring with holes).
示例
var polygon = turf.polygon([[[112, -21], [116, -36], [146, -39], [153, -24], [133, -10], [112, -21]]]);var mask = turf.polygon([[[90, -55], [170, -55], [170, 10], [90, 10], [90, -55]]]);var masked = turf.mask(polygon, mask);
npm install @turf/mask
找到落在(多個)多邊形內的點。
參數
參數 | 類型 | 描述 |
---|---|---|
points | (Feauture|FeatureCollection ) | Points as input search |
polygons | (FeatureCollection|Geometry|Feature <(Polygon|MultiPolygon)>) | Points must be within these (Multi)Polygon(s) |
返回
FeatureCollection - points that land within at least one polygon
示例
var points = turf.points([ [-46.6318, -23.5523], [-46.6246, -23.5325], [-46.6062, -23.5513], [-46.663, -23.554], [-46.643, -23.557]]);var searchWithin = turf.polygon([[ [-46.653,-23.543], [-46.634,-23.5346], [-46.613,-23.543], [-46.614,-23.559], [-46.631,-23.567], [-46.653,-23.560], [-46.653,-23.543]]]);var ptsWithin = turf.pointsWithinPolygon(points, searchWithin);
npm install @turf/points-within-polygon
取一個點和一個多邊形或多多邊形,并確定該點是否位于該多邊形內。多邊形可以是凸的,也可以是凹的。
> npm install @turf/boolean-point-in-polygon
參數
參數 | 類型 | 描述 |
---|---|---|
point | Coord | input point |
polygon | Feature <(Polygon|MultiPolygon)> | input polygon or multipolygon |
options | Object | Optional parameters: see below |
options選項
屬性 | 類型 | 默認值 | 描述 |
---|---|---|---|
ignoreBoundary | boolean | false | True if polygon boundary should be ignored when determining if the point is inside the polygon otherwise false. |
返回
boolean - true if the Point is inside the Polygon; false if the Point is not inside the Polygon
示例
var pt = turf.point([-77, 44]);var poly = turf.polygon([[ [-81, 41], [-81, 47], [-72, 47], [-72, 41], [-81, 41]]]);turf.booleanPointInPolygon(pt, poly);//= true
本文由 貴州做網站公司 整理發布,部分圖文來源于互聯網,如有侵權,請聯系我們刪除,謝謝!
網絡推廣與網站優化公司(網絡優化與推廣專家)作為數字營銷領域的核心服務提供方,其價值在于通過技術手段與策略規劃幫助企業提升線上曝光度、用戶轉化率及品牌影響力。這...
在當今數字化時代,公司網站已成為企業展示形象、傳遞信息和開展業務的重要平臺。然而,對于許多公司來說,網站建設的價格是一個關鍵考量因素。本文將圍繞“公司網站建設價...
在當今的數字化時代,企業網站已成為企業展示形象、吸引客戶和開展業務的重要平臺。然而,對于許多中小企業來說,高昂的網站建設費用可能會成為其發展的瓶頸。幸運的是,隨...
成都哪家工作室拍藝術照拍的更好呢?1.木沙英花婚紗攝影工作室地址:嬌子北二路61號馮丹國際地鐵1號線金融城A出口顧客評價:能在沐沙英花拍婚紗照真的超級開心。剛到店的時候,感覺超級驚喜。給我的感覺是,只要是女生,我都會喜歡。2.八點半去照相館地址:較場壩街東方廣場B座停車場入口2單元1207室客戶評價:體驗很棒。提前兩天預約。到達當天,店家會提前打電話確認時間。到了就不用等了,馬上就可以開始化妝。3...
elle的包包是幾線品牌?Elle男包屬于中高檔。Elle品牌來自法國時尚雜志《ELLE PARIS》。因為雜志的流行,延伸到男女裝、童裝、皮鞋、手表、裝飾展品的誕生和發展?來自法國的Elle時尚品牌,通過對流行趨勢的精準分析和傳播,精選并專注于時尚產品。經過半個世界的沉淀,ELLE已經成為著名的時尚品牌。elle男包是什么檔次?高檔Elle男包屬于高端階層。雖然不是奢侈品,但在時尚界很有影響力...
泰國龍普托大師330年肉身不壞是真的嗎? ;師父龍佩是大成王朝時期的傳奇圣僧。因為他是傳說中的圣僧,沒有正統的歷史記載,只有寺廟本身的記載,沒見過肉身,也沒聽說過肉身不壞。需要注意的是,有很多不法商人把蠟像當肉吃。滿意我的回復,請接受回答,謝謝。泰國龍普托大師330年肉身不壞是真的嗎? ;師父龍佩是大成王朝時期的傳奇圣僧。因為他是傳說中的圣僧,沒有正統的歷史記載,只有寺廟本身的記載,沒見過肉身...