在java中将纬度和经度转换为北距和东距?

发布于 2024-10-05 08:03:01 字数 30 浏览 2 评论 0原文

如何在Java中将纬度和经度转换为北向和东向?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

救赎№ 2024-10-12 08:03:02

如果您不想引入 GeoTools 并且发现 JCoord 在 Maven 中心不再可用,可以使用一个小型库将 OSGB(英国的东/北)从 DSTL 转换为 WGS84:https://github.com/dstl/osgb

改编自自述文件:

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

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]);
阳光①夏 2024-10-12 08:03:01

我假设您指的是英国 OSGB 东移和北移。这个背后的三角学很搞笑,但是你可以使用 JCoord 库来实现这一点,它使它变得很容易(如果相当的话) CPU 密集型)。


为了跟进下面 @DD 的评论,JCoord 有一个问题,即您必须确保在从东/北转换为纬度/经度时使用正确的基准,反之亦然。

以@DD 的代码为例:

LatLng latLng = new OSRef(394251,806376).toLatLng();

这将返回使用 OSGB36 基准的纬度/经度,即英国地图上使用的“平坦地球”近似值。这与大多数应用程序(包括街道地图)中使用的 WSG84 数据有很大不同,后者将世界建模为一个球体(或多或少)。

为了转换为 WGS84 纬度/经度,您需要明确说明:

LatLng latLng = new OSRef(394251,806376).toLatLng();
latLng.toWGS84();

这会将纬度/经度调整为正确的基准。

请注意 javadocOsRef.toLatLng() 的 a> 对此非常清楚:

使用 OSGB36 基准将此 OSGB 网格引用转换为纬度/经度对。请注意,根据应用程序,LatLng 对象可能需要转换为 WGS84 数据

坐标基准之间的转换并不简单。如果看起来很简单,那么您可能做错了。

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.

千紇 2024-10-12 08:03:01

尝试地理工具:

       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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文