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

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

initialize AbstractTileSourceLayerTest

  • Property svn:eol-style set to native
File size: 8.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;
8import java.util.Objects;
9
10import org.openstreetmap.gui.jmapviewer.Tile;
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.Main;
16import org.openstreetmap.josm.data.coor.EastNorth;
17import org.openstreetmap.josm.data.coor.LatLon;
18import org.openstreetmap.josm.data.projection.Projecting;
19import org.openstreetmap.josm.data.projection.ShiftedProjecting;
20import org.openstreetmap.josm.gui.MapView;
21import org.openstreetmap.josm.gui.MapViewState.MapViewPoint;
22
23/**
24 * This class handles tile coordinate management and computes their position in the map view.
25 * @author Michael Zangl
26 * @since 10651
27 */
28public class TileCoordinateConverter {
29 private final MapView mapView;
30 private final TileSourceDisplaySettings settings;
31 private final TileSource tileSource;
32
33 /**
34 * Create a new coordinate converter for the map view.
35 * @param mapView The map view.
36 * @param tileSource The tile source to use when converting coordinates.
37 * @param settings displacement settings.
38 * @throws NullPointerException if one argument is null
39 */
40 public TileCoordinateConverter(MapView mapView, TileSource tileSource, TileSourceDisplaySettings settings) {
41 this.mapView = Objects.requireNonNull(mapView, "mapView");
42 this.tileSource = Objects.requireNonNull(tileSource, "tileSource");
43 this.settings = Objects.requireNonNull(settings, "settings");
44 }
45
46 private MapViewPoint pos(ICoordinate ll) {
47 return mapView.getState().getPointFor(new LatLon(ll)).add(settings.getDisplacement());
48 }
49
50 private MapViewPoint pos(IProjected p) {
51 return mapView.getState().getPointFor(new EastNorth(p)).add(settings.getDisplacement());
52 }
53
54 /**
55 * Apply reverse shift to EastNorth coordinate.
56 *
57 * @param en EastNorth coordinate representing a pixel on screen
58 * @return IProjected coordinate as it would e.g. be sent to a WMS server
59 */
60 public IProjected shiftDisplayToServer(EastNorth en) {
61 return en.subtract(settings.getDisplacement()).toProjected();
62 }
63
64 /**
65 * Gets the projecting instance to use to convert between latlon and eastnorth coordinates.
66 * @return The {@link Projecting} instance.
67 */
68 public Projecting getProjecting() {
69 return new ShiftedProjecting(mapView.getProjection(), settings.getDisplacement());
70 }
71
72 /**
73 * Gets the top left position of the tile inside the map view.
74 * @param x x tile index
75 * @param y y tile index
76 * @param zoom zoom level
77 * @return the position
78 */
79 public Point2D getPixelForTile(int x, int y, int zoom) {
80 ICoordinate coord = tileSource.tileXYToLatLon(x, y, zoom);
81 return pos(coord).getInView();
82 }
83
84 /**
85 * Gets the top left position of the tile inside the map view.
86 * @param tile The tile
87 * @return The position.
88 */
89 public Point2D getPixelForTile(Tile tile) {
90 return getPixelForTile(tile.getXtile(), tile.getYtile(), tile.getZoom());
91 }
92
93 /**
94 * Convert screen pixel coordinate to tile position at certain zoom level.
95 * @param sx x coordinate (screen pixel)
96 * @param sy y coordinate (screen pixel)
97 * @param zoom zoom level
98 * @return the tile
99 */
100 public TileXY getTileforPixel(int sx, int sy, int zoom) {
101 if (requiresReprojection()) {
102 LatLon ll = getProjecting().eastNorth2latlonClamped(mapView.getEastNorth(sx, sy));
103 return tileSource.latLonToTileXY(ll.toCoordinate(), zoom);
104 } else {
105 IProjected p = shiftDisplayToServer(mapView.getEastNorth(sx, sy));
106 return tileSource.projectedToTileXY(p, zoom);
107 }
108 }
109
110 /**
111 * Gets the position of the tile inside the map view.
112 * @param tile The tile
113 * @return The positon as a rectangle in screen coordinates
114 */
115 public Rectangle2D getRectangleForTile(Tile tile) {
116 ICoordinate c1 = tile.getTileSource().tileXYToLatLon(tile);
117 ICoordinate c2 = tile.getTileSource().tileXYToLatLon(tile.getXtile() + 1, tile.getYtile() + 1, tile.getZoom());
118
119 return pos(c1).rectTo(pos(c2)).getInView();
120 }
121
122 /**
123 * Returns a quadrilateral formed by the 4 corners of the tile in screen coordinates.
124 *
125 * If the tile is rectangular, this will be the exact border of the tile.
126 * The tile may be more oddly shaped due to reprojection, then it is an approximation
127 * of the tile outline.
128 * @param tile the tile
129 * @return quadrilateral tile outline in screen coordinates
130 */
131 public Shape getScreenQuadrilateralForTile(Tile tile) {
132 Point2D p00 = this.getPixelForTile(tile.getXtile(), tile.getYtile(), tile.getZoom());
133 Point2D p10 = this.getPixelForTile(tile.getXtile() + 1, tile.getYtile(), tile.getZoom());
134 Point2D p11 = this.getPixelForTile(tile.getXtile() + 1, tile.getYtile() + 1, tile.getZoom());
135 Point2D p01 = this.getPixelForTile(tile.getXtile(), tile.getYtile() + 1, tile.getZoom());
136
137 Path2D pth = new Path2D.Double();
138 pth.moveTo(p00.getX(), p00.getY());
139 pth.lineTo(p01.getX(), p01.getY());
140 pth.lineTo(p11.getX(), p11.getY());
141 pth.lineTo(p10.getX(), p10.getY());
142 pth.closePath();
143 return pth;
144 }
145
146 /**
147 * Returns average number of screen pixels per tile pixel for current mapview
148 * @param zoom zoom level
149 * @return average number of screen pixels per tile pixel
150 */
151 public double getScaleFactor(int zoom) {
152 TileXY t1, t2;
153 if (requiresReprojection()) {
154 LatLon topLeft = mapView.getLatLon(0, 0);
155 LatLon botRight = mapView.getLatLon(mapView.getWidth(), mapView.getHeight());
156 t1 = tileSource.latLonToTileXY(topLeft.toCoordinate(), zoom);
157 t2 = tileSource.latLonToTileXY(botRight.toCoordinate(), zoom);
158 } else {
159 EastNorth topLeftEN = mapView.getEastNorth(0, 0);
160 EastNorth botRightEN = mapView.getEastNorth(mapView.getWidth(), mapView.getHeight());
161 t1 = tileSource.projectedToTileXY(topLeftEN.toProjected(), zoom);
162 t2 = tileSource.projectedToTileXY(botRightEN.toProjected(), zoom);
163 }
164 int screenPixels = mapView.getWidth()*mapView.getHeight();
165 double tilePixels = Math.abs((t2.getY()-t1.getY())*(t2.getX()-t1.getX())*tileSource.getTileSize()*tileSource.getTileSize());
166 if (screenPixels == 0 || tilePixels == 0) return 1;
167 return screenPixels/tilePixels;
168 }
169
170 /**
171 * Get {@link TileAnchor} for a tile in screen pixel coordinates.
172 * @param tile the tile
173 * @return position of the tile in screen coordinates
174 */
175 public TileAnchor getScreenAnchorForTile(Tile tile) {
176 if (requiresReprojection()) {
177 ICoordinate c1 = tile.getTileSource().tileXYToLatLon(tile);
178 ICoordinate c2 = tile.getTileSource().tileXYToLatLon(tile.getXtile() + 1, tile.getYtile() + 1, tile.getZoom());
179 return new TileAnchor(pos(c1).getInView(), pos(c2).getInView());
180 } else {
181 IProjected p1 = tileSource.tileXYtoProjected(tile.getXtile(), tile.getYtile(), tile.getZoom());
182 IProjected p2 = tileSource.tileXYtoProjected(tile.getXtile() + 1, tile.getYtile() + 1, tile.getZoom());
183 return new TileAnchor(pos(p1).getInView(), pos(p2).getInView());
184 }
185 }
186
187 /**
188 * Return true if tiles need to be reprojected from server projection to display projection.
189 * @return true if tiles need to be reprojected from server projection to display projection
190 */
191 public boolean requiresReprojection() {
192 return !tileSource.getServerCRS().equals(Main.getProjection().toCode());
193 }
194}
Note: See TracBrowser for help on using the repository browser.