主题
工具模块
工具模块提供量算、分析、几何和投影等实用功能。
G.utils
G.utils 是一个对象,包含以下子模块和快捷方法:
ts
G.utils.measurement // 量算模块
G.utils.analysis // 分析模块
G.utils.geom // 几何工具
G.utils.projection // 投影工具
G.utils.distance(points) // 快捷方法:计算两点距离
G.utils.length(line) // 快捷方法:计算线长度G.utils.distance(points)
计算两点间的距离。
ts
G.utils.distance(point1: Coordinate, point2: Coordinate): number| 参数 | 类型 | 描述 |
|---|---|---|
| point1 | [number, number] | 起点坐标 |
| point2 | [number, number] | 终点坐标 |
返回值:距离值(单位取决于当前坐标系)。
ts
const d = G.utils.distance([116.39, 39.90], [116.40, 39.91]);
// d > 0G.utils.length(line)
计算线要素的长度。
ts
G.utils.length(line: LineString | Geometry): number| 参数 | 类型 | 描述 |
|---|---|---|
| line | LineString | Geometry | 线几何对象 |
返回值:长度值(单位取决于当前坐标系)。
ts
const line = G.geom.lineString([[116, 39], [117, 40]]);
const len = G.utils.length(line);
// len >= 0G.utils.measurement
量算模块,提供更全面的测量功能。
ts
G.utils.measurement包含的方法(来自 @gmap/geo 的 Measurement):
| 方法 | 说明 |
|---|---|
| distance(p1, p2) | 计算两点距离 |
| length(geom) | 计算线长度 |
| area(geom) | 计算面积 |
| perimeter(geom) | 计算周长 |
G.utils.analysis
分析模块,提供空间分析功能。
ts
G.utils.analysis包含的方法(来自 @gmap/geo 的 Analysis):
| 方法 | 说明 |
|---|---|
| buffer(geom, dist) | 缓冲区分析 |
| intersect(a, b) | 相交分析 |
| union(a, b) | 合并分析 |
| difference(a, b) | 差集分析 |
| centroid(geom) | 质心计算 |
G.utils.geom
几何工具模块,提供几何操作和验证。
ts
G.utils.geom| 方法 | 说明 |
|---|---|
| contains(outer, inner) | 判断包含关系 |
| intersects(a, b) | 判断相交 |
| distance(a, b) | 计算距离 |
| centroid(geom) | 计算质心 |
| area(geom) | 计算面积 |
| length(geom) | 计算长度 |
G.utils.projection
投影工具模块,提供坐标转换功能。
ts
G.utils.projection| 方法 | 说明 |
|---|---|
| transform(coord, from, to) | 坐标系转换 |
| getDistance(c1, c2) | 计算球面距离 |
ts
// 转换坐标系
const wgs84 = G.utils.projection.transform([116, 39], 'EPSG:4326', 'EPSG:3857');投影工具 — G.projection()
创建投影对象用于坐标系转换。
ts
G.projection(code: string, definition?: string): Projection| 参数 | 类型 | 描述 |
|---|---|---|
| code | string | 坐标系代码(如 'EPSG:4326')(必填) |
| definition | string | Proj4 投影定义字符串 |
ts
const p = G.projection('EPSG:4326', '+proj=longlat +datum=WGS84');
p.transform([116, 39], 'EPSG:3857'); // 坐标转换
p.forward([116, 39]); // 地理坐标 -> 投影坐标
p.inverse([116, 39]); // 投影坐标 -> 地理坐标
p.equals(G.projection('EPSG:4326')); // true