1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.imagery;
|
---|
3 |
|
---|
4 | import java.awt.Point;
|
---|
5 |
|
---|
6 | import org.openstreetmap.gui.jmapviewer.Projected;
|
---|
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.IProjected;
|
---|
11 | import org.openstreetmap.gui.jmapviewer.tilesources.TMSTileSource;
|
---|
12 | import org.openstreetmap.gui.jmapviewer.tilesources.TileSourceInfo;
|
---|
13 | import org.openstreetmap.josm.data.Bounds;
|
---|
14 | import org.openstreetmap.josm.data.ProjectionBounds;
|
---|
15 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
16 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
17 | import org.openstreetmap.josm.data.projection.Projection;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * Base class for different WMS tile sources those based on URL templates and those based on WMS endpoints
|
---|
21 | * @author Wiktor Niesiobędzki
|
---|
22 | * @since 10990
|
---|
23 | */
|
---|
24 | public abstract class AbstractWMSTileSource extends TMSTileSource {
|
---|
25 |
|
---|
26 | private EastNorth anchorPosition;
|
---|
27 | private int[] tileXMin;
|
---|
28 | private int[] tileYMin;
|
---|
29 | private int[] tileXMax;
|
---|
30 | private int[] tileYMax;
|
---|
31 | private double[] degreesPerTile;
|
---|
32 | private static final float SCALE_DENOMINATOR_ZOOM_LEVEL_1 = 559082264.0287178f;
|
---|
33 | private Projection tileProjection;
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Constructs a new {@code AbstractWMSTileSource}.
|
---|
37 | * @param info tile source info
|
---|
38 | * @param tileProjection the tile projection
|
---|
39 | */
|
---|
40 | public AbstractWMSTileSource(TileSourceInfo info, Projection tileProjection) {
|
---|
41 | super(info);
|
---|
42 | this.tileProjection = tileProjection;
|
---|
43 | }
|
---|
44 |
|
---|
45 | private void initAnchorPosition(Projection proj) {
|
---|
46 | Bounds worldBounds = proj.getWorldBoundsLatLon();
|
---|
47 | EastNorth min = proj.latlon2eastNorth(worldBounds.getMin());
|
---|
48 | EastNorth max = proj.latlon2eastNorth(worldBounds.getMax());
|
---|
49 | this.anchorPosition = new EastNorth(min.east(), max.north());
|
---|
50 | }
|
---|
51 |
|
---|
52 | public void setTileProjection(Projection tileProjection) {
|
---|
53 | this.tileProjection = tileProjection;
|
---|
54 | initProjection();
|
---|
55 | }
|
---|
56 |
|
---|
57 | public Projection getTileProjection() {
|
---|
58 | return this.tileProjection;
|
---|
59 | }
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Initializes class with current projection in JOSM. This call is needed every time projection changes.
|
---|
63 | */
|
---|
64 | public void initProjection() {
|
---|
65 | initProjection(this.tileProjection);
|
---|
66 | }
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Initializes class with projection in JOSM. This call is needed every time projection changes.
|
---|
70 | * @param proj new projection that shall be used for computations
|
---|
71 | */
|
---|
72 | public void initProjection(Projection proj) {
|
---|
73 | initAnchorPosition(proj);
|
---|
74 | ProjectionBounds worldBounds = proj.getWorldBoundsBoxEastNorth();
|
---|
75 |
|
---|
76 | EastNorth topLeft = new EastNorth(worldBounds.getMin().east(), worldBounds.getMax().north());
|
---|
77 | EastNorth bottomRight = new EastNorth(worldBounds.getMax().east(), worldBounds.getMin().north());
|
---|
78 |
|
---|
79 | // use 256 as "tile size" to keep the scale in line with default tiles in Mercator projection
|
---|
80 | double crsScale = 256 * 0.28e-03 / proj.getMetersPerUnit();
|
---|
81 | tileXMin = new int[getMaxZoom() + 1];
|
---|
82 | tileYMin = new int[getMaxZoom() + 1];
|
---|
83 | tileXMax = new int[getMaxZoom() + 1];
|
---|
84 | tileYMax = new int[getMaxZoom() + 1];
|
---|
85 | degreesPerTile = new double[getMaxZoom() + 1];
|
---|
86 |
|
---|
87 | for (int zoom = 1; zoom <= getMaxZoom(); zoom++) {
|
---|
88 | // use well known scale set "GoogleCompatibile" from OGC WMTS spec to calculate number of tiles per zoom level
|
---|
89 | // this makes the zoom levels "glued" to standard TMS zoom levels
|
---|
90 | degreesPerTile[zoom] = (SCALE_DENOMINATOR_ZOOM_LEVEL_1 / Math.pow(2d, zoom - 1d)) * crsScale;
|
---|
91 | TileXY minTileIndex = eastNorthToTileXY(topLeft, zoom);
|
---|
92 | tileXMin[zoom] = minTileIndex.getXIndex();
|
---|
93 | tileYMin[zoom] = minTileIndex.getYIndex();
|
---|
94 | TileXY maxTileIndex = eastNorthToTileXY(bottomRight, zoom);
|
---|
95 | tileXMax[zoom] = maxTileIndex.getXIndex();
|
---|
96 | tileYMax[zoom] = maxTileIndex.getYIndex();
|
---|
97 | }
|
---|
98 | }
|
---|
99 |
|
---|
100 | @Override
|
---|
101 | public ICoordinate tileXYToLatLon(Tile tile) {
|
---|
102 | return tileXYToLatLon(tile.getXtile(), tile.getYtile(), tile.getZoom());
|
---|
103 | }
|
---|
104 |
|
---|
105 | @Override
|
---|
106 | public ICoordinate tileXYToLatLon(TileXY xy, int zoom) {
|
---|
107 | return tileXYToLatLon(xy.getXIndex(), xy.getYIndex(), zoom);
|
---|
108 | }
|
---|
109 |
|
---|
110 | @Override
|
---|
111 | public ICoordinate tileXYToLatLon(int x, int y, int zoom) {
|
---|
112 | return CoordinateConversion.llToCoor(tileProjection.eastNorth2latlon(getTileEastNorth(x, y, zoom)));
|
---|
113 | }
|
---|
114 |
|
---|
115 | private TileXY eastNorthToTileXY(EastNorth enPoint, int zoom) {
|
---|
116 | double scale = getDegreesPerTile(zoom);
|
---|
117 | return new TileXY(
|
---|
118 | (enPoint.east() - anchorPosition.east()) / scale,
|
---|
119 | (anchorPosition.north() - enPoint.north()) / scale
|
---|
120 | );
|
---|
121 | }
|
---|
122 |
|
---|
123 | @Override
|
---|
124 | public TileXY latLonToTileXY(double lat, double lon, int zoom) {
|
---|
125 | EastNorth enPoint = tileProjection.latlon2eastNorth(new LatLon(lat, lon));
|
---|
126 | return eastNorthToTileXY(enPoint, zoom);
|
---|
127 | }
|
---|
128 |
|
---|
129 | @Override
|
---|
130 | public TileXY latLonToTileXY(ICoordinate point, int zoom) {
|
---|
131 | return latLonToTileXY(point.getLat(), point.getLon(), zoom);
|
---|
132 | }
|
---|
133 |
|
---|
134 | @Override
|
---|
135 | public int getTileXMax(int zoom) {
|
---|
136 | return tileXMax[zoom];
|
---|
137 | }
|
---|
138 |
|
---|
139 | @Override
|
---|
140 | public int getTileXMin(int zoom) {
|
---|
141 | return tileXMin[zoom];
|
---|
142 | }
|
---|
143 |
|
---|
144 | @Override
|
---|
145 | public int getTileYMax(int zoom) {
|
---|
146 | return tileYMax[zoom];
|
---|
147 | }
|
---|
148 |
|
---|
149 | @Override
|
---|
150 | public int getTileYMin(int zoom) {
|
---|
151 | return tileYMin[zoom];
|
---|
152 | }
|
---|
153 |
|
---|
154 | @Override
|
---|
155 | public Point latLonToXY(double lat, double lon, int zoom) {
|
---|
156 | double scale = getDegreesPerTile(zoom) / getTileSize();
|
---|
157 | EastNorth point = tileProjection.latlon2eastNorth(new LatLon(lat, lon));
|
---|
158 | return new Point(
|
---|
159 | (int) Math.round((point.east() - anchorPosition.east()) / scale),
|
---|
160 | (int) Math.round((anchorPosition.north() - point.north()) / scale)
|
---|
161 | );
|
---|
162 | }
|
---|
163 |
|
---|
164 | @Override
|
---|
165 | public Point latLonToXY(ICoordinate point, int zoom) {
|
---|
166 | return latLonToXY(point.getLat(), point.getLon(), zoom);
|
---|
167 | }
|
---|
168 |
|
---|
169 | @Override
|
---|
170 | public ICoordinate xyToLatLon(Point point, int zoom) {
|
---|
171 | return xyToLatLon(point.x, point.y, zoom);
|
---|
172 | }
|
---|
173 |
|
---|
174 | @Override
|
---|
175 | public ICoordinate xyToLatLon(int x, int y, int zoom) {
|
---|
176 | double scale = getDegreesPerTile(zoom) / getTileSize();
|
---|
177 | EastNorth ret = new EastNorth(
|
---|
178 | anchorPosition.east() + x * scale,
|
---|
179 | anchorPosition.north() - y * scale
|
---|
180 | );
|
---|
181 | return CoordinateConversion.llToCoor(tileProjection.eastNorth2latlon(ret));
|
---|
182 | }
|
---|
183 |
|
---|
184 | protected EastNorth getTileEastNorth(int x, int y, int z) {
|
---|
185 | double scale = getDegreesPerTile(z);
|
---|
186 | return new EastNorth(
|
---|
187 | anchorPosition.east() + x * scale,
|
---|
188 | anchorPosition.north() - y * scale
|
---|
189 | );
|
---|
190 | }
|
---|
191 |
|
---|
192 | private double getDegreesPerTile(int zoom) {
|
---|
193 | return degreesPerTile[zoom];
|
---|
194 | }
|
---|
195 |
|
---|
196 | @Override
|
---|
197 | public IProjected tileXYtoProjected(int x, int y, int zoom) {
|
---|
198 | EastNorth en = getTileEastNorth(x, y, zoom);
|
---|
199 | return new Projected(en.east(), en.north());
|
---|
200 | }
|
---|
201 |
|
---|
202 | @Override
|
---|
203 | public TileXY projectedToTileXY(IProjected p, int zoom) {
|
---|
204 | return eastNorthToTileXY(new EastNorth(p.getEast(), p.getNorth()), zoom);
|
---|
205 | }
|
---|
206 |
|
---|
207 | @Override
|
---|
208 | public String getServerCRS() {
|
---|
209 | return this.tileProjection.toCode();
|
---|
210 | }
|
---|
211 | }
|
---|