source: osm/applications/editors/josm/plugins/imagery_offset_db/src/iodb/ImageryOffsetTools.java@ 28008

Last change on this file since 28008 was 28008, checked in by zverik, 12 years ago

Something works :)

File size: 4.0 KB
Line 
1package iodb;
2
3import java.util.HashMap;
4import java.util.List;
5import org.openstreetmap.josm.Main;
6import org.openstreetmap.josm.data.coor.EastNorth;
7import org.openstreetmap.josm.data.coor.LatLon;
8import org.openstreetmap.josm.data.projection.Projection;
9import org.openstreetmap.josm.gui.MapView;
10import org.openstreetmap.josm.gui.layer.ImageryLayer;
11
12/**
13 * Some common static methods for querying imagery layers.
14 *
15 * @author zverik
16 */
17public class ImageryOffsetTools {
18 private static HashMap<String, String> imageryAliases;
19
20 public static ImageryLayer getTopImageryLayer() {
21 List<ImageryLayer> layers = Main.map.mapView.getLayersOfType(ImageryLayer.class);
22 for( ImageryLayer layer : layers ) {
23 if( layer.isVisible() ) {
24 return layer;
25 }
26 }
27 return null;
28 }
29
30 private static LatLon getMapCenter() {
31 Projection proj = Main.getProjection();
32 return Main.map == null || Main.map.mapView == null
33 ? new LatLon(0, 0) : proj.eastNorth2latlon(Main.map.mapView.getCenter());
34 }
35
36 public static LatLon getLayerOffset( ImageryLayer layer, LatLon center ) {
37 Projection proj = Main.getProjection();
38 EastNorth offsetCenter = proj.latlon2eastNorth(center);
39 EastNorth centerOffset = offsetCenter.add(layer.getDx(), layer.getDy()); // todo: add or substract?
40 LatLon offsetLL = proj.eastNorth2latlon(centerOffset);
41 return offsetLL;
42 }
43
44 public static void applyLayerOffset( ImageryLayer layer, ImageryOffset offset ) {
45 Projection proj = Main.getProjection();
46 EastNorth center = proj.latlon2eastNorth(offset.getPosition());
47 EastNorth offsetPos = proj.latlon2eastNorth(offset.getImageryPos());
48 layer.setOffset(offsetPos.getX() - center.getX(), offsetPos.getY() - center.getY()); // todo: + or -?
49 }
50
51 public static String getImageryID( ImageryLayer layer ) {
52 if( layer == null )
53 return null;
54
55 String url = layer.getInfo().getUrl();
56 if( url == null )
57 return null;
58
59 if( imageryAliases == null )
60 loadImageryAliases();
61 for( String substr : imageryAliases.keySet() )
62 if( url.contains(substr) )
63 return imageryAliases.get(substr);
64
65 return url; // todo: strip parametric parts, etc
66 }
67
68 private static void loadImageryAliases() {
69 if( imageryAliases == null )
70 imageryAliases = new HashMap<String, String>();
71 else
72 imageryAliases.clear();
73
74 // { substring, alias }
75 imageryAliases.put("bing", "bing");
76 // todo: load from a resource?
77 }
78
79 // Following three methods were snatched from TMSLayer
80 private static double latToTileY(double lat, int zoom) {
81 double l = lat / 180 * Math.PI;
82 double pf = Math.log(Math.tan(l) + (1 / Math.cos(l)));
83 return Math.pow(2.0, zoom - 1) * (Math.PI - pf) / Math.PI;
84 }
85
86 private static double lonToTileX(double lon, int zoom) {
87 return Math.pow(2.0, zoom - 3) * (lon + 180.0) / 45.0;
88 }
89
90 public static int getCurrentZoom() {
91 if (Main.map == null || Main.map.mapView == null) {
92 return 1;
93 }
94 MapView mv = Main.map.mapView;
95 LatLon topLeft = mv.getLatLon(0, 0);
96 LatLon botRight = mv.getLatLon(mv.getWidth(), mv.getHeight());
97 double x1 = lonToTileX(topLeft.lon(), 1);
98 double y1 = latToTileY(topLeft.lat(), 1);
99 double x2 = lonToTileX(botRight.lon(), 1);
100 double y2 = latToTileY(botRight.lat(), 1);
101
102 int screenPixels = mv.getWidth() * mv.getHeight();
103 double tilePixels = Math.abs((y2 - y1) * (x2 - x1) * 256 * 256);
104 if (screenPixels == 0 || tilePixels == 0) {
105 return 1;
106 }
107 double factor = screenPixels / tilePixels;
108 double result = Math.log(factor) / Math.log(2) / 2 + 1;
109 int intResult = (int) Math.floor(result);
110 return intResult;
111 }
112}
Note: See TracBrowser for help on using the repository browser.