altoslib: Don't record 'pad' state in FlightSeries
[fw/altos] / altoslib / AltosMapTransform.java
1 /*
2  * Copyright © 2014 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 package org.altusmetrum.altoslib_11;
20
21 import java.io.*;
22 import java.lang.Math;
23 import java.util.*;
24 import java.util.concurrent.*;
25
26 public class AltosMapTransform {
27
28         double  scale_x, scale_y;
29
30         double  offset_x, offset_y;
31
32         public AltosLatLon lat_lon (AltosPointDouble point) {
33                 double lat, lon;
34                 double rads;
35
36                 lon = point.x/scale_x;
37                 rads = 2 * Math.atan(Math.exp(-point.y/scale_y));
38                 lat = Math.toDegrees(rads - Math.PI/2);
39
40                 return new AltosLatLon(lat,lon);
41         }
42
43         public AltosLatLon lat_lon (AltosPointInt point) {
44                 return lat_lon(new AltosPointDouble(point.x, point.y));
45         }
46
47         public AltosPointDouble screen_point(AltosPointInt screen) {
48                 return new AltosPointDouble(screen.x + offset_x, screen.y + offset_y);
49         }
50
51         public AltosPointDouble screen_point(AltosPointDouble screen) {
52                 return new AltosPointDouble(screen.x + offset_x, screen.y + offset_y);
53         }
54
55         public double hypot(AltosLatLon a, AltosLatLon b) {
56                 AltosPointDouble        a_pt = point(a);
57                 AltosPointDouble        b_pt = point(b);
58
59                 return Math.hypot(a_pt.x - b_pt.x, a_pt.y - b_pt.y);
60         }
61
62         public AltosLatLon screen_lat_lon(AltosPointInt screen) {
63                 return lat_lon(screen_point(screen));
64         }
65
66         public AltosLatLon screen_lat_lon(AltosPointDouble screen) {
67                 return lat_lon(screen_point(screen));
68         }
69
70         public AltosPointDouble point(AltosLatLon lat_lon) {
71                 double x, y;
72                 double e;
73
74                 x = lat_lon.lon * scale_x;
75
76                 e = Math.sin(Math.toRadians(lat_lon.lat));
77                 e = Math.max(e,-(1-1.0E-15));
78                 e = Math.min(e,  1-1.0E-15 );
79
80                 y = 0.5*Math.log((1+e)/(1-e))*-scale_y;
81
82                 return new AltosPointDouble(x, y);
83         }
84
85         public AltosPointDouble screen(AltosPointDouble point) {
86                 return new AltosPointDouble(point.x - offset_x, point.y - offset_y);
87         }
88
89         public AltosPointInt screen(AltosPointInt point) {
90                 return new AltosPointInt((int) (point.x - offset_x + 0.5),
91                                          (int) (point.y - offset_y + 0.5));
92         }
93
94         public AltosRectangle screen(AltosMapRectangle map_rect) {
95                 AltosPointDouble        ul = screen(map_rect.ul);
96                 AltosPointDouble        lr = screen(map_rect.lr);
97
98                 return new AltosRectangle((int) ul.x, (int) ul.y, (int) (lr.x - ul.x), (int) (lr.y - ul.y));
99         }
100
101         public AltosPointDouble screen(AltosLatLon lat_lon) {
102                 return screen(point(lat_lon));
103         }
104
105         private boolean has_location;
106
107         public boolean has_location() {
108                 return has_location;
109         }
110
111         public AltosMapTransform(int width, int height, int zoom, AltosLatLon centre_lat_lon) {
112                 scale_x = 256/360.0 * Math.pow(2, zoom);
113                 scale_y = 256/(2.0*Math.PI) * Math.pow(2, zoom);
114
115                 AltosPointDouble centre_pt = point(centre_lat_lon);
116
117                 has_location = (centre_lat_lon.lat != 0 || centre_lat_lon.lon != 0);
118                 offset_x = centre_pt.x - width / 2.0;
119                 offset_y = centre_pt.y - height / 2.0;
120         }
121
122         public static double lon_from_distance(double lat, double distance) {
123                 double c = AltosGreatCircle.earth_radius * Math.cos(lat * Math.PI / 180) * 2 * Math.PI;
124
125                 if (c < 10)
126                         return 0;
127                 return distance/c * 360.0;
128         }
129 }