/** * 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 public void validateLongitude_legalValue_returnThatValue() { assertEquals(10d, LatLongUtils.validateLongitude(10d), 0d); }
private static void verifyInvalidLongitude(double longitude) { try { LatLongUtils.validateLongitude(longitude); Assert.fail("longitude: " + longitude); } catch (IllegalArgumentException e) { Assert.assertTrue(true); } }
@Test public void validateLongitudeTest() { LatLongUtils.validateLongitude(LatLongUtils.LONGITUDE_MAX); LatLongUtils.validateLongitude(LatLongUtils.LONGITUDE_MIN); verifyInvalidLongitude(Double.NaN); verifyInvalidLongitude(Math.nextAfter(LatLongUtils.LONGITUDE_MAX, Double.POSITIVE_INFINITY)); verifyInvalidLongitude(Math.nextAfter(LatLongUtils.LONGITUDE_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; }