Package: WGS84PseudoMercatorProjection
WGS84PseudoMercatorProjection
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
coordinatesToMapPoint(MapCoordinates, double) |
|
|
|
|
|
||||||||||||||||||||
mapPointToCoordinates(MapPoint, double) |
|
|
|
|
|
||||||||||||||||||||
metersPerPixel(MapCoordinates, double) |
|
|
|
|
|
||||||||||||||||||||
radiansPerPixel(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.mapview.spi;
27:
28: import jakarta.annotation.Nonnull;
29: import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
30: import it.tidalwave.mapview.MapCoordinates;
31: import it.tidalwave.mapview.MapPoint;
32: import it.tidalwave.mapview.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 <a href="https://epsg.io/3857">WGS84 Pseudo Mercator Projection (EPSG:3857)</a>.
41: *
42: * @author Fabrizio Giudici
43: *
44: **************************************************************************************************************************************************************/
45: @API(status = EXPERIMENTAL)
46: @RequiredArgsConstructor
47: public class WGS84PseudoMercatorProjection implements Projection
48: {
49: private static final double EARTH_RADIUS = 6378137;
50:
51: private final int tileSize;
52:
53: /***********************************************************************************************************************************************************
54: * {@inheritDoc}
55: **********************************************************************************************************************************************************/
56: @Override @Nonnull
57: public MapPoint coordinatesToMapPoint (@Nonnull final MapCoordinates coordinates, final double zoomLevel)
58: {
59: final double pixelPerRadians = 1.0 / radiansPerPixel(zoomLevel);
60: final double sinLat = sin(toRadians(coordinates.latitude()));
61: final double y = (PI - 0.5 * log((1 + sinLat) / (1 - sinLat))) * pixelPerRadians;
62: final double x = (PI + toRadians(coordinates.longitude())) * pixelPerRadians;
63: return MapPoint.of(x, y);
64: }
65:
66: /***********************************************************************************************************************************************************
67: * {@inheritDoc}
68: **********************************************************************************************************************************************************/
69: @Override @Nonnull @SuppressFBWarnings("FL_FLOATS_AS_LOOP_COUNTERS")
70: public MapCoordinates mapPointToCoordinates (@Nonnull final MapPoint mapPoint, final double zoomLevel)
71: {
72: final double radiansPerPixel = radiansPerPixel(zoomLevel);
73: final double exp = exp(2 * (PI - mapPoint.y() * radiansPerPixel));
74: final double lat = toDegrees(asin((exp - 1) / (exp + 1)));
75: double lon = toDegrees(mapPoint.x() * radiansPerPixel - PI);
76:
77:• while (lon <= -180)
78: {
79: lon += 360;
80: }
81:
82:• while (lon > 180)
83: {
84: lon -= 360;
85: }
86:
87: return MapCoordinates.of(lat, lon);
88: }
89:
90: /***********************************************************************************************************************************************************
91: * {@inheritDoc}
92: **********************************************************************************************************************************************************/
93: @Override
94: public final double metersPerPixel (@Nonnull final MapCoordinates coordinates, final double zoomLevel)
95: {
96: return EARTH_RADIUS * radiansPerPixel(zoomLevel) * cos(toRadians(coordinates.latitude()));
97: }
98:
99: /***********************************************************************************************************************************************************
100: * {@return the angle corresponding to a pixel}.
101: * @param zoomLevel the zoom level
102: **********************************************************************************************************************************************************/
103: private double radiansPerPixel (final double zoomLevel)
104: {
105: return 2.0 * PI / (pow(2, zoomLevel) * tileSize);
106: }
107: }