Skip to content

Commit d99ef05

Browse files
committed
使用Python定位到女朋友的位置
1 parent 71f64e6 commit d99ef05

File tree

13 files changed

+667
-1
lines changed

13 files changed

+667
-1
lines changed

.DS_Store

2 KB
Binary file not shown.

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,8 @@
4141

4242
## Node.js 爬虫
4343

44-
* [使用 puppeteer 爬取简书文章并保存到本地](./js/jian_shu.js)
44+
* [使用 puppeteer 爬取简书文章并保存到本地](./js/jian_shu.js)
45+
46+
## 其他
47+
48+
* [使用 Python 定位到女朋友的位置](./获取女友的位置)

获取女友的位置/.DS_Store

8 KB
Binary file not shown.

获取女友的位置/.idea/inspectionProfiles/Project_Default.xml

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

获取女友的位置/.idea/misc.xml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

获取女友的位置/.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

获取女友的位置/.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

获取女友的位置/.idea/workspace.xml

Lines changed: 340 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

获取女友的位置/.idea/地理位置.iml

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

获取女友的位置/main.py

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
#!/usr/bin/env python
2+
# encoding: utf-8
3+
4+
"""
5+
@version: v1.0
6+
@author: xag
7+
@license: Apache Licence
8+
9+
@site: http://www.xingag.top
10+
@software: PyCharm
11+
@file: meta_picture.py.py
12+
@time: 2019-08-23 16:23
13+
@description:高德坐标拾取网站:https://lbs.amap.com/console/show/picker
14+
"""
15+
16+
import os
17+
import exifread
18+
from decimal import Decimal
19+
from position_utils import *
20+
import requests
21+
import json
22+
import datetime
23+
24+
25+
# pip3 install exifread
26+
27+
28+
class Location(object):
29+
30+
def __init__(self, image_path):
31+
self.img_path = image_path
32+
33+
self.api_key = "你申请的AK"
34+
35+
self.url_get_position = 'https://restapi.amap.com/v3/geocode/regeo?key={}&location={}'
36+
37+
def run(self):
38+
coordinate = self.__get_image_ability()
39+
40+
print(f'获取到经度、纬度是:{coordinate}')
41+
42+
if not coordinate:
43+
return
44+
45+
# 根据经度和纬度,获取到详细地址
46+
address = self.__get_address(coordinate)
47+
48+
# 检验坐标值
49+
# https://lbs.amap.com/console/show/picker
50+
print(f'你女朋友当前位置在:{address}')
51+
52+
def __get_address(self, location):
53+
"""
54+
根据坐标得到详细地址
55+
:param location: 经纬度值
56+
:return:
57+
"""
58+
resp = requests.get(self.url_get_position.format(self.api_key, location))
59+
60+
location_data = json.loads(resp.text)
61+
62+
address = location_data.get('regeocode').get('formatted_address')
63+
64+
return address
65+
66+
def __format_lati_long_data(self, data):
67+
"""
68+
对经度和纬度数据做处理,保留6位小数
69+
:param data: 原始经度和纬度值
70+
:return:
71+
"""
72+
# 删除左右括号和空格
73+
data_list_tmp = str(data).replace('[', '').replace(']', '').split(',')
74+
data_list = [data.strip() for data in data_list_tmp]
75+
76+
# 替换秒的值
77+
data_tmp = data_list[-1].split('/')
78+
79+
# 秒的值
80+
data_sec = int(data_tmp[0]) / int(data_tmp[1]) / 3600
81+
82+
# 替换分的值
83+
data_tmp = data_list[-2]
84+
85+
# 分的值
86+
data_minute = int(data_tmp) / 60
87+
88+
# 度的值
89+
data_degree = int(data_list[0])
90+
91+
# 由于高德API只能识别到小数点后的6位
92+
# 需要转换为浮点数,并保留为6位小数
93+
result = "%.6f" % (data_degree + data_minute + data_sec)
94+
return float(result)
95+
96+
def __get_image_ability(self):
97+
"""
98+
获取图片的属性值,包含:经纬度、拍摄时间等
99+
:param picture_name:
100+
:return:
101+
"""
102+
103+
# 利用exifread库,读取图片的属性
104+
img_exif = exifread.process_file(open(self.img_path, 'rb'))
105+
106+
# 能够读取到属性
107+
if img_exif:
108+
# 纬度数
109+
latitude_gps = img_exif['GPS GPSLatitude']
110+
111+
# N,S 南北纬方向
112+
latitude_direction = img_exif['GPS GPSLatitudeRef']
113+
114+
# 经度数
115+
longitude_gps = img_exif['GPS GPSLongitude']
116+
117+
# E,W 东西经方向
118+
longitude_direction = img_exif['GPS GPSLongitudeRef']
119+
120+
# 拍摄时间
121+
take_time = img_exif['EXIF DateTimeOriginal']
122+
123+
is_lie = self.judge_time_met(take_time)
124+
125+
if is_lie:
126+
print('很遗憾的通知你,你的女朋友在撒谎!!!')
127+
return
128+
129+
# 纬度、经度、拍摄时间
130+
if latitude_gps and longitude_gps and take_time:
131+
132+
# 对纬度、经度值原始值作进一步的处理
133+
latitude = self.__format_lati_long_data(latitude_gps)
134+
longitude = self.__format_lati_long_data(longitude_gps)
135+
136+
# print(f'{longitude},{latitude}')
137+
138+
# 注意:由于gps获取的坐标在国内高德等主流地图上逆编码不够精确,这里需要转换为火星坐标系
139+
location = wgs84togcj02(longitude, latitude)
140+
141+
return f'{location[0]},{location[1]}'
142+
else:
143+
print(f'获取的图片数据属性不完整')
144+
return ''
145+
else:
146+
print('抱歉,图片不是原图,没法获取到图片属性。')
147+
return ''
148+
149+
def judge_time_met(self, take_time):
150+
"""
151+
判断拍摄时间是否是在今天
152+
:param take_time:
153+
:return:
154+
"""
155+
# 拍摄时间
156+
format_time = str(take_time).split(" ")[0].replace(":", "-")
157+
158+
# 当天日期
159+
today = str(datetime.date.today())
160+
161+
if format_time == today:
162+
return True
163+
else:
164+
return False
165+
166+
167+
if __name__ == '__main__':
168+
# 女朋友发过来的图片【原图】
169+
location = Location('./picture/11441566648796_.pic_hd.jpg')
170+
171+
# 找到女朋友的地理位置
172+
location.run()

0 commit comments

Comments
 (0)