how to convert latitude and longitude to northing and easting in Java?
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:
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:
If you click on the UTM easting and UTM northing input box, it will auto select the value. You can also get the lat long and the UTM values by clicking on the map. World Geodetic System WGS84 standard is used on this latitude and longitude to UTM converter.
Then you can apply the converse of the projection transformation to get Latitudes and Longitudes on a datum. You can either find the transformation (usually a fairly straightforward mathematical function, see the wikipedia link above) and implement it yourself, or use a 3rd-party library such as Geotools or GeoAPI.
I'm assuming you mean UK OSGB easting and northings. The trigonometry behind this one is hilarious, but you can use the JCoord library for this, it makes it easy (if quite CPU intensive).
To follow up on @DD's comments below, there is a gotcha with JCoord, in that you have to make sure you're using the correct datum when converting from Easting/Northing to Lat/Long, and vice versa.
Take @DD's code:
LatLng latLng = new OSRef(394251,806376).toLatLng();
This will return a Lat/Long which uses the OSGB36 datum, i.e. the "flat earth" approximation used on UK maps. This is substantially different to the WSG84 datum used in most applications (including Streetmap), which models the world as a sphere (more or less).
In order to convert to a WGS84 lat/long, you need to make it explicit:
LatLng latLng = new OSRef(394251,806376).toLatLng();
latLng.toWGS84();
This will adjust the lat-long to the correct datum.
Note that the javadoc for OsRef.toLatLng()
is very clear about this:
Convert this OSGB grid reference to a latitude/longitude pair using the OSGB36 datum. Note that, the LatLng object may need to be converted to the WGS84 datum depending on the application
Conversion between coordinate datum is not simple. If it looks simple, you're likely doing it wrong.
Try GeoTools:
CRSAuthorityFactory crsFac = ReferencingFactoryFinder
.getCRSAuthorityFactory("EPSG", null);
CoordinateReferenceSystem wgs84crs = crsFac
.createCoordinateReferenceSystem("4326");
CoordinateReferenceSystem osgbCrs = crsFac
.createCoordinateReferenceSystem("27700");
CoordinateOperation op = new DefaultCoordinateOperationFactory()
.createOperation(osgbCrs, wgs84crs);
DirectPosition eastNorth = new GeneralDirectPosition(easting, northing);
DirectPosition latLng = op.getMathTransform().transform(eastNorth,
eastNorth);
double latitude=latLng.getOrdinate(0);
double longitude=latLng.getOrdinate(1);
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-main</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-epsg-hsql</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-xml</artifactId>
<version>${geotools.version}</version>
</dependency>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With