MercatorProjection.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.spi;

  27. import jakarta.annotation.Nonnull;
  28. import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
  29. import it.tidalwave.mapviewer.MapCoordinates;
  30. import it.tidalwave.mapviewer.MapPoint;
  31. import it.tidalwave.mapviewer.Projection;
  32. import lombok.RequiredArgsConstructor;
  33. import static java.lang.Math.*;

  34. /***************************************************************************************************************************************************************
  35.  *
  36.  * An implementation of the Mercator Projection.
  37.  *
  38.  * @author  Fabrizio Giudici
  39.  *
  40.  **************************************************************************************************************************************************************/
  41. @RequiredArgsConstructor
  42. public class MercatorProjection implements Projection
  43.   {
  44.     private static final double EARTH_RADIUS = 6378137;

  45.     private static final double EARTH_CIRCUMFERENCE = EARTH_RADIUS * 2.0 * PI;

  46.     private final int tileSize;

  47.     /***********************************************************************************************************************************************************
  48.      * {@inheritDoc}
  49.      **********************************************************************************************************************************************************/
  50.     @Override @Nonnull
  51.     public MapPoint coordinatesToMapPoint (@Nonnull final MapCoordinates coordinates, final double zoomLevel)
  52.       {
  53.         final double arc = arc(zoomLevel);
  54.         final double sinLat = sin(toRadians(coordinates.latitude()));
  55.         final double metersX = EARTH_RADIUS * toRadians(coordinates.longitude());
  56.         final double metersY = EARTH_RADIUS / 2 * log((1 + sinLat) / (1 - sinLat));
  57.         final double x = (EARTH_CIRCUMFERENCE / 2 + metersX) / arc;
  58.         final double y = (EARTH_CIRCUMFERENCE / 2 - metersY) / arc;
  59.         return MapPoint.of(x, y);
  60.       }

  61.     /***********************************************************************************************************************************************************
  62.      * {@inheritDoc}
  63.      **********************************************************************************************************************************************************/
  64.     @Override @Nonnull @SuppressFBWarnings("FL_FLOATS_AS_LOOP_COUNTERS")
  65.     public MapCoordinates mapPointToCoordinates (@Nonnull final MapPoint mapPoint, final double zoomLevel)
  66.       {
  67.         final double arc = arc(zoomLevel);
  68.         final double metersX = mapPoint.x() * arc - EARTH_CIRCUMFERENCE / 2;
  69.         final double metersY = EARTH_CIRCUMFERENCE / 2 - mapPoint.y() * arc;
  70.         final double exp = exp(metersY / (EARTH_RADIUS / 2));
  71.         double lon = toDegrees(metersX / EARTH_RADIUS);
  72.         final double lat = toDegrees(asin((exp - 1) / (exp + 1)));

  73.         while (lon <= -180)
  74.           {
  75.             lon += 360;
  76.           }

  77.         while (lon > 180)
  78.           {
  79.             lon -= 360;
  80.           }

  81.         return MapCoordinates.of(lat, lon);
  82.       }

  83.     /***********************************************************************************************************************************************************
  84.      * {@inheritDoc}
  85.      **********************************************************************************************************************************************************/
  86.     @Override
  87.     public final double metersPerPixel (@Nonnull final MapCoordinates coordinates, final double zoomLevel)
  88.       {
  89.         return arc(zoomLevel) * cos(toRadians(coordinates.latitude()));
  90.       }
  91.    
  92.     /***********************************************************************************************************************************************************
  93.      * {@return the arc length, in meters, that corresponds to a tile}.
  94.      * @param   zoomLevel   the zoom level
  95.      **********************************************************************************************************************************************************/
  96.     private double arc (final double zoomLevel)
  97.       {
  98.         return EARTH_CIRCUMFERENCE / (pow(2, zoomLevel) * tileSize); // was EARTH_CIRCUMFERENCE / ((1 << (maxZoomLevel - zoomLevel)) * tileSize);
  99.       }
  100.   }