giser-convert-crs 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +195 -231
- package/dist/geometryCrs.esm.js +487 -0
- package/dist/geometryCrs.umd.js +495 -0
- package/dist/giserConvertCrs.esm.js +630 -458
- package/dist/giserConvertCrs.esm.js.map +1 -0
- package/dist/giserConvertCrs.min.js +2 -0
- package/dist/giserConvertCrs.min.js.map +1 -0
- package/dist/giserConvertCrs.umd.js +630 -458
- package/dist/giserConvertCrs.umd.js.map +1 -0
- package/dist/index.d.ts +167 -117
- package/package.json +14 -3
package/README.md
CHANGED
|
@@ -1,231 +1,195 @@
|
|
|
1
|
-
# giserConvertCrs
|
|
2
|
-
|
|
3
|
-
`giserConvertCrs`
|
|
4
|
-
|
|
5
|
-
> 注意:npm 包名需要保持小写,因此发布包名仍为 `giser-convert-crs`;代码变量、浏览器全局变量、构建文件名统一使用 `giserConvertCrs`。
|
|
6
|
-
|
|
7
|
-
##
|
|
8
|
-
|
|
9
|
-
所有转换方法的 `fromCRS` 和 `toCRS`
|
|
10
|
-
|
|
11
|
-
| 坐标系 | 可用写法 |
|
|
12
|
-
| --- | --- |
|
|
13
|
-
| WGS-84 | `wgs84`, `wgs-84`, `EPSG:4326`, `epsg:4326` |
|
|
14
|
-
|
|
|
15
|
-
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
```
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
```js
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
'
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
giserConvertCrs.transformWKT('POINT(116.397 39.908)', 'wgs84', 'gcj02');
|
|
197
|
-
giserConvertCrs.transformWKT('POLYGON((116 39, 117 39, 117 40, 116 39))', 'wgs84', 'gcj02');
|
|
198
|
-
```
|
|
199
|
-
|
|
200
|
-
### `giserConvertCrs.WKT`
|
|
201
|
-
|
|
202
|
-
```js
|
|
203
|
-
const geojson = giserConvertCrs.WKT.parse('MULTIPOINT((116 39), (117 40))');
|
|
204
|
-
const wkt = giserConvertCrs.WKT.stringify({
|
|
205
|
-
type: 'LineString',
|
|
206
|
-
coordinates: [[116, 39, 10], [117, 40, 20]]
|
|
207
|
-
});
|
|
208
|
-
```
|
|
209
|
-
|
|
210
|
-
### `giserConvertCrs.GPS`
|
|
211
|
-
|
|
212
|
-
```js
|
|
213
|
-
giserConvertCrs.GPS.wgs84_gcj02(116.397, 39.908);
|
|
214
|
-
giserConvertCrs.GPS.gcj02_wgs84(116.403243, 39.909403);
|
|
215
|
-
giserConvertCrs.GPS.gcj02_wgs84_precise(116.403243, 39.909403);
|
|
216
|
-
giserConvertCrs.GPS.gcj02_bd09ll(116.403243, 39.909403);
|
|
217
|
-
giserConvertCrs.GPS.bd09ll_gcj02(116.409, 39.915);
|
|
218
|
-
giserConvertCrs.GPS.wgs84_bd09ll(116.397, 39.908);
|
|
219
|
-
giserConvertCrs.GPS.bd09ll_wgs84(116.409, 39.915);
|
|
220
|
-
```
|
|
221
|
-
|
|
222
|
-
## 注意事项
|
|
223
|
-
|
|
224
|
-
1. 坐标顺序固定为 `[lon, lat]`。
|
|
225
|
-
2. WGS-84 和 GCJ-02 的加偏转换只对中国范围内坐标生效,中国境外坐标会原样返回。
|
|
226
|
-
3. `gcj02_wgs84` 是常规近似反算,`gcj02_wgs84_precise` 是更高精度反算。
|
|
227
|
-
4. 不支持的坐标系会抛出错误,例如 `epsg:3857`。
|
|
228
|
-
|
|
229
|
-
## License
|
|
230
|
-
|
|
231
|
-
MIT
|
|
1
|
+
# giserConvertCrs
|
|
2
|
+
|
|
3
|
+
`giserConvertCrs` 是一个高性能几何坐标系转换工具库,支持 WGS-84、GCJ-02、BD-09、Web Mercator (EPSG:3857) 与 CGCS2000 之间的点、线、面、数组、GeoJSON 和 WKT 转换。
|
|
4
|
+
|
|
5
|
+
> 注意:npm 包名需要保持小写,因此发布包名仍为 `giser-convert-crs`;代码变量、浏览器全局变量、构建文件名统一使用 `giserConvertCrs`。
|
|
6
|
+
|
|
7
|
+
## 坐标系支持
|
|
8
|
+
|
|
9
|
+
所有转换方法的 `fromCRS` 和 `toCRS` 都支持大小写不敏感与常见别名写法:
|
|
10
|
+
|
|
11
|
+
| 坐标系 | 说明 | 可用写法 |
|
|
12
|
+
| --- | --- | --- |
|
|
13
|
+
| WGS-84 | 国际标准经纬度 | `wgs84`, `wgs-84`, `EPSG:4326`, `epsg:4326`, `4326` |
|
|
14
|
+
| CGCS2000 | 2000国家大地坐标系 | `cgcs2000`, `cgcs-2000`, `EPSG:4490`, `epsg:4490`, `4490` |
|
|
15
|
+
| GCJ-02 | 国测局火星坐标系(高德、腾讯) | `gcj02`, `gcj-02` |
|
|
16
|
+
| BD-09 | 百度经纬度坐标系 | `bd09ll`, `bd09`, `bd-09` |
|
|
17
|
+
| Web Mercator | 墨卡托平面米制投影(Mapbox/Leaflet 底图) | `EPSG:3857`, `epsg:3857`, `3857`, `EPSG:900913`, `webmercator` |
|
|
18
|
+
|
|
19
|
+
```js
|
|
20
|
+
// 常见转换示例
|
|
21
|
+
giserConvertCrs.transform([116.397, 39.908], 'wgs84', 'gcj02');
|
|
22
|
+
giserConvertCrs.transform([116.397, 39.908], 'EPSG:4326', 'bd09');
|
|
23
|
+
giserConvertCrs.transform([116.397, 39.908], 'wgs84', 'EPSG:3857');
|
|
24
|
+
giserConvertCrs.transform([116.397, 39.908], 'cgcs2000', 'gcj02');
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
坐标顺序固定为 `[lon, lat]`,即经度在前、纬度在后。三维坐标也支持,例如 `[lon, lat, height]`,转换后会保留高度值。
|
|
28
|
+
|
|
29
|
+
## 支持的输入格式
|
|
30
|
+
|
|
31
|
+
### 单个点
|
|
32
|
+
|
|
33
|
+
```js
|
|
34
|
+
[116.397, 39.908]
|
|
35
|
+
[116.397, 39.908, 45]
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### 点数组
|
|
39
|
+
|
|
40
|
+
```js
|
|
41
|
+
[
|
|
42
|
+
[116.397, 39.908],
|
|
43
|
+
[117.2, 40.1]
|
|
44
|
+
]
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### GeoJSON
|
|
48
|
+
|
|
49
|
+
支持标准 GeoJSON 几何对象(Point, LineString, Polygon, MultiPoint, MultiLineString, MultiPolygon, GeometryCollection)以及 Feature 与 FeatureCollection。如果包含 `bbox` 包围盒,会同步转换。
|
|
50
|
+
|
|
51
|
+
```js
|
|
52
|
+
const geojson = {
|
|
53
|
+
type: 'FeatureCollection',
|
|
54
|
+
features: [
|
|
55
|
+
{
|
|
56
|
+
type: 'Feature',
|
|
57
|
+
properties: { name: '示例点' },
|
|
58
|
+
geometry: {
|
|
59
|
+
type: 'Point',
|
|
60
|
+
coordinates: [116.397, 39.908]
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
]
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
giserConvertCrs.transformGeoJSON(geojson, 'wgs84', 'gcj02');
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### WKT
|
|
70
|
+
|
|
71
|
+
支持 Point, LineString, Polygon, MultiPoint, MultiLineString, MultiPolygon, GeometryCollection:
|
|
72
|
+
|
|
73
|
+
```js
|
|
74
|
+
'POINT(116.397 39.908)'
|
|
75
|
+
'LINESTRING(116 39, 117 40)'
|
|
76
|
+
'POLYGON((116 39, 117 39, 117 40, 116 39))'
|
|
77
|
+
'GEOMETRYCOLLECTION(POINT(116 39), LINESTRING(116 39, 117 40))'
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## 安装
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
npm install giser-convert-crs
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## 引入方式
|
|
87
|
+
|
|
88
|
+
### ES Module
|
|
89
|
+
|
|
90
|
+
```js
|
|
91
|
+
import giserConvertCrs, { WKT, GPS, ProjectionTransform } from 'giser-convert-crs';
|
|
92
|
+
|
|
93
|
+
const point = giserConvertCrs.transform([116.397, 39.908], 'wgs84', 'gcj02');
|
|
94
|
+
const mercator = giserConvertCrs.transform([116.397, 39.908], 'wgs84', 'EPSG:3857');
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### Script 标签(CDN 或本地)
|
|
98
|
+
|
|
99
|
+
```html
|
|
100
|
+
<!-- 生产压缩版 -->
|
|
101
|
+
<script src="https://unpkg.com/giser-convert-crs/dist/giserConvertCrs.min.js"></script>
|
|
102
|
+
<script>
|
|
103
|
+
const point = giserConvertCrs.transform([116.397, 39.908], 'wgs84', 'gcj02');
|
|
104
|
+
</script>
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
## API 说明
|
|
108
|
+
|
|
109
|
+
### `giserConvertCrs.transform(input, fromCRS, toCRS, options)`
|
|
110
|
+
|
|
111
|
+
统一转换入口,自动识别输入类型(坐标数组、GeoJSON、WKT 字符串),并返回对应结构。
|
|
112
|
+
|
|
113
|
+
**配置项 `options`**(可选):
|
|
114
|
+
- `mutate` (`boolean`, 默认 `false`):大数据量优化选项。设为 `true` 时直接在原坐标数组上就地修改,零内存开销,大幅降低 GC 压力。
|
|
115
|
+
- `precise` (`boolean`, 默认 `true`):逆算反解精度模式。开启后反算误差在 $10^{-10}$ 度(亚毫米级)以内。
|
|
116
|
+
- `precision` (`number`, 可选):当输出为 WKT 字符串时,控制保留的小数位数(如 `precision: 6`)。
|
|
117
|
+
|
|
118
|
+
```js
|
|
119
|
+
// 常规转换
|
|
120
|
+
giserConvertCrs.transform([116.397, 39.908], 'wgs84', 'gcj02');
|
|
121
|
+
|
|
122
|
+
// 大数据量数组就地修改(极速零拷贝模式)
|
|
123
|
+
giserConvertCrs.transform(hugePointArray, 'wgs84', 'gcj02', { mutate: true });
|
|
124
|
+
|
|
125
|
+
// WKT 输出保留 6 位小数
|
|
126
|
+
giserConvertCrs.transform('POINT(116.39712345 39.90812345)', 'wgs84', 'gcj02', { precision: 6 });
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
### `giserConvertCrs.transformPoint(coord, fromCRS, toCRS, options)`
|
|
130
|
+
|
|
131
|
+
```js
|
|
132
|
+
giserConvertCrs.transformPoint([116.397, 39.908], 'wgs84', 'gcj02');
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
### `giserConvertCrs.transformPoints(points, fromCRS, toCRS, options)`
|
|
136
|
+
|
|
137
|
+
```js
|
|
138
|
+
giserConvertCrs.transformPoints([[116.397, 39.908], [117.2, 40.1]], 'wgs84', 'gcj02');
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
### `giserConvertCrs.transformGeoJSON(geojson, fromCRS, toCRS, options)`
|
|
142
|
+
|
|
143
|
+
```js
|
|
144
|
+
const result = giserConvertCrs.transformGeoJSON(geojson, 'wgs84', 'gcj02');
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
### `giserConvertCrs.transformWKT(wkt, fromCRS, toCRS, options)`
|
|
148
|
+
|
|
149
|
+
```js
|
|
150
|
+
giserConvertCrs.transformWKT('POINT(116.397 39.908)', 'wgs84', 'gcj02', { precision: 6 });
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
### `giserConvertCrs.WKT`
|
|
154
|
+
|
|
155
|
+
```js
|
|
156
|
+
// 解析 WKT 为 GeoJSON
|
|
157
|
+
const geojson = giserConvertCrs.WKT.parse('MULTIPOINT((116 39), (117 40))');
|
|
158
|
+
|
|
159
|
+
// 序列化为 WKT,支持可选精度设置
|
|
160
|
+
const wkt = giserConvertCrs.WKT.stringify({
|
|
161
|
+
type: 'LineString',
|
|
162
|
+
coordinates: [[116, 39, 10], [117, 40, 20]]
|
|
163
|
+
}, { precision: 6 });
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
### `giserConvertCrs.GPS` 底层算法
|
|
167
|
+
|
|
168
|
+
```js
|
|
169
|
+
// GCJ-02 与 WGS-84
|
|
170
|
+
GPS.wgs84_gcj02(116.397, 39.908);
|
|
171
|
+
GPS.gcj02_wgs84(116.403243, 39.909403); // 单次近似反算
|
|
172
|
+
GPS.gcj02_wgs84_precise(116.403243, 39.909403); // 3次定点高精度迭代反算
|
|
173
|
+
|
|
174
|
+
// BD-09 与 GCJ-02 / WGS-84
|
|
175
|
+
GPS.gcj02_bd09ll(116.403243, 39.909403);
|
|
176
|
+
GPS.bd09ll_gcj02(116.409, 39.915);
|
|
177
|
+
GPS.bd09ll_gcj02_precise(116.409, 39.915);
|
|
178
|
+
GPS.wgs84_bd09ll(116.397, 39.908);
|
|
179
|
+
GPS.bd09ll_wgs84_precise(116.409, 39.915);
|
|
180
|
+
|
|
181
|
+
// Web Mercator (EPSG:3857)
|
|
182
|
+
GPS.wgs84_epsg3857(116.397, 39.908);
|
|
183
|
+
GPS.epsg3857_wgs84(12957254.77, 4852582.08);
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
## 注意事项
|
|
187
|
+
|
|
188
|
+
1. 经纬度坐标顺序固定为 `[lon, lat]`。
|
|
189
|
+
2. WGS-84 与 GCJ-02 的加偏转换只对中国国境内坐标生效,境外坐标自动原样返回。
|
|
190
|
+
3. CGCS2000(国家2000坐标系)在民用级精度下长半轴与 WGS-84 完全一致,扁率差异在 0.1 毫米以内,已无缝别名支持。
|
|
191
|
+
4. 如需处理大规模轨迹流或大文件 GeoJSON,推荐开启 `{ mutate: true }` 选项以消除内存与 GC 开销。
|
|
192
|
+
|
|
193
|
+
## License
|
|
194
|
+
|
|
195
|
+
MIT
|