2 * Copyright © 2014 Keith Packard <keithp@keithp.com>
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.
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.
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.
18 package org.altusmetrum.altosuilib_2;
21 import java.awt.event.*;
24 import java.lang.Math;
25 import java.awt.geom.*;
27 import java.util.concurrent.*;
28 import org.altusmetrum.altoslib_4.*;
30 public class AltosUIMapTransform {
32 double scale_x, scale_y;
34 double offset_x, offset_y;
36 public AltosUILatLon lat_lon (Point2D.Double point) {
40 lon = point.x/scale_x;
41 rads = 2 * Math.atan(Math.exp(-point.y/scale_y));
42 lat = Math.toDegrees(rads - Math.PI/2);
44 return new AltosUILatLon(lat,lon);
47 public Point2D.Double screen_point(Point screen) {
48 return new Point2D.Double(screen.x + offset_x, screen.y + offset_y);
51 public AltosUILatLon screen_lat_lon(Point screen) {
52 return lat_lon(screen_point(screen));
55 public Point2D.Double point(AltosUILatLon lat_lon) {
59 x = lat_lon.lon * scale_x;
61 e = Math.sin(Math.toRadians(lat_lon.lat));
62 e = Math.max(e,-(1-1.0E-15));
63 e = Math.min(e, 1-1.0E-15 );
65 y = 0.5*Math.log((1+e)/(1-e))*-scale_y;
67 return new Point2D.Double(x, y);
70 public Point2D.Double screen(Point2D.Double point) {
71 return new Point2D.Double(point.x - offset_x, point.y - offset_y);
74 public Point screen(Point point) {
75 return new Point((int) (point.x - offset_x + 0.5),
76 (int) (point.y - offset_y + 0.5));
79 public Rectangle screen(AltosUIMapRectangle map_rect) {
80 Point2D.Double ul = screen(map_rect.ul);
81 Point2D.Double lr = screen(map_rect.lr);
83 return new Rectangle((int) ul.x, (int) ul.y, (int) (lr.x - ul.x), (int) (lr.y - ul.y));
86 public Point2D.Double screen(AltosUILatLon lat_lon) {
87 return screen(point(lat_lon));
90 private boolean has_location;
92 public boolean has_location() {
96 public AltosUIMapTransform(int width, int height, int zoom, AltosUILatLon centre_lat_lon) {
97 scale_x = 256/360.0 * Math.pow(2, zoom);
98 scale_y = 256/(2.0*Math.PI) * Math.pow(2, zoom);
100 Point2D.Double centre_pt = point(centre_lat_lon);
102 has_location = (centre_lat_lon.lat != 0 || centre_lat_lon.lon != 0);
103 offset_x = centre_pt.x - width / 2.0;
104 offset_y = centre_pt.y - height / 2.0;