Skip to content

几何类型 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: numberGeometry返回缓冲区几何
contains(other)other: Geometryboolean是否包含另一几何
intersects(other)other: Geometryboolean是否与另一几何相交
within(other)other: Geometryboolean是否在另一几何内部
touches(other)other: Geometryboolean是否与另一几何接触
overlaps(other)other: Geometryboolean是否与另一几何重叠
distance(other)other: Geometrynumber返回到另一几何的距离(米)
simplify(tolerance)tolerance: numberGeometryDouglas-Peucker 简化
transform(fn)fn: (coord) => CoordinateGeometry坐标变换
clone()Geometry深拷贝
toGeoJSON()object导出 GeoJSON
toWKT()string导出 WKT
equals(other)other: Geometryboolean几何是否相等

辅助函数

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);
}

属性

属性类型说明
xnumber经度(getter/setter)
ynumber纬度(getter/setter)
znumber | undefined高度(getter/setter,可选)

方法

方法参数返回值说明
set(coord)coord: Coordinatevoid设置坐标
get()Coordinate获取坐标
getCoords()Coordinateget()
add(pt)pt: PointPoint坐标加法
subtract(pt)pt: PointPoint坐标减法
getExtent()Extent返回点的外接矩形
getCentroid()Point返回自身
getArea()0点面积为 0
getLength()0点长度为 0
distance(other)other: Geometrynumber到另一几何的距离
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: Pointthis添加点
remove(index)index: numberthis移除指定索引的点
getAt(index)index: numberPoint获取指定索引的点
countnumber点的数量(getter)
toArray()Point[]返回点数组
getCentroid()Point所有点的质心
clone()MultiPoint深拷贝
toGeoJSON(){ type: 'MultiPoint', coordinates }GeoJSON 输出
simplify(tolerance)tolerance: numberMultiPoint基于距离的简化

示例

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); // 4

LineString

线串,由一系列坐标点按顺序连接而成。

构造函数

ts
class LineString extends Geometry {
  readonly type = 'LineString';
  private coords: Coordinate[];

  constructor(coords: Coordinate[]);
}

方法

方法参数返回值说明
addCoord(coord)coord: Coordinatethis在末尾添加坐标
insertCoord(index, coord)index: number, coord: Coordinatethis在指定位置插入坐标
removeCoord(index)index: numberthis移除指定索引坐标
setCoord(index, coord)index: number, coord: Coordinatethis替换指定坐标
getCoords()Coordinate[]获取所有坐标
getAt(index)index: numberCoordinate获取指定索引坐标
getPointAtDistance(d)d: numberPoint沿线路指定距离处的点
reverse()this反转线段顺序
densify(interval)interval: numberLineString按间隔加密线段
getExtent()Extent外接矩形
getCentroid()Point质心
getArea()0线面积为 0
getLength()number线总长度(米)
distance(other)other: Geometrynumber到另一几何的最短距离
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: numberLineString获取指定索引的线
countnumber线的数量(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;
}

方法

方法参数返回值说明
countnumber环的数量(getter)
getOuterRing()LineString外环
getHoles()LineString[]孔洞数组
addHole(ring)ring: LineString | Coordinate[]this添加孔洞
removeHole(index)index: numberthis移除孔洞
isClockwise()boolean外环是否顺时针
contains(other)other: Geometryboolean是否包含另一几何(点在面内判断)
getExtent()Extent外接矩形
getCentroid()Point质心
getArea()number面积(扣除孔洞)
getLength()number外环周长
clone()Polygon深拷贝
toGeoJSON(){ type: 'Polygon', coordinates }GeoJSON 输出
simplify(tolerance)tolerance: numberPolygon简化

静态方法

方法参数返回值说明
Polygon.fromExtent(extent)extent: ExtentPolygon从外接矩形创建多边形

示例

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)[]);
}

方法

方法参数返回值说明
countnumber多边形数量(getter)
getAt(index)index: numberPolygon获取指定索引的多边形
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 });
}

属性

属性类型默认值说明
centerPoint圆心
radiusnumber半径(米)
segmentsnumber64近似多边形的分段数

方法

方法参数返回值说明
toPolygon()Polygon转为近似多边形
getExtent()Extent外接矩形
getCentroid()Point圆心
getArea()number面积(平方米)
getLength()number周长(米)
contains(other)other: Geometryboolean是否包含另一几何
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);
}

属性

属性类型说明
westnumber最西经度
southnumber最南纬度
eastnumber最东经度
northnumber最北纬度

方法

方法参数返回值说明
expand(delta)delta: numberthis向外扩展(米)
union(other)other: BoundingBoxthis合并另一个包围盒
intersection(other)other: BoundingBoxthis | null取交集
contains(x)x: Point | Coordinateboolean是否包含点
intersects(other)other: BoundingBoxboolean是否相交
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 }
  );
}

属性

属性类型默认值说明
centerPoint中心点
radiusXnumberX 轴半径(米)
radiusYnumberY 轴半径(米)
rotationnumber0旋转角度(度)

方法

方法参数返回值说明
toPolygon()Polygon转为近似多边形
getExtent()Extent外接矩形
getCentroid()Point中心点
getArea()number面积(平方米)
getLength()number周长
clone()Ellipse深拷贝
toGeoJSON()objectGeoJSON 输出

示例

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] }
  );
}

属性

属性类型说明
centerPoint中心点
dimensions[number, number, number][宽, 深, 高](米)
rotation[number, number, number]旋转角度 [rx, ry, rz](度)

方法

方法参数返回值说明
getExtent()Extent2D 外接矩形(投影到地面)
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);
}

属性

属性类型说明
centerPoint球心
radiusnumber半径(米)

方法

方法参数返回值说明
getExtent()Extent2D 外接矩形
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.159

Cylinder3D

三维圆柱体。

构造函数

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 }
  );
}

属性

属性类型默认值说明
centerPoint底面中心
radiusnumber半径(米)
heightnumber高度(米)
slicesnumber32圆周分段数

方法

方法参数返回值说明
getExtent()Extent2D 外接矩形
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()); // 30

Cone3D

三维圆锥体。

构造函数

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 }
  );
}

属性

属性类型默认值说明
centerPoint底面中心
radiusnumber底面半径(米)
heightnumber高度(米)
slicesnumber32圆周分段数

方法

方法参数返回值说明
getExtent()Extent2D 外接矩形
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: Geometrythis添加几何
countnumber几何数量(getter)
getAt(index)index: numberGeometry获取指定索引的几何
toArray()Geometry[]返回几何数组
getExtent()Extent合并所有几何的外接矩形
getCentroid()Point所有几何质心的质心
getArea()number所有几何面积之和
getLength()number所有几何长度之和
contains(other)other: Geometryboolean任一几何包含即可
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

四川省交通运输综合地理服务平台 地图开发框架