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