1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.layer.imagery;
|
---|
3 |
|
---|
4 | import java.awt.geom.Point2D;
|
---|
5 | import java.awt.geom.Rectangle2D;
|
---|
6 |
|
---|
7 | import org.openstreetmap.gui.jmapviewer.Tile;
|
---|
8 | import org.openstreetmap.gui.jmapviewer.TileXY;
|
---|
9 | import org.openstreetmap.gui.jmapviewer.interfaces.ICoordinate;
|
---|
10 | import org.openstreetmap.gui.jmapviewer.interfaces.TileSource;
|
---|
11 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
12 | import org.openstreetmap.josm.data.projection.Projecting;
|
---|
13 | import org.openstreetmap.josm.data.projection.ShiftedProjecting;
|
---|
14 | import org.openstreetmap.josm.gui.MapView;
|
---|
15 | import org.openstreetmap.josm.gui.MapViewState.MapViewPoint;
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * This class handles tile coordinate management and computes their position in the map view.
|
---|
19 | * @author Michael Zangl
|
---|
20 | * @since 10651
|
---|
21 | */
|
---|
22 | public class TileCoordinateConverter {
|
---|
23 | private MapView mapView;
|
---|
24 | private TileSourceDisplaySettings settings;
|
---|
25 | private TileSource tileSource;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Create a new coordinate converter for the map view.
|
---|
29 | * @param mapView The map view.
|
---|
30 | * @param tileSource The tile source to use when converting coordinates.
|
---|
31 | * @param settings displacement settings.
|
---|
32 | */
|
---|
33 | public TileCoordinateConverter(MapView mapView, TileSource tileSource, TileSourceDisplaySettings settings) {
|
---|
34 | this.mapView = mapView;
|
---|
35 | this.tileSource = tileSource;
|
---|
36 | this.settings = settings;
|
---|
37 | }
|
---|
38 |
|
---|
39 | private MapViewPoint pos(ICoordinate ll) {
|
---|
40 | return mapView.getState().getPointFor(new LatLon(ll)).add(settings.getDisplacement());
|
---|
41 | }
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Gets the projecting instance to use to convert between latlon and eastnorth coordinates.
|
---|
45 | * @return The {@link Projecting} instance.
|
---|
46 | */
|
---|
47 | public Projecting getProjecting() {
|
---|
48 | return new ShiftedProjecting(mapView.getProjection(), settings.getDisplacement());
|
---|
49 | }
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Gets the top left position of the tile inside the map view.
|
---|
53 | * @param tile The tile
|
---|
54 | * @return The positon.
|
---|
55 | */
|
---|
56 | public Point2D getPixelForTile(Tile tile) {
|
---|
57 | ICoordinate coord = tile.getTileSource().tileXYToLatLon(tile);
|
---|
58 | return pos(coord).getInView();
|
---|
59 | }
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Gets the position of the tile inside the map view.
|
---|
63 | * @param tile The tile
|
---|
64 | * @return The positon.
|
---|
65 | */
|
---|
66 | public Rectangle2D getRectangleForTile(Tile tile) {
|
---|
67 | ICoordinate c1 = tile.getTileSource().tileXYToLatLon(tile);
|
---|
68 | ICoordinate c2 = tile.getTileSource().tileXYToLatLon(tile.getXtile() + 1, tile.getYtile() + 1, tile.getZoom());
|
---|
69 |
|
---|
70 | return pos(c1).rectTo(pos(c2)).getInView();
|
---|
71 | }
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Returns average number of screen pixels per tile pixel for current mapview
|
---|
75 | * @param zoom zoom level
|
---|
76 | * @return average number of screen pixels per tile pixel
|
---|
77 | */
|
---|
78 | public double getScaleFactor(int zoom) {
|
---|
79 | LatLon topLeft = mapView.getLatLon(0, 0);
|
---|
80 | LatLon botRight = mapView.getLatLon(mapView.getWidth(), mapView.getHeight());
|
---|
81 | TileXY t1 = tileSource.latLonToTileXY(topLeft.toCoordinate(), zoom);
|
---|
82 | TileXY t2 = tileSource.latLonToTileXY(botRight.toCoordinate(), zoom);
|
---|
83 |
|
---|
84 | int screenPixels = mapView.getWidth()*mapView.getHeight();
|
---|
85 | double tilePixels = Math.abs((t2.getY()-t1.getY())*(t2.getX()-t1.getX())*tileSource.getTileSize()*tileSource.getTileSize());
|
---|
86 | if (screenPixels == 0 || tilePixels == 0) return 1;
|
---|
87 | return screenPixels/tilePixels;
|
---|
88 | }
|
---|
89 | }
|
---|