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