1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.layer.imagery;
|
---|
3 |
|
---|
4 | import java.awt.GraphicsConfiguration;
|
---|
5 | import java.awt.Polygon;
|
---|
6 | import java.awt.Rectangle;
|
---|
7 | import java.awt.Shape;
|
---|
8 | import java.awt.geom.AffineTransform;
|
---|
9 | import java.awt.geom.Point2D;
|
---|
10 | import java.awt.geom.Rectangle2D;
|
---|
11 | import java.util.Objects;
|
---|
12 | import java.util.Optional;
|
---|
13 |
|
---|
14 | import org.openstreetmap.gui.jmapviewer.Tile;
|
---|
15 | import org.openstreetmap.gui.jmapviewer.TileXY;
|
---|
16 | import org.openstreetmap.gui.jmapviewer.interfaces.ICoordinate;
|
---|
17 | import org.openstreetmap.gui.jmapviewer.interfaces.IProjected;
|
---|
18 | import org.openstreetmap.gui.jmapviewer.interfaces.TileSource;
|
---|
19 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
20 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
21 | import org.openstreetmap.josm.data.imagery.CoordinateConversion;
|
---|
22 | import org.openstreetmap.josm.data.projection.Projecting;
|
---|
23 | import org.openstreetmap.josm.data.projection.ProjectionRegistry;
|
---|
24 | import org.openstreetmap.josm.data.projection.ShiftedProjecting;
|
---|
25 | import org.openstreetmap.josm.gui.MapView;
|
---|
26 | import org.openstreetmap.josm.gui.MapViewState.MapViewPoint;
|
---|
27 | import org.openstreetmap.josm.tools.JosmRuntimeException;
|
---|
28 | import org.openstreetmap.josm.tools.bugreport.BugReport;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * This class handles tile coordinate management and computes their position in the map view.
|
---|
32 | * @author Michael Zangl
|
---|
33 | * @since 10651
|
---|
34 | */
|
---|
35 | public class TileCoordinateConverter {
|
---|
36 | private final MapView mapView;
|
---|
37 | private final TileSourceDisplaySettings settings;
|
---|
38 | private final TileSource tileSource;
|
---|
39 | private final AffineTransform transform;
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Create a new coordinate converter for the map view.
|
---|
43 | * @param mapView The map view.
|
---|
44 | * @param tileSource The tile source to use when converting coordinates.
|
---|
45 | * @param settings displacement settings.
|
---|
46 | * @throws NullPointerException if one argument is null
|
---|
47 | */
|
---|
48 | public TileCoordinateConverter(MapView mapView, TileSource tileSource, TileSourceDisplaySettings settings) {
|
---|
49 | this.mapView = Objects.requireNonNull(mapView, "mapView");
|
---|
50 | this.tileSource = Objects.requireNonNull(tileSource, "tileSource");
|
---|
51 | this.settings = Objects.requireNonNull(settings, "settings");
|
---|
52 | this.transform = Optional.ofNullable(mapView.getGraphicsConfiguration())
|
---|
53 | .map(GraphicsConfiguration::getDefaultTransform)
|
---|
54 | .orElseGet(AffineTransform::new);
|
---|
55 | }
|
---|
56 |
|
---|
57 | private MapViewPoint pos(ICoordinate ll) {
|
---|
58 | return mapView.getState().getPointFor(CoordinateConversion.coorToLL(ll)).add(settings.getDisplacement());
|
---|
59 | }
|
---|
60 |
|
---|
61 | private MapViewPoint pos(IProjected p) {
|
---|
62 | return mapView.getState().getPointFor(CoordinateConversion.projToEn(p)).add(settings.getDisplacement());
|
---|
63 | }
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * Apply reverse shift to EastNorth coordinate.
|
---|
67 | *
|
---|
68 | * @param en EastNorth coordinate representing a pixel on screen
|
---|
69 | * @return IProjected coordinate as it would e.g. be sent to a WMS server
|
---|
70 | */
|
---|
71 | public IProjected shiftDisplayToServer(EastNorth en) {
|
---|
72 | return CoordinateConversion.enToProj(en.subtract(settings.getDisplacement()));
|
---|
73 | }
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Gets the projecting instance to use to convert between latlon and eastnorth coordinates.
|
---|
77 | * @return The {@link Projecting} instance.
|
---|
78 | */
|
---|
79 | public Projecting getProjecting() {
|
---|
80 | return new ShiftedProjecting(mapView.getProjection(), settings.getDisplacement());
|
---|
81 | }
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Gets the top left position of the tile inside the map view.
|
---|
85 | * @param x x tile index
|
---|
86 | * @param y y tile index
|
---|
87 | * @param zoom zoom level
|
---|
88 | * @return the position
|
---|
89 | */
|
---|
90 | public Point2D getPixelForTile(int x, int y, int zoom) {
|
---|
91 | try {
|
---|
92 | ICoordinate coord = tileSource.tileXYToLatLon(x, y, zoom);
|
---|
93 | if (Double.isNaN(coord.getLat()) || Double.isNaN(coord.getLon())) {
|
---|
94 | throw new JosmRuntimeException("tileXYToLatLon returned " + coord);
|
---|
95 | }
|
---|
96 | return pos(coord).getInView();
|
---|
97 | } catch (RuntimeException e) {
|
---|
98 | throw BugReport.intercept(e).put("tileSource", tileSource).put("x", x).put("y", y).put("zoom", zoom);
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * Gets the top left position of the tile inside the map view.
|
---|
104 | * @param tile The tile
|
---|
105 | * @return The position.
|
---|
106 | */
|
---|
107 | public Point2D getPixelForTile(Tile tile) {
|
---|
108 | return getPixelForTile(tile.getXtile(), tile.getYtile(), tile.getZoom());
|
---|
109 | }
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * Convert screen pixel coordinate to tile position at certain zoom level.
|
---|
113 | * @param sx x coordinate (screen pixel)
|
---|
114 | * @param sy y coordinate (screen pixel)
|
---|
115 | * @param zoom zoom level
|
---|
116 | * @return the tile
|
---|
117 | */
|
---|
118 | public TileXY getTileforPixel(int sx, int sy, int zoom) {
|
---|
119 | if (requiresReprojection()) {
|
---|
120 | LatLon ll = getProjecting().eastNorth2latlonClamped(mapView.getEastNorth(sx, sy));
|
---|
121 | return tileSource.latLonToTileXY(CoordinateConversion.llToCoor(ll), zoom);
|
---|
122 | } else {
|
---|
123 | IProjected p = shiftDisplayToServer(mapView.getEastNorth(sx, sy));
|
---|
124 | return tileSource.projectedToTileXY(p, zoom);
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * Gets the position of the tile inside the map view.
|
---|
130 | * @param tile The tile
|
---|
131 | * @return The position as a rectangle in screen coordinates
|
---|
132 | */
|
---|
133 | public Rectangle2D getRectangleForTile(Tile tile) {
|
---|
134 | ICoordinate c1 = tile.getTileSource().tileXYToLatLon(tile);
|
---|
135 | ICoordinate c2 = tile.getTileSource().tileXYToLatLon(tile.getXtile() + 1, tile.getYtile() + 1, tile.getZoom());
|
---|
136 |
|
---|
137 | return pos(c1).rectTo(pos(c2)).getInView();
|
---|
138 | }
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * Returns a shape that approximates the outline of the tile in screen coordinates.
|
---|
142 | *
|
---|
143 | * If the tile is rectangular, this will be the exact border of the tile.
|
---|
144 | * The tile may be more oddly shaped due to reprojection, then it is an approximation
|
---|
145 | * of the tile outline.
|
---|
146 | * @param tile the tile
|
---|
147 | * @return tile outline in screen coordinates
|
---|
148 | */
|
---|
149 | public Shape getTileShapeScreen(Tile tile) {
|
---|
150 | if (requiresReprojection()) {
|
---|
151 | Point2D p00 = this.getPixelForTile(tile.getXtile(), tile.getYtile(), tile.getZoom());
|
---|
152 | Point2D p10 = this.getPixelForTile(tile.getXtile() + 1, tile.getYtile(), tile.getZoom());
|
---|
153 | Point2D p11 = this.getPixelForTile(tile.getXtile() + 1, tile.getYtile() + 1, tile.getZoom());
|
---|
154 | Point2D p01 = this.getPixelForTile(tile.getXtile(), tile.getYtile() + 1, tile.getZoom());
|
---|
155 | return new Polygon(new int[] {
|
---|
156 | (int) Math.round(p00.getX()),
|
---|
157 | (int) Math.round(p01.getX()),
|
---|
158 | (int) Math.round(p11.getX()),
|
---|
159 | (int) Math.round(p10.getX())},
|
---|
160 | new int[] {
|
---|
161 | (int) Math.round(p00.getY()),
|
---|
162 | (int) Math.round(p01.getY()),
|
---|
163 | (int) Math.round(p11.getY()),
|
---|
164 | (int) Math.round(p10.getY())}, 4);
|
---|
165 | } else {
|
---|
166 | Point2D p00 = this.getPixelForTile(tile.getXtile(), tile.getYtile(), tile.getZoom());
|
---|
167 | Point2D p11 = this.getPixelForTile(tile.getXtile() + 1, tile.getYtile() + 1, tile.getZoom());
|
---|
168 | return new Rectangle((int) Math.round(p00.getX()), (int) Math.round(p00.getY()),
|
---|
169 | (int) Math.round(p11.getX()) - (int) Math.round(p00.getX()),
|
---|
170 | (int) Math.round(p11.getY()) - (int) Math.round(p00.getY()));
|
---|
171 | }
|
---|
172 | }
|
---|
173 |
|
---|
174 | /**
|
---|
175 | * Returns average number of screen pixels per tile pixel for current mapview
|
---|
176 | * @param zoom zoom level
|
---|
177 | * @return average number of screen pixels per tile pixel
|
---|
178 | */
|
---|
179 | public double getScaleFactor(int zoom) {
|
---|
180 | TileXY t1, t2;
|
---|
181 | if (requiresReprojection()) {
|
---|
182 | LatLon topLeft = mapView.getLatLon(0, 0);
|
---|
183 | LatLon botRight = mapView.getLatLon(mapView.getWidth(), mapView.getHeight());
|
---|
184 | t1 = tileSource.latLonToTileXY(CoordinateConversion.llToCoor(topLeft), zoom);
|
---|
185 | t2 = tileSource.latLonToTileXY(CoordinateConversion.llToCoor(botRight), zoom);
|
---|
186 | } else {
|
---|
187 | EastNorth topLeftEN = mapView.getEastNorth(0, 0);
|
---|
188 | EastNorth botRightEN = mapView.getEastNorth(mapView.getWidth(), mapView.getHeight());
|
---|
189 | t1 = tileSource.projectedToTileXY(CoordinateConversion.enToProj(topLeftEN), zoom);
|
---|
190 | t2 = tileSource.projectedToTileXY(CoordinateConversion.enToProj(botRightEN), zoom);
|
---|
191 | }
|
---|
192 | int screenPixels = (int) (mapView.getWidth() * mapView.getHeight() * transform.getScaleX() * transform.getScaleY());
|
---|
193 | double tilePixels = Math.abs((t2.getY()-t1.getY())*(t2.getX()-t1.getX())*tileSource.getTileSize()*tileSource.getTileSize());
|
---|
194 | if (screenPixels == 0 || tilePixels == 0) return 1;
|
---|
195 | return screenPixels/tilePixels;
|
---|
196 | }
|
---|
197 |
|
---|
198 | /**
|
---|
199 | * Get {@link TileAnchor} for a tile in screen pixel coordinates.
|
---|
200 | * @param tile the tile
|
---|
201 | * @return position of the tile in screen coordinates
|
---|
202 | */
|
---|
203 | public TileAnchor getScreenAnchorForTile(Tile tile) {
|
---|
204 | if (requiresReprojection()) {
|
---|
205 | ICoordinate c1 = tile.getTileSource().tileXYToLatLon(tile);
|
---|
206 | ICoordinate c2 = tile.getTileSource().tileXYToLatLon(tile.getXtile() + 1, tile.getYtile() + 1, tile.getZoom());
|
---|
207 | return new TileAnchor(pos(c1).getInView(), pos(c2).getInView());
|
---|
208 | } else {
|
---|
209 | IProjected p1 = tileSource.tileXYtoProjected(tile.getXtile(), tile.getYtile(), tile.getZoom());
|
---|
210 | IProjected p2 = tileSource.tileXYtoProjected(tile.getXtile() + 1, tile.getYtile() + 1, tile.getZoom());
|
---|
211 | return new TileAnchor(pos(p1).getInView(), pos(p2).getInView());
|
---|
212 | }
|
---|
213 | }
|
---|
214 |
|
---|
215 | /**
|
---|
216 | * Return true if tiles need to be reprojected from server projection to display projection.
|
---|
217 | * @return true if tiles need to be reprojected from server projection to display projection
|
---|
218 | */
|
---|
219 | public boolean requiresReprojection() {
|
---|
220 | return !Objects.equals(tileSource.getServerCRS(), ProjectionRegistry.getProjection().toCode());
|
---|
221 | }
|
---|
222 | }
|
---|