需要原料:一张原图
引入需要的模块
import exifread # 引入图片读取模块
import math # 运算模块
import requests # api请求
import json # 处理api返回的json
获取图片元数据
转换高德的坐标轴
img_exif = exifread.process_file(open("你家boss原图.jpg", 'rb')) #读取你的图片
if img_exif:
latitude_gps = img_exif['GPS GPSLatitude']
latitude_direction = img_exif['GPS GPSLatitudeRef']
longitude_gps = img_exif['GPS GPSLongitude']
longitude_direction = img_exif['GPS GPSLongitudeRef']
take_time = img_exif['EXIF DateTimeOriginal']
# 输出看一看,有没有,没有就完犊子搞不了,好人一生平安
print(latitude_gps)
print(latitude_direction)
print(longitude_gps)
print(longitude_direction)
print(take_time)
#一些必要的参数
x_pi = 3.14159265358979324 * 3000.0 / 180.0
pi = 3.14159265358979 # π
a = 6378245.0 # 长半轴
ee = 0.00669342162296594323 # 地球扁率
def out_of_china(lng, lat):
if lng < 72.004 or lng > 137.8347 or lat < 0.8293 or lat > 55.8271:
return 1
#坐标轴转换
def wgs84togcj02(lng, lat): # WGS84转GCJ02(火星坐标系)
if out_of_china(lng, lat) == 1: # 判断是否在国内
return lng, lat
dlat = transformLat(lat - 35.0, lng - 105.0 )
dlng = transformLon(lat - 35.0, lng - 105.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 format_lati_long_data(data):# 删除左右括号和空格
data_list_tmp = str(data).replace('[', '').replace(']', '').split(',')
data_list = [data.strip() for data in data_list_tmp]# 替换秒的值
data_tmp = data_list[-1].split('/')# 秒的值
data_sec = int(data_tmp[0]) / int(data_tmp[1]) / 3600# 替换分的值
data_tmp = data_list[-2]# 分的值
data_minute = int(data_tmp) / 60# 度的值
data_degree = int(data_list[0])
result = "%.6f" % (data_degree + data_minute + data_sec)#保留小数点后六位,高德可识别
return float(result)
def transformLat(lat,lon):
ret = -100.0 + 2.0 * lat + 3.0 * lon + 0.2 * lon * lon + 0.1 * lat * lon +0.2 * math.sqrt(abs(lat))
ret += (20.0 * math.sin(6.0 * lat * math.pi) + 20.0 * math.sin(2.0 * lat * math.pi)) * 2.0 / 3.0
ret += (20.0 * math.sin(lon * math.pi) + 40.0 * math.sin(lon / 3.0 * math.pi)) * 2.0 / 3.0
ret += (160.0 * math.sin(lon / 12.0 * math.pi) + 320 * math.sin(lon * math.pi / 30.0)) * 2.0 / 3.0
return ret
def transformLon(lat,lon):
ret = 300.0 + lat + 2.0 * lon + 0.1 * lat * lat + 0.1 * lat * lon + 0.1 * math.sqrt(abs(lat))
ret += (20.0 * math.sin(6.0 * lat * math.pi) + 20.0 * math.sin(2.0 * lat * math.pi)) * 2.0 / 3.0
ret += (20.0 * math.sin(lat * math.pi) + 40.0 * math.sin(lat / 3.0 * math.pi)) * 2.0 / 3.0
ret += (150.0 * math.sin(lat / 12.0 * math.pi) + 300.0 * math.sin(lat / 30.0 * math.pi)) * 2.0 / 3.0
return ret
转换脚本代码借鉴:宣哥(https://blog.csdn.net/pashine/article/details/81283390)
def get_address(location):
urls = "https://restapi.amap.com/v3/geocode/regeo?key=你自己的高德KEY="+ str(lon6) + "," +str(lat6) +"&poitype=&radius=1000&extensions=all&batch=false&roadlevel=0"
resp = requests.get(urls)
location_data = json.loads(resp.text)
address = location_data.get('regeocode').get('formatted_address')
return address
高德api:
主程序:
longitude = format_lati_long_data(longitude_gps)
latitude = format_lati_long_data(latitude_gps )
location = wgs84togcj02(longitude, latitude)
print(location)
lon6 = "%.6f" % location[0]#高德保留六位小数位点
lat6 = "%.6f" % location[1]
print(str(lat6))
print(get_address(location))
看来boss去打球去了,可她平时不打球啊,新的爱好吧!