Skip to content

Package: MapArea

MapArea

nameinstructionbranchcomplexitylinemethod
checkLatitude(double, String)
M: 7 C: 9
56%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 1 C: 2
67%
M: 0 C: 1
100%
checkLongitude(double, String)
M: 7 C: 9
56%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 1 C: 2
67%
M: 0 C: 1
100%
contains(MapArea)
M: 0 C: 28
100%
M: 2 C: 6
75%
M: 2 C: 3
60%
M: 0 C: 1
100%
M: 0 C: 1
100%
contains(MapCoordinates)
M: 47 C: 0
0%
M: 14 C: 0
0%
M: 8 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
getCenter()
M: 0 C: 33
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
isAcrossGreenwichAntimeridian()
M: 0 C: 10
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
of(double, double, double, double)
M: 18 C: 24
57%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 6
86%
M: 0 C: 1
100%
toString()
M: 0 C: 29
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

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;
27:
28: import jakarta.annotation.Nonnull;
29: import org.apiguardian.api.API;
30: import lombok.EqualsAndHashCode;
31: import lombok.Getter;
32: import lombok.RequiredArgsConstructor;
33: import static org.apiguardian.api.API.Status.STABLE;
34: import static lombok.AccessLevel.PRIVATE;
35:
36: /***************************************************************************************************************************************************************
37: *
38: * A rectangular area on the map.
39: *
40: * @author Fabrizio Giudici
41: *
42: **************************************************************************************************************************************************************/
43: @API(status = STABLE)
44: @RequiredArgsConstructor(access = PRIVATE) @Getter @EqualsAndHashCode
45: public class MapArea
46: {
47: private final double north;
48:
49: private final double east;
50:
51: private final double south;
52:
53: private final double west;
54:
55: /***********************************************************************************************************************************************************
56: * {@return a new area}.
57: * @param north the north limit
58: * @param east the east limit
59: * @param south the south limit
60: * @param west the west limit
61: * @throws IllegalArgumentException when the values are unfeasible
62: **********************************************************************************************************************************************************/
63: @Nonnull
64: public static MapArea of (final double north, final double east, final double south, final double west)
65: {
66: checkLatitude(north, "north");
67: checkLatitude(south, "south");
68: checkLongitude(east, "east");
69: checkLongitude(west, "west");
70:
71:• if (north < south)
72: {
73: throw new IllegalArgumentException(String.format("north (%f) must be greater on equal than south (%f)", north, south));
74: }
75:
76: return new MapArea(north, east, south, west);
77: }
78:
79: /***********************************************************************************************************************************************************
80: * {@return the center of this area}.
81: **********************************************************************************************************************************************************/
82: @Nonnull
83: public MapCoordinates getCenter()
84: {
85: var longitudeCenter = (west + east) / 2;
86:
87:• if (isAcrossGreenwichAntimeridian())
88: {
89: longitudeCenter = longitudeCenter - 180;
90:
91:• if (longitudeCenter <= -180)
92: {
93: longitudeCenter = 360 + longitudeCenter;
94: }
95: }
96:
97: return MapCoordinates.of((north + south) / 2, longitudeCenter);
98: }
99:
100: /***********************************************************************************************************************************************************
101: * {@return {@code true} if this area spans across the Greenwich antimeridian (180° W)}.
102: **********************************************************************************************************************************************************/
103: public boolean isAcrossGreenwichAntimeridian()
104: {
105:• return east < west;
106: }
107:
108: /***********************************************************************************************************************************************************
109: * {@return {@code true} if this area contains the given coordinates}.
110: * @param coordinates the coordinates
111: **********************************************************************************************************************************************************/
112: public boolean contains (@Nonnull final MapCoordinates coordinates)
113: {
114:• return coordinates.latitude() <= north && coordinates.latitude() >= south
115:• && isAcrossGreenwichAntimeridian() ? coordinates.longitude() >= west && coordinates.longitude() <= east
116:• : coordinates.longitude() >= east && coordinates.longitude() <= west;
117: }
118:
119: /***********************************************************************************************************************************************************
120: * {@return {@code true} if this area contains the given area}.
121: * @param that the area to compare
122: **********************************************************************************************************************************************************/
123: public boolean contains (@Nonnull final MapArea that)
124: {
125:• return north >= that.north && south <= that.south && west <= that.west && east >= that.east;
126: }
127:
128: /***********************************************************************************************************************************************************
129: * {@inheritDoc}
130: **********************************************************************************************************************************************************/
131: @Override @Nonnull
132: public String toString()
133: {
134: return String.format("(n=%.6f, e=%.6f, s=%.6f, w=%.6f)", north, east, south, west);
135: }
136:
137: /***********************************************************************************************************************************************************
138: *
139: **********************************************************************************************************************************************************/
140: private static void checkLatitude (final double latitude, @Nonnull final String name)
141: {
142:• if (latitude < -90 || latitude > 90)
143: {
144: throw new IllegalStateException("Latitude must be in range [-90, 90]: " + name + "=" + latitude);
145: }
146: }
147:
148: /***********************************************************************************************************************************************************
149: *
150: **********************************************************************************************************************************************************/
151: private static void checkLongitude (final double longitude, @Nonnull final String name)
152: {
153:• if (longitude <= -180 || longitude > 180)
154: {
155: throw new IllegalStateException("Longitude must be in range (-180, 180]: " + name + "=" + longitude);
156: }
157: }
158: }