在 esmodule 環(huán)境下使用 jsts
1 說(shuō)明
jsts 完全就是根據(jù)其老媽 jts 的 java 包結(jié)構(gòu)移植的,除了部分分析功能需要額外注意外,基本上所有的子模塊的根路徑位于 jsts/org/locationtech/jts 模塊下。
Geometry 類
Geometry 類符合 OGC 簡(jiǎn)單要素規(guī)范的設(shè)計(jì)。它有若干個(gè)子類,例如點(diǎn)線面等。
Geometry 在 JTS 上有非常多實(shí)用的空間分析函數(shù):
buffer, buffer, buffer, compareTo, compareTo, contains, convexHull, copy, coveredBy, covers, crosses, difference, disjoint, distance, equals, equals, equalsExact, equalsNorm, equalsTopo, geometryChanged, getArea, getCentroid, getEnvelope, getEnvelopeInternal, getFactory, getGeometryN, getInteriorPoint, getLength, getNumGeometries, getPrecisionModel, getSRID, getUserData, hashCode, intersection, intersects, isRectangle, isValid, isWithinDistance, norm, overlaps, relate, relate, setSRID, setUserData, symDifference, toString, toText, touches, union, union, within
但是不見(jiàn)得所有的都在 JSTS 中有,見(jiàn)第 3 節(jié)的 buffer 舉例。
turf 的分析函數(shù)在特定條件下是計(jì)算失敗的(已驗(yàn)證),所以我才不得不求助于 JSTS
2 安裝
pnpm add jsts && pnpm add @types/jsts --save-dev
# or
npm install jsts && npm install @types/jsts --save-dev
# or
yarn add jsts && yarn add @types/jsts --save-dev
@types/jsts 是類型提示庫(kù)
3 使用
以 buffer 為例
import JSTSWKTReader from 'jsts/org/locationtech/jts/io/WKTReader'
import JSTSGeoJSONWriter from 'jsts/org/locationtech/jts/io/GeoJSONWriter'
import JSTSBufferOp from 'jsts/org/locationtech/jts/operation/buffer/BufferOp'
const wkt = `POINT (0 0)`
const bufferCenter = new JSTSWKTReader().read(wkt)
const bufferResult = JSTSBufferOp.bufferOp(
bufferCenter,
this.bufferRadius
) // instanceof Geometry
const bufferResultGeoJSON = new JSTSGeoJSONWriter().write(bufferResult)

浙公網(wǎng)安備 33010602011771號(hào)