1 Star 0 Fork 9

AiRGL / gcoord

forked from zheng_shuwu / gcoord 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

gcoord

npm version Build Status codecov gzip size LICENSE

gcoord( geographic coordinates)是一个处理地理坐标的js库

它能够处理GeoJSON,能够在不同坐标系之间做转换

  • 轻量,零依赖,gzip后大小仅3kb
  • 兼容性强,能在node环境以及所有现代浏览器(IE8+)中运行
  • 稳定高效,100%测试覆盖
  • 支持转换GeoJSON

关于坐标系

我们通常用经纬度来表示一个地理位置,但是由于一些原因,我们从不同渠道得到的经纬度信息可能并不是在同一个坐标系下。

  • 高德地图、腾讯地图以及谷歌中国区地图使用的是GCJ-02坐标系
  • 百度地图使用的是BD-09坐标系
  • 底层接口(HTML5 Geolocation或ios、安卓API)通过GPS设备获取的坐标使用的是WGS-84坐标系

不同的坐标系之间可能有几十到几百米的偏移,所以在开发基于地图的产品,或者做地理数据可视化时,我们需要修正不同坐标系之间的偏差。

WGS-84 - 世界大地测量系统

WGS-84(World Geodetic System, WGS)是使用最广泛的坐标系,也是世界通用的坐标系,GPS设备得到的经纬度就是在WGS84坐标系下的经纬度。通常通过底层接口得到的定位信息都是WGS84坐标系。

GCJ-02 - 国测局坐标

GCJ-02(G-Guojia国家,C-Cehui测绘,J-Ju局),又被称为火星坐标系,是一种基于WGS-84制定的大地测量系统,由中国国测局制定。此坐标系所采用的混淆算法会在经纬度中加入随机的偏移。

国家规定,中国大陆所有公开地理数据都需要至少用GCJ-02进行加密,也就是说我们从国内公司的产品中得到的数据,一定是经过了加密的。绝大部分国内互联网地图提供商都是使用GCJ-02坐标系,包括高德地图,谷歌地图中国区等。

导航电子地图在公开出版、销售、传播、展示和使用前,必须进行空间位置技术处理。
— GB 20263―2006《导航电子地图安全处理技术基本要求》,4.1

BD-09 - 百度坐标系

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

API


transform( input, from, to )

进行坐标转换

参数

返回值

geojson | Array<number>

示例

// 将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为坐标系,目标支持以下几种坐标系

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

feature( geometry[, properties[, options ] ] )

生成一个 GeoJSON Feature

参数

  • geometry Geometry 输入geometry
  • properties Object 属性 (可选, 默认值 {})
  • options Object 选项 (可选, 默认值 {})

返回值

Feature

示例

var geometry = {
    "type": "Point",
    "coordinates": [ 110, 50 ]
};

var feature = gcoord.feature( geometry );

geometry( type, coordinates[, options ] )

生成一个GeoJSON Geometry 如果需要创建GeometryCollection,可以使用gcoord.geometryCollection

参数

  • type string Geometry 类型
  • coordinates Array<number> 坐标
  • options Object 选项 (可选, 默认值 {})
    • options.bbox Array<number> 外包围框 [west, south, east, north]

返回值

Geometry

示例

var type = 'Point';
var coordinates = [ 110, 50 ];

var geometry = gcoord.geometry( type, coordinates );

point( coordinates[, properties[, options ] ] )

生成一个 PointFeature

参数

  • coordinates Array<number> 坐标
  • properties Object 属性 (可选, 默认值 {})
  • options Object 选项 (可选, 默认值 {})

返回值

Feature<Point>

示例

var point = gcoord.point([-75.343, 39.984]);

//=point

points( coordinates[, properties[, options ] ] )

生成一个 PointFeatureCollection

参数

  • coordinates Array<Array<number>> 坐标
  • properties Object 每个feature的属性 (可选, 默认值 {})
  • options Object 选项 (可选, 默认值 {})
    • options.bbox Array<number> 外包围框 [west, south, east, north]
    • options.id (string | number) FeatureCollection的id

返回值

FeatureCollection<Point>

示例

var points = gcoord.points([
  [-75, 39],
  [-80, 45],
  [-78, 50]
]);

//=points

polygon( coordinates[, properties[, options ] ] )

生成一个 Polygon Feature

参数

返回值

Feature<Polygon>

示例

var polygon = gcoord.polygon([[[-5, 52], [-4, 56], [-2, 51], [-7, 54], [-5, 52]]], { name: 'poly1' });

//=polygon

polygons( coordinates[, properties[, options ] ] )

生成一个 Polygon FeatureCollection

参数

返回值

FeatureCollection<Polygon>

示例

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( coordinates[, properties[, options ] ] )

生成一个 LineString Feature

参数

返回值

Feature<LineString>

示例

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

lineStrings( coordinates[, properties[, options ] ] )

生成一个 LineString FeatureCollection

参数

返回值

FeatureCollection<LineString>

示例

var linestrings = gcoord.lineStrings([
  [[-24, 63], [-23, 60], [-25, 65], [-20, 69]],
  [[-14, 43], [-13, 40], [-15, 45], [-10, 49]]
]);

//=linestrings

featureCollection( features[, options ] )

生成一个 FeatureCollection.

参数

返回值

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

multiLineString( coordinates[, properties[, options ] ] )

生成一个 Feature<MultiLineString>

参数

返回值

Feature<MultiLineString>

示例

var multiLine = gcoord.multiLineString([[[0,0],[10,10]]]);

//=multiLine

multiPoint( coordinates[, properties[, options ] ] )

生成一个 Feature<MultiPoint>

参数

返回值

Feature<MultiPoint>

示例

var multiPt = gcoord.multiPoint([[0,0],[10,10]]);

//=multiPt

multiPolygon( coordinates[, properties[, options ] ] )

生成一个 Feature<MultiPolygon>

参数

返回值

Feature<MultiPolygon>

示例

var multiPoly = gcoord.multiPolygon([[[[0,0],[0,10],[10,10],[10,0],[0,0]]]]);

//=multiPoly

geometryCollection( geometries[, properties[, options ] ] )

生成一个 Feature<GeometryCollection>

参数

  • geometries Array<Geometry> 一个 GeoJSON Geometries数组
  • properties Object 属性 (可选, 默认值 {})
  • options Object 选项 (可选, 默认值 {})

返回值

Feature<GeometryCollection>

示例

var pt = {
    "type": "Point",
      "coordinates": [100, 0]
    };
var line = {
    "type": "LineString",
    "coordinates": [ [101, 0], [102, 1] ]
  };
var collection = gcoord.geometryCollection([pt, line]);

//=collection

LICENSE

MIT

MIT License Copyright (c) 2018 Jiulong Hu Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

地理坐标库,支持坐标系的相互转换(WGS84,GCJ02,BD09等),支持GeoJSON 展开 收起
JavaScript
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
JavaScript
1
https://gitee.com/airgl/gcoord.git
git@gitee.com:airgl/gcoord.git
airgl
gcoord
gcoord
master

搜索帮助