OpenStreetMapTileSource.java

  1. /*
  2.  * *************************************************************************************************************************************************************
  3.  *
  4.  * MapView: a JavaFX map renderer for tile-based servers
  5.  * http://tidalwave.it/projects/mapview
  6.  *
  7.  * Copyright (C) 2024 - 2025 by Tidalwave s.a.s. (http://tidalwave.it)
  8.  *
  9.  * *************************************************************************************************************************************************************
  10.  *
  11.  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
  12.  * You may obtain a copy of the License at
  13.  *
  14.  *     http://www.apache.org/licenses/LICENSE-2.0
  15.  *
  16.  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  17.  * CONDITIONS OF ANY KIND, either express or implied.  See the License for the specific language governing permissions and limitations under the License.
  18.  *
  19.  * *************************************************************************************************************************************************************
  20.  *
  21.  * git clone https://bitbucket.org/tidalwave/mapview-src
  22.  * git clone https://github.com/tidalwave-it/mapview-src
  23.  *
  24.  * *************************************************************************************************************************************************************
  25.  */
  26. package it.tidalwave.mapviewer;

  27. import jakarta.annotation.Nonnull;
  28. import java.net.URI;
  29. import java.net.URISyntaxException;
  30. import it.tidalwave.mapviewer.spi.MercatorProjection;
  31. import it.tidalwave.mapviewer.spi.TileSourceSupport;
  32. import lombok.extern.slf4j.Slf4j;

  33. /***************************************************************************************************************************************************************
  34.  *
  35.  * The OpenStreetMap source of map tiles.
  36.  *
  37.  * @author  Fabrizio Giudici
  38.  *
  39.  **************************************************************************************************************************************************************/
  40. @Slf4j
  41. public class OpenStreetMapTileSource extends TileSourceSupport
  42.   {
  43.     private static final int TILE_SIZE = 256;

  44.     private static final int TOP_ZOOM_LEVEL = 19;
  45.    
  46.     private static final int DEFAULT_ZOOM_LEVEL = 9;

  47.     /** The pattern, in {@link String#format(String, Object...)} syntax, of the URI. */
  48.     @Nonnull
  49.     protected final String pattern;

  50.     /***********************************************************************************************************************************************************
  51.      * Creates a new instance.
  52.      **********************************************************************************************************************************************************/
  53.     public OpenStreetMapTileSource()
  54.       {
  55.         this(TOP_ZOOM_LEVEL, "https://tile.openstreetmap.org/%d/%d/%d.png", "OpenStreetMap", "OpenStreetMap");
  56.       }

  57.     /***********************************************************************************************************************************************************
  58.      * Constructor for subclasses.
  59.      * @param   maxZoom         the maximum allowed zoom level
  60.      * @param   pattern         the URI pattern
  61.      * @param   displayName     the display name of the provider
  62.      * @param   cachePrefix     the prefix for the cache
  63.      **********************************************************************************************************************************************************/
  64.     protected OpenStreetMapTileSource (final int maxZoom,
  65.                                        @Nonnull final String pattern,
  66.                                        @Nonnull final String displayName,
  67.                                        @Nonnull final String cachePrefix)
  68.       {
  69.         super(new MercatorProjection(TILE_SIZE), 1, maxZoom, DEFAULT_ZOOM_LEVEL, TILE_SIZE, displayName, cachePrefix);
  70.         this.pattern = pattern;
  71.       }

  72.     /***********************************************************************************************************************************************************
  73.      * {@inheritDoc}
  74.      **********************************************************************************************************************************************************/
  75.     @Override @Nonnull
  76.     public final URI getTileUri (final int column, final int row, final int zoom)
  77.       {
  78.         try
  79.           {
  80.             return new URI(String.format(pattern, zoom, column, row));
  81.           }
  82.         catch (URISyntaxException e)
  83.           {
  84.             throw new RuntimeException(e);
  85.           }
  86.       }

  87.     /***********************************************************************************************************************************************************
  88.      * {@inheritDoc}
  89.      **********************************************************************************************************************************************************/
  90.     @Override @Nonnull
  91.     public String toString()
  92.       {
  93.         return getClass().getSimpleName();
  94.       }
  95.   }