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