Crawl the latitude and longitude based on Baidu map open platform Web Service api and convert it to WGS84 coordinates

preface

  • Generally speaking, this blog has few original things. The main content is to integrate the codes of multiple bloggers and reference websites, and realize a complete system process of crawling longitude and latitude based on Baidu map open platform Web Service api and converting it into WGS84 coordinates. The link of the corresponding author's article will be attached at the back of the article. If there is infringement, please contact me for modification.

Creating web service application on Baidu map open platform

  • Baidu open platform website` Baidu map open platform

  • Before creating a new application, you need to apply for an account and pass the authentication.

  • Click console – > application management – > my application – > create an application, where the application type is the server, and all enabled services can be selected by default. The request verification method is IP white name verification. 0.0.0.0 means that there is no restriction on IP. It can be set to this IP in the test stage. It is recommended to modify it after the application is officially launched.
    Figure 1, 2, 3, 4

Crawl the latitude and longitude of the corresponding location based on web Service api

  • My original intention is to make a route map of the Silk Road, so the places where I climb longitude and latitude are all places on the Silk Road, and the original data is stored in excel in the following format.
    chart

  • There are many on-site codes for longitude and latitude crawling on the Internet. I'll just attach them directly. There's nothing to say about the code. The code running environment is annaconda's jupyter notebook.

import urllib
import math
import pandas as pd
import requests
import json
def BaiduQuery(address, currentkey):
    url = 'http://api.map.baidu.com/geocoder/v2/'
    params = { 'address' : address,           
               'ak' : currentkey, # Baidu key
               'output': 'json'     }  # Set the output result to json format
    res = requests.get(url,params)
    jd = json.loads(res.text)    
    # Convert json format to Python dictionary
    #The longitude and latitude coordinates of the location that cannot be searched are set to 0,0
    if jd['status'] == 1:
        coords = {'lng': 0, 'lat': 0}
    else:
    #Baidu coordinates to Mars coordinates
        coords = jd['result']['location']
        coords_1 = baidu_togcj02(coords['lng'], coords['lat'])
        coords['lng'] = coords_1[0]['x']
        coords['lat'] = coords_1[0]['y']
     #Mars coordinates to WGS84
        reverses_coord = gcj02_to_wgs84(coords['lng'], coords['lat'])
        coords['lng'] = reverses_coord[0]
        coords['lat'] = reverses_coord[1]
    return coords
  • The careful readers may have found that the coordinates of my blog have been changed twice, once from Mars coordinates (GCJ02) and once from Mars coordinates to WGS84. I will answer the reasons below.

Introduction to Baidu coordinate conversion parameters

  • The coordinate conversion of web server is very limited, which is through( http://api.map.baidu.com/geoconv/v1/? )The website obtains the corresponding conversion results. The complete request method of the website is as follows

  • 'http://api.map.baidu.com/geoconv/v1/?coords='+data+'&from=5&to=3&output=json'+'&ak=yourak'

  • In the above, data is a coordinate pair in the form of "66.968649,39.656794". ad is the AK generated by the creation application. The main parameters are from and to, indicating the initial coordinate system and target coordinate system.

  • from: number / / source coordinate types, including:
    1: Angular coordinates obtained by GPS equipment, WGS84 coordinates;
    2: Metric coordinates obtained by GPS and coordinates used by sogou map;
    3: Coordinates used for google map, soso map, aliyun map, mapabc map and amap map, and coordinates of National Survey Bureau (GCJ02);
    4: The metric coordinates corresponding to the map coordinates in the list in 3;
    5: Longitude and latitude coordinates adopted by Baidu map;
    6: Metric coordinates adopted by Baidu map;
    7: mapbar map coordinates;
    8: 51 map coordinates

  • to: number / / target coordinate types, including:
    3: Coordinates of National Survey Bureau (GCJ02);
    4: Corresponding metric coordinates in 3;
    5: Bd09ll (Baidu latitude and longitude coordinates);
    6: Bd09mc (Baidu metric latitude and longitude coordinates)

  • It can be seen that Baidu coordinate conversion web Service api does not support the conversion of Baidu coordinates directly to WGS84, but it can be converted to Mars coordinates. At the same time, there are many conversion methods from Mars coordinates to WGS84 on the Internet, and they are public formulas. The disclosing party is unknown, the accuracy guarantee is limited, and scientific research should be used with caution.

  • Next is the code. The code is the method to convert Baidu coordinates to Mars coordinates.

def baidu_togcj02(lng, lat):
    data = str(lng)+','+str(lat)
    url = 'http://api.map.baidu.com/geoconv/v1/?coords='+data+'&from=5&to=3&output=json'+'&ak=sHlqv5QdGaZzeIcpAT2gbVe8aFevOpXm'
    response = requests.get(url) #Make a request
    answer = response.json() #json   
    coords = answer['result']
    return coords
  • Note: this method returns a list. The first item in the list is a dictionary containing coordinate pairs, with X and y as key s, so the reference method is coords_1[0] ['x'] and coords_1[0][‘y’]

Mars coordinates to WGS84

  • Go directly to the code. There's nothing to say. Mars coordinates are not offset from foreign coordinates, so foreign coordinates don't need to be changed.

pi = math.pi  # π
x_pi = pi * 3000.0 / 180.0
a = 6378245.0  # Long half axis
ee = 0.00669342162296594323  # Eccentricity squared
def gcj02_to_wgs84(lng, lat):
    """
    GCJ02(Mars coordinate system)turn GPS84
    :param lng:Longitude of Mars coordinate system
    :param lat:Latitude of Mars coordinate system
    :return:
    """
    if out_of_china(lng, lat):
        return [lng, lat]
    dlat = _transformlat(lng - 105.0, lat - 35.0)
    dlng = _transformlng(lng - 105.0, lat - 35.0)
    radlat = lat / 180.0 * pi
    magic = math.sin(radlat)
    magic = 1 - ee * magic * magic
    sqrtmagic = math.sqrt(magic)
    dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * pi)
    dlng = (dlng * 180.0) / (a / sqrtmagic * math.cos(radlat) * pi)
    mglat = lat + dlat
    mglng = lng + dlng
    return [lng * 2 - mglng, lat * 2 - mglat]


def _transformlat(lng, lat):
    ret = -100.0 + 2.0 * lng + 3.0 * lat + 0.2 * lat * lat + \
          0.1 * lng * lat + 0.2 * math.sqrt(math.fabs(lng))
    ret += (20.0 * math.sin(6.0 * lng * pi) + 20.0 *
            math.sin(2.0 * lng * pi)) * 2.0 / 3.0
    ret += (20.0 * math.sin(lat * pi) + 40.0 *
            math.sin(lat / 3.0 * pi)) * 2.0 / 3.0
    ret += (160.0 * math.sin(lat / 12.0 * pi) + 320 *
            math.sin(lat * pi / 30.0)) * 2.0 / 3.0
    return ret


def _transformlng(lng, lat):
    ret = 300.0 + lng + 2.0 * lat + 0.1 * lng * lng + \
          0.1 * lng * lat + 0.1 * math.sqrt(math.fabs(lng))
    ret += (20.0 * math.sin(6.0 * lng * pi) + 20.0 *
            math.sin(2.0 * lng * pi)) * 2.0 / 3.0
    ret += (20.0 * math.sin(lng * pi) + 40.0 *
            math.sin(lng / 3.0 * pi)) * 2.0 / 3.0
    ret += (150.0 * math.sin(lng / 12.0 * pi) + 300.0 *
            math.sin(lng / 30.0 * pi)) * 2.0 / 3.0
    return ret


def out_of_china(lng, lat):
    """
    Judge whether it is in China, and do not offset if it is not in China
    :param lng:
    :param lat:
    :return:
    """
    return not (lng > 73.66 and lng < 135.05 and lat > 3.86 and lat < 53.55)
  • The above is the method, and the test code is as follows

coords = BaiduQuery('Broken leaf', currentkey="AK")
print(coords)
df = pd.read_excel('List of cities along the Silk Road.xlsx')
height,width = df.shape
print(df.head(3))
for i in range(0, height):
    coords = BaiduQuery(df['city'][i], currentkey="AK")
    df['lng'][i] = coords['lng']
    df['lat'][i] = coords['lat']
#Export results to Excel
df.to_excel('city_coords.xls')
print("Data crawling succeeded, stored!")

Keywords: Python

Added by we4freelance on Sat, 19 Feb 2022 09:52:23 +0200