How to Convert Between Latitude and Longitude and Easting and Northing Co-ordinates in Java
When working with geographical data it’s often useful to convert between the longitude and latitude coordinate system and the easting and northing co-ordinate system, which is used by the UK’s Ordnance Survey. This article explains how to perform these conversions in Java with Jcoord.
Jcoord
Jcoord is an easy-to-use Java library for converting between latitude and longitude coordinates and easting and northing coordinates. In this article I’ll be using two Jcoord classes, OSRef
and LatLng
, to perform these conversions. To use them yourself, you’ll need to import them into your Java class file:
import uk.me.jstott.jcoord.OSRef;
import uk.me.jstott.jcoord.LatLng;
You’ll also need to add the Jcoord JAR file jcoord-1.0.jar to your classpath.
Converting from Longitude and Latitude to Eastings and Northings
To convert from latitude and longitude to eastings and northings, we first create an instance of Jcoord’s LatLng
class with the latitude and longitude coordinates of the point we want to convert:
double latitude = 51.509904;
double longitude = -0.135929;
LatLng latLng = new LatLng(latitude, longitude);
Next we call the toOSRef()
method of the LatLng
class to return an instance of Jcoord’s OSRef
class:
OSRef osRef = latLng.toOSRef();
The OSRef
class has two methods, getEasting()
and getNorthing()
, that we use to retrieve the converted point:
double easting = osRef.getEasting();
double northing = osRef.getNorthing();
Converting from Eastings and Northings to Latitude and Longitude
To convert from eastings and northings to latitude and longitude, we first create an instance of the OSRef
class with the easting and northing coordinates of the point we want to convert:
double easting = 529344.690264;
double northing = 180698.015941;
OSRef osRef = new OSRef(easting, northing);
Next, we call the toLatLng()
method of the OSRef
class to return an instance of the LatLng
class:
LatLng latLng = osRef.toLatLng();
The LatLng
class has two methods, getLat()
and getLng()
, that we use to retrieve the converted point:
double latitude = latLng.getLat();
double longitude = latLng.getLng();