如何使用JMapProjLib、Proj4j或Proj4js在不同坐标系之间转换坐标

How to convert coordinates between different coordinate systems with JMapProjLib, Proj4j or Proj4js?

本文关键字:坐标系 之间 坐标 转换 Proj4js JMapProjLib 何使用 Proj4j      更新时间:2023-09-26

我正在创建一个Android应用程序(使用Java),该应用程序将从文件中读取SWEREF99TM坐标,并将其转换为WGS84(long-lat)坐标,并沿oposite方向。为此,我将使用JMapProjLibProj4jProj4js(我将使用Java的ScriptEngine访问的JavaScript库)。

问题是我不知道如何使用这些库,因为文档太少了
我尝试过JMapProjLib-JavaLibrary,但它没有给我正确的结果,我也尝试过使用Proj4js 2.3.3,但我甚至无法获得后面提到的工作。。。

我目前正在使用的JavaScript(和一点HTML)代码,带有Proj4js 2.3.3(目前我只在HTML文件中尝试这个脚本,所以当转换完成时,浏览器应该显示一条弹出消息,说"完成!"):

<!DOCTYPE html>
<html>
<head>
    <title>Proj4js Testing</title>
</head>
<body onload="convertCoordinates()">
    <script type"text/javascript" src="proj4js.js"></script>
    <script type="text/javascript">
        // Convert coordinates
        function convertCoordinates() 
            // Define the target projection (SWEREF99 TM)
            var targetProjection = "+proj=utm +zone=33 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs";
            // Convert the WGS84 coordinates Lon: 15 and Lat: 55 to SWEREF99 TM coordinates
            proj4(targetProjection, [15, 55]);
            // Just show an alert message telling that the code has executed properly
            alert("Done!");
        }
    </script>
</body>
</html>

我正在尝试的Java代码(使用JMapProjLib):

/**
 * Convert a long-lat coordinate to a SWEREF99 TM coordinate.
 * 
 * @param longLatCoordinate The long-lat coordinate to convert.
 * 
 * @return The converted SWEREF99 TM coordinate.
 */
public static SWEREF99TMCoordinate longLatToSWEREF99TM(LongLatCoordinate longLatCoordinate) {
    // Create a new SWEREF99 TM projection
    Projection projection = ProjectionFactory.fromPROJ4Specification(new String[] {
            "+proj=tmerc",
            "+zone=33",
            "+ellps=GRS80",
            "+towgs84=0,0,0,0,0,0,0",
            "+units=m",
            "+no_defs"
    });
    // Convert the LongLatCoordinate to a Point2D.Double
    Point2D.Double longLatPoint = new Point2D.Double(longLatCoordinate.getLongitude(), longLatCoordinate.getLatitude());
    // Get the location as a SWEREF99 TM Point2D.Double
    Point2D.Double sweref99TMPoint = projection.transform(longLatPoint, new Point2D.Double());
    // COnvert the location to a SWEREF99TMCoordinate
    SWEREF99TMCoordinate sweref99TMCoordinate = new SWEREF99TMCoordinate(sweref99TMPoint.x, sweref99TMPoint.y);
    // Return the projected coordinate
    return sweref99TMCoordinate;
}

引用到我的Java项目的JMapProjLib-库由git存储库中文件夹src/com/jhlabs/mapsrc/coordsys/中的内容组成,该存储库由我使用Eclipse编译为.jar文件。

由于JavaScript-库Proj4js在我看来更"出色",我更喜欢将其与Java ScriptEngine结合使用,所以我现在真正需要的只是某种文档,或者可以向我解释这一点的人。

您可以检查此存储库并提取您需要的内容:

https://github.com/dsegerss/Sonardrone/tree/master/src/org/sonardrone/proj/positions