samedi 31 janvier 2015

How to convert between SWEREF99 TM (GRS80) and long-lat (WGS84) coordinate with JMapProjLib (Java version of Proj4)?


I'm making an android app which is reading SWEREF99 TM (GRS80) coordinates from a file, but I need to convert them to long-lat (WGS84) coordinates, and also in the oposite direction (WGS84 -> GRS80). For that I'm using JMapProjLib, which is a "Java porting" of the C library Proj4. I'm having problems with converting between the both. Here is the code I use (Placed in SWEREF99TMConverter class):



/**
* 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 Coordinate longLatToSWEREF99TM(Coordinate longLatCoordinate) {
// Create a new SWEREF99 TM projection
Projection projection = ProjectionFactory.fromPROJ4Specification(new String[] {
"+proj=utm",
"+zone=33",
"+ellps=GRS80",
"+towgs84=0,0,0,0,0,0,0",
"+units=m",
"+no_defs"
});

// Convert the long-lat coordinate to a Point2D.Double
Point2D.Double coordinate = new Point2D.Double(longLatCoordinate.x, longLatCoordinate.y);
// Get the location as a SWEREF99 TM Point2D.Double
Point2D.Double sweref99TMPoint = projection.transform(coordinate, new Point2D.Double());

// Convert the Point2D.Double to a Coordinate and return it
return new Coordinate(sweref99TMPoint.getX(), sweref99TMPoint.getY());
}

/**
* Convert a SWEREF99 TM coordinate to a long-lat coordinate.
*
* @param sweref99TMCoordinate The SWEREF99 TM coordinate to convert.
*
* @return The converted long-lat coordinate.
*/
public static Coordinate sweref99TMToLongLat(Coordinate sweref99TMCoordinate) {
// Create a new WGS84 Projection
Projection projection = ProjectionFactory.fromPROJ4Specification(new String[] {
"+proj=longlat",
"+ellps=WGS84",
"+nodefs"
});

// Convert the SWEREF99 TM coordinate to a Point2D.Double
Point2D.Double coordinate = new Point2D.Double(sweref99TMCoordinate.x, sweref99TMCoordinate.y);
// Get the location as a long-lat Point2D.Double
Point2D.Double longLatPoint = projection.transform(coordinate, new Point2D.Double());

// Convert the Point2D.Double to a Coordinate and return it
return new Coordinate(longLatPoint.getX(), longLatPoint.getY());
}


I found another question about converting long-lat (WGS84) coordinates to SWEREF99 TM (GRS80) coordinates here, but it doesn't really work for me...


When calling this:



Coordinate coordinate1 = SWEREF99TMConverter.sweref99TMToLongLat(new Coordinate(546280.89, 6477506.89));
System.out.println("X: " + coordinate1.x + ", Y: " + coordinate1.y);

Coordinate coordinate2 = SWEREF99TMConverter.longLatToSWEREF99TM(new Coordinate(coordinate1.x, coordinate1.y));
System.out.println("X: " + coordinate2.x + ", Y: " + coordinate2.y);


I get the following output:



X: 546280.89, Y: 6477506.89
X: 3499505.029714266, Y: 1.6513960574356137E7


But it should give me the following output:



X: 15.79263771, Y: 58.43582986
X: 546280.89, Y: 6477506.89


(I checked the "correct" output for the same coordinates that I inputed in the methods on epsg.io)


Because there isn't any good documentation for JMapProjLib, it makes it even harder to figure everything out. If anyone who knows something about this would like to explain what I am doing wrong, I would be more than pleased!





Aucun commentaire:

Enregistrer un commentaire