Package: MercatorProjection
MercatorProjection
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
arc(double) |
|
|
|
|
|
||||||||||||||||||||
coordinatesToMapPoint(MapCoordinates, double) |
|
|
|
|
|
||||||||||||||||||||
mapPointToCoordinates(MapPoint, double) |
|
|
|
|
|
||||||||||||||||||||
metersPerPixel(MapCoordinates, double) |
|
|
|
|
|
Coverage
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:
28: import jakarta.annotation.Nonnull;
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: *
37: * An implementation of the Mercator Projection.
38: *
39: * @author Fabrizio Giudici
40: *
41: **************************************************************************************************************************************************************/
42: @RequiredArgsConstructor
43: public class MercatorProjection implements Projection
44: {
45: private static final double EARTH_RADIUS = 6378137;
46:
47: private static final double EARTH_CIRCUMFERENCE = EARTH_RADIUS * 2.0 * PI;
48:
49: private final int tileSize;
50:
51: /***********************************************************************************************************************************************************
52: * {@inheritDoc}
53: **********************************************************************************************************************************************************/
54: @Override @Nonnull
55: public MapPoint coordinatesToMapPoint (@Nonnull final MapCoordinates coordinates, final double zoomLevel)
56: {
57: final double arc = arc(zoomLevel);
58: final double sinLat = sin(toRadians(coordinates.latitude()));
59: final double metersX = EARTH_RADIUS * toRadians(coordinates.longitude());
60: final double metersY = EARTH_RADIUS / 2 * log((1 + sinLat) / (1 - sinLat));
61: final double x = (EARTH_CIRCUMFERENCE / 2 + metersX) / arc;
62: final double y = (EARTH_CIRCUMFERENCE / 2 - metersY) / arc;
63: return MapPoint.of(x, y);
64: }
65:
66: /***********************************************************************************************************************************************************
67: * {@inheritDoc}
68: **********************************************************************************************************************************************************/
69: @Override @Nonnull
70: public MapCoordinates mapPointToCoordinates (@Nonnull final MapPoint mapPoint, final double zoomLevel)
71: {
72: final double arc = arc(zoomLevel);
73: final double metersX = mapPoint.x() * arc - EARTH_CIRCUMFERENCE / 2;
74: final double metersY = EARTH_CIRCUMFERENCE / 2 - mapPoint.y() * arc;
75: final double exp = exp(metersY / (EARTH_RADIUS / 2));
76: double lon = toDegrees(metersX / EARTH_RADIUS);
77: final double lat = toDegrees(asin((exp - 1) / (exp + 1)));
78:
79:• if (lon <= -180)
80: {
81: lon = 360 + lon;
82: }
83:• else if (lon > 180)
84: {
85: lon = lon - 360;
86: }
87:
88: return MapCoordinates.of(lat, lon);
89: }
90:
91: /***********************************************************************************************************************************************************
92: * {@inheritDoc}
93: **********************************************************************************************************************************************************/
94: @Override
95: public final double metersPerPixel (@Nonnull final MapCoordinates coordinates, final double zoomLevel)
96: {
97: return arc(zoomLevel) * cos(toRadians(coordinates.latitude()));
98: }
99:
100: /***********************************************************************************************************************************************************
101: * {@return the arc length, in meters, that corresponds to a tile}.
102: * @param zoomLevel the zoom level
103: **********************************************************************************************************************************************************/
104: private double arc (final double zoomLevel)
105: {
106: return EARTH_CIRCUMFERENCE / (pow(2, zoomLevel) * tileSize); // was EARTH_CIRCUMFERENCE / ((1 << (maxZoomLevel - zoomLevel)) * tileSize);
107: }
108: }