主题
几何类型 Geometry
标准版提供 15 种几何类型,覆盖二维、三维以及几何集合。所有类型继承自抽象基类 Geometry,共享统一的空间运算接口。
Geometry(抽象基类)
所有几何类型的公共基类,定义了空间查询、变换、序列化等核心方法。
ts
abstract class Geometry {
abstract readonly type: string;
}方法
| 方法 | 参数 | 返回值 | 说明 |
|---|---|---|---|
getType() | — | string | 返回几何类型标识 |
getExtent() | — | Extent | null | 返回外接矩形 [west, south, east, north] |
getCentroid() | — | Point | 返回质心点 |
getArea() | — | number | 返回面积(平方米) |
getLength() | — | number | 返回长度/周长(米) |
buffer(distance) | distance: number | Geometry | 返回缓冲区几何 |
contains(other) | other: Geometry | boolean | 是否包含另一几何 |
intersects(other) | other: Geometry | boolean | 是否与另一几何相交 |
within(other) | other: Geometry | boolean | 是否在另一几何内部 |
touches(other) | other: Geometry | boolean | 是否与另一几何接触 |
overlaps(other) | other: Geometry | boolean | 是否与另一几何重叠 |
distance(other) | other: Geometry | number | 返回到另一几何的距离(米) |
simplify(tolerance) | tolerance: number | Geometry | Douglas-Peucker 简化 |
transform(fn) | fn: (coord) => Coordinate | Geometry | 坐标变换 |
clone() | — | Geometry | 深拷贝 |
toGeoJSON() | — | object | 导出 GeoJSON |
toWKT() | — | string | 导出 WKT |
equals(other) | other: Geometry | boolean | 几何是否相等 |
辅助函数
ts
import { Geometry } from '@gmap/standard';
function degToRad(d: number): number; // 角度转弧度
function radToDeg(r: number): number; // 弧度转角度
function haversine(a: Coordinate, b: Coordinate): number; // Haversine 距离
function euclidean(a: Coordinate, b: Coordinate): number; // 欧氏距离
function pointInRing(pt: Coordinate, ring: Coordinate[]): boolean; // 点在环内Point
二维/三维点。
构造函数
ts
class Point extends Geometry {
readonly type = 'Point';
coord: Coordinate;
constructor(coord: Coordinate);
}属性
| 属性 | 类型 | 说明 |
|---|---|---|
x | number | 经度(getter/setter) |
y | number | 纬度(getter/setter) |
z | number | undefined | 高度(getter/setter,可选) |
方法
| 方法 | 参数 | 返回值 | 说明 |
|---|---|---|---|
set(coord) | coord: Coordinate | void | 设置坐标 |
get() | — | Coordinate | 获取坐标 |
getCoords() | — | Coordinate | 同 get() |
add(pt) | pt: Point | Point | 坐标加法 |
subtract(pt) | pt: Point | Point | 坐标减法 |
getExtent() | — | Extent | 返回点的外接矩形 |
getCentroid() | — | Point | 返回自身 |
getArea() | — | 0 | 点面积为 0 |
getLength() | — | 0 | 点长度为 0 |
distance(other) | other: Geometry | number | 到另一几何的距离 |
clone() | — | Point | 深拷贝 |
toGeoJSON() | — | { type: 'Point', coordinates } | GeoJSON 输出 |
示例
ts
import { Point } from '@gmap/standard';
const p = new Point([116.397, 39.908, 50]);
console.log(p.x); // 116.397
console.log(p.y); // 39.908
console.log(p.z); // 50
const p2 = new Point([116.5, 40.0]);
console.log(p.distance(p2)); // ~15432 (米)
const moved = p.add(new Point([0.1, 0.1]));
console.log(moved.get()); // [116.497, 40.008, 50]MultiPoint
多点集合。
构造函数
ts
class MultiPoint extends Geometry {
readonly type = 'MultiPoint';
points: Point[];
constructor(points: (Coordinate | Point)[]);
}方法
| 方法 | 参数 | 返回值 | 说明 |
|---|---|---|---|
add(point) | point: Point | this | 添加点 |
remove(index) | index: number | this | 移除指定索引的点 |
getAt(index) | index: number | Point | 获取指定索引的点 |
count | — | number | 点的数量(getter) |
toArray() | — | Point[] | 返回点数组 |
getCentroid() | — | Point | 所有点的质心 |
clone() | — | MultiPoint | 深拷贝 |
toGeoJSON() | — | { type: 'MultiPoint', coordinates } | GeoJSON 输出 |
simplify(tolerance) | tolerance: number | MultiPoint | 基于距离的简化 |
示例
ts
import { MultiPoint } from '@gmap/standard';
const mp = new MultiPoint([[0, 0], [1, 1], [2, 2]]);
console.log(mp.count); // 3
mp.add(new Point([3, 3]));
console.log(mp.count); // 4LineString
线串,由一系列坐标点按顺序连接而成。
构造函数
ts
class LineString extends Geometry {
readonly type = 'LineString';
private coords: Coordinate[];
constructor(coords: Coordinate[]);
}方法
| 方法 | 参数 | 返回值 | 说明 |
|---|---|---|---|
addCoord(coord) | coord: Coordinate | this | 在末尾添加坐标 |
insertCoord(index, coord) | index: number, coord: Coordinate | this | 在指定位置插入坐标 |
removeCoord(index) | index: number | this | 移除指定索引坐标 |
setCoord(index, coord) | index: number, coord: Coordinate | this | 替换指定坐标 |
getCoords() | — | Coordinate[] | 获取所有坐标 |
getAt(index) | index: number | Coordinate | 获取指定索引坐标 |
getPointAtDistance(d) | d: number | Point | 沿线路指定距离处的点 |
reverse() | — | this | 反转线段顺序 |
densify(interval) | interval: number | LineString | 按间隔加密线段 |
getExtent() | — | Extent | 外接矩形 |
getCentroid() | — | Point | 质心 |
getArea() | — | 0 | 线面积为 0 |
getLength() | — | number | 线总长度(米) |
distance(other) | other: Geometry | number | 到另一几何的最短距离 |
clone() | — | LineString | 深拷贝 |
toGeoJSON() | — | { type: 'LineString', coordinates } | GeoJSON 输出 |
示例
ts
import { LineString } from '@gmap/standard';
const line = new LineString([
[116.3, 39.9],
[116.4, 39.95],
[116.5, 40.0],
]);
console.log(line.getLength()); // ~16800 (米)
console.log(line.getAt(1)); // [116.4, 39.95]
const dense = line.densify(500); // 每 500 米加密一个点
line.reverse();
console.log(line.getCoords()); // [[116.5, 40.0], [116.4, 39.95], [116.3, 39.9]]MultiLineString
多线集合。
构造函数
ts
class MultiLineString extends Geometry {
readonly type = 'MultiLineString';
private lines: LineString[];
constructor(lines: (Coordinate[] | LineString)[]);
}方法
| 方法 | 参数 | 返回值 | 说明 |
|---|---|---|---|
getAt(index) | index: number | LineString | 获取指定索引的线 |
count | — | number | 线的数量(getter) |
toArray() | — | LineString[] | 返回线数组 |
getCentroid() | — | Point | 所有线的质心 |
getArea() | — | 0 | 多线面积为 0 |
getLength() | — | number | 所有线的总长度 |
clone() | — | MultiLineString | 深拷贝 |
toGeoJSON() | — | { type: 'MultiLineString', coordinates } | GeoJSON 输出 |
示例
ts
import { MultiLineString } from '@gmap/standard';
const ml = new MultiLineString([
[[0, 0], [1, 1]],
[[2, 2], [3, 3]],
]);
console.log(ml.count); // 2
console.log(ml.getLength());Polygon
多边形,支持外环和孔洞。
构造函数
ts
class Polygon extends Geometry {
readonly type = 'Polygon';
private rings: Coordinate[][];
constructor(rings: Coordinate[][]);
static fromExtent(extent: Extent): Polygon;
}方法
| 方法 | 参数 | 返回值 | 说明 |
|---|---|---|---|
count | — | number | 环的数量(getter) |
getOuterRing() | — | LineString | 外环 |
getHoles() | — | LineString[] | 孔洞数组 |
addHole(ring) | ring: LineString | Coordinate[] | this | 添加孔洞 |
removeHole(index) | index: number | this | 移除孔洞 |
isClockwise() | — | boolean | 外环是否顺时针 |
contains(other) | other: Geometry | boolean | 是否包含另一几何(点在面内判断) |
getExtent() | — | Extent | 外接矩形 |
getCentroid() | — | Point | 质心 |
getArea() | — | number | 面积(扣除孔洞) |
getLength() | — | number | 外环周长 |
clone() | — | Polygon | 深拷贝 |
toGeoJSON() | — | { type: 'Polygon', coordinates } | GeoJSON 输出 |
simplify(tolerance) | tolerance: number | Polygon | 简化 |
静态方法
| 方法 | 参数 | 返回值 | 说明 |
|---|---|---|---|
Polygon.fromExtent(extent) | extent: Extent | Polygon | 从外接矩形创建多边形 |
示例
ts
import { Polygon, Point } from '@gmap/standard';
// 简单多边形
const poly = new Polygon([[
[116.0, 39.0],
[117.0, 39.0],
[117.0, 40.0],
[116.0, 40.0],
[116.0, 39.0],
]]);
console.log(poly.getArea()); // ~12300000000 (平方米)
console.log(poly.isClockwise()); // false
// 带孔洞的多边形
const outer = [[0, 0], [10, 0], [10, 10], [0, 10], [0, 0]];
const hole = [[2, 2], [8, 2], [8, 8], [2, 8], [2, 2]];
const polyWithHole = new Polygon([outer, hole]);
console.log(polyWithHole.getHoles().length); // 1
console.log(polyWithHole.contains(new Point([5, 5]))); // false (在孔洞内)
// 从范围创建
const box = Polygon.fromExtent([116, 39, 117, 40]);MultiPolygon
多面集合。
构造函数
ts
class MultiPolygon extends Geometry {
readonly type = 'MultiPolygon';
private polygons: Polygon[];
constructor(polygons: (Coordinate[][][] | Polygon)[]);
}方法
| 方法 | 参数 | 返回值 | 说明 |
|---|---|---|---|
count | — | number | 多边形数量(getter) |
getAt(index) | index: number | Polygon | 获取指定索引的多边形 |
toArray() | — | Polygon[] | 返回多边形数组 |
getCentroid() | — | Point | 质心 |
getArea() | — | number | 总面积 |
clone() | — | MultiPolygon | 深拷贝 |
toGeoJSON() | — | { type: 'MultiPolygon', coordinates } | GeoJSON 输出 |
示例
ts
import { MultiPolygon } from '@gmap/standard';
const mp = new MultiPolygon([
[[[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]],
[[[2, 2], [3, 2], [3, 3], [2, 3], [2, 2]]],
]);
console.log(mp.count); // 2
console.log(mp.getArea());Circle
圆,由中心点和半径定义。
构造函数
ts
class Circle extends Geometry {
readonly type = 'Circle';
center: Point;
radius: number;
segments: number;
constructor(center: Coordinate | Point, radius: number, options?: { segments?: number });
}属性
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
center | Point | — | 圆心 |
radius | number | — | 半径(米) |
segments | number | 64 | 近似多边形的分段数 |
方法
| 方法 | 参数 | 返回值 | 说明 |
|---|---|---|---|
toPolygon() | — | Polygon | 转为近似多边形 |
getExtent() | — | Extent | 外接矩形 |
getCentroid() | — | Point | 圆心 |
getArea() | — | number | 面积(平方米) |
getLength() | — | number | 周长(米) |
contains(other) | other: Geometry | boolean | 是否包含另一几何 |
clone() | — | Circle | 深拷贝 |
toGeoJSON() | — | { type: 'Circle', center, radius } | GeoJSON 输出 |
示例
ts
import { Circle } from '@gmap/standard';
const circle = new Circle([116.397, 39.908], 5000);
console.log(circle.getArea()); // ~78539816 (平方米)
console.log(circle.getLength()); // ~31415.9 (米)
const polygon = circle.toPolygon(); // 转为 64 边近似多边形BoundingBox
轴对齐包围盒(AABB)。
构造函数
ts
class BoundingBox extends Geometry {
readonly type = 'BoundingBox';
west: number;
south: number;
east: number;
north: number;
constructor(extent: Extent);
}属性
| 属性 | 类型 | 说明 |
|---|---|---|
west | number | 最西经度 |
south | number | 最南纬度 |
east | number | 最东经度 |
north | number | 最北纬度 |
方法
| 方法 | 参数 | 返回值 | 说明 |
|---|---|---|---|
expand(delta) | delta: number | this | 向外扩展(米) |
union(other) | other: BoundingBox | this | 合并另一个包围盒 |
intersection(other) | other: BoundingBox | this | null | 取交集 |
contains(x) | x: Point | Coordinate | boolean | 是否包含点 |
intersects(other) | other: BoundingBox | boolean | 是否相交 |
toPolygon() | — | Polygon | 转为多边形 |
toArray() | — | Extent | 返回 [west, south, east, north] |
getArea() | — | number | 面积(平方米) |
getLength() | — | number | 周长(米) |
clone() | — | BoundingBox | 深拷贝 |
toGeoJSON() | — | { type: 'BoundingBox', extent } | GeoJSON 输出 |
示例
ts
import { BoundingBox, Point } from '@gmap/standard';
const bbox = new BoundingBox([116.0, 39.0, 117.0, 40.0]);
console.log(bbox.contains([116.5, 39.5])); // true
bbox.expand(1000); // 向外扩展 1km
console.log(bbox.toArray());
const poly = bbox.toPolygon();Ellipse
椭圆,由中心点和 X/Y 轴半径定义。
构造函数
ts
class Ellipse extends Geometry {
readonly type = 'Ellipse';
center: Point;
radiusX: number;
radiusY: number;
rotation: number;
constructor(
center: Coordinate,
rx: number,
ry: number,
options?: { rotation?: number; segments?: number }
);
}属性
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
center | Point | — | 中心点 |
radiusX | number | — | X 轴半径(米) |
radiusY | number | — | Y 轴半径(米) |
rotation | number | 0 | 旋转角度(度) |
方法
| 方法 | 参数 | 返回值 | 说明 |
|---|---|---|---|
toPolygon() | — | Polygon | 转为近似多边形 |
getExtent() | — | Extent | 外接矩形 |
getCentroid() | — | Point | 中心点 |
getArea() | — | number | 面积(平方米) |
getLength() | — | number | 周长 |
clone() | — | Ellipse | 深拷贝 |
toGeoJSON() | — | object | GeoJSON 输出 |
示例
ts
import { Ellipse } from '@gmap/standard';
const ellipse = new Ellipse([116.397, 39.908], 5000, 3000, { rotation: 45 });
console.log(ellipse.getArea()); // ~47123889
const poly = ellipse.toPolygon(); // 转为近似多边形Box3D
三维包围盒。
构造函数
ts
class Box3D extends Geometry {
readonly type = 'Box3D';
center: Point;
dimensions: [number, number, number]; // [宽, 深, 高](米)
rotation: [number, number, number]; // [rx, ry, rz](度)
constructor(
center: Coordinate,
dimensions: [number, number, number],
options?: { rotation?: [number, number, number] }
);
}属性
| 属性 | 类型 | 说明 |
|---|---|---|
center | Point | 中心点 |
dimensions | [number, number, number] | [宽, 深, 高](米) |
rotation | [number, number, number] | 旋转角度 [rx, ry, rz](度) |
方法
| 方法 | 参数 | 返回值 | 说明 |
|---|---|---|---|
getExtent() | — | Extent | 2D 外接矩形(投影到地面) |
getCentroid() | — | Point | 中心点 |
getArea() | — | number | 表面积(平方米) |
getLength() | — | number | 三边之和 |
clone() | — | Box3D | 深拷贝 |
toGeoJSON() | — | { type: 'Box3D', center, dimensions, rotation } | GeoJSON 输出 |
示例
ts
import { Box3D } from '@gmap/standard';
const box = new Box3D([116.397, 39.908, 0], [50, 30, 20], {
rotation: [0, 0, 45],
});
console.log(box.getArea()); // 6200 (平方米)
console.log(box.dimensions); // [50, 30, 20]Sphere3D
三维球体。
构造函数
ts
class Sphere3D extends Geometry {
readonly type = 'Sphere3D';
center: Point;
radius: number;
constructor(center: Coordinate, radius: number);
}属性
| 属性 | 类型 | 说明 |
|---|---|---|
center | Point | 球心 |
radius | number | 半径(米) |
方法
| 方法 | 参数 | 返回值 | 说明 |
|---|---|---|---|
getExtent() | — | Extent | 2D 外接矩形 |
getCentroid() | — | Point | 球心 |
getArea() | — | number | 表面积 4 * PI * r^2 |
getLength() | — | number | 周长 2 * PI * r |
clone() | — | Sphere3D | 深拷贝 |
toGeoJSON() | — | { type: 'Sphere3D', center, radius } | GeoJSON 输出 |
示例
ts
import { Sphere3D } from '@gmap/standard';
const sphere = new Sphere3D([116.397, 39.908, 100], 50);
console.log(sphere.getArea()); // ~31415.9
console.log(sphere.getLength()); // ~314.159Cylinder3D
三维圆柱体。
构造函数
ts
class Cylinder3D extends Geometry {
readonly type = 'Cylinder3D';
center: Point;
radius: number;
height: number;
slices: number;
constructor(
center: Coordinate,
radius: number,
height: number,
options?: { slices?: number }
);
}属性
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
center | Point | — | 底面中心 |
radius | number | — | 半径(米) |
height | number | — | 高度(米) |
slices | number | 32 | 圆周分段数 |
方法
| 方法 | 参数 | 返回值 | 说明 |
|---|---|---|---|
getExtent() | — | Extent | 2D 外接矩形 |
getCentroid() | — | Point | 中心点 |
getArea() | — | number | 表面积 2 * PI * r * (r + h) |
getLength() | — | number | 高度 |
clone() | — | Cylinder3D | 深拷贝 |
toGeoJSON() | — | { type: 'Cylinder3D', center, radius, height, slices } | GeoJSON 输出 |
示例
ts
import { Cylinder3D } from '@gmap/standard';
const cylinder = new Cylinder3D([116.397, 39.908, 0], 10, 30);
console.log(cylinder.getArea()); // ~2513.27
console.log(cylinder.getHeight()); // 30Cone3D
三维圆锥体。
构造函数
ts
class Cone3D extends Geometry {
readonly type = 'Cone3D';
center: Point;
radius: number;
height: number;
slices: number;
constructor(
center: Coordinate,
radius: number,
height: number,
options?: { slices?: number }
);
}属性
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
center | Point | — | 底面中心 |
radius | number | — | 底面半径(米) |
height | number | — | 高度(米) |
slices | number | 32 | 圆周分段数 |
方法
| 方法 | 参数 | 返回值 | 说明 |
|---|---|---|---|
getExtent() | — | Extent | 2D 外接矩形 |
getCentroid() | — | Point | 中心点 |
getArea() | — | number | 表面积 |
getLength() | — | number | 高度 |
clone() | — | Cone3D | 深拷贝 |
toGeoJSON() | — | { type: 'Cone3D', center, radius, height, slices } | GeoJSON 输出 |
示例
ts
import { Cone3D } from '@gmap/standard';
const cone = new Cone3D([116.397, 39.908, 0], 15, 40, { slices: 48 });
console.log(cone.getArea());GeometryCollection
几何集合,可包含任意类型的几何体。
构造函数
ts
class GeometryCollection extends Geometry {
readonly type = 'GeometryCollection';
private geometries: Geometry[];
constructor(geometries: Geometry[]);
}方法
| 方法 | 参数 | 返回值 | 说明 |
|---|---|---|---|
add(geom) | geom: Geometry | this | 添加几何 |
count | — | number | 几何数量(getter) |
getAt(index) | index: number | Geometry | 获取指定索引的几何 |
toArray() | — | Geometry[] | 返回几何数组 |
getExtent() | — | Extent | 合并所有几何的外接矩形 |
getCentroid() | — | Point | 所有几何质心的质心 |
getArea() | — | number | 所有几何面积之和 |
getLength() | — | number | 所有几何长度之和 |
contains(other) | other: Geometry | boolean | 任一几何包含即可 |
clone() | — | GeometryCollection | 深拷贝 |
toGeoJSON() | — | { type: 'GeometryCollection', geometries } | GeoJSON 输出 |
示例
ts
import { GeometryCollection, Point, LineString, Polygon } from '@gmap/standard';
const gc = new GeometryCollection([
new Point([0, 0]),
new LineString([[1, 1], [2, 2]]),
new Polygon([[[3, 3], [4, 3], [4, 4], [3, 4], [3, 3]]]),
]);
console.log(gc.count); // 3
console.log(gc.getArea());
gc.add(new Point([5, 5]));
console.log(gc.count); // 4