forked from ankanban/Pokemon_Go_API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocation.py
131 lines (111 loc) · 3.68 KB
/
location.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import pokemon_pb2
import base64
import struct
import config
import math
from math import radians, cos, sin, asin, sqrt
from geopy.distance import vincenty
from geopy.geocoders import GoogleV3
COORDS_LATITUDE = 0
COORDS_LONGITUDE = 0
COORDS_ALTITUDE = 0
FLOAT_LAT = 0
FLOAT_LONG = 0
def get_location_coords():
return (COORDS_LATITUDE, COORDS_LONGITUDE, COORDS_ALTITUDE)
def get_lat():
return COORDS_LATITUDE
def get_lot():
return COORDS_LONGITUDE
def set_location(location_name):
geolocator = GoogleV3()
loc = geolocator.geocode(location_name)
print('[!] Your given location: {}'.format(loc.address.encode('utf-8')))
set_location_coords(loc.latitude, loc.longitude, loc.altitude)
def set_location_coords(lat, long, alt):
if config.debug:
print('[!] lat/long/alt: {} {} {}'.format(lat, long, alt))
global COORDS_LATITUDE, COORDS_LONGITUDE, COORDS_ALTITUDE
global FLOAT_LAT, FLOAT_LONG
FLOAT_LAT = lat
FLOAT_LONG = long
COORDS_LATITUDE = f2i(lat)
COORDS_LONGITUDE = f2i(long)
COORDS_ALTITUDE = f2i(alt)
def i2f(int):
return struct.unpack('<Q', struct.pack('<d', int))[0]
def f2h(float):
return hex(struct.unpack('<Q', struct.pack('<d', float))[0])
def f2i(float):
return struct.unpack('<Q', struct.pack('<d', float))[0]
def l2f(float):
return struct.unpack('d', struct.pack('Q', int(bin(float), 0)))[0]
def h2f(hex):
return struct.unpack('<d', struct.pack('<Q', int(hex,16)))[0]
def get_near(map):
ms=[]
for cell in [map]:
for block in cell.b:
for obj in block.c:
for stop in obj.s:
if distance(stop.lat,stop.lon,COORDS_LATITUDE,COORDS_LONGITUDE):
ms.append((stop.name,stop.lat,stop.lon,get_distance(stop.lat,stop.lon,COORDS_LATITUDE,COORDS_LONGITUDE)))
return ms
def get_near_p(map):
ms=[]
for cell in [map]:
for block in cell.b:
for obj in block.c:
for stop in obj.p:
if distance(stop.lat,stop.lon,COORDS_LATITUDE,COORDS_LONGITUDE):
ms.append((stop.t.type,stop.lat,stop.lon,stop.name,stop.hash,get_distance(stop.lat,stop.lon,COORDS_LATITUDE,COORDS_LONGITUDE)))
return ms
def distance(lat1, lon1,lat2, lon2):
lat1=l2f(lat1)
lon1=l2f(lon1)
lat2=l2f(lat2)
lon2=l2f(lon2)
radius = 6371 # km *1000 m
dlat = math.radians(lat2-lat1)
dlon = math.radians(lon2-lon1)
a = math.sin(dlat/2) * math.sin(dlat/2) + math.cos(math.radians(lat1)) \
* math.cos(math.radians(lat2)) * math.sin(dlon/2) * math.sin(dlon/2)
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
d = radius * c * 1000
return d<config.distance
def get_distance(lat1, lon1,lat2, lon2):
lat1=l2f(lat1)
lon1=l2f(lon1)
lat2=l2f(lat2)
lon2=l2f(lon2)
radius = 6371 # km *1000 m
dlat = math.radians(lat2-lat1)
dlon = math.radians(lon2-lon1)
a = math.sin(dlat/2) * math.sin(dlat/2) + math.cos(math.radians(lat1)) \
* math.cos(math.radians(lat2)) * math.sin(dlon/2) * math.sin(dlon/2)
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
d = radius * c * 1000
return d
def haversine(lon1, lat1, lon2, lat2):
lat1=l2f(lat1)
lon1=l2f(lon1)
lat2=l2f(lat2)
lon2=l2f(lon2)
"""
Calculate the great circle distance between two points
on the earth (specified in decimal degrees)
"""
# convert decimal degrees to radians
lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
# haversine formula
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
c = 2 * asin(sqrt(a))
r = 6371 # Radius of earth in kilometers. Use 3956 for miles
return c * r * 1000
def is_near(locx,locy,myx,myy):
tmp1 = (l2f(locx), l2f(locy))
tmp2 = (l2f(myx), l2f(myy))
res=vincenty(tmp1, tmp2).meters
return res<config.distance