import json
import exifread
import requests
class Search:
def __init__(self):
self.url = """http://restapi.amap.com/v3/geocode/regeo?key={key}
&location={longitude},{latitude}&poitype=&radius=&xtensions=&batch=false&roadlevel=0"""
self.key = '' # 需要去高德获取
self.headers = {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/79.0.3945.88 Safari/537.36",
}
self.longitude = None # 经度
self.lg_float = None # 经度小数
self.latitude = None # 纬度
self.lt_float = None # 纬度小数
def get_gps_by_photo(self, ptah):
"""
读取照片中 GPS 信息
@param ptah@ 获取照片的本地路径
@return@
"""
with open(ptah, 'rb') as f:
contents = exifread.process_file(f)
for key in contents:
if key == "GPS GPSLongitude":
self.longitude = self.create_string(contents[key].values)
self.lg_float = self.transform_float(contents[key].values, contents['GPS GPSLatitudeRef'])
# print("经度 =", self.longitude, contents['GPS GPSLatitudeRef'])
print("经度小数 =", self.lg_float)
elif key == "GPS GPSLatitude":
self.latitude = self.create_string(contents[key].values)
self.lt_float = self.transform_float(contents[key].values, contents['GPS GPSLatitudeRef'])
# print("纬度 =", self.latitude, contents['GPS GPSLongitudeRef'])
print("纬度小数 =", self.lt_float)
# if self.longitude is not None or self.latitude is not None:
# print('+' + self.latitude + ',' + '+' + self.longitude)
self.get_address(self.lg_float, self.lt_float)
def create_string(self, content):
"""
拼接计算经纬度
@param content@
@return@ /d°/d′/d″
"""
one = str(content[0]) + '°'
two = str(content[1]) + '′'
three = str(float(str(content[2]).split('/')[0]) / float(str(content[2]).split('/')[1])) + '″'
return one + two + three
def transform_float(self, content, direction):
"""
将经纬度转化为小数
@param content@
@return@ float
"""
first = content[0].num
middle = content[1].num / 60
last = (float(str(content[2]).split('/')[0]) / float(str(content[2]).split('/')[1]) / 3600)
return (first + middle + last) * (-1 if direction in ('W', 'S') else 1)
def get_address(self, longitude, latitude):
"""
请求高德逆地理编码
@param longitude@
@param latitude@
@return@
"""
response = requests.get(url=self.url.format(key=self.key,
longitude=longitude,
latitude=latitude), headers=self.headers)
if response.content:
result = json.loads(response.text)
print('地址:{address}'.format(address=result['regeocode']['formatted_address']))
else:
print('没有响应!')
if __name__ == '__main__':
s = Search()
s.get_gps_by_photo("D:/GPS_position.jpg")