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 edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
30: import it.tidalwave.mapviewer.MapCoordinates;
31: import it.tidalwave.mapviewer.MapPoint;
32: import it.tidalwave.mapviewer.Projection;
33: import org.apiguardian.api.API;
34: import lombok.RequiredArgsConstructor;
35: import static org.apiguardian.api.API.Status.EXPERIMENTAL;
36: import static java.lang.Math.*;
37:
38: /***************************************************************************************************************************************************************
39: *
40: * An implementation of the Mercator Projection.
41: *
42: * @author Fabrizio Giudici
43: *
44: **************************************************************************************************************************************************************/
45: @API(status = EXPERIMENTAL)
46: @RequiredArgsConstructor
47: public class MercatorProjection implements Projection
48: {
49: private static final double EARTH_RADIUS = 6378137;
50:
51: private static final double EARTH_CIRCUMFERENCE = EARTH_RADIUS * 2.0 * PI;
52:
53: private final int tileSize;
54:
55: /***********************************************************************************************************************************************************
56: * {@inheritDoc}
57: **********************************************************************************************************************************************************/
58: @Override @Nonnull
59: public MapPoint coordinatesToMapPoint (@Nonnull final MapCoordinates coordinates, final double zoomLevel)
60: {
61: final double arc = arc(zoomLevel);
62: final double sinLat = sin(toRadians(coordinates.latitude()));
63: final double metersX = EARTH_RADIUS * toRadians(coordinates.longitude());
64: final double metersY = EARTH_RADIUS / 2 * log((1 + sinLat) / (1 - sinLat));
65: final double x = (EARTH_CIRCUMFERENCE / 2 + metersX) / arc;
66: final double y = (EARTH_CIRCUMFERENCE / 2 - metersY) / arc;
67: return MapPoint.of(x, y);
68: }
69:
70: /***********************************************************************************************************************************************************
71: * {@inheritDoc}
72: **********************************************************************************************************************************************************/
73: @Override @Nonnull @SuppressFBWarnings("FL_FLOATS_AS_LOOP_COUNTERS")
74: public MapCoordinates mapPointToCoordinates (@Nonnull final MapPoint mapPoint, final double zoomLevel)
75: {
76: final double arc = arc(zoomLevel);
77: final double metersX = mapPoint.x() * arc - EARTH_CIRCUMFERENCE / 2;
78: final double metersY = EARTH_CIRCUMFERENCE / 2 - mapPoint.y() * arc;
79: final double exp = exp(metersY / (EARTH_RADIUS / 2));
80: double lon = toDegrees(metersX / EARTH_RADIUS);
81: final double lat = toDegrees(asin((exp - 1) / (exp + 1)));
82:
83:• while (lon <= -180)
84: {
85: lon += 360;
86: }
87:
88:• while (lon > 180)
89: {
90: lon -= 360;
91: }
92:
93: return MapCoordinates.of(lat, lon);
94: }
95:
96: /***********************************************************************************************************************************************************
97: * {@inheritDoc}
98: **********************************************************************************************************************************************************/
99: @Override
100: public final double metersPerPixel (@Nonnull final MapCoordinates coordinates, final double zoomLevel)
101: {
102: return arc(zoomLevel) * cos(toRadians(coordinates.latitude()));
103: }
104:
105: /***********************************************************************************************************************************************************
106: * {@return the arc length, in meters, that corresponds to a tile}.
107: * @param zoomLevel the zoom level
108: **********************************************************************************************************************************************************/
109: private double arc (final double zoomLevel)
110: {
111: return EARTH_CIRCUMFERENCE / (pow(2, zoomLevel) * tileSize);
112: }
113: }