convert latitude and longitude to northing and easting in java?
how to开发者_开发百科 convert latitude and longitude to northing and easting in Java?
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 don't feel like pulling in GeoTools and you discover JCoord is no longer available in Maven central, there's a small library for converting OSGB (UK's Easting/Northing) into WGS84 from DSTL: https://github.com/dstl/osgb
Adapted from the README:
import uk.gov.dstl.geo.osgb.Constants;
import uk.gov.dstl.geo.osgb.EastingNorthingConversion;
import uk.gov.dstl.geo.osgb.OSGB36;
// ...
//Convert from Easting and Northing into Cartesian Coordinates (LatLon)
double[] latlonOSGB38 = EastingNorthingConversion.toLatLon(
new double[]{ easting, northing },
Constants.ELLIPSOID_AIRY1830_MAJORAXIS,
Constants.ELLIPSOID_AIRY1830_MINORAXIS,
Constants.NATIONALGRID_N0,
Constants.NATIONALGRID_E0,
Constants.NATIONALGRID_F0,
Constants.NATIONALGRID_LAT0,
Constants.NATIONALGRID_LON0);
//Convert from LatLon (OSGB) to WGS84
double[] latlonWGS84 = OSGB36.toWGS84(latlonOSGB38[0], latlonOSGB38[1]);
精彩评论