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.altoslib_7;
21 import java.lang.Math;
23 import java.util.concurrent.*;
25 public class AltosMapTransform {
27 double scale_x, scale_y;
29 double offset_x, offset_y;
31 public AltosLatLon lat_lon (AltosPointDouble point) {
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);
39 return new AltosLatLon(lat,lon);
42 public AltosPointDouble screen_point(AltosPointInt screen) {
43 return new AltosPointDouble(screen.x + offset_x, screen.y + offset_y);
46 public AltosPointDouble screen_point(AltosPointDouble screen) {
47 return new AltosPointDouble(screen.x + offset_x, screen.y + offset_y);
50 public AltosLatLon screen_lat_lon(AltosPointInt screen) {
51 return lat_lon(screen_point(screen));
54 public AltosLatLon screen_lat_lon(AltosPointDouble screen) {
55 return lat_lon(screen_point(screen));
58 public AltosPointDouble point(AltosLatLon lat_lon) {
62 x = lat_lon.lon * scale_x;
64 e = Math.sin(Math.toRadians(lat_lon.lat));
65 e = Math.max(e,-(1-1.0E-15));
66 e = Math.min(e, 1-1.0E-15 );
68 y = 0.5*Math.log((1+e)/(1-e))*-scale_y;
70 return new AltosPointDouble(x, y);
73 public AltosPointDouble screen(AltosPointDouble point) {
74 return new AltosPointDouble(point.x - offset_x, point.y - offset_y);
77 public AltosPointInt screen(AltosPointInt point) {
78 return new AltosPointInt((int) (point.x - offset_x + 0.5),
79 (int) (point.y - offset_y + 0.5));
82 public AltosRectangle screen(AltosMapRectangle map_rect) {
83 AltosPointDouble ul = screen(map_rect.ul);
84 AltosPointDouble lr = screen(map_rect.lr);
86 return new AltosRectangle((int) ul.x, (int) ul.y, (int) (lr.x - ul.x), (int) (lr.y - ul.y));
89 public AltosPointDouble screen(AltosLatLon lat_lon) {
90 return screen(point(lat_lon));
93 private boolean has_location;
95 public boolean has_location() {
99 public AltosMapTransform(int width, int height, int zoom, AltosLatLon centre_lat_lon) {
100 scale_x = 256/360.0 * Math.pow(2, zoom);
101 scale_y = 256/(2.0*Math.PI) * Math.pow(2, zoom);
103 AltosPointDouble centre_pt = point(centre_lat_lon);
105 has_location = (centre_lat_lon.lat != 0 || centre_lat_lon.lon != 0);
106 offset_x = centre_pt.x - width / 2.0;
107 offset_y = centre_pt.y - height / 2.0;