Search code examples
javajavascriptcoordinate-systemswgs84proj4js

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


I'm creating an Android app (with Java) which is going to read SWEREF99 TM coordinates from a file, and convert them to WGS84 (long-lat) coordinates, and in the oposite direction. To do this, I'm going to use either JMapProjLib, Proj4j or Proj4js (JavaScript library which I'm going to access using Java's ScriptEngine).

The problem is that I can't figure out how to use the libraries, due to verry little documentation...
I've tried the JMapProjLib-JavaLibrary, but it is not giving me the correct result, and I've also tried using Proj4js 2.3.3, but I can't even get the later mentioned working...

JavaScript (and a bit of HTML) code that I'm currently using, with Proj4js 2.3.3 (currently I only try this script in an HTML file, so the browser should show a pop-up message saying "Done!" when the conversion is done):

<!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 code that I'm trying (with 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;
}

The JMapProjLib-library which is referenced to my Java project consists of the contents in the folder src/com/jhlabs/map and src/coordsys/ in the git-repository compiled to a .jar file using Eclipse, by me.

As the JavaScript-library Proj4js looks more "well-done" to me, I would prefer using it combined with the Java ScriptEngine, so what I really need now is just some kind of documentation, or someone who can explain this to me.


Solution

  • You can check this repository and extract what you need:

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