001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.data.imagery; 003 004import java.awt.Point; 005import java.text.DecimalFormat; 006import java.text.DecimalFormatSymbols; 007import java.text.NumberFormat; 008import java.util.Locale; 009 010import org.openstreetmap.gui.jmapviewer.Projected; 011import org.openstreetmap.gui.jmapviewer.TileXY; 012import org.openstreetmap.gui.jmapviewer.interfaces.ICoordinate; 013import org.openstreetmap.gui.jmapviewer.interfaces.IProjected; 014import org.openstreetmap.gui.jmapviewer.tilesources.TMSTileSource; 015import org.openstreetmap.gui.jmapviewer.tilesources.TileSourceInfo; 016import org.openstreetmap.josm.data.Bounds; 017import org.openstreetmap.josm.data.ProjectionBounds; 018import org.openstreetmap.josm.data.coor.EastNorth; 019import org.openstreetmap.josm.data.coor.LatLon; 020import org.openstreetmap.josm.data.projection.Projection; 021 022/** 023 * Base class for different WMS tile sources those based on URL templates and those based on WMS endpoints 024 * @author Wiktor Niesiobędzki 025 * @since 10990 026 */ 027public abstract class AbstractWMSTileSource extends TMSTileSource { 028 029 static final NumberFormat LATLON_FORMAT = new DecimalFormat("###0.0000000", new DecimalFormatSymbols(Locale.US)); 030 031 private EastNorth anchorPosition; 032 private int[] tileXMin; 033 private int[] tileYMin; 034 private int[] tileXMax; 035 private int[] tileYMax; 036 private double[] degreesPerTile; 037 private static final double SCALE_DENOMINATOR_ZOOM_LEVEL_1 = 5.59082264028718e08; 038 private Projection tileProjection; 039 040 /** 041 * Constructs a new {@code AbstractWMSTileSource}. 042 * @param info tile source info 043 * @param tileProjection the tile projection 044 */ 045 protected AbstractWMSTileSource(TileSourceInfo info, Projection tileProjection) { 046 super(info); 047 this.tileProjection = tileProjection; 048 } 049 050 private void initAnchorPosition(Projection proj) { 051 Bounds worldBounds = proj.getWorldBoundsLatLon(); 052 EastNorth min = proj.latlon2eastNorth(worldBounds.getMin()); 053 EastNorth max = proj.latlon2eastNorth(worldBounds.getMax()); 054 this.anchorPosition = new EastNorth(min.east(), max.north()); 055 } 056 057 public void setTileProjection(Projection tileProjection) { 058 this.tileProjection = tileProjection; 059 initProjection(); 060 } 061 062 public Projection getTileProjection() { 063 return this.tileProjection; 064 } 065 066 /** 067 * Initializes class with current projection in JOSM. This call is needed every time projection changes. 068 */ 069 public void initProjection() { 070 initProjection(this.tileProjection); 071 } 072 073 /** 074 * Initializes class with projection in JOSM. This call is needed every time projection changes. 075 * @param proj new projection that shall be used for computations 076 */ 077 public void initProjection(Projection proj) { 078 initAnchorPosition(proj); 079 ProjectionBounds worldBounds = proj.getWorldBoundsBoxEastNorth(); 080 081 EastNorth topLeft = new EastNorth(worldBounds.getMin().east(), worldBounds.getMax().north()); 082 EastNorth bottomRight = new EastNorth(worldBounds.getMax().east(), worldBounds.getMin().north()); 083 084 // use 256 as "tile size" to keep the scale in line with default tiles in Mercator projection 085 double crsScale = 256 * 0.28e-03 / proj.getMetersPerUnit(); 086 tileXMin = new int[getMaxZoom() + 1]; 087 tileYMin = new int[getMaxZoom() + 1]; 088 tileXMax = new int[getMaxZoom() + 1]; 089 tileYMax = new int[getMaxZoom() + 1]; 090 degreesPerTile = new double[getMaxZoom() + 1]; 091 092 for (int zoom = 1; zoom <= getMaxZoom(); zoom++) { 093 // use well known scale set "GoogleCompatible" from OGC WMTS spec to calculate number of tiles per zoom level 094 // this makes the zoom levels "glued" to standard TMS zoom levels 095 degreesPerTile[zoom] = (SCALE_DENOMINATOR_ZOOM_LEVEL_1 / Math.pow(2d, zoom - 1d)) * crsScale; 096 TileXY minTileIndex = eastNorthToTileXY(topLeft, zoom); 097 tileXMin[zoom] = minTileIndex.getXIndex(); 098 tileYMin[zoom] = minTileIndex.getYIndex(); 099 TileXY maxTileIndex = eastNorthToTileXY(bottomRight, zoom); 100 tileXMax[zoom] = maxTileIndex.getXIndex(); 101 tileYMax[zoom] = maxTileIndex.getYIndex(); 102 } 103 } 104 105 @Override 106 public ICoordinate tileXYToLatLon(int x, int y, int zoom) { 107 return CoordinateConversion.llToCoor(tileProjection.eastNorth2latlon(getTileEastNorth(x, y, zoom))); 108 } 109 110 private TileXY eastNorthToTileXY(EastNorth enPoint, int zoom) { 111 double scale = getDegreesPerTile(zoom); 112 return new TileXY( 113 (enPoint.east() - anchorPosition.east()) / scale, 114 (anchorPosition.north() - enPoint.north()) / scale 115 ); 116 } 117 118 @Override 119 public TileXY latLonToTileXY(double lat, double lon, int zoom) { 120 EastNorth enPoint = tileProjection.latlon2eastNorth(new LatLon(lat, lon)); 121 return eastNorthToTileXY(enPoint, zoom); 122 } 123 124 @Override 125 public int getTileXMax(int zoom) { 126 return tileXMax[zoom]; 127 } 128 129 @Override 130 public int getTileXMin(int zoom) { 131 return tileXMin[zoom]; 132 } 133 134 @Override 135 public int getTileYMax(int zoom) { 136 return tileYMax[zoom]; 137 } 138 139 @Override 140 public int getTileYMin(int zoom) { 141 return tileYMin[zoom]; 142 } 143 144 @Override 145 public Point latLonToXY(double lat, double lon, int zoom) { 146 double scale = getDegreesPerTile(zoom) / getTileSize(); 147 EastNorth point = tileProjection.latlon2eastNorth(new LatLon(lat, lon)); 148 return new Point( 149 (int) Math.round((point.east() - anchorPosition.east()) / scale), 150 (int) Math.round((anchorPosition.north() - point.north()) / scale) 151 ); 152 } 153 154 @Override 155 public ICoordinate xyToLatLon(int x, int y, int zoom) { 156 double scale = getDegreesPerTile(zoom) / getTileSize(); 157 EastNorth ret = new EastNorth( 158 anchorPosition.east() + x * scale, 159 anchorPosition.north() - y * scale 160 ); 161 return CoordinateConversion.llToCoor(tileProjection.eastNorth2latlon(ret)); 162 } 163 164 protected EastNorth getTileEastNorth(int x, int y, int z) { 165 double scale = getDegreesPerTile(z); 166 return new EastNorth( 167 anchorPosition.east() + x * scale, 168 anchorPosition.north() - y * scale 169 ); 170 } 171 172 private double getDegreesPerTile(int zoom) { 173 return degreesPerTile[zoom]; 174 } 175 176 @Override 177 public IProjected tileXYtoProjected(int x, int y, int zoom) { 178 EastNorth en = getTileEastNorth(x, y, zoom); 179 return new Projected(en.east(), en.north()); 180 } 181 182 @Override 183 public TileXY projectedToTileXY(IProjected p, int zoom) { 184 return eastNorthToTileXY(new EastNorth(p.getEast(), p.getNorth()), zoom); 185 } 186 187 @Override 188 public String getServerCRS() { 189 return this.tileProjection.toCode(); 190 } 191 192 protected String getBbox(int zoom, int tilex, int tiley, boolean switchLatLon) { 193 EastNorth nw = getTileEastNorth(tilex, tiley, zoom); 194 EastNorth se = getTileEastNorth(tilex + 1, tiley + 1, zoom); 195 196 double w = nw.getX(); 197 double n = nw.getY(); 198 199 double s = se.getY(); 200 double e = se.getX(); 201 202 return switchLatLon ? 203 getBboxstr(s, w, n, e) 204 : getBboxstr(w, s, e, n); 205 } 206 207 private static String getBboxstr(double x1, double x2, double x3, double x4) { 208 return LATLON_FORMAT.format(x1) + 209 ',' + 210 LATLON_FORMAT.format(x2) + 211 ',' + 212 LATLON_FORMAT.format(x3) + 213 ',' + 214 LATLON_FORMAT.format(x4); 215 } 216}