/** * @return The upper left corner {@link Location} of this {@link Rectangle} */ public Location upperLeft() { return new Location(this.upperRight.getLatitude(), this.lowerLeft.getLongitude()); }
@Override public Rectangle bounds() { return this.location.bounds(); }
/** * Query country boundaries which cover given {@link Location}, with an extension square box * * @param location * Any {@link Location} * @param extension * Extension {@link Distance} * @return a list of {@link CountryBoundary} */ public List<CountryBoundary> boundaries(final Location location, final Distance extension) { return this.boundaries(location.boxAround(extension)); }
/** * Creates a {@link GeodeticCoordinate} at the given {@link Location}, at mean sea level. * * @param location * The {@link Location} of the coordinate. */ public GeodeticCoordinate(final Location location) { this.latitude = location.getLatitude(); this.longitude = location.getLongitude(); this.altitude = Altitude.MEAN_SEA_LEVEL; }
@Override public SortedSet<SnappedEdge> snaps(final Location point, final Distance threshold) { final SortedSet<SnappedEdge> snaps = new TreeSet<>(); for (final Edge edge : this.edgesIntersecting(point.boxAround(threshold))) { final SnappedEdge candidate = new SnappedEdge(point.snapTo(edge.asPolyLine()), edge); snaps.add(candidate); } return snaps; }
@Test public void testMidPointAccuracyAndSpeed() { final Location location1 = new Location(Latitude.degrees(51.127), Longitude.degrees(1.338)); final Location location2 = new Location(Latitude.degrees(50.964), Longitude.degrees(1.853)); final Time beginning = Time.now(); final Location derivedMidPoint = location1.shiftAlongGreatCircle( location1.headingTo(location2), location1.distanceTo(location2).scaleBy(Ratio.HALF)); System.out.println("Derived Duration: " + beginning.elapsedSince()); System.out.println(derivedMidPoint.toString() + "\n"); final Time beginning2 = Time.now(); final Location calculatedMidPoint = location1.midPoint(location2); System.out.println("Calculated Duration: " + beginning2.elapsedSince()); System.out.println(calculatedMidPoint.toString() + "\n"); final Time beginning3 = Time.now(); final Location calculatedLoxodromicMidPoint = location1.loxodromicMidPoint(location2); System.out.println("Calculated Loxodromic Duration: " + beginning3.elapsedSince()); System.out.println(calculatedLoxodromicMidPoint.toString() + "\n"); }
.distanceTo(this.rectangle1.upperRight()); final Rectangle collapsedRectangle1 = this.rectangle1.contract(rectangle1CornerToCorner); Assert.assertEquals(collapsedRectangle1.center().bounds(), collapsedRectangle1); Assert.assertEquals(Surface.forDm7Squared(0), collapsedRectangle1.surface()); .distanceTo(this.rectangle1.upperRight()).scaleBy(.51); final Rectangle collapsedHorizontally = this.rectangle1 .contract(contractRectangle1Distance); this.rectangle1.contract(contractRectangle1Distance.scaleBy(10)).surface()); Assert.assertEquals(Math.round(contractRectangle1Distance.asMeters()), Math.round(new Location(this.rectangle1.lowerLeft().getLatitude(), collapsedHorizontally.middle().getLongitude()) .distanceTo(collapsedHorizontally.lowerLeft()).asMeters())); .distanceTo(this.rectangle2.lowerLeft()).scaleBy(.51); final Rectangle collapsedVertically = this.rectangle2.contract(contractRectangle2Distance); Assert.assertEquals(Surface.forDm7Squared(0), collapsedVertically.surface()); this.rectangle2.contract(contractRectangle2Distance.scaleBy(10)).surface()); Assert.assertEquals(Math.round(contractRectangle2Distance.asMeters()), Math.round(new Location(collapsedVertically.middle().getLatitude(), this.rectangle2.lowerLeft().getLongitude()) .distanceTo(collapsedVertically.lowerLeft()).asMeters())); Assert.assertEquals(this.rectangle1.center().bounds(), collapsedHorizontally.contract(Distance.TEN_MILES)); Assert.assertEquals(this.rectangle2.center().bounds(), collapsedVertically.contract(Distance.TEN_MILES));
@Test public void testCrossingAntimeridian() { final Location one = new Location(Latitude.degrees(37), Longitude.degrees(179.998)); final Location two = new Location(Latitude.degrees(37), Longitude.degrees(179.999)); // This one is actually -180.0 final Location thr = new Location(Latitude.degrees(37), Longitude.degrees(180.0)); final Location fur = new Location(Latitude.degrees(37), Longitude.degrees(-179.999)); logger.info("one equirectangular two: {}", one.equirectangularDistanceTo(two)); logger.info("two equirectangular thr: {}", two.equirectangularDistanceTo(thr)); logger.info("thr equirectangular fur: {}", thr.equirectangularDistanceTo(fur)); logger.info("one haversine two: {}", one.haversineDistanceTo(two)); logger.info("two haversine thr: {}", two.haversineDistanceTo(thr)); logger.info("thr haversine fur: {}", thr.haversineDistanceTo(fur)); logger.info("one mixed two: {}", one.distanceTo(two)); logger.info("two mixed thr: {}", two.distanceTo(thr)); logger.info("thr mixed fur: {}", thr.distanceTo(fur)); logger.info("one heading two: {}", one.headingTo(two)); logger.info("two heading thr: {}", two.headingTo(thr)); logger.info("thr heading fur: {}", thr.headingTo(fur)); Assert.assertEquals(one.distanceTo(two).asMeters(), two.distanceTo(thr).asMeters(), 3); Assert.assertEquals(two.distanceTo(thr).asMeters(), thr.distanceTo(fur).asMeters(), 3); Assert.assertEquals(one.headingTo(two).asDegrees(), two.headingTo(thr).asDegrees(), 3); Assert.assertEquals(two.headingTo(thr).asDegrees(), thr.headingTo(fur).asDegrees(), 3); }
@Test public void testMidPoint() { final Location location1 = new Location(Latitude.degrees(52.205), Longitude.degrees(0.119)); final Location location2 = new Location(Latitude.degrees(48.857), Longitude.degrees(2.351)); final Location midpoint = location1.midPoint(location2); Assert.assertEquals(50.5363269, midpoint.getLatitude().asDegrees(), DELTA); Assert.assertEquals(1.2746141, midpoint.getLongitude().asDegrees(), DELTA); }
@Test public void testLoxodromicMidPoint() { final Location location1 = new Location(Latitude.degrees(51.127), Longitude.degrees(1.338)); final Location location2 = new Location(Latitude.degrees(50.964), Longitude.degrees(1.853)); final Location midpoint = location1.loxodromicMidPoint(location2); Assert.assertEquals(51.0455, midpoint.getLatitude().asDegrees(), DELTA); Assert.assertEquals(1.5957265, midpoint.getLongitude().asDegrees(), DELTA); }
/** * @param location * The {@link Location} to test * @return True if one of the vertices of this {@link PolyLine} is the provided {@link Location} */ public boolean contains(final Location location) { for (final Location thisLocation : this) { if (thisLocation.equals(location)) { return true; } } return false; }
@Override public Node convert(final Location object) { return new Node(new CommonEntityData(object.asConcatenation(), 0, new Date(), new OsmUser(0, "osm"), 0), object.getLatitude().asDegrees(), object.getLongitude().asDegrees()); } }
@Override public Location backwardConvert(final Coordinate coordinate) { return new Location(Latitude.degrees(coordinate.y), Longitude.degrees(coordinate.x)); }
/** * Generate a random {@link PolyLine} within bounds. * * @param numberPoints * The number of points in the {@link PolyLine} * @param bounds * The bounds for the points to be in * @return The random {@link PolyLine} */ public static PolyLine random(final int numberPoints, final Rectangle bounds) { final List<Location> locations = new ArrayList<>(); IntStream.range(0, numberPoints).forEach(index -> locations.add(Location.random(bounds))); return new PolyLine(locations); }
final Distance searchDistance, final Distance toleranceDistance) final Rectangle bounds = location.bounds().expand(searchDistance); buildNodeSpatialIndexIfNecessary(); final SortedSet<Node> nodes = new TreeSet<>((node1, node2) -> final Distance distance1 = location.distanceTo(node1.getLocation()); final Distance distance2 = location.distanceTo(node2.getLocation()); final double difference = distance2.asMillimeters() - distance1.asMillimeters(); if (difference > 0.0) for (final Node candidate : nodes) final Distance distance = location.distanceTo(candidate.getLocation()); if (distance.isLessThanOrEqualTo(toleranceDistance))
/** * Get the offset from the start of the node's location * * @param node * The location to test * @param occurrenceIndex * In case of a self intersecting polyline (one or more locations appear more than * once), indicate the index at which this method should return the location. 0 would * be first occurrence, 1 second, etc. * @return The offset ratio from the start of the {@link PolyLine} */ public Ratio offsetFromStart(final Location node, final int occurrenceIndex) { final Distance max = this.length(); Distance candidate = Distance.ZERO; Location previous = this.first(); int index = 0; for (final Location location : this) { candidate = candidate.add(previous.distanceTo(location)); if (location.equals(node) && occurrenceIndex == index++) { return Ratio.ratio(candidate.asMeters() / max.asMeters()); } previous = location; } throw new CoreException("The location {} is not a node of the PolyLine", node); }
@Test public void testHeadingTo() { final Location location1 = new Location(Latitude.degrees(37.336900), Longitude.degrees(-122.005414)); final Location location2 = new Location(Latitude.degrees(37.332758), Longitude.degrees(-122.005409)); Assert.assertTrue(Heading.degrees(180).difference(location1.headingTo(location2)) .isLessThan(Angle.degrees(1))); }
/** * @param other * The other {@link Location} to test * @return True if this {@link Location} and the other {@link Location} to test are on the same * East-West line. */ public boolean hasSameLatitudeAs(final Location other) { return this.getLatitude().equals(other.getLatitude()); }
/** * @return The distance between the origin and the snapped {@link Location} */ public Distance getDistance() { return this.origin.distanceTo(this); }
/** * @param other * The other {@link Location} to test * @return True if this {@link Location} is east of the other {@link Location}. */ public boolean isEastOf(final Location other) { return this.getLongitude().isGreaterThan(other.getLongitude()); }