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

Last change on this file since 11865 was 11865, checked in by Don-vip, 7 years ago

see #7427 - checkstyle

  • Property svn:eol-style set to native
File size: 7.8 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.TileXY;
11import org.openstreetmap.gui.jmapviewer.interfaces.ICoordinate;
12import org.openstreetmap.gui.jmapviewer.interfaces.IProjected;
13import org.openstreetmap.gui.jmapviewer.interfaces.TileSource;
14import org.openstreetmap.josm.Main;
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 getPixelForTile(tile.getXtile(), tile.getYtile(), tile.getZoom());
89 }
90
91 /**
92 * Convert screen pixel coordinate to tile position at certain zoom level.
93 * @param sx x coordinate (screen pixel)
94 * @param sy y coordinate (screen pixel)
95 * @param zoom zoom level
96 * @return the tile
97 */
98 public TileXY getTileforPixel(int sx, int sy, int zoom) {
99 if (requiresReprojection()) {
100 LatLon ll = getProjecting().eastNorth2latlonClamped(mapView.getEastNorth(sx, sy));
101 return tileSource.latLonToTileXY(ll.toCoordinate(), zoom);
102 } else {
103 IProjected p = shiftDisplayToServer(mapView.getEastNorth(sx, sy));
104 return tileSource.projectedToTileXY(p, zoom);
105 }
106 }
107
108 /**
109 * Gets the position of the tile inside the map view.
110 * @param tile The tile
111 * @return The positon as a rectangle in screen coordinates
112 */
113 public Rectangle2D getRectangleForTile(Tile tile) {
114 ICoordinate c1 = tile.getTileSource().tileXYToLatLon(tile);
115 ICoordinate c2 = tile.getTileSource().tileXYToLatLon(tile.getXtile() + 1, tile.getYtile() + 1, tile.getZoom());
116
117 return pos(c1).rectTo(pos(c2)).getInView();
118 }
119
120 /**
121 * Returns a quadrilateral formed by the 4 corners of the tile in screen coordinates.
122 *
123 * If the tile is rectangular, this will be the exact border of the tile.
124 * The tile may be more oddly shaped due to reprojection, then it is an approximation
125 * of the tile outline.
126 * @param tile the tile
127 * @return quadrilateral tile outline in screen coordinates
128 */
129 public Shape getScreenQuadrilateralForTile(Tile tile) {
130 Point2D p00 = this.getPixelForTile(tile.getXtile(), tile.getYtile(), tile.getZoom());
131 Point2D p10 = this.getPixelForTile(tile.getXtile() + 1, tile.getYtile(), tile.getZoom());
132 Point2D p11 = this.getPixelForTile(tile.getXtile() + 1, tile.getYtile() + 1, tile.getZoom());
133 Point2D p01 = this.getPixelForTile(tile.getXtile(), tile.getYtile() + 1, tile.getZoom());
134
135 Path2D pth = new Path2D.Double();
136 pth.moveTo(p00.getX(), p00.getY());
137 pth.lineTo(p01.getX(), p01.getY());
138 pth.lineTo(p11.getX(), p11.getY());
139 pth.lineTo(p10.getX(), p10.getY());
140 pth.closePath();
141 return pth;
142 }
143
144 /**
145 * Returns average number of screen pixels per tile pixel for current mapview
146 * @param zoom zoom level
147 * @return average number of screen pixels per tile pixel
148 */
149 public double getScaleFactor(int zoom) {
150 TileXY t1, t2;
151 if (requiresReprojection()) {
152 LatLon topLeft = mapView.getLatLon(0, 0);
153 LatLon botRight = mapView.getLatLon(mapView.getWidth(), mapView.getHeight());
154 t1 = tileSource.latLonToTileXY(topLeft.toCoordinate(), zoom);
155 t2 = tileSource.latLonToTileXY(botRight.toCoordinate(), zoom);
156 } else {
157 EastNorth topLeftEN = mapView.getEastNorth(0, 0);
158 EastNorth botRightEN = mapView.getEastNorth(mapView.getWidth(), mapView.getHeight());
159 t1 = tileSource.projectedToTileXY(topLeftEN.toProjected(), zoom);
160 t2 = tileSource.projectedToTileXY(botRightEN.toProjected(), zoom);
161 }
162 int screenPixels = mapView.getWidth()*mapView.getHeight();
163 double tilePixels = Math.abs((t2.getY()-t1.getY())*(t2.getX()-t1.getX())*tileSource.getTileSize()*tileSource.getTileSize());
164 if (screenPixels == 0 || tilePixels == 0) return 1;
165 return screenPixels/tilePixels;
166 }
167
168 /**
169 * Get {@link TileAnchor} for a tile in screen pixel coordinates.
170 * @param tile the tile
171 * @return position of the tile in screen coordinates
172 */
173 public TileAnchor getScreenAnchorForTile(Tile tile) {
174 if (requiresReprojection()) {
175 ICoordinate c1 = tile.getTileSource().tileXYToLatLon(tile);
176 ICoordinate c2 = tile.getTileSource().tileXYToLatLon(tile.getXtile() + 1, tile.getYtile() + 1, tile.getZoom());
177 return new TileAnchor(pos(c1).getInView(), pos(c2).getInView());
178 } else {
179 IProjected p1 = tileSource.tileXYtoProjected(tile.getXtile(), tile.getYtile(), tile.getZoom());
180 IProjected p2 = tileSource.tileXYtoProjected(tile.getXtile() + 1, tile.getYtile() + 1, tile.getZoom());
181 return new TileAnchor(pos(p1).getInView(), pos(p2).getInView());
182 }
183 }
184
185 /**
186 * Return true if tiles need to be reprojected from server projection to display projection.
187 * @return true if tiles need to be reprojected from server projection to display projection
188 */
189 public boolean requiresReprojection() {
190 return !tileSource.getServerCRS().equals(Main.getProjection().toCode());
191 }
192}
Note: See TracBrowser for help on using the repository browser.