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