source: josm/trunk/src/org/openstreetmap/josm/gui/layer/imagery/TileCoordinateConverter.java@ 11845

Last change on this file since 11845 was 11845, checked in by bastiK, 7 years ago

see #7427 - better return type

  • Property svn:eol-style set to native
File size: 6.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.layer.imagery;
3
4import java.awt.Shape;
5import java.awt.geom.Path2D;
6import java.awt.geom.Point2D;
7import java.awt.geom.Rectangle2D;
8
9import org.openstreetmap.gui.jmapviewer.Tile;
10import org.openstreetmap.gui.jmapviewer.TileAnchor;
11import org.openstreetmap.gui.jmapviewer.TileXY;
12import org.openstreetmap.gui.jmapviewer.interfaces.ICoordinate;
13import org.openstreetmap.gui.jmapviewer.interfaces.IProjected;
14import org.openstreetmap.gui.jmapviewer.interfaces.TileSource;
15import org.openstreetmap.josm.data.coor.EastNorth;
16import org.openstreetmap.josm.data.coor.LatLon;
17import org.openstreetmap.josm.data.projection.Projecting;
18import org.openstreetmap.josm.data.projection.ShiftedProjecting;
19import org.openstreetmap.josm.gui.MapView;
20import org.openstreetmap.josm.gui.MapViewState.MapViewPoint;
21
22/**
23 * This class handles tile coordinate management and computes their position in the map view.
24 * @author Michael Zangl
25 * @since 10651
26 */
27public class TileCoordinateConverter {
28 private final MapView mapView;
29 private final TileSourceDisplaySettings settings;
30 private final TileSource tileSource;
31
32 /**
33 * Create a new coordinate converter for the map view.
34 * @param mapView The map view.
35 * @param tileSource The tile source to use when converting coordinates.
36 * @param settings displacement settings.
37 */
38 public TileCoordinateConverter(MapView mapView, TileSource tileSource, TileSourceDisplaySettings settings) {
39 this.mapView = mapView;
40 this.tileSource = tileSource;
41 this.settings = settings;
42 }
43
44 private MapViewPoint pos(ICoordinate ll) {
45 return mapView.getState().getPointFor(new LatLon(ll)).add(settings.getDisplacement());
46 }
47
48 private MapViewPoint pos(IProjected p) {
49 return mapView.getState().getPointFor(new EastNorth(p)).add(settings.getDisplacement());
50 }
51
52 /**
53 * Apply reverse shift to EastNorth coordinate.
54 *
55 * @param en EastNorth coordinate representing a pixel on screen
56 * @return IProjected coordinate as it would e.g. be sent to a WMS server
57 */
58 public IProjected shiftDisplayToServer(EastNorth en) {
59 return en.subtract(settings.getDisplacement()).toProjected();
60 }
61
62 /**
63 * Gets the projecting instance to use to convert between latlon and eastnorth coordinates.
64 * @return The {@link Projecting} instance.
65 */
66 public Projecting getProjecting() {
67 return new ShiftedProjecting(mapView.getProjection(), settings.getDisplacement());
68 }
69
70 /**
71 * Gets the top left position of the tile inside the map view.
72 * @param x x tile index
73 * @param y y tile index
74 * @param zoom zoom level
75 * @return the position
76 */
77 public Point2D getPixelForTile(int x, int y, int zoom) {
78 ICoordinate coord = tileSource.tileXYToLatLon(x, y, zoom);
79 return pos(coord).getInView();
80 }
81
82 /**
83 * Gets the top left position of the tile inside the map view.
84 * @param tile The tile
85 * @return The position.
86 */
87 public Point2D getPixelForTile(Tile tile) {
88 return this.getPixelForTile(tile.getXtile(), tile.getYtile(), tile.getZoom());
89 }
90
91 /**
92 * Gets the position of the tile inside the map view.
93 * @param tile The tile
94 * @return The positon.
95 */
96 public Rectangle2D getRectangleForTile(Tile tile) {
97 ICoordinate c1 = tile.getTileSource().tileXYToLatLon(tile);
98 ICoordinate c2 = tile.getTileSource().tileXYToLatLon(tile.getXtile() + 1, tile.getYtile() + 1, tile.getZoom());
99
100 return pos(c1).rectTo(pos(c2)).getInView();
101 }
102
103 /**
104 * Returns a quadrilateral formed by the 4 corners of the tile in screen coordinates.
105 *
106 * If the tile is rectangular, this will be the exact border of the tile.
107 * The tile may be more oddly shaped due to reprojection, then it is an approximation
108 * of the tile outline.
109 * @param tile the tile
110 * @return quadrilateral tile outline in screen coordinates
111 */
112 public Shape getScreenQuadrilateralForTile(Tile tile) {
113 Point2D p00 = this.getPixelForTile(tile.getXtile(), tile.getYtile(), tile.getZoom());
114 Point2D p10 = this.getPixelForTile(tile.getXtile() + 1, tile.getYtile(), tile.getZoom());
115 Point2D p11 = this.getPixelForTile(tile.getXtile() + 1, tile.getYtile() + 1, tile.getZoom());
116 Point2D p01 = this.getPixelForTile(tile.getXtile(), tile.getYtile() + 1, tile.getZoom());
117
118 Path2D pth = new Path2D.Double();
119 pth.moveTo(p00.getX(), p00.getY());
120 pth.lineTo(p01.getX(), p01.getY());
121 pth.lineTo(p11.getX(), p11.getY());
122 pth.lineTo(p10.getX(), p10.getY());
123 pth.closePath();
124 return pth;
125 }
126
127 /**
128 * Returns average number of screen pixels per tile pixel for current mapview
129 * @param zoom zoom level
130 * @return average number of screen pixels per tile pixel
131 */
132 public double getScaleFactor(int zoom) {
133 LatLon topLeft = mapView.getLatLon(0, 0);
134 LatLon botRight = mapView.getLatLon(mapView.getWidth(), mapView.getHeight());
135 TileXY t1 = tileSource.latLonToTileXY(topLeft.toCoordinate(), zoom);
136 TileXY t2 = tileSource.latLonToTileXY(botRight.toCoordinate(), zoom);
137
138 int screenPixels = mapView.getWidth()*mapView.getHeight();
139 double tilePixels = Math.abs((t2.getY()-t1.getY())*(t2.getX()-t1.getX())*tileSource.getTileSize()*tileSource.getTileSize());
140 if (screenPixels == 0 || tilePixels == 0) return 1;
141 return screenPixels/tilePixels;
142 }
143
144 /**
145 * Get {@link TileAnchor} for a tile in screen pixel coordinates.
146 * @param tile the tile
147 * @return position of the tile in screen coordinates
148 */
149 public TileAnchor getScreenAnchorForTile(Tile tile) {
150 IProjected p1 = tileSource.tileXYtoProjected(tile.getXtile(), tile.getYtile(), tile.getZoom());
151 IProjected p2 = tileSource.tileXYtoProjected(tile.getXtile() + 1, tile.getYtile() + 1, tile.getZoom());
152 return new TileAnchor(pos(p1).getInView(), pos(p2).getInView());
153 }
154}
Note: See TracBrowser for help on using the repository browser.