coordtransform coordinate transformation

A tool module that provides conversion between Baidu coordinates (BD09), National Survey Bureau coordinates (Mars coordinates, GCJ02), and WGS84 coordinate systems. Also provides the python version of the code https://github.com/wandergis/coordTransform_py


Support node, browser (AMD method and direct reference method)

why write this module

With the rise of the mobile Internet, almost every app will collect the user's location. If you happen to be dealing with code related to geolocation and don't know the geographic coordinate system, you will definitely be confused by the various coordinate systems of the Great Celestial Dynasty. The purpose of writing this module is also because the coordinates obtained by the app in the project are obtained by Baidu sdk. When doing webgis visualization, there are various deviations and various coordinates are wrong. The mood at that time is the picture below.

image

The current status of the coordinate system of Internet maps

Earth coordinates (WGS84)

  • International standard, coordinate system for data taken from GPS devices
  • Coordinate system used by international map providers

Mars coordinate (GCJ-02) is also called the coordinate system of the National Survey Bureau

  • Chinese standard, the coordinate data obtained from the positioning of the mobile device of the Bank of China uses this coordinate system
  • National regulations: Various map systems (including electronic forms) published in China must at least use GCJ-02 to encrypt the geographic location for the first time.

Baidu coordinates (BD-09)

  • Baidu standard, Baidu SDK, Baidu map, Geocoding use
  • (It was already messed up, and Baidu made a second encryption on the coordinates of Mars)

Things to keep in mind during development

  • Get latitude and longitude (GPS) coordinates from the device

        如果使用的是百度sdk那么可以获得百度坐标(bd09)或者火星坐标(GCJ02),默认是bd09
        如果使用的是ios的原生定位库,那么获得的坐标是WGS84
        如果使用的是高德sdk,那么获取的坐标是GCJ02
    
  • Coordinate system used by Internet online maps

    火星坐标系:
            iOS 地图(其实是高德)
            Google 地图
            搜搜、阿里云、高德地图
    百度坐标系:
            当然只有百度地图
    WGS84坐标系:
            国际标准,谷歌国外地图、osm地图等国外的地图一般都是这个
    

    take a chestnut

    The app of the author's company uses Baidu's sdk, and it needs to do web visualization of positioning coordinates. The js api provided by Baidu map cannot meet the needs, so leaflet is used for visualization. Here we will talk about Baidu map, which uses the coordinate system. It is inconsistent with the origin of the cut map, and its biasing is nonlinear, so it cannot be loaded by the commonly used loading method, and the base map that uses it is abandoned. It is the coordinates of the National Bureau of Surveying and Surveying, which is the GCJ02 coordinate system. If you simply superimpose the latitude and longitude obtained by the app, it is possible that your original location in Baidu Building will be displayed at Xierqi Metro Station or even further away, so you need to turn bd09. Into the gcj02 coordinate system, at this time, this library has a place to use, batch conversion of points and then load them on the base map, so that the points can be displayed in the position where they should appear.

    In addition, if you get some WGS84 coordinates and want to load them into various basemaps, you can convert between the basemap coordinate system and your data coordinate system according to this library. Hope it will be useful to everyone.


install

npm install coordtransform

Example & Usage

1 NodeJs usage

//国测局坐标(火星坐标,比如高德地图在用),百度坐标,wgs84坐标(谷歌国外以及绝大部分国外在线地图使用的坐标)
var coordtransform=require('coordtransform');
//百度经纬度坐标转国测局坐标
var bd09togcj02=coordtransform.bd09togcj02(116.404, 39.915);
//国测局坐标转百度经纬度坐标
var gcj02tobd09=coordtransform.gcj02tobd09(116.404, 39.915);
//wgs84转国测局坐标
var wgs84togcj02=coordtransform.wgs84togcj02(116.404, 39.915);
//国测局坐标转wgs84坐标
var gcj02towgs84=coordtransform.gcj02towgs84(116.404, 39.915);
console.log(bd09togcj02);
console.log(gcj02tobd09);
console.log(wgs84togcj02);
console.log(gcj02towgs84);
//result
//bd09togcj02:   [ 116.39762729119315, 39.90865673957631 ]
//gcj02tobd09:   [ 116.41036949371029, 39.92133699351021 ]
//wgs84togcj02:  [ 116.41024449916938, 39.91640428150164 ]
//gcj02towgs84:  [ 116.39775550083061, 39.91359571849836 ]

2 Browser usage directly refers to index.js in the directory, there will be a global object of coordtransform exposed, and it also supports loading with AMD loader

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>coordTransform</title>
</head>
<body>
<h1>请按F12打开控制台查看结果</h1>
<script src="index.js"></script>
<script>
    //国测局坐标(火星坐标,比如高德地图在用),百度坐标,wgs84坐标(谷歌国外以及绝大部分国外在线地图使用的坐标)
    //百度经纬度坐标转国测局坐标
    var bd09togcj02 = coordtransform.bd09togcj02(116.404, 39.915);
    //国测局坐标转百度经纬度坐标
    var gcj02tobd09 = coordtransform.gcj02tobd09(116.404, 39.915);
    //wgs84转国测局坐标
    var wgs84togcj02 = coordtransform.wgs84togcj02(116.404, 39.915);
    //国测局坐标转wgs84坐标
    var gcj02towgs84 = coordtransform.gcj02towgs84(116.404, 39.915);
    console.log(bd09togcj02);
    console.log(gcj02tobd09);
    console.log(wgs84togcj02);
    console.log(gcj02towgs84);
    //result
    //bd09togcj02:   [ 116.39762729119315, 39.90865673957631 ]
    //gcj02tobd09:   [ 116.41036949371029, 39.92133699351021 ]
    //wgs84togcj02:  [ 116.41024449916938, 39.91640428150164 ]
    //gcj02towgs84:  [ 116.39775550083061, 39.91359571849836 ]
</script>
</body>
</html>

everyone

  • Mercator coordinates
  • geojson conversion
  • Batch conversion
  • turf plugin
  • leaflet plugin

sometipes

For those who do GIS, the base map is still very important to us. Sometimes the base map from abroad is very beautiful, but after changing it, it is found that the coordinates are all wrong, so I wrote this package to help you complete the coordinate conversion, and prepare It is written as an extension of leaflet, which is convenient for everyone to use. Please star if you like children's shoes, O(∩_∩)O

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326718699&siteId=291194637