gcoord( geographic coordinates)是一个处理地理坐标的js库
它能够处理GeoJSON,能够在不同坐标系之间做转换
- 轻量,零依赖,gzip后大小仅3kb
- 兼容性强,能在node环境以及所有现代浏览器(IE8+)中运行
- 稳定高效,100%测试覆盖
- 支持转换GeoJSON
我们通常用经纬度来表示一个地理位置,但是由于一些原因,我们从不同渠道得到的经纬度信息可能并不是在同一个坐标系下。
- 高德地图、腾讯地图以及谷歌中国区地图使用的是GCJ-02坐标系
- 百度地图使用的是BD-09坐标系
- 底层接口(HTML5 Geolocation或ios、安卓API)通过GPS设备获取的坐标使用的是WGS-84坐标系
不同的坐标系之间可能有几十到几百米的偏移,所以在开发基于地图的产品,或者做地理数据可视化时,我们需要修正不同坐标系之间的偏差。
WGS-84(World Geodetic System, WGS)是使用最广泛的坐标系,也是世界通用的坐标系,GPS设备得到的经纬度就是在WGS84坐标系下的经纬度。通常通过底层接口得到的定位信息都是WGS84坐标系。
GCJ-02(G-Guojia国家,C-Cehui测绘,J-Ju局),又被称为火星坐标系,是一种基于WGS-84制定的大地测量系统,由中国国测局制定。此坐标系所采用的混淆算法会在经纬度中加入随机的偏移。
国家规定,中国大陆所有公开地理数据都需要至少用GCJ-02进行加密,也就是说我们从国内公司的产品中得到的数据,一定是经过了加密的。绝大部分国内互联网地图提供商都是使用GCJ-02坐标系,包括高德地图,谷歌地图中国区等。
导航电子地图在公开出版、销售、传播、展示和使用前,必须进行空间位置技术处理。
— GB 20263―2006《导航电子地图安全处理技术基本要求》,4.1
BD-09(Baidu, BD)是百度地图使用的地理坐标系,其在GCJ-02上多增加了一次变换,用来保护用户隐私。从百度产品中得到的坐标都是BD-09坐标系。
不同坐标系下的点在百度地图上会有偏移
GCJ-02和BD-09都是用来对地理数据进行加密的,所以也不会公开逆向转换的方法。理论上,GCJ-02的加密过程是不可逆的,但是可以通过一些方法来逼近接原始坐标,并且这种方式的精度很高。gcoord使用的纠偏方式达到了厘米级的精度,能满足绝大多数情况。
通过npm安装
npm install gcoord --save
或者直接下载gcoord.js
例如从手机的GPS得到一个经纬度坐标,需要将其展示在百度地图上,则应该将当前坐标从WGS-84坐标系转换为BD-09坐标系
var result = gcoord.transform(
[ 116.403988, 39.914266 ], // 经纬度坐标
gcoord.WGS84, // 当前坐标系
gcoord.BD09 // 目标坐标系
);
console.log( result ); // [ 116.41661560068297, 39.92196580126834 ]
同时gcoord还可以生成GeoJSON以及转换GeoJSON的坐标系,详细使用方式可以参考API
- 坐标转换
- GeoJSON
进行坐标转换
参数
返回值
示例
// 将GCJ02坐标转换为WGS84坐标
var result = gcoord.transform( [ 123, 45 ], gcoord.GCJ02, gcoord.WGS84 );
console.log( result ); // [ 122.99395597, 44.99804071 ]
// 转换GeoJSON坐标
var geojson = {
"type": "Point",
"coordinates": [ 123, 45 ]
}
gcoord.transform( geojson, gcoord.GCJ02, gcoord.WGS84 );
console.log( geojson.coordinates ); // [ 122.99395597, 44.99804071 ]
// 生成GeoJSON并转换坐标
var geojson = gcoord.point( [ 123, 45 ] );
gcoord.transform( geojson, gcoord.GCJ02, gcoord.WGS84 );
console.log( geojson.coordinates ); // [ 122.99395597, 44.99804071 ]
返回数组或GeoJSON对象(由输入决定),注意:当输入为geojson时,transform会改变输入对象
CRS为坐标系,目标支持以下几种坐标系
CRS | 说明 |
---|---|
gcoord.WGS84 | WGS-84坐标系,GPS设备获取的经纬度坐标 |
gcoord.GCJ02 | GCJ-02坐标系,google中国地图、soso地图、aliyun地图、mapabc地图和高德地图所用的经纬度坐标 |
gcoord.BD09 | BD-O9坐标系,百度地图采用的经纬度坐标 |
gcoord.WGS1984 | WGS-84坐标系别名,同WGS-84 |
gcoord.EPSG4326 | WGS-84坐标系别名,同WGS-84 |
生成一个 GeoJSON Feature
参数
返回值
示例
var geometry = {
"type": "Point",
"coordinates": [ 110, 50 ]
};
var feature = gcoord.feature( geometry );
生成一个GeoJSON Geometry
如果需要创建GeometryCollection,可以使用gcoord.geometryCollection
参数
返回值
示例
var type = 'Point';
var coordinates = [ 110, 50 ];
var geometry = gcoord.geometry( type, coordinates );
参数
返回值
示例
var point = gcoord.point([-75.343, 39.984]);
//=point
参数
coordinates
Array<Array<number>> 坐标properties
Object 每个feature的属性 (可选, 默认值{}
)options
Object 选项 (可选, 默认值{}
)
返回值
示例
var points = gcoord.points([
[-75, 39],
[-80, 45],
[-78, 50]
]);
//=points
参数
coordinates
Array<Array<Array<number>>> 坐标properties
Object 属性 (可选, 默认值{}
)options
Object 选项 (可选, 默认值{}
)
返回值
示例
var polygon = gcoord.polygon([[[-5, 52], [-4, 56], [-2, 51], [-7, 54], [-5, 52]]], { name: 'poly1' });
//=polygon
生成一个 Polygon FeatureCollection
参数
coordinates
Array<Array<Array<Array<number>>>> 坐标properties
Object 属性 (可选, 默认值{}
)options
Object 选项 (可选, 默认值{}
)
返回值
示例
var polygons = gcoord.polygons([
[[[-5, 52], [-4, 56], [-2, 51], [-7, 54], [-5, 52]]],
[[[-15, 42], [-14, 46], [-12, 41], [-17, 44], [-15, 42]]],
]);
//=polygons
生成一个 LineString Feature
参数
coordinates
Array<Array<number>> 坐标properties
Object 属性 (可选, 默认值{}
)options
Object 选项 (可选, 默认值{}
)
返回值
示例
var linestring1 = gcoord.lineString([[-24, 63], [-23, 60], [-25, 65], [-20, 69]], {name: 'line 1'});
var linestring2 = gcoord.lineString([[-14, 43], [-13, 40], [-15, 45], [-10, 49]], {name: 'line 2'});
//=linestring1
//=linestring2
生成一个 LineString FeatureCollection
参数
coordinates
Array<Array<number>> 坐标properties
Object 属性 (可选, 默认值{}
)options
Object 选项 (可选, 默认值{}
)
返回值
示例
var linestrings = gcoord.lineStrings([
[[-24, 63], [-23, 60], [-25, 65], [-20, 69]],
[[-14, 43], [-13, 40], [-15, 45], [-10, 49]]
]);
//=linestrings
生成一个 FeatureCollection.
参数
返回值
示例
var locationA = gcoord.point([-75.343, 39.984], {name: 'Location A'});
var locationB = gcoord.point([-75.833, 39.284], {name: 'Location B'});
var locationC = gcoord.point([-75.534, 39.123], {name: 'Location C'});
var collection = gcoord.featureCollection([
locationA,
locationB,
locationC
]);
//=collection
参数
coordinates
Array<Array<Array<number>>> 坐标properties
Object 属性 (可选, 默认值{}
)options
Object 选项 (可选, 默认值{}
)
返回值
示例
var multiLine = gcoord.multiLineString([[[0,0],[10,10]]]);
//=multiLine
生成一个 Feature<MultiPoint>
参数
coordinates
Array<Array<number>> 坐标properties
Object 属性 (可选, 默认值{}
)options
Object 选项 (可选, 默认值{}
)
返回值
示例
var multiPt = gcoord.multiPoint([[0,0],[10,10]]);
//=multiPt
参数
coordinates
Array<Array<Array<Array<number>>>> 坐标properties
Object 属性 (可选, 默认值{}
)options
Object 选项 (可选, 默认值{}
)
返回值
示例
var multiPoly = gcoord.multiPolygon([[[[0,0],[0,10],[10,10],[10,0],[0,0]]]]);
//=multiPoly
生成一个 Feature<GeometryCollection>
参数
geometries
Array<Geometry> 一个 GeoJSON Geometries数组properties
Object 属性 (可选, 默认值{}
)options
Object 选项 (可选, 默认值{}
)
返回值
示例
var pt = {
"type": "Point",
"coordinates": [100, 0]
};
var line = {
"type": "LineString",
"coordinates": [ [101, 0], [102, 1] ]
};
var collection = gcoord.geometryCollection([pt, line]);
//=collection
MIT