Changeset 36223 in osm for applications/viewer/jmapviewer/src
- Timestamp:
- 2024-03-11T21:33:09+01:00 (10 months ago)
- Location:
- applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer
- Files:
-
- 1 added
- 22 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/AttributionSupport.java
r33332 r36223 19 19 20 20 public class AttributionSupport { 21 public static final Font ATTR_FONT = new Font("Arial", Font.PLAIN, 10); 22 public static final Font ATTR_LINK_FONT; 21 23 22 24 private Attributed source; … … 25 27 private String attrTermsText; 26 28 private String attrTermsUrl; 27 public static final Font ATTR_FONT = new Font("Arial", Font.PLAIN, 10);28 public static final Font ATTR_LINK_FONT;29 29 30 30 protected Rectangle attrTextBounds; -
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/Coordinate.java
r35355 r36223 74 74 public boolean equals(Object o) { 75 75 if (this == o) return true; 76 if ( !(o instanceof Coordinate)) return false;76 if (o == null || !this.getClass().equals(o.getClass())) return false; 77 77 Coordinate that = (Coordinate) o; 78 78 return Double.compare(that.x, x) == 0 && Double.compare(that.y, y) == 0; -
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/DefaultMapController.java
r32724 r36223 137 137 break; 138 138 default: 139 throw new RuntimeException("Unsupported button");139 throw new JMapViewerRuntimeException("Unsupported button"); 140 140 } 141 141 } -
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/Demo.java
r34766 r36223 5 5 import java.awt.Cursor; 6 6 import java.awt.Point; 7 import java.awt.event.ItemEvent;8 import java.awt.event.ItemListener;9 7 import java.awt.event.MouseAdapter; 10 8 import java.awt.event.MouseEvent; … … 84 82 new BingAerialTileSource(), 85 83 }); 86 tileSourceSelector.addItemListener(new ItemListener() { 87 @Override 88 public void itemStateChanged(ItemEvent e) { 89 map().setTileSource((TileSource) e.getItem()); 90 } 91 }); 84 tileSourceSelector.addItemListener(e -> map().setTileSource((TileSource) e.getItem())); 92 85 JComboBox<TileLoader> tileLoaderSelector; 93 86 tileLoaderSelector = new JComboBox<>(new TileLoader[] {new OsmTileLoader(map())}); 94 tileLoaderSelector.addItemListener(new ItemListener() { 95 @Override 96 public void itemStateChanged(ItemEvent e) { 97 map().setTileLoader((TileLoader) e.getItem()); 98 } 99 }); 87 tileLoaderSelector.addItemListener(e -> map().setTileLoader((TileLoader) e.getItem())); 100 88 map().setTileLoader((TileLoader) tileLoaderSelector.getSelectedItem()); 101 89 panelTop.add(tileSourceSelector); -
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/FeatureAdapter.java
r35321 r36223 299 299 try { 300 300 Desktop.getDesktop().browse(new URI(url)); 301 } catch (IOException e) { 302 e.printStackTrace(); 303 } catch (URISyntaxException e) { 304 e.printStackTrace(); 301 } catch (IOException | URISyntaxException e) { 302 getLogger(FeatureAdapter.class).log(Level.SEVERE, e.getMessage(), e); 305 303 } 306 304 } else { -
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/JMapViewer.java
r35533 r36223 13 13 import java.util.Collections; 14 14 import java.util.List; 15 import java.util.logging.Level; 15 16 16 17 import javax.swing.ImageIcon; … … 137 138 tileSource = new OsmTileSource.Mapnik(); 138 139 tileController = new TileController(tileSource, tileCache, this); 139 mapMarkerList = Collections.synchronizedList(new ArrayList< MapMarker>());140 mapPolygonList = Collections.synchronizedList(new ArrayList< MapPolygon>());141 mapRectangleList = Collections.synchronizedList(new ArrayList< MapRectangle>());140 mapMarkerList = Collections.synchronizedList(new ArrayList<>()); 141 mapPolygonList = Collections.synchronizedList(new ArrayList<>()); 142 mapRectangleList = Collections.synchronizedList(new ArrayList<>()); 142 143 mapMarkersVisible = true; 143 144 mapRectanglesVisible = true; … … 197 198 return new ImageIcon(FeatureAdapter.readImage(url)); 198 199 } catch (IOException e) { 199 e.printStackTrace();200 FeatureAdapter.getLogger(JMapViewer.class).log(Level.FINE, e.getMessage(), e); 200 201 } 201 202 } … … 502 503 else if (p != null) { 503 504 Integer radius = getLatOffset(marker.getLat(), marker.getLon(), marker.getRadius(), false); 504 radius = radius == null ? null : p.y - radius;505 radius = radius == null ? null : (p.y - radius); 505 506 return radius; 506 507 } else … … 543 544 public double getMeterPerPixel() { 544 545 Point origin = new Point(5, 5); 545 Point center = new Point(getWidth() / 2, getHeight() / 2);546 547 double pDistance = center .distance(origin);546 Point centerPixel = new Point(getWidth() / 2, getHeight() / 2); 547 548 double pDistance = centerPixel.distance(origin); 548 549 549 550 ICoordinate originCoord = getPosition(origin); 550 ICoordinate centerCoord = getPosition(center );551 ICoordinate centerCoord = getPosition(centerPixel); 551 552 552 553 double mDistance = tileSource.getDistance(originCoord.getLat(), originCoord.getLon(), … … 560 561 super.paintComponent(g); 561 562 562 int iMove = 0;563 int iMove; 563 564 564 565 int tilesize = tileSource.getTileSize(); … … 1101 1102 public void setTileSource(TileSource tileSource) { 1102 1103 if (tileSource.getMaxZoom() > MAX_ZOOM) 1103 throw new RuntimeException("Maximum zoom level too high");1104 throw new JMapViewerRuntimeException("Maximum zoom level too high"); 1104 1105 if (tileSource.getMinZoom() < MIN_ZOOM) 1105 throw new RuntimeException("Minimum zoom level too low");1106 throw new JMapViewerRuntimeException("Minimum zoom level too low"); 1106 1107 ICoordinate position = getPosition(); 1107 1108 this.tileSource = tileSource; -
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/MemoryTileCache.java
r35527 r36223 4 4 import java.util.HashMap; 5 5 import java.util.Map; 6 import java.util.logging.Level; 6 7 import java.util.logging.Logger; 7 8 … … 18 19 public class MemoryTileCache implements TileCache { 19 20 20 protected static final Logger log = Logger.getLogger(MemoryTileCache.class.getName()); 21 private static final Logger LOGGER = Logger.getLogger(MemoryTileCache.class.getName()); 22 /** 23 * @deprecated since 2.20 (create a class specific logger) 24 */ 25 @Deprecated 26 protected static final Logger log = LOGGER; 21 27 22 28 /** … … 79 85 } 80 86 } catch (NullPointerException e) { 81 log.warning(e.getMessage());87 LOGGER.log(Level.WARNING, e.getMessage(), e); 82 88 } 83 89 } -
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/OsmMercator.java
r35190 r36223 58 58 */ 59 59 public long getMaxPixels(int aZoomlevel) { 60 return tileSize * (1 << aZoomlevel);60 return tileSize * (1L << aZoomlevel); 61 61 } 62 62 … … 162 162 long mp = getMaxPixels(aZoomlevel); 163 163 double y = mp * (0.5 - (log / (4.0 * Math.PI))); 164 return Math.min(y, mp - 1 );164 return Math.min(y, mp - 1d); 165 165 } 166 166 -
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/OsmTileLoader.java
r35526 r36223 177 177 // ignore malformed Cache-Control headers 178 178 if (JMapViewer.debug) { 179 System.err.println(e.getMessage());179 LOG.log(Level.FINE, e.getMessage(), e); 180 180 } 181 181 } -
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/Projected.java
r35341 r36223 62 62 if (this == obj) 63 63 return true; 64 if ( !(obj instanceof Projected))64 if (obj == null || !this.getClass().equals(obj.getClass())) 65 65 return false; 66 66 final Projected other = (Projected) obj; -
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/Style.java
r32030 r36223 8 8 9 9 public class Style { 10 private static final AlphaComposite TRANSPARENCY = AlphaComposite.getInstance(AlphaComposite.SRC_OVER); 11 private static final AlphaComposite OPAQUE = AlphaComposite.getInstance(AlphaComposite.SRC); 12 10 13 private Color color; 11 14 private Color backColor; 12 15 private Stroke stroke; 13 16 private Font font; 14 15 private static final AlphaComposite TRANSPARENCY = AlphaComposite.getInstance(AlphaComposite.SRC_OVER);16 private static final AlphaComposite OPAQUE = AlphaComposite.getInstance(AlphaComposite.SRC);17 17 18 18 public Style() { -
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/Tile.java
r35527 r36223 12 12 import java.util.Objects; 13 13 import java.util.concurrent.Callable; 14 import java.util.logging.Level; 14 15 15 16 import javax.imageio.ImageIO; … … 84 85 return FeatureAdapter.readImage(JMapViewer.class.getResource(path)); 85 86 } catch (IOException | IllegalArgumentException ex) { 86 ex.printStackTrace();87 FeatureAdapter.getLogger(Tile.class).log(Level.SEVERE, ex.getMessage(), ex); 87 88 return null; 88 89 } … … 110 111 } catch (Exception e) { 111 112 // this should not happen here 112 throw new RuntimeException(e);113 throw new JMapViewerRuntimeException(e); 113 114 } 114 115 } … … 125 126 * this way we can avoid object creation until we're sure it's needed 126 127 */ 127 final CachedCallable<BufferedImage> tmpImage = new CachedCallable<>(new Callable<BufferedImage>() { 128 @Override 129 public BufferedImage call() throws Exception { 130 return new BufferedImage(source.getTileSize(), source.getTileSize(), BufferedImage.TYPE_INT_ARGB); 131 } 132 }); 128 final CachedCallable<BufferedImage> tmpImage = new CachedCallable<>(() -> 129 new BufferedImage(source.getTileSize(), source.getTileSize(), BufferedImage.TYPE_INT_ARGB)); 133 130 134 131 for (int zoomDiff = 1; zoomDiff < 5; zoomDiff++) { … … 146 143 * something to draw 147 144 */ 148 CachedCallable<Graphics2D> graphics = new CachedCallable<>(new Callable<Graphics2D>() { 149 @Override 150 public Graphics2D call() throws Exception { 151 Graphics2D g = (Graphics2D) tmpImage.call().getGraphics(); 152 g.setTransform(AffineTransform.getScaleInstance(scale, scale)); 153 return g; 154 } 145 CachedCallable<Graphics2D> graphics = new CachedCallable<>(() -> { 146 Graphics2D g = (Graphics2D) tmpImage.call().getGraphics(); 147 g.setTransform(AffineTransform.getScaleInstance(scale, scale)); 148 return g; 155 149 }); 156 150 … … 177 171 final int factor = 1 << zoomDiff; 178 172 final double scale = factor; 179 CachedCallable<Graphics2D> graphics = new CachedCallable<>(new Callable<Graphics2D>() { 180 @Override 181 public Graphics2D call() throws Exception { 182 Graphics2D g = (Graphics2D) tmpImage.call().getGraphics(); 183 AffineTransform at = new AffineTransform(); 184 int translateX = (xtile % factor) * source.getTileSize(); 185 int translateY = (ytile % factor) * source.getTileSize(); 186 at.setTransform(scale, 0, 0, scale, -translateX, -translateY); 187 g.setTransform(at); 188 return g; 189 } 190 173 CachedCallable<Graphics2D> graphics = new CachedCallable<>(() -> { 174 Graphics2D g = (Graphics2D) tmpImage.call().getGraphics(); 175 AffineTransform at = new AffineTransform(); 176 int translateX = (xtile % factor) * source.getTileSize(); 177 int translateY = (ytile % factor) * source.getTileSize(); 178 at.setTransform(scale, 0, 0, scale, -translateX, -translateY); 179 g.setTransform(at); 180 return g; 191 181 }); 192 182 … … 341 331 if (this == obj) 342 332 return true; 343 if (obj == null || ! (obj instanceof Tile))333 if (obj == null || !this.getClass().equals(obj.getClass())) 344 334 return false; 345 335 final Tile other = (Tile) obj; -
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/checkBoxTree/CheckBoxNodeEditor.java
r32030 r36223 3 3 4 4 import java.awt.Component; 5 import java.awt.event.ItemEvent;6 5 import java.awt.event.ItemListener; 7 6 import java.awt.event.MouseAdapter; … … 85 84 86 85 // editor always selected / focused 87 final ItemListener itemListener = new ItemListener() { 88 89 @Override 90 public void itemStateChanged(final ItemEvent itemEvent) { 91 if (stopCellEditing()) { 92 fireEditingStopped(); 93 } 86 final ItemListener itemListener = itemEvent -> { 87 if (stopCellEditing()) { 88 fireEditingStopped(); 94 89 } 95 90 }; -
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/checkBoxTree/CheckBoxNodePanel.java
r32030 r36223 33 33 check.getModel().setArmed(true); 34 34 } else { 35 check.setSelected(bool .booleanValue());35 check.setSelected(bool); 36 36 check.getModel().setArmed(false); 37 37 } -
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/events/JMVCommandEvent.java
r32682 r36223 17 17 } 18 18 19 private COMMAND command;20 19 /** 21 20 * 22 21 */ 23 22 private static final long serialVersionUID = 8701544867914969620L; 23 24 private COMMAND command; 24 25 25 26 public JMVCommandEvent(COMMAND cmd, Object source) { -
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/interfaces/IProjected.java
r33285 r36223 4 4 /** 5 5 * Projected coordinates (east / north space). 6 * 6 * <p> 7 7 * For most projections, one unit in projected space is roughly one meter, but 8 8 * can also be degrees or feet. -
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/tilesources/AbstractTMSTileSource.java
r36138 r36223 179 179 for (final int v : byteDigest) { 180 180 int vn = (v & 0xf0) >> 4; 181 hexChars[j++] = (char) (vn + (vn >= 10 ? 'a' - 10: '0'));181 hexChars[j++] = (char) (vn + (vn >= 10 ? ('a' - 10) : '0')); 182 182 vn = (v & 0xf); 183 hexChars[j++] = (char) (vn + (vn >= 10 ? 'a' - 10: '0'));183 hexChars[j++] = (char) (vn + (vn >= 10 ? ('a' - 10) : '0')); 184 184 } 185 185 for (String val: searchEntry.getValue()) { -
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/tilesources/BingAerialTileSource.java
r36174 r36223 32 32 import org.openstreetmap.gui.jmapviewer.FeatureAdapter; 33 33 import org.openstreetmap.gui.jmapviewer.JMapViewer; 34 import org.openstreetmap.gui.jmapviewer.JMapViewerRuntimeException; 34 35 import org.openstreetmap.gui.jmapviewer.interfaces.ICoordinate; 35 36 import org.w3c.dom.Document; … … 62 63 /** Original Bing API key created by Potlatch2 developers in 2010 */ 63 64 private static final String API_KEY = "Arzdiw4nlOJzRwOz__qailc8NiR31Tt51dN2D7cm57NrnceZnCpgOkmJhNpGoppU"; 64 65 66 private static final Pattern PATTERN_SUBDOMAIN = Pattern.compile("\\{subdomain}"); 67 private static final Pattern PATTERN_QUADKEY = Pattern.compile("\\{quadkey}"); 68 private static final Pattern PATTERN_CULTURE = Pattern.compile("\\{culture}"); 69 65 70 private volatile Future<List<Attribution>> attributions; // volatile is required for getAttribution(), see below. 66 71 private String imageUrlTemplate; 67 72 private int imageryZoomMax = Integer.MIN_VALUE; 68 73 private String[] subdomains; 69 70 private static final Pattern subdomainPattern = Pattern.compile("\\{subdomain}");71 private static final Pattern quadkeyPattern = Pattern.compile("\\{quadkey}");72 private static final Pattern culturePattern = Pattern.compile("\\{culture}");73 74 private String brandLogoUri; 74 75 private String layer = "Aerial"; … … 108 109 109 110 String url = imageUrlTemplate; 110 url = subdomainPattern.matcher(url).replaceAll(subdomain);111 url = quadkeyPattern.matcher(url).replaceAll(computeQuadTree(zoom, tilex, tiley));111 url = PATTERN_SUBDOMAIN.matcher(url).replaceAll(subdomain); 112 url = PATTERN_QUADKEY.matcher(url).replaceAll(computeQuadTree(zoom, tilex, tiley)); 112 113 113 114 return url; … … 289 290 return attributions.get(); 290 291 } catch (ExecutionException ex) { 291 throw new RuntimeException(ex);292 throw new JMapViewerRuntimeException(ex); 292 293 } catch (InterruptedException ign) { 293 294 LOG.log(Level.SEVERE, "InterruptedException: {0}", ign.getMessage()); … … 319 320 String noHttpTemplate = template.replace("http://ecn.{subdomain}.tiles.virtualearth.net/", 320 321 "https://ecn.{subdomain}.tiles.virtualearth.net/"); 321 this.imageUrlTemplate = culturePattern.matcher(noHttpTemplate).replaceAll(Locale.getDefault().toString());322 this.imageUrlTemplate = PATTERN_CULTURE.matcher(noHttpTemplate).replaceAll(Locale.getDefault().toString()); 322 323 } 323 324 -
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/tilesources/OsmTileSource.java
r34766 r36223 32 32 @Override 33 33 public String getBaseUrl() { 34 String url = String.format(this.baseUrl, new Object[] {SERVER[serverNum]});34 String url = String.format(this.baseUrl, SERVER[serverNum]); 35 35 serverNum = (serverNum + 1) % SERVER.length; 36 36 return url; … … 58 58 @Override 59 59 public String getBaseUrl() { 60 String url = String.format(this.baseUrl, new Object[] {SERVER[serverNum]});60 String url = String.format(this.baseUrl, SERVER[serverNum]); 61 61 serverNum = (serverNum + 1) % SERVER.length; 62 62 return url; … … 65 65 /** 66 66 * Get the thunderforest API key. 67 * 68 * Needs to be registered at their web 67 * <p> 68 * Needs to be registered at their website. 69 69 * @return the API key 70 70 */ -
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/tilesources/ScanexTileSource.java
r34777 r36223 14 14 /** 15 15 * This tilesource uses different to OsmMercator projection. 16 * 16 * <p> 17 17 * Earth is assumed an ellipsoid in this projection, unlike 18 18 * sphere in OsmMercator, so latitude calculation differs a lot. 19 * 19 * <p> 20 20 * The longitude calculation is the same as in OsmMercator, 21 21 * we inherit it from AbstractTMSTileSource. … … 24 24 */ 25 25 public class ScanexTileSource extends TMSTileSource { 26 private static final String DEFAULT_URL = "http ://maps.kosmosnimki.ru";26 private static final String DEFAULT_URL = "https://maps.kosmosnimki.ru"; 27 27 private static final int DEFAULT_MAXZOOM = 14; 28 28 private static final String API_KEY = "4018C5A9AECAD8868ED5DEB2E41D09F7"; 29 30 // Latitude to Y and back calculations. 31 private static final double RADIUS_E = 6_378_137; /* radius of Earth at equator, m */ 32 private static final double EQUATOR = 40075016.68557849; /* equator length, m */ 33 private static final double E = 0.0818191908426; /* eccentricity of Earth's ellipsoid */ 29 34 30 35 private enum ScanexLayer { … … 50 55 /** IRS by default */ 51 56 private ScanexLayer layer = ScanexLayer.IRS; 52 private TemplatedTMSTileSource TemplateSource = null;57 private TemplatedTMSTileSource templateSource; 53 58 54 59 /** cached latitude used in {@link #tileYToLat(double, int)} */ … … 63 68 String url = info.getUrl(); 64 69 65 /* *70 /* 66 71 * The formulae in tileYToLat() and latToTileY() have 2^8 67 72 * hardcoded in them, so explicitly state that. For now 68 * the assignment matches OsmMercator.DEFAUL _TILE_SIZE, and73 * the assignment matches OsmMercator.DEFAULT_TILE_SIZE, and 69 74 * thus is extraneous. But let it be there just in case if 70 75 * OsmMercator changes. … … 82 87 } 83 88 } 84 /* *If not "irs" or "spot" keyword, then a custom URL. */89 /* If not "irs" or "spot" keyword, then a custom URL. */ 85 90 TemplatedTMSTileSource.checkUrl(info.getUrl()); 86 this. TemplateSource = new TemplatedTMSTileSource(info);91 this.templateSource = new TemplatedTMSTileSource(info); 87 92 } 88 93 … … 94 99 @Override 95 100 public String getTileUrl(int zoom, int tilex, int tiley) { 96 if (this. TemplateSource != null)97 return this. TemplateSource.getTileUrl(zoom, tilex, tiley);101 if (this.templateSource != null) 102 return this.templateSource.getTileUrl(zoom, tilex, tiley); 98 103 else 99 104 return this.getBaseUrl() + getTilePath(zoom, tilex, tiley); … … 102 107 @Override 103 108 public String getTilePath(int zoom, int tilex, int tiley) { 104 int tmp = (int) Math.pow(2.0, zoom - 1 );109 int tmp = (int) Math.pow(2.0, zoom - 1d); 105 110 106 111 tilex = tilex - tmp; … … 109 114 return this.layer.getUri() + "&apikey=" + API_KEY + "&x=" + tilex + "&y=" + tiley + "&z=" + zoom; 110 115 } 111 112 // Latitude to Y and back calculations.113 private static final double RADIUS_E = 6378137; /* radius of Earth at equator, m */114 private static final double EQUATOR = 40075016.68557849; /* equator length, m */115 private static final double E = 0.0818191908426; /* eccentricity of Earth's ellipsoid */116 116 117 117 @Override … … 143 143 return new Coordinate( 144 144 tileYToLat(y, zoom), 145 osmMercator.xToLon( x * getTileSize(), zoom)145 osmMercator.xToLon((long) x * getTileSize(), zoom) 146 146 ); 147 147 } 148 148 149 private double latToTileY(double lat, int zoom) {149 private static double latToTileY(double lat, int zoom) { 150 150 double tmp = Math.tan(Math.PI/4 * (1 + lat/90)); 151 151 double pow = Math.pow(Math.tan(Math.PI/4 + Math.asin(E * Math.sin(Math.toRadians(lat)))/2), E); … … 183 183 double cosl = Math.cos(lat); 184 184 185 zoom = (int) Math.pow(2.0, zoom - 1 );185 zoom = (int) Math.pow(2.0, zoom - 1d); 186 186 double ec = Math.exp((1 - y/zoom)*Math.PI); 187 187 -
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/tilesources/TMSTileSource.java
r33313 r36223 75 75 public ICoordinate tileXYToLatLon(int x, int y, int zoom) { 76 76 return new Coordinate( 77 osmMercator.yToLat( y * getTileSize(), zoom),78 osmMercator.xToLon( x * getTileSize(), zoom)77 osmMercator.yToLat((long) y * getTileSize(), zoom), 78 osmMercator.xToLon((long) x * getTileSize(), zoom) 79 79 ); 80 80 } -
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/tilesources/TemplatedTMSTileSource.java
r35918 r36223 17 17 * Handles templated TMS Tile Source. Templated means, that some patterns within 18 18 * URL gets substituted. 19 * 19 * <p> 20 20 * Supported parameters 21 * {zoom} - substituted with zoom level 22 * {z} - as above 23 * {NUMBER-zoom} - substituted with result of equation "NUMBER - zoom", 24 * eg. {20-zoom} for zoom level 15 will result in 5 in this place 25 * {zoom+number} - substituted with result of equation "zoom + number", 26 * eg. {zoom+5} for zoom level 15 will result in 20. 27 * {x} - substituted with X tile number 28 * {y} - substituted with Y tile number 29 * {!y} - substituted with Yahoo Y tile number 30 * {-y} - substituted with reversed Y tile number 31 * {apikey} - substituted with API key retrieved for the imagery id 32 * {switch:VAL_A,VAL_B,VAL_C,...} - substituted with one of VAL_A, VAL_B, VAL_C. Usually 33 * used to specify many tile servers 34 * {header:(HEADER_NAME,HEADER_VALUE)} - sets the headers to be sent to tile server 21 * <ul> 22 * <li>{zoom} - substituted with zoom level</li> 23 * <li>{z} - as above</li> 24 * <li>{NUMBER-zoom} - substituted with result of equation "NUMBER - zoom", 25 * eg. {20-zoom} for zoom level 15 will result in 5 in this place</li> 26 * <li>{zoom+number} - substituted with result of equation "zoom + number", 27 * eg. {zoom+5} for zoom level 15 will result in 20.</li> 28 * <li>{x} - substituted with X tile number</li> 29 * <li>{y} - substituted with Y tile number</li> 30 * <li>{!y} - substituted with Yahoo Y tile number</li> 31 * <li>{-y} - substituted with reversed Y tile number</li> 32 * <li>{apikey} - substituted with API key retrieved for the imagery id</li> 33 * <li>{switch:VAL_A,VAL_B,VAL_C,...} - substituted with one of VAL_A, VAL_B, VAL_C. Usually 34 * used to specify many tile servers</li> 35 * <li>{header:(HEADER_NAME,HEADER_VALUE)} - sets the headers to be sent to tile server</li> 36 * </ul> 35 37 */ 36 38 public class TemplatedTMSTileSource extends TMSTileSource implements TemplatedTileSource { 39 40 // CHECKSTYLE.OFF: SingleSpaceSeparator 41 private static final String COOKIE_HEADER = "Cookie"; 42 private static final Pattern PATTERN_ZOOM = Pattern.compile("\\{(?:(\\d+)-)?z(?:oom)?([+-]\\d+)?}"); 43 private static final Pattern PATTERN_X = Pattern.compile("\\{x}"); 44 private static final Pattern PATTERN_Y = Pattern.compile("\\{y}"); 45 private static final Pattern PATTERN_Y_YAHOO = Pattern.compile("\\{!y}"); 46 private static final Pattern PATTERN_NEG_Y = Pattern.compile("\\{-y}"); 47 private static final Pattern PATTERN_SWITCH = Pattern.compile("\\{switch:([^}]+)}"); 48 private static final Pattern PATTERN_HEADER = Pattern.compile("\\{header\\(([^,]+),([^}]+)\\)}"); 49 private static final Pattern PATTERN_API_KEY = Pattern.compile("\\{apikey}"); 50 private static final Pattern PATTERN_PARAM = Pattern.compile("\\{((?:\\d+-)?z(?:oom)?(:?[+-]\\d+)?|x|y|!y|-y|switch:([^}]+))}"); 51 52 // CHECKSTYLE.ON: SingleSpaceSeparator 53 54 private static final Pattern[] ALL_PATTERNS = { 55 PATTERN_HEADER, PATTERN_ZOOM, PATTERN_X, PATTERN_Y, PATTERN_Y_YAHOO, PATTERN_NEG_Y, PATTERN_SWITCH, PATTERN_API_KEY 56 }; 37 57 38 58 private Random rand; 39 59 private String[] randomParts; 40 60 private final Map<String, String> headers = new HashMap<>(); 41 private boolean inverse_zoom = false; 42 private int zoom_offset = 0; 43 44 // CHECKSTYLE.OFF: SingleSpaceSeparator 45 private static final String COOKIE_HEADER = "Cookie"; 46 private static final Pattern PATTERN_ZOOM = Pattern.compile("\\{(?:(\\d+)-)?z(?:oom)?([+-]\\d+)?\\}"); 47 private static final Pattern PATTERN_X = Pattern.compile("\\{x\\}"); 48 private static final Pattern PATTERN_Y = Pattern.compile("\\{y\\}"); 49 private static final Pattern PATTERN_Y_YAHOO = Pattern.compile("\\{!y\\}"); 50 private static final Pattern PATTERN_NEG_Y = Pattern.compile("\\{-y\\}"); 51 private static final Pattern PATTERN_SWITCH = Pattern.compile("\\{switch:([^}]+)\\}"); 52 private static final Pattern PATTERN_HEADER = Pattern.compile("\\{header\\(([^,]+),([^}]+)\\)\\}"); 53 private static final Pattern PATTERN_API_KEY = Pattern.compile("\\{apikey\\}"); 54 private static final Pattern PATTERN_PARAM = Pattern.compile("\\{((?:\\d+-)?z(?:oom)?(:?[+-]\\d+)?|x|y|!y|-y|switch:([^}]+))\\}"); 55 56 // CHECKSTYLE.ON: SingleSpaceSeparator 57 58 private static final Pattern[] ALL_PATTERNS = { 59 PATTERN_HEADER, PATTERN_ZOOM, PATTERN_X, PATTERN_Y, PATTERN_Y_YAHOO, PATTERN_NEG_Y, PATTERN_SWITCH, PATTERN_API_KEY 60 }; 61 private boolean inverse_zoom; 62 private int zoom_offset; 61 63 62 64 /** … … 137 139 case "z": // PATTERN_ZOOM 138 140 case "zoom": 139 replacement = Integer.toString((inverse_zoom ? - 1 *zoom : zoom) + zoom_offset);141 replacement = Integer.toString((inverse_zoom ? -zoom : zoom) + zoom_offset); 140 142 break; 141 143 case "x": // PATTERN_X … … 146 148 break; 147 149 case "!y": // PATTERN_Y_YAHOO 148 replacement = Integer.toString((int) Math.pow(2, zoom -1)-1-tiley);150 replacement = Integer.toString((int) Math.pow(2, zoom - 1d) - 1 - tiley); 149 151 break; 150 152 case "-y": // PATTERN_NEG_Y … … 157 159 // handle switch/zoom here, as group will contain parameters and switch will not work 158 160 if (PATTERN_ZOOM.matcher("{" + matcher.group(1) + "}").matches()) { 159 replacement = Integer.toString((inverse_zoom ? - 1 *zoom : zoom) + zoom_offset);161 replacement = Integer.toString((inverse_zoom ? -zoom : zoom) + zoom_offset); 160 162 } else if (PATTERN_SWITCH.matcher("{" + matcher.group(1) + "}").matches()) { 161 163 replacement = getRandomPart(randomParts); … … 180 182 public static void checkUrl(String url) { 181 183 assert url != null && !"".equals(url) : "URL cannot be null or empty"; 182 Matcher m = Pattern.compile("\\{[^}]* \\}").matcher(url);184 Matcher m = Pattern.compile("\\{[^}]*}").matcher(url); 183 185 while (m.find()) { 184 186 boolean isSupportedPattern = Arrays.stream(ALL_PATTERNS).anyMatch(pattern -> pattern.matcher(m.group()).matches());
Note:
See TracChangeset
for help on using the changeset viewer.