Search code examples
javagisjts

Converting British National Grid (EPSG:7405), coordinates into latitude and longitude coordinates Using Java( JTS prefered ) programmatically


Is there a way to use the Java to convert(British National Grid (10 Figure Grid Reference),) into lat/long?

for example, I have a 558832.516608631,180065.50201851176 and would like to generate the lat/long using Java or JTS.

I've seen questions/solutions using Javacript, but no solutions using Java alone.

I've tried this using JTS:

   Coordinate c = new Coordinate(394028.93262359675,806122.3097467106);

    com.vividsolutions.jts.geom.Point p = gf.createPoint(c);

    CoordinateReferenceSystem utmCrs = CRS.decode("EPSG:3785");
    MathTransform mathTransform = CRS.findMathTransform(utmCrs, DefaultGeographicCRS.WGS84, false);
    com.vividsolutions.jts.geom.Point p1 = (com.vividsolutions.jts.geom.Point) JTS.transform(p, mathTransform);

    System.out.println(p1.getCoordinate());

but the coordinates do not translate to lat/long that I could use in google maps.

Thanks.

http://www.movable-type.co.uk/scripts/latlong-os-gridref.html contains a forumla to do this I believe, but i rather use a java lib.

here is the GeoJson Polygon I am trying to fix: https://gist.githubusercontent.com/boundaries-io/089ca86fdc88b1065f5ee3dd12af684b/raw/e9f8b54ae17d835ad8560fcb81d693201235e386/example.geojson

Attempted to use:

CoordinateReferenceSystem utmCrs = CRS.decode("EPSG:7405");

however,

org.opengis.referencing.operation.OperationNotFoundException: No transformation available from system "CompoundCRS[OSGB 1936 / British National Grid + ODN height]" to "GeographicCRS[WGS84(DD)]".
    at  

The final solution was to use:

CoordinateReferenceSystem utmCrs = CRS.decode("EPSG:27700");

Solution

  • Oups, unless I was mistaken somewhere, British National Grid is EPSG:27700. EPSG:3785 is a deprecated popular mercator projection.

    But you could also have a look at EPSG:7405 which is declared as OSGB 1936 / British National Grid + ODN height. In fact I think that you want this one because you have cited SGB1936 in a comment and EPSG:7405 is OSGB 1936, while EPSG:27700 is OSBG36.

    In fact, EPSG:27700 can be used without any problem on any tools because it does not depend on a specific height grid. In contrast, EPSG:7405, is more accurate because it explicitely uses the ODN height data... but requires it to be loaded in the converter tool. It can be done on a true Proj4 installation (but explaination for it is far beyond my current knowledge), and may or may not be done in Java tools. Questions about it should be asked on Geographic Information Sytems. But if a 2 meter precision is acceptable, just convert from EPSG:27700 to EPSG:4326 (WGS84 lat-lon). Using the online tool from epsg.io, your example point 394028.93262359675,806122.3097467106 is transformed in lon=-2°6'1.122", lat=57°8'45.42" in the center of Aberdeen.