locationtech / proj4j

Java port of the Proj.4 library for coordinate reprojection
Other
181 stars 71 forks source link

Transformation error from 5514 to 4326 #80

Closed mikimikimikia closed 1 year ago

mikimikimikia commented 2 years ago

I would like to point out the mistake from ticket #25 again.

The conversion from 4326 to 5514 works correctly, but in the opposite direction (5514 -> 4326) the calculation still returns incorrect values of approx. 24.8299 -59.94829. It doesn't really matter what values I enter, there are always numbers similar to those listed (numbers in distant decimal positions and Z value change).

For example, my code:

CRSFactory crsFactory = new CRSFactory(); CoordinateReferenceSystem t1 = crsFactory.createFromName("epsg:4326"); CoordinateReferenceSystem t2 = crsFactory.createFromName("epsg:5514"); CoordinateTransformFactory ctFactory = new CoordinateTransformFactory(); CoordinateTransform tran = ctFactory.createTransform(t2, t1); ProjCoordinate result_p2 = new ProjCoordinate(); ProjCoordinate vstup=new ProjCoordinate( -743093.7321490766, -1044381.7725184687); tran.transform(vstup, result_p2);

This code is supposed to return 14.42, 50.075, but it does return 24.82998756399294 -59.94829824087518. Similar result numbers are for any input. Obviously there will be an error somewhere in the transformation equation. It works in one direction, not inversely.

davidklika commented 1 year ago

Hello I went throug the code and I think that the bug is in the KrovakProjection class projectInverse method. When the method is called, x and y holds values in degrees and dst in meters (original values). x and y values are totally ignored in the method and the trigonometric functions are called on dst.x and dst.y values in meters.

When I replaced lines 157-158 with this: ro = hypot(x, y); eps = atan2(-x, -y); the results gets much better (just output axes are reversed and not sure about precision). Such change makes preceding 6 lines and the czech flag redundant, so I am not sure about the consequences.

Can someone help to fix that?

BTW: many calculations does not depend on the input so it can be precomputed in the class constructor and reused.

rhuitl commented 1 year ago

97 fixes this issue for me. Are you @mikimikimikia or @davidklika able to verify it fixes the issue for you?

davidklika commented 1 year ago

The fix works for me as well, thank you :)

rhuitl commented 1 year ago

@pomadchin I think we can close this ticket now :)

pomadchin commented 1 year ago

Thanks! Closed via #97.