forked from HowDoesExcelWork/RocketMap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransform.py
80 lines (67 loc) · 2.82 KB
/
transform.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
import math
import geopy
import geopy.distance
import random
a = 6378245.0
ee = 0.00669342162296594323
pi = 3.14159265358979324
def transform_from_wgs_to_gcj(latitude, longitude):
if is_location_out_of_china(latitude, longitude):
adjust_lat, adjust_lon = latitude, longitude
else:
adjust_lat = transform_lat(longitude - 105, latitude - 35.0)
adjust_lon = transform_long(longitude - 105, latitude - 35.0)
rad_lat = latitude / 180.0 * pi
magic = math.sin(rad_lat)
magic = 1 - ee * magic * magic
math.sqrt_magic = math.sqrt(magic)
adjust_lat = (adjust_lat *
180.0) / ((a * (1 - ee)) / (magic *
math.sqrt_magic) * pi)
adjust_lon = (adjust_lon *
180.0) / (a / math.sqrt_magic * math.cos(rad_lat) * pi)
adjust_lat += latitude
adjust_lon += longitude
# Print 'transfromed from ', wgs_loc, ' to ', adjust_loc.
return adjust_lat, adjust_lon
def is_location_out_of_china(latitude, longitude):
if (longitude < 72.004 or longitude > 137.8347 or
latitude < 0.8293 or latitude > 55.8271):
return True
return False
def transform_lat(x, y):
lat = (-100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y +
0.1 * x * y + 0.2 * math.sqrt(abs(x)))
lat += (20.0 * math.sin(6.0 * x * pi) + 20.0 *
math.sin(2.0 * x * pi)) * 2.0 / 3.0
lat += (20.0 * math.sin(y * pi) + 40.0 *
math.sin(y / 3.0 * pi)) * 2.0 / 3.0
lat += (160.0 * math.sin(y / 12.0 * pi) + 320 *
math.sin(y * pi / 30.0)) * 2.0 / 3.0
return lat
def transform_long(x, y):
lon = (300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y +
0.1 * math.sqrt(abs(x)))
lon += (20.0 * math.sin(6.0 * x * pi) + 20.0 *
math.sin(2.0 * x * pi)) * 2.0 / 3.0
lon += (20.0 * math.sin(x * pi) + 40.0 *
math.sin(x / 3.0 * pi)) * 2.0 / 3.0
lon += (150.0 * math.sin(x / 12.0 * pi) + 300.0 *
math.sin(x / 30.0 * pi)) * 2.0 / 3.0
return lon
def get_new_coords(init_loc, distance, bearing):
"""
Given an initial lat/lng, a distance(in kms), and a bearing (degrees),
this will calculate the resulting lat/lng coordinates.
"""
origin = geopy.Point(init_loc[0], init_loc[1])
destination = geopy.distance.distance(kilometers=distance).destination(
origin, bearing)
return (destination.latitude, destination.longitude)
# Apply a location jitter.
def jitter_location(location=None, maxMeters=10):
origin = geopy.Point(location[0], location[1])
b = random.randint(0, 360)
d = math.sqrt(random.random()) * (float(maxMeters) / 1000)
destination = geopy.distance.distance(kilometers=d).destination(origin, b)
return (destination.latitude, destination.longitude, location[2])