Skip to content

Commit efc3fcb

Browse files
committed
Fix: calculate and test the rollLongitude result
1 parent 80e90f9 commit efc3fcb

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

core/src/main/java/org/mapfish/print/attribute/map/CenterScaleMapBounds.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,12 +180,19 @@ private ReferencedEnvelope computeGeodeticBBox(
180180
}
181181
}
182182

183-
private double rollLongitude(final double x) {
184-
return x - (((int) (x + Math.signum(x) * 180)) / 360) * 360.0;
183+
double rollLongitude(final double x) {
184+
return modulo(x + 180, 360.0) - 180;
185185
}
186186

187-
private double rollLatitude(final double y) {
188-
return y - (((int) (y + Math.signum(y) * 90)) / 180) * 180.0;
187+
private static double modulo(double a, double b) {
188+
return b - ((-a % b) + b) % b;
189+
}
190+
191+
double rollLatitude(final double y) {
192+
if (y > 90 || y < -90) {
193+
throw new IllegalArgumentException("Latitude must be between -90 and 90");
194+
}
195+
return y;
189196
}
190197

191198
@Override

core/src/test/java/org/mapfish/print/attribute/map/CenterScaleMapBoundsTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.mapfish.print.attribute.map;
22

33
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertThrows;
45

56
import java.awt.Rectangle;
67
import org.geotools.api.referencing.FactoryException;
@@ -111,4 +112,36 @@ public void testZoomOut() {
111112
assertEquals(envelope.getMinY() * 2, newEnvelope.getMinY(), delta);
112113
assertEquals(envelope.getMaxY() * 2, newEnvelope.getMaxY(), delta);
113114
}
115+
116+
@Test
117+
public void testRollLongitude() {
118+
final double denominator = 10000.0;
119+
final CenterScaleMapBounds bounds =
120+
new CenterScaleMapBounds(DefaultGeographicCRS.WGS84, 0.0, 0.0, denominator);
121+
122+
assertEquals(0.0, bounds.rollLongitude(0.0), 1e-6);
123+
assertEquals(180.0, bounds.rollLongitude(-180.0), 1e-6);
124+
assertEquals(180.0, bounds.rollLongitude(180.0), 1e-6);
125+
assertEquals(-179.0, bounds.rollLongitude(181.0), 1e-6);
126+
assertEquals(179.0, bounds.rollLongitude(-181.0), 1e-6);
127+
assertEquals(1.0, bounds.rollLongitude(361.0), 1e-6);
128+
assertEquals(-1.0, bounds.rollLongitude(-361.0), 1e-6);
129+
}
130+
131+
@Test
132+
public void testRollLatitude() {
133+
final double denominator = 10000.0;
134+
final CenterScaleMapBounds bounds =
135+
new CenterScaleMapBounds(DefaultGeographicCRS.WGS84, 0.0, 0.0, denominator);
136+
137+
assertEquals(0.0, bounds.rollLatitude(0.0), 1e-6);
138+
assertEquals(90.0, bounds.rollLatitude(90.0), 1e-6);
139+
assertEquals(-90.0, bounds.rollLatitude(-90.0), 1e-6);
140+
assertThrows(IllegalArgumentException.class, () -> bounds.rollLatitude(91.0));
141+
assertThrows(IllegalArgumentException.class, () -> bounds.rollLatitude(-91.0));
142+
assertThrows(IllegalArgumentException.class, () -> bounds.rollLatitude(180.0));
143+
assertThrows(IllegalArgumentException.class, () -> bounds.rollLatitude(-180.0));
144+
assertThrows(IllegalArgumentException.class, () -> bounds.rollLatitude(181.0));
145+
assertThrows(IllegalArgumentException.class, () -> bounds.rollLatitude(-181.0));
146+
}
114147
}
11 Bytes
Loading

0 commit comments

Comments
 (0)