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

て烟熏妆下的殇ゞ 提交于 2021-01-28 03:30:33

问题


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");

回答1:


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.




回答2:


Not sure if this will help, because it does involve GeoTools as well as JTS, but I've used this successfully before. I don't know how all-inclusive it is in terms of the transforms it will do.

  public static Geometry transform(Geometry inputFeature, CoordinateReferenceSystem source, CoordinateReferenceSystem target) {
    Geometry out = null;
    try {
      final MathTransform mathTransform = CRS.findMathTransform(source, target, true);
      out = JTS.transform(inputFeature, mathTransform);
    } catch (Exception e) {
      LOGGER.error("Exception occurred during transform for input: " + inputFeature, e);
    }
    return out;
  }

Here are my imports for these objects

import org.geotools.coverage.grid.GridCoverage2D;
import org.geotools.coverage.grid.GridEnvelope2D;
import org.geotools.coverage.grid.GridGeometry2D;
import org.geotools.coverage.grid.io.AbstractGridFormat;
import org.geotools.coverage.grid.io.OverviewPolicy;

import org.geotools.gce.geotiff.GeoTiffReader;
import org.geotools.geometry.Envelope2D;
import org.geotools.geometry.jts.JTS;
import org.geotools.referencing.CRS;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.jaitools.jts.CoordinateSequence2D;
import org.opengis.parameter.GeneralParameterValue;
import org.opengis.parameter.ParameterValue;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.MathTransform;

And here are my POM entries that you'll probably need

       <dependency>

            <groupId>org.geotools</groupId>
            <artifactId>gt-epsg-hsql</artifactId>
            <version>15-RC1</version>
        </dependency>
<dependency>
  <groupId>org.geotools</groupId>
  <artifactId>gt-geojson</artifactId>
  <version>15-RC1</version>
</dependency>



        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-geotiff</artifactId>
            <version>15-RC1</version>
        </dependency>

        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-wms</artifactId>
            <version>15-RC1</version>
        </dependency>

Hope this helps, and I've also taken the approach of using something like PostGIS or Oracle spatial to do the transform in sql if you are already running a DB server.

UPDATE:

I think it worked for this specific case this code:

 Coordinate c = new Coordinate(394028.93262359675,806122.3097467106);

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

      CoordinateReferenceSystem utmCrs = CRS.decode("EPSG:3785");
      Geometry transform = transform(p, utmCrs, DefaultGeographicCRS.WGS84);

produced this geom (in WKT) POINT (3.5396221256107805 7.270235925793633)

which looks good to me (intuitively at least)



来源:https://stackoverflow.com/questions/46690657/converting-british-national-grid-epsg7405-coordinates-into-latitude-and-long

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!