View Javadoc
1   package fr.ifremer.dali.map.osm;
2   
3   /*-
4    * #%L
5    * Dali :: UI
6    * %%
7    * Copyright (C) 2017 - 2018 Ifremer
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Affero General Public License as published by
11   * the Free Software Foundation, either version 3 of the License, or
12   * (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU Affero General Public License
20   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21   * #L%
22   */
23  
24  import org.geotools.tile.Tile;
25  import org.geotools.tile.TileFactory;
26  import org.geotools.tile.TileService;
27  import org.geotools.tile.impl.WebMercatorTileService;
28  import org.geotools.tile.impl.ZoomLevel;
29  import org.geotools.tile.impl.osm.OSMTileFactory;
30  
31  /**
32   * Fixed Tile service
33   *
34   * @author peck7 on 15/12/2017.
35   */
36  public class OSMTileFactory2 extends OSMTileFactory {
37  
38      /**
39       * Overridden method that return a correct Tile
40       */
41      @Override
42      public Tile findTileAtCoordinate(double lon, double lat, ZoomLevel zoomLevel, TileService service) {
43          lat = TileFactory.normalizeDegreeValue(lat, 90);
44          lon = TileFactory.normalizeDegreeValue(lon, 180);
45  
46          lat = OSMTileFactory.moveInRange(lat, WebMercatorTileService.MIN_LATITUDE, WebMercatorTileService.MAX_LATITUDE);
47  
48          int xTile = (int) Math.floor((lon + 180) / 360 * (1 << zoomLevel.getZoomLevel()));
49          int yTile = (int) Math.floor(
50                  (1 - Math.log(Math.tan(lat * Math.PI / 180) + 1 / Math.cos(lat * Math.PI / 180))
51                          / Math.PI) / 2 * (1 << zoomLevel.getZoomLevel()));
52  
53          // FIX LP : Normalize x & y to not return x or y < 0
54          xTile = Math.max(xTile, 0);
55          yTile = Math.max(yTile, 0);
56  
57          return new OSMTile2(xTile, yTile, zoomLevel, (OSMService2) service);
58      }
59  
60      @Override
61      public Tile findLowerNeighbour(Tile tile, TileService service) {
62          return new OSMTile2(tile.getTileIdentifier().getLowerNeighbour(), (OSMService2) service);
63      }
64  
65      @Override
66      public Tile findRightNeighbour(Tile tile, TileService service) {
67          return new OSMTile2(tile.getTileIdentifier().getRightNeighbour(), (OSMService2) service);
68      }
69  }