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