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:
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:
Next we call the toOSRef()
method of the LatLng
class to return an instance of Jcoord’s OSRef
class:
The OSRef
class has two methods, getEasting()
and getNorthing()
, that we use to retrieve the converted point:
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:
Next, we call the toLatLng()
method of the OSRef
class to return an instance of the LatLng
class:
The LatLng
class has two methods, getLat()
and getLng()
, that we use to retrieve the converted point: