1 | #!/usr/bin/python |
---|
2 | #----------------------------------------------------------------------------- |
---|
3 | # Projection code (lat/long to screen conversions) |
---|
4 | # |
---|
5 | # Usage: |
---|
6 | # (library code for pyroute GUI, not for direct use) |
---|
7 | #----------------------------------------------------------------------------- |
---|
8 | # Copyright 2007, Oliver White |
---|
9 | # |
---|
10 | # This program is free software: you can redistribute it and/or modify |
---|
11 | # it under the terms of the GNU General Public License as published by |
---|
12 | # the Free Software Foundation, either version 3 of the License, or |
---|
13 | # (at your option) any later version. |
---|
14 | # |
---|
15 | # This program is distributed in the hope that it will be useful, |
---|
16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
18 | # GNU General Public License for more details. |
---|
19 | # |
---|
20 | # You should have received a copy of the GNU General Public License |
---|
21 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
22 | #----------------------------------------------------------------------------- |
---|
23 | |
---|
24 | class Projection: |
---|
25 | def __init__(self): |
---|
26 | self.xyValid = 0 |
---|
27 | self.llValid = 0 |
---|
28 | def isValid(self): |
---|
29 | return(self.xyValid and self.llValid) |
---|
30 | def setView(self,x,y,w,h): |
---|
31 | self.w = w / 2 |
---|
32 | self.h = h / 2 |
---|
33 | self.xc = x + self.w |
---|
34 | self.yc = y + self.h |
---|
35 | self.xyValid = 1 |
---|
36 | def recentre(self,lat,lon,scale = None): |
---|
37 | self.lat = lat |
---|
38 | self.lon = lon |
---|
39 | if(scale != None): |
---|
40 | self.scale = scale # TODO: scale and scaleCosLat |
---|
41 | self.findEdges() |
---|
42 | self.llValid = 1 |
---|
43 | def findEdges(self): |
---|
44 | """(S,W,E,N) are derived from (lat,lon,scale)""" |
---|
45 | self.S = self.lat - self.scale |
---|
46 | self.N = self.lat + self.scale |
---|
47 | self.W = self.lon - self.scale |
---|
48 | self.E = self.lon + self.scale |
---|
49 | def nudge(self,dx,dy,scale): |
---|
50 | self.lat = self.lat + dy * scale * self.scale |
---|
51 | self.lon = self.lon + dx * scale * self.scale |
---|
52 | self.findEdges() |
---|
53 | def nudgeZoom(self,amount): |
---|
54 | self.scale = self.scale * (1 + amount) |
---|
55 | self.findEdges() |
---|
56 | def ll2xy(self,lat,lon): |
---|
57 | px = (lon - self.lon) / self.scale |
---|
58 | py = (lat - self.lat) / self.scale |
---|
59 | x = self.xc + self.w * px |
---|
60 | y = self.yc - self.h * py |
---|
61 | return(x,y) |
---|
62 | def xy2ll(self,x,y): |
---|
63 | px = (x - self.xc) / self.w |
---|
64 | py = (y - self.yc) / self.h |
---|
65 | lon = self.lon + px * self.scale |
---|
66 | lat = self.lat - py * self.scale |
---|
67 | return(lat,lon) |
---|
68 | def onscreen(self,x,y): |
---|
69 | return(x >= 0 and x < 2 * self.w and y >= 0 and y < 2 * self.h) |
---|
70 | def relXY(self,x,y): |
---|
71 | return(x/(2*self.w), y/(2*self.h)) |
---|
72 | |
---|