#! /usr/bin/env python
# -*- coding: utf-8 -*-
import math

x_pi = 3.14159265358979324 * 3000.0 / 180.0
pi = 3.1415926535897932384626  # PI.
a = 6378245.0  # semi-major axis
ee = 0.00669342162296594323  # Eccentricity squared
coordinate = []
lng = []
lat = []
converted_lng = []
converted_lat = []


def gcj02_to_bd09(lng, lat) :
    Param LNG: Mars coordinates longitude: Param Lat: Mars coordinates latitude :return: """
    z = math.sqrt(lng * lng + lat * lat) + 0.00002 * math.sin(lat * x_pi)
    theta = math.atan2(lat, lng) + 0.000003 * math.cos(lng * x_pi)
    bd_lng = z * math.cos(theta) + 0.0065
    bd_lat = z * math.sin(theta) + 0.006
    return [bd_lng, bd_lat]


def bd09_to_gcj02(bd_lon, bd_lat) :
    Baidu coordinate system (BD-09) to Mars coordinate system (GCJ-02) Baidu -- > Google, Autonavi: Param bd_Lat: Baidu coordinates latitude: Param bd_lon: Baidu coordinates longitude :return: transformed coordinate list form """
    x = bd_lon - 0.0065
    y = bd_lat - 0.006
    z = math.sqrt(x * x + y * y) - 0.00002 * math.sin(y * x_pi)
    theta = math.atan2(y, x) - 0.000003 * math.cos(x * x_pi)
    gg_lng = z * math.cos(theta)
    gg_lat = z * math.sin(theta)
    return [gg_lng, gg_lat]


def wgs84_to_gcj02(lng, lat) :
    WGS84 to GCJ02(Mars coordinate system) : PARam LNG: Longitude of WGS84 coordinate system: Param Lat: latitude of WGS84 coordinate system :return: """
    if out_of_china(lng, lat):  # Determine if it is in the country
        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 [mglng, mglat]


def gcj02_to_wgs84(lng, lat) :
    """ GCJ02(Mars coordinate system) to GPS84: Param LNG: Mars coordinate system longitude: Param Lat: Mars coordinate system latitude :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 bd09_to_wgs84(bd_lon, bd_lat) :
    lon, lat = bd09_to_gcj02(bd_lon, bd_lat)
    return gcj02_to_wgs84(lon, lat)


def wgs84_to_bd09(lon, lat) :
    lon, lat = wgs84_to_gcj02(lon, lat)
    return gcj02_to_bd09(lon, lat)


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) :
    """ Determine whether it is in the country, not in the country do not offset :param LNG: :param lat: :return: ""
    return not (lng > 73.66 and lng < 135.05 and lat > 3.86 and lat < 53.55)


pathIn = "1.txt"
pathOut = "output1.txt"
result = []
count = 0
if __name__ == '__main__':
    with open(pathIn, 'r') as file:
        lines = file.readlines()
        for line in lines:
            line1 = line.strip()
            tmp = line1.split(",")
            lng.append(float(tmp[0]))
            lat.append(float(tmp[1]))
    try:
        for i in range(len(lng)):
            converted = wgs84_to_bd09(lng[i], lat[i])
            with open(pathOut, 'a') as f:
                print(str(converted))
                f.writelines(str(converted) + "\n")
                count += 1
            print("The first" + str(count) + "Data write success...")
    except Exception as err:
        print(err)
Copy the code