Ticket #12186: TemplatedWMSTileSource.patch
| File TemplatedWMSTileSource.patch, 5.7 KB (added by , 10 years ago) |
|---|
-
src/org/openstreetmap/josm/data/imagery/TemplatedWMSTileSource.java
21 21 import org.openstreetmap.gui.jmapviewer.interfaces.TemplatedTileSource; 22 22 import org.openstreetmap.gui.jmapviewer.tilesources.TMSTileSource; 23 23 import org.openstreetmap.josm.Main; 24 import org.openstreetmap.josm.data.Bounds; 24 25 import org.openstreetmap.josm.data.ProjectionBounds; 25 26 import org.openstreetmap.josm.data.coor.EastNorth; 26 27 import org.openstreetmap.josm.data.coor.LatLon; … … 37 38 public class TemplatedWMSTileSource extends TMSTileSource implements TemplatedTileSource { 38 39 private final Map<String, String> headers = new ConcurrentHashMap<>(); 39 40 private final Set<String> serverProjections; 40 private EastNorth topLeftCorner; 41 private EastNorth anchorPosition; 42 private int[] tileXMin; 43 private int[] tileYMin; 41 44 private int[] tileXMax; 42 45 private int[] tileYMax; 43 46 private double[] degreesPerTile; … … 88 91 initProjection(Main.getProjection()); 89 92 } 90 93 94 private void initAnchorPosition(Projection proj) { 95 Bounds worldBounds = proj.getWorldBoundsLatLon(); 96 EastNorth min = proj.latlon2eastNorth(worldBounds.getMin()); 97 EastNorth max = proj.latlon2eastNorth(worldBounds.getMax()); 98 this.anchorPosition = new EastNorth(min.east(), max.north()); 99 } 100 91 101 /** 92 102 * Initializes class with projection in JOSM. This call is needed every time projection changes. 93 103 * @param proj new projection that shall be used for computations 94 104 */ 95 105 public void initProjection(Projection proj) { 106 initAnchorPosition(proj); 96 107 ProjectionBounds worldBounds = proj.getWorldBoundsBoxEastNorth(); 97 EastNorth min = worldBounds.getMin();98 EastNorth max = worldBounds.getMax();99 this.topLeftCorner = new EastNorth(min.east(), max.north());100 108 109 EastNorth topLeft = new EastNorth(worldBounds.getMin().east(), worldBounds.getMax().north()); 101 110 EastNorth bottomRight = new EastNorth(worldBounds.getMax().east(), worldBounds.getMin().north()); 102 111 103 112 // use 256 as "tile size" to keep the scale in line with default tiles in Mercator projection 104 113 double crsScale = 256 * 0.28e-03 / proj.getMetersPerUnit(); 114 tileXMin = new int[getMaxZoom() + 1]; 115 tileYMin = new int[getMaxZoom() + 1]; 105 116 tileXMax = new int[getMaxZoom() + 1]; 106 117 tileYMax = new int[getMaxZoom() + 1]; 107 118 degreesPerTile = new double[getMaxZoom() + 1]; … … 110 121 // use well known scale set "GoogleCompatibile" from OGC WMTS spec to calculate number of tiles per zoom level 111 122 // this makes the zoom levels "glued" to standard TMS zoom levels 112 123 degreesPerTile[zoom] = (SCALE_DENOMINATOR_ZOOM_LEVEL_1 / Math.pow(2, zoom - 1)) * crsScale; 124 TileXY minTileIndex = eastNorthToTileXY(topLeft, zoom); 125 tileXMin[zoom] = minTileIndex.getXIndex(); 126 tileYMin[zoom] = minTileIndex.getYIndex(); 113 127 TileXY maxTileIndex = eastNorthToTileXY(bottomRight, zoom); 114 128 tileXMax[zoom] = maxTileIndex.getXIndex(); 115 129 tileYMax[zoom] = maxTileIndex.getYIndex(); 130 //System.err.println("z:"+zoom+" xmin="+tileXMin[zoom]+" xmax="+tileXMax[zoom]+" ymin="+tileYMin[zoom]+" ymax="+tileYMax[zoom]); 116 131 } 117 132 } 118 133 … … 246 261 private TileXY eastNorthToTileXY(EastNorth enPoint, int zoom) { 247 262 double scale = getDegreesPerTile(zoom); 248 263 return new TileXY( 249 (enPoint.east() - topLeftCorner.east()) / scale,250 ( topLeftCorner.north() - enPoint.north()) / scale264 (enPoint.east() - anchorPosition.east()) / scale, 265 (anchorPosition.north() - enPoint.north()) / scale 251 266 ); 252 267 } 253 268 … … 263 278 264 279 @Override 265 280 public int getTileXMin(int zoom) { 266 return 0;281 return tileXMin[zoom]; 267 282 } 268 283 269 284 @Override … … 273 288 274 289 @Override 275 290 public int getTileYMin(int zoom) { 276 return 0;291 return tileYMin[zoom]; 277 292 } 278 293 279 294 @Override … … 281 296 double scale = getDegreesPerTile(zoom) / getTileSize(); 282 297 EastNorth point = Main.getProjection().latlon2eastNorth(new LatLon(lat, lon)); 283 298 return new Point( 284 (int) Math.round((point.east() - topLeftCorner.east()) / scale),285 (int) Math.round(( topLeftCorner.north() - point.north()) / scale)299 (int) Math.round((point.east() - anchorPosition.east()) / scale), 300 (int) Math.round((anchorPosition.north() - point.north()) / scale) 286 301 ); 287 302 } 288 303 … … 301 316 double scale = getDegreesPerTile(zoom) / getTileSize(); 302 317 Projection proj = Main.getProjection(); 303 318 EastNorth ret = new EastNorth( 304 topLeftCorner.east() + x * scale,305 topLeftCorner.north() - y * scale319 anchorPosition.east() + x * scale, 320 anchorPosition.north() - y * scale 306 321 ); 307 322 return proj.eastNorth2latlon(ret).toCoordinate(); 308 323 } … … 349 364 protected EastNorth getTileEastNorth(int x, int y, int z) { 350 365 double scale = getDegreesPerTile(z); 351 366 return new EastNorth( 352 topLeftCorner.east() + x * scale,353 topLeftCorner.north() - y * scale367 anchorPosition.east() + x * scale, 368 anchorPosition.north() - y * scale 354 369 ); 355 370 } 356 371
