/** * Project a degree latitude/longitude point into a sinusoidal projection planar space for spatial hash cell ids * computation * * @param point * point to be projected * @return array of projected coordinates */ public static double[] projectToIndexSpace(Point point) { double[] projectedCoordinates = new double[2]; projectedCoordinates[0] = point.getLongitudeRad() * Math.cos( point.getLatitudeRad() ); projectedCoordinates[1] = point.getLatitudeRad(); return projectedCoordinates; }
/** * Compute distance point and other location given by its latitude and longitude in decimal degrees * * @param latitude in decimal degrees * @param longitude in decimal degrees * @return the distance between the points * @see <a href="http://www.movable-type.co.uk/scripts/latlong.html">Distance haversine formula</a> */ public double getDistanceTo(final double latitude,final double longitude) { double destinationLatitudeRadians = normalizeLatitude( latitude ) * GeometricConstants.TO_RADIANS_RATIO; double destinationLongitudeRadians = normalizeLongitude( longitude ) * GeometricConstants.TO_RADIANS_RATIO; final double dLat = ( destinationLatitudeRadians - getLatitudeRad() ) / 2.0d; final double dLon = ( destinationLongitudeRadians - getLongitudeRad() ) / 2.0d; final double a = Math.pow( Math.sin( dLat ), 2 ) + Math.pow( Math.sin( dLon ), 2 ) * Math.cos( getLatitudeRad() ) * Math.cos( destinationLatitudeRadians ); final double c = 2.0d * Math.atan2( Math.sqrt( a ), Math.sqrt( 1.0d - a ) ); return c * GeometricConstants.EARTH_MEAN_RADIUS_KM; }
/** * Project a degree latitude/longitude point into a sinusoidal projection planar space for spatial hash cell ids * computation * * @param point * point to be projected * @return array of projected coordinates */ public static double[] projectToIndexSpace(Point point) { double[] projectedCoordinates = new double[2]; projectedCoordinates[0] = point.getLongitudeRad() * Math.cos( point.getLatitudeRad() ); projectedCoordinates[1] = point.getLatitudeRad(); return projectedCoordinates; }
/** * Compute distance point and other location given by its latitude and longitude in decimal degrees * * @param latitude in decimal degrees * @param longitude in decimal degrees * @return the distance between the points * @see <a href="http://www.movable-type.co.uk/scripts/latlong.html">Distance haversine formula</a> */ public double getDistanceTo(final double latitude,final double longitude) { double destinationLatitudeRadians = normalizeLatitude( latitude ) * GeometricConstants.TO_RADIANS_RATIO; double destinationLongitudeRadians = normalizeLongitude( longitude ) * GeometricConstants.TO_RADIANS_RATIO; final double dLat = ( destinationLatitudeRadians - getLatitudeRad() ) / 2.0d; final double dLon = ( destinationLongitudeRadians - getLongitudeRad() ) / 2.0d; final double a = Math.pow( Math.sin( dLat ), 2 ) + Math.pow( Math.sin( dLon ), 2 ) * Math.cos( getLatitudeRad() ) * Math.cos( destinationLatitudeRadians ); final double c = 2.0d * Math.atan2( Math.sqrt( a ), Math.sqrt( 1.0d - a ) ); return c * GeometricConstants.EARTH_MEAN_RADIUS_KM; }
/** * Calculate end of travel point * * @param distance to travel * @param heading of travel in decimal degree * @return arrival point * @see <a href="http://www.movable-type.co.uk/scripts/latlong.html">Compute destination</a> */ public Point computeDestination(double distance, double heading) { double headingRadian = heading * GeometricConstants.TO_RADIANS_RATIO; double destinationLatitudeRadian = Math.asin( Math.sin( getLatitudeRad() ) * Math.cos( distance / GeometricConstants.EARTH_MEAN_RADIUS_KM ) + Math.cos( getLatitudeRad() ) * Math.sin( distance / GeometricConstants.EARTH_MEAN_RADIUS_KM ) * Math.cos( headingRadian ) ); double destinationLongitudeRadian = getLongitudeRad() + Math.atan2( Math.sin( headingRadian ) * Math.sin( distance / GeometricConstants.EARTH_MEAN_RADIUS_KM ) * Math.cos( getLatitudeRad() ), Math.cos( distance / GeometricConstants.EARTH_MEAN_RADIUS_KM ) - Math.sin( getLatitudeRad() ) * Math.sin( destinationLatitudeRadian ) ); return fromRadians( destinationLatitudeRadian, destinationLongitudeRadian ); }
/** * Calculate end of travel point * * @param distance to travel * @param heading of travel in decimal degree * @return arrival point * @see <a href="http://www.movable-type.co.uk/scripts/latlong.html">Compute destination</a> */ public Point computeDestination(double distance, double heading) { double headingRadian = heading * GeometricConstants.TO_RADIANS_RATIO; double destinationLatitudeRadian = Math.asin( Math.sin( getLatitudeRad() ) * Math.cos( distance / GeometricConstants.EARTH_MEAN_RADIUS_KM ) + Math.cos( getLatitudeRad() ) * Math.sin( distance / GeometricConstants.EARTH_MEAN_RADIUS_KM ) * Math.cos( headingRadian ) ); double destinationLongitudeRadian = getLongitudeRad() + Math.atan2( Math.sin( headingRadian ) * Math.sin( distance / GeometricConstants.EARTH_MEAN_RADIUS_KM ) * Math.cos( getLatitudeRad() ), Math.cos( distance / GeometricConstants.EARTH_MEAN_RADIUS_KM ) - Math.sin( getLatitudeRad() ) * Math.sin( destinationLatitudeRadian ) ); return fromRadians( destinationLatitudeRadian, destinationLongitudeRadian ); }
@Test public void computeDestinationTest() { Point point = Point.fromDegrees( 45, 4 ); Point destination = point.computeDestination( 100, 45 ); Assert.assertEquals( 0.796432523, destination.getLatitudeRad(), 0.00001 ); Assert.assertEquals( 0.08568597, destination.getLongitudeRad(), 0.00001 ); }