/** * Constructs a new LatLong with the given latitude and longitude values, measured in * degrees. * * @param latitude the latitude value in degrees. * @param longitude the longitude value in degrees. * @throws IllegalArgumentException if the latitude or longitude value is invalid. */ public LatLong(double latitude, double longitude) throws IllegalArgumentException { this.latitude = LatLongUtils.validateLatitude(latitude); this.longitude = LatLongUtils.validateLongitude(longitude); }
/** * Constructs a new LatLong from a Well-Known-Text (WKT) representation of a point. * For example: POINT(13.4125 52.52235) * <p/> * WKT is used in PostGIS and other spatial databases. * * @param wellKnownText is the WKT point which describes the new LatLong, this needs to be in * degrees using a WGS84 representation. The coordinate order in the POINT is * defined as POINT(long lat). */ public LatLong(String wellKnownText) { Matcher m = WKT_POINT_PATTERN.matcher(wellKnownText); m.matches(); this.longitude = LatLongUtils.validateLongitude(Double.parseDouble(m.group(1))); this.latitude = LatLongUtils.validateLatitude(Double.parseDouble(m.group(2))); }
/** * @param minLatitude the minimum latitude coordinate in degrees. * @param minLongitude the minimum longitude coordinate in degrees. * @param maxLatitude the maximum latitude coordinate in degrees. * @param maxLongitude the maximum longitude coordinate in degrees. * @throws IllegalArgumentException if a coordinate is invalid. */ public BoundingBox(double minLatitude, double minLongitude, double maxLatitude, double maxLongitude) { LatLongUtils.validateLatitude(minLatitude); LatLongUtils.validateLongitude(minLongitude); LatLongUtils.validateLatitude(maxLatitude); LatLongUtils.validateLongitude(maxLongitude); if (minLatitude > maxLatitude) { throw new IllegalArgumentException("invalid latitude range: " + minLatitude + ' ' + maxLatitude); } else if (minLongitude > maxLongitude) { throw new IllegalArgumentException("invalid longitude range: " + minLongitude + ' ' + maxLongitude); } this.minLatitude = minLatitude; this.minLongitude = minLongitude; this.maxLatitude = maxLatitude; this.maxLongitude = maxLongitude; }
@Test(expected = IllegalArgumentException.class) public void validateLatitude_lowerThanMinValue_throwException() { LatLongUtils.validateLatitude(LatLongUtils.LATITUDE_MIN - 1); }
@Test(expected = IllegalArgumentException.class) public void validateLongitude_lowerThanMinValue_throwException() { LatLongUtils.validateLatitude(LatLongUtils.LONGITUDE_MIN - 1); } }
@Test(expected = IllegalArgumentException.class) public void validateLongitude_higherThanMaxValue_throwException() { LatLongUtils.validateLatitude(LatLongUtils.LONGITUDE_MAX + 1); }
@Test(expected = IllegalArgumentException.class) public void validateLatitude_higherThanMaxValue_throwException() { LatLongUtils.validateLatitude(LatLongUtils.LATITUDE_MAX + 1); }
private static void verifyInvalidLatitude(double latitude) { try { LatLongUtils.validateLatitude(latitude); Assert.fail("latitude: " + latitude); } catch (IllegalArgumentException e) { Assert.assertTrue(true); } }
@Test public void validateLatitude_legalValue_returnThatValue() { assertEquals(10d, LatLongUtils.validateLatitude(10d), 0d); }
@Test public void validateLatitudeTest() { LatLongUtils.validateLatitude(LatLongUtils.LATITUDE_MAX); LatLongUtils.validateLatitude(LatLongUtils.LATITUDE_MIN); verifyInvalidLatitude(Double.NaN); verifyInvalidLatitude(Math.nextAfter(LatLongUtils.LATITUDE_MAX, Double.POSITIVE_INFINITY)); verifyInvalidLatitude(Math.nextAfter(LatLongUtils.LATITUDE_MIN, Double.NEGATIVE_INFINITY)); }
/** * Constructs a new LatLong with the given latitude and longitude values, measured in * degrees. * * @param latitude the latitude value in degrees. * @param longitude the longitude value in degrees. * @throws IllegalArgumentException if the latitude or longitude value is invalid. */ public LatLong(double latitude, double longitude) throws IllegalArgumentException { this.latitude = LatLongUtils.validateLatitude(latitude); this.longitude = LatLongUtils.validateLongitude(longitude); }
/** * Constructs a new LatLong from a Well-Known-Text (WKT) representation of a point. * For example: POINT(13.4125 52.52235) * <p/> * WKT is used in PostGIS and other spatial databases. * * @param wellKnownText is the WKT point which describes the new LatLong, this needs to be in * degrees using a WGS84 representation. The coordinate order in the POINT is * defined as POINT(long lat). */ public LatLong(String wellKnownText) { Matcher m = WKT_POINT_PATTERN.matcher(wellKnownText); m.matches(); this.longitude = LatLongUtils.validateLongitude(Double.parseDouble(m.group(1))); this.latitude = LatLongUtils.validateLatitude(Double.parseDouble(m.group(2))); }
/** * @param minLatitude the minimum latitude coordinate in degrees. * @param minLongitude the minimum longitude coordinate in degrees. * @param maxLatitude the maximum latitude coordinate in degrees. * @param maxLongitude the maximum longitude coordinate in degrees. * @throws IllegalArgumentException if a coordinate is invalid. */ public BoundingBox(double minLatitude, double minLongitude, double maxLatitude, double maxLongitude) { LatLongUtils.validateLatitude(minLatitude); LatLongUtils.validateLongitude(minLongitude); LatLongUtils.validateLatitude(maxLatitude); LatLongUtils.validateLongitude(maxLongitude); if (minLatitude > maxLatitude) { throw new IllegalArgumentException("invalid latitude range: " + minLatitude + ' ' + maxLatitude); } else if (minLongitude > maxLongitude) { throw new IllegalArgumentException("invalid longitude range: " + minLongitude + ' ' + maxLongitude); } this.minLatitude = minLatitude; this.minLongitude = minLongitude; this.maxLatitude = maxLatitude; this.maxLongitude = maxLongitude; }