aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'ingrex/utils.py')
-rwxr-xr-xingrex/utils.py80
1 files changed, 80 insertions, 0 deletions
diff --git a/ingrex/utils.py b/ingrex/utils.py
new file mode 100755
index 0000000..8090f15
--- /dev/null
+++ b/ingrex/utils.py
@@ -0,0 +1,80 @@
1"Map Utils"
2from math import pi, sin, cos, tan, asin, radians, sqrt, log
3
4def calc_tile(lng, lat, zoomlevel):
5 tilecounts = [1,1,1,40,40,80,80,320,1E3,2E3,2E3,4E3,8E3,16E3,16E3,32E3]
6 rlat = radians(lat)
7 tilecount = tilecounts[zoomlevel]
8 xtile = int((lng + 180.0) / 360.0 * tilecount)
9 ytile = int((1.0 - log(tan(rlat) + (1 / cos(rlat))) / pi) / 2.0 * tilecount)
10 return xtile, ytile
11
12def calc_dist(lat1, lng1, lat2, lng2):
13 lat1, lng1, lat2, lng2 = map(radians, [lat1, lng1, lat2, lng2])
14 dlat = lat1 - lat2
15 dlng = lng1 - lng2
16 a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlng/2)**2
17 c = 2* asin(sqrt(a))
18 m = 6367.0 * c * 1000
19 return m
20
21def point_in_poly(x, y, poly):
22 n = len(poly)
23 inside = False
24 p1x,p1y = poly[0]
25 for i in range(n+1):
26 p2x,p2y = poly[i % n]
27 if y > min(p1y, p2y):
28 if y <= max(p1y, p2y):
29 if x <= max(p1x, p2x):
30 if p1y != p2y:
31 xints = (y - p1y) * (p2x - p1x) / (p2y - p1y) + p1x
32 if p1x == p2x or x <= xints:
33 inside = not inside
34 p1x,p1y = p2x,p2y
35 return inside
36
37def transform(wgLat, wgLon):
38 """
39 transform(latitude,longitude) , WGS84
40 return (latitude,longitude) , GCJ02
41 """
42 a = 6378245.0
43 ee = 0.00669342162296594323
44 if (outOfChina(wgLat, wgLon)):
45 mgLat = wgLat
46 mgLon = wgLon
47 return mgLat,mgLon
48 dLat = transformLat(wgLon - 105.0, wgLat - 35.0)
49 dLon = transformLon(wgLon - 105.0, wgLat - 35.0)
50 radLat = wgLat / 180.0 * pi
51 magic = sin(radLat)
52 magic = 1 - ee * magic * magic
53 sqrtMagic = sqrt(magic)
54 dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi)
55 dLon = (dLon * 180.0) / (a / sqrtMagic * cos(radLat) * pi)
56 mgLat = wgLat + dLat
57 mgLon = wgLon + dLon
58 return mgLat,mgLon
59
60def outOfChina(lat, lon):
61 if (lon < 72.004 or lon > 137.8347):
62 return True
63 if (lat < 0.8293 or lat > 55.8271):
64 return True
65 return False
66
67def transformLat(x, y):
68 ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * sqrt(abs(x))
69 ret += (20.0 * sin(6.0 * x * pi) + 20.0 * sin(2.0 * x * pi)) * 2.0 / 3.0
70 ret += (20.0 * sin(y * pi) + 40.0 * sin(y / 3.0 * pi)) * 2.0 / 3.0
71 ret += (160.0 * sin(y / 12.0 * pi) + 320 * sin(y * pi / 30.0)) * 2.0 / 3.0
72 return ret
73
74def transformLon(x, y):
75 ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * sqrt(abs(x))
76 ret += (20.0 * sin(6.0 * x * pi) + 20.0 * sin(2.0 * x * pi)) * 2.0 / 3.0
77 ret += (20.0 * sin(x * pi) + 40.0 * sin(x / 3.0 * pi)) * 2.0 / 3.0
78 ret += (150.0 * sin(x / 12.0 * pi) + 300.0 * sin(x / 30.0 * pi)) * 2.0 / 3.0
79 return ret
80
Powered by cgit v1.2.3 (git 2.41.0)