1 | #!/usr/bin/python |
---|
2 | #---------------------------------------------------------------------------- |
---|
3 | # Describes the feature you're standing on (road, rail, etc) |
---|
4 | #---------------------------------------------------------------------------- |
---|
5 | # Copyright 2008, Oliver White |
---|
6 | # |
---|
7 | # This program is free software: you can redistribute it and/or modify |
---|
8 | # it under the terms of the GNU General Public License as published by |
---|
9 | # the Free Software Foundation, either version 3 of the License, or |
---|
10 | # (at your option) any later version. |
---|
11 | # |
---|
12 | # This program is distributed in the hope that it will be useful, |
---|
13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | # GNU General Public License for more details. |
---|
16 | # |
---|
17 | # You should have received a copy of the GNU General Public License |
---|
18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
19 | #--------------------------------------------------------------------------- |
---|
20 | from parseOsm import parseOsm |
---|
21 | import tiledata |
---|
22 | from tilenames import * |
---|
23 | import sys |
---|
24 | |
---|
25 | def distancePointToLine(x,y,x1,y1,x2,y2): |
---|
26 | """http://www.allegro.cc/forums/thread/589720""" |
---|
27 | A = x - x1 |
---|
28 | B = y - y1 |
---|
29 | C = x2 - x1 |
---|
30 | D = y2 - y1 |
---|
31 | |
---|
32 | dot = A * C + B * D |
---|
33 | len_sq = C * C + D * D |
---|
34 | if len_sq==0: |
---|
35 | dist= A*A +B*B |
---|
36 | return(dist) |
---|
37 | |
---|
38 | param = dot / len_sq |
---|
39 | |
---|
40 | if(param < 0): |
---|
41 | xx = x1 |
---|
42 | yy = y1 |
---|
43 | elif(param > 1): |
---|
44 | xx = x2 |
---|
45 | yy = y2 |
---|
46 | else: |
---|
47 | xx = x1 + param * C |
---|
48 | yy = y1 + param * D |
---|
49 | |
---|
50 | dx = x - xx |
---|
51 | dy = y - yy |
---|
52 | dist = dx * dx + dy * dy |
---|
53 | return(dist) |
---|
54 | |
---|
55 | def describe(lat,lon): |
---|
56 | """Find the way you're nearest to, and return a description of it""" |
---|
57 | (sx,sy) = latlon2relativeXY(lat,lon) |
---|
58 | |
---|
59 | # download local data |
---|
60 | z = tiledata.DownloadLevel() |
---|
61 | (x,y) = tileXY(lat, lon, z) |
---|
62 | filename = tiledata.GetOsmTileData(z,x,y) |
---|
63 | |
---|
64 | # load into memory |
---|
65 | a = parseOsm(filename) |
---|
66 | |
---|
67 | # look for nearest way |
---|
68 | (mindist, name) = (1E+10, "not found") |
---|
69 | for w in a.ways.values(): |
---|
70 | (lastx,lasty,lastvalid) = (0,0,False) |
---|
71 | for n in w['n']: # loop nodes in way |
---|
72 | (x,y) = (n['lon'], n['lat']) |
---|
73 | if(lastvalid): |
---|
74 | distance = distancePointToLine(sx,sy,lastx,lasty,x,y) |
---|
75 | if(distance < mindist): |
---|
76 | tempname = w['t'].get('name', w['t'].get('ref', None)) |
---|
77 | if(tempname != None): |
---|
78 | mindist = distance |
---|
79 | name=tempname |
---|
80 | (lastx,lasty,lastvalid) = (x,y,True) |
---|
81 | return(name) |
---|
82 | |
---|
83 | if(__name__ == "__main__"): |
---|
84 | if(len(sys.argv) >= 3): |
---|
85 | print describe(float(sys.argv[1]), float(sys.argv[2])) |
---|
86 | else: |
---|
87 | print "no position supplied, using default" |
---|
88 | print describe(51.678935, -0.826256) |
---|