Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert latitude and longitude to northing and easting in java?

how to convert latitude and longitude to northing and easting in Java?

like image 310
Udhaya Avatar asked Nov 30 '10 12:11

Udhaya


People also ask

How do I convert between latitude and longitude coordinates 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:

How do I convert 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:

How to convert latitude and longitude to UTM?

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.

How do I get latitudes and longitudes from a datum?

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.


2 Answers

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.

like image 109
skaffman Avatar answered Oct 18 '22 02:10

skaffman


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>
like image 4
DD. Avatar answered Oct 18 '22 01:10

DD.