/** * @return The overall heading of the {@link PolyLine}: the heading between the start point and * the end point. */ public Optional<Heading> overallHeading() { if (this.isPoint()) { logger.warn("Cannot compute a segment's heading when the polyline has zero length : {}", this); return Optional.empty(); } return Optional.ofNullable(this.first().headingTo(this.last())); }
/** * @return The {@link Segment}'s {@link Heading}. In case the segment is the same start and end * locations, then the result is empty. */ public Optional<Heading> heading() { if (this.isPoint()) { logger.warn( "Cannot compute a segment's heading when the segment is a point with same start and end {}", this.start()); return Optional.empty(); } return Optional.of(this.start().headingTo(this.end())); }
@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 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"); }
@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))); }