主题
投影 Projection
Projection 类提供坐标参考系统(CRS)之间的坐标转换能力,支持自定义投影定义。
Projection
构造函数
ts
class Projection {
readonly id: string;
constructor(id: string, def: string);
}参数
| 参数 | 类型 | 说明 |
|---|---|---|
id | string | 投影标识符(如 'EPSG:4326') |
def | string | 投影定义字符串(Proj4 格式) |
方法
| 方法 | 参数 | 返回值 | 说明 |
|---|---|---|---|
transform(coord, targetCRS) | coord: Coordinate, targetCRS: string | Coordinate | 坐标转换 |
transformMany(coords, targetCRS) | coords: Coordinate[], targetCRS: string | Coordinate[] | 批量坐标转换 |
forward(coord) | coord: Coordinate | Pixel | 地理坐标 -> 投影像素 |
inverse(pixel) | pixel: Pixel | Coordinate | 投影像素 -> 地理坐标 |
getUnitsPerMeter() | — | number | 获取每米对应的投影单位数 |
equals(o) | o: Projection | boolean | 是否为同一投影 |
示例
创建投影
ts
import { Projection } from '@gmap/standard';
// WGS84 (EPSG:4326)
const wgs84 = new Projection(
'EPSG:4326',
'+proj=longlat +datum=WGS84 +no_defs'
);
// Web Mercator (EPSG:3857)
const webMercator = new Projection(
'EPSG:3857',
'+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +no_defs'
);
// CGCS2000 (EPSG:4490)
const cgcs2000 = new Projection(
'EPSG:4490',
'+proj=longlat +datum=CGCS2000 +no_defs'
);坐标转换
ts
import { Projection } from '@gmap/standard';
const wgs84 = new Projection('EPSG:4326', '+proj=longlat +datum=WGS84');
const webMercator = new Projection('EPSG:3857', '+proj=merc ...');
// WGS84 -> Web Mercator
const mercatorCoord = wgs84.transform([116.397, 39.908], 'EPSG:3857');
console.log(mercatorCoord); // [12946768.64, 4851854.47] (约)
// Web Mercator -> WGS84
const wgs84Coord = webMercator.transform([12946768.64, 4851854.47], 'EPSG:4326');
console.log(wgs84Coord); // [116.397, 39.908] (约)批量转换
ts
const wgs84 = new Projection('EPSG:4326', '+proj=longlat +datum=WGS84');
const coords = [
[116.397, 39.908],
[116.407, 39.918],
[116.417, 39.928],
];
const mercatorCoords = wgs84.transformMany(coords, 'EPSG:3857');
console.log(mercatorCoords); // 3 个 Web Mercator 坐标前向/反向投影
ts
const wgs84 = new Projection('EPSG:4326', '+proj=longlat +datum=WGS84');
// 地理坐标 -> 像素
const pixel = wgs84.forward([116.397, 39.908]);
console.log(pixel); // [116.397, 39.908] (原始坐标即像素)
// 像素 -> 地理坐标
const coord = wgs84.inverse([100, 200]);
console.log(coord); // [100, 200]比较投影
ts
const wgs84a = new Projection('EPSG:4326', '+proj=longlat +datum=WGS84');
const wgs84b = new Projection('EPSG:4326', '+proj=longlat +datum=WGS84');
const mercator = new Projection('EPSG:3857', '+proj=merc ...');
console.log(wgs84a.equals(wgs84b)); // true
console.log(wgs84a.equals(mercator)); // false获取单位比率
ts
const wgs84 = new Projection('EPSG:4326', '+proj=longlat +datum=WGS84');
const webMercator = new Projection('EPSG:3857', '+proj=merc ...');
console.log(wgs84.getUnitsPerMeter()); // ~0.00000899 (度/米)
console.log(webMercator.getUnitsPerMeter()); // 1 (米/米)与 ProjectionUtils 配合
Projection 类可以与 ProjectionUtils 命名空间配合使用,实现全局投影管理。
ts
import { Projection, ProjectionUtils } from '@gmap/standard';
// 注册自定义投影
const myProj = new Projection('EPSG:4547', '+proj=tmerc +lat_0=0 +lon_0=117 ...');
ProjectionUtils.register('EPSG:4547', myProj);
// 查询已注册投影
const proj = ProjectionUtils.get('EPSG:4547');
console.log(proj?.id); // 'EPSG:4547'
// 列出所有投影
const all = ProjectionUtils.list();
console.log(all); // ['EPSG:4547', ...]
// 检查是否支持
const supported = ProjectionUtils.isSupported('EPSG:4547'); // true常用投影定义
| 标识 | 名称 | Proj4 定义 |
|---|---|---|
EPSG:4326 | WGS 84 | +proj=longlat +datum=WGS84 +no_defs |
EPSG:3857 | Web Mercator | +proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +no_defs |
EPSG:4490 | CGCS 2000 | +proj=longlat +datum=CGCS2000 +no_defs |
EPSG:4547 | CGCS 2000 / 3-degree Gauss-Kruger zone 39 | +proj=tmerc +lat_0=0 +lon_0=117 +k=1 +x_0=39500000 +y_0=0 +datum=CGCS2000 +units=m +no_defs |