Calculating the Bearing/Direction to Har Habayis Using the Zmanim API

Vintage Map with CompassAn earlier “Bearing to Yerushalayim and Zmanim Map” post demonstrated the use of JavaScript to render the bearing to Har Habayis on a Google Map. A more detailed follow-up post “Technical Information about the Bearing to Yerushalayim Map” dealt with detailed technical information on these calculations. The main Bearing to Yerushalayim and Zmanim Map page usually has the most up-to-date information on the subject. What was not detailed in previously published posts and pages was that most of the calculations available via JavaScript are now in the core Zmanim API. Available since the July 2008 beta 2 release of version 1.1 is the ability to calculate bearings/directions using both the great circle and rhumb line methods in Java. The GeoLocation class was modified to calculate the great circle bearings (both initial and final), and rhumb line bearing from any GeoLocation object to another. In addition, distance calculation between the two points using both of these line types is supported. What was not ported from the JavaScript version was the less accurate Haversine formula, or the simpler spherical law of cosines algorithm that yields nearly identical results. Instead, the Zmanim API uses the far more accurate Vincenty formulae using the WGS84 ellipsoid model of the earth. Published by the geodesist/mathematician Thaddeus Vincenty, it is said to be accurate to about one-half millimeter, more than adequate for our calculation. The code in the API is a Java port of the previously published, slightly modified version of Chris Veness’s JavaScript implementation. Below is a simple Java example of generating bearing and distances.

Note: The code sample has been updated for the KosherJava Zmanim 3.x API, where GeoLocation takes a ZoneId instead of a TimeZone. The output shown below is from running it against version 3.1.0.
/**
 * This program demonstrates how to calculate bearing to Yerushalayim
 * using the kosherjava.com Zmanim API. Both the great circle and
 * rhumb line methods are shown.
 * To compile, ensure that the Zmanim jar is in your classpath.
 */
import com.kosherjava.zmanim.util.GeoLocation;
import java.time.ZoneId;

public class BearingToYerushalayim {
	public static void main(String[] args) {
		GeoLocation lakewood = new GeoLocation("Lakewood, NJ", 40.09596, -74.22213, 0, ZoneId.of("America/New_York"));
		GeoLocation harHabayis = new GeoLocation("Har Habayis", 31.77805, 35.235149, 0, ZoneId.of("Asia/Jerusalem"));

		double greatCircleInitialBearing = lakewood.getGeodesicInitialBearing(harHabayis);
		double greatCircleDistance = lakewood.getGeodesicDistance(harHabayis);

		double rhumbLineBearing = lakewood.getRhumbLineBearing(harHabayis);
		double rhumbLineDistance = lakewood.getRhumbLineDistance(harHabayis);

		System.out.println("Great circle initial bearing: " + greatCircleInitialBearing + " degrees");
		System.out.println("Great circle distance: " + greatCircleDistance / 1000 + " km");

		System.out.println("Rhumb line bearing: " + rhumbLineBearing + " degrees");
		System.out.println("Rhumb line distance: " + rhumbLineDistance / 1000 + " km");
	}
}

The output of the above code is:

Great circle initial bearing: 53.81786828306803 degrees
Great circle distance: 9244.617197329102 km
Rhumb line bearing: 95.37151951258849 degrees
Rhumb line distance: 9891.161030560183 km

Leave a Reply

Your email address will not be published. Required fields are marked *

To post code, wrap it in a shortcode for its language, such as [java]…[/java], [js]…[/js] or [php]…[/php], or [code]…[/code] for plain text.