Skip to content

wrong result while using proj_create_crs_to_crs()? #2087

Closed

Description

@mbudaj

Example of problem

I'm not able to use proj_create_crs_to_crs() to get correct transformation between two CRSs defined using the traditional "+proj=..." string definition. For the sake of simplicity I use transformation of a location 48N 19E from "latlong" to "utm zone 34" in the following example.

#include <iostream>
#include <proj.h>

using namespace std;

int main() {
    double d2r = 3.1415926535/180.0;

    // variant 1
    PJ* P = proj_create_crs_to_crs(PJ_DEFAULT_CTX, "+proj=latlong", "+proj=utm +zone=34", NULL);
    // variant 2
//  PJ* P = proj_create_crs_to_crs(PJ_DEFAULT_CTX, "+proj=latlong +type=crs", "+proj=utm +zone=34 +type=crs", NULL);
    // variant 3
//  PJ* P = proj_create(PJ_DEFAULT_CTX, "+proj=pipeline +step +inv +proj=latlong +step +proj=utm +zone=34");

    if (P==0) {
      cout << "PROJ error: " << proj_errno(P) << " -- " << proj_errno_string(proj_errno(P)) << endl;
      proj_destroy(P);
      return(1);
    }

    PJ_COORD res;
    res = proj_trans(P, PJ_FWD, proj_coord(19.0 * d2r, 48.0 * d2r, 0.0, 0));
    cout << fixed << res.xyz.x << " " << res.xyz.y << " " << res.xyz.z << endl;

    proj_destroy(P);
    return 0;
}

Problem description

When using Proj 6.3.1, both variant 1 and 2 return incorrect result:
-1851536.106814 99013.628434 0.000000
In Proj 5.2.0 both variants result in an error -1 (no arguments in initialization list).

Expected Output

Variant 3 which uses pipelines returns the correct result in both Proj 6.3.1 and 5.2.0:
350812.124995 5318235.613731 0.000000

Environment Information

  • PROJ version: 6.3.1 and 5.2.0
  • Operation System Information: Debian bullseye and buster.

Installation method

  • apt-get

Activity

  1. kbevers commented on Mar 21, 2020

    @kbevers
    Member

    When using Proj 6.3.1, both variant 1 and 2 return incorrect result:
    -1851536.106814 99013.628434 0.000000

    You are inputting radians when you should be inputting degrees. I suspect it will work fine if you skip the conversions from degrees to radians.

    In Proj 5.2.0 both variants result in an error -1 (no arguments in initialization list).

    Yeah, proj_create_from_crs_to_crs only takes EPSG-codes in PROJ 5.x, so inputting a proj-string will not work. Use functions from proj_api.h for that when using 5.x

    Variant 3 which uses pipelines returns the correct result in both Proj 6.3.1 and 5.2.0:
    350812.124995 5318235.613731 0.000000

    Yes, because here it is correct to input radians.

    The problem you are facing is that with PROJ 6 and later proj_create_crs_to_crs respects the input units of the CRS, which more or less always means using degrees rather than radians. Behind the scenes the PJ object returned contains an elaborate pipeline. As seen by this call to projinfo:

    $ projinfo -s "+proj=latlong +type=crs" -t "+proj=utm +zone=34 +type=crs" -o PROJ
    Candidate operations found: 1
    -------------------------------------
    Operation n°1:
    
    EPSG:16034, UTM zone 34N, 0 m, unknown domain of validity
    
    PROJ string:
    +proj=pipeline +step +proj=unitconvert +xy_in=deg +xy_out=rad +step +proj=utm +zone=34 +ellps=WGS84
    

    proj_create is the tool for super users that knows what they are doing. You supply the pipeline so you are expected to know what it needs to perform correctly. proj_create_crs_to_crs holds your hand as much as possible.

    Long story short, PROJ performs as expected.

  2. mbudaj commented on Mar 22, 2020

    @mbudaj
    Author

    Thanks, that solved this case.

    I have two subsequent questions, though:


    1) I still can't get proj_create_crs_to_crs() working in 6.3.1 for one variant of krovak projection.

    PJ* P = proj_create_crs_to_crs(PJ_DEFAULT_CTX, "+proj=krovak +czech +ellps=bessel +towgs84=485.021,169.465,483.839,7.786342,4.397554,4.102655,0", "+proj=utm +zone=34", NULL);
    res = proj_trans(P, PJ_FWD, proj_coord(379033, 1208895, 2025.31, 0));
    

    returns the correct result 401375.61 5421243.27 2025.35

    If I omit the parameter +czech which serves historically just to reverse the axes (from GIS west/north to Krovak's east/south) and reverse the sign of input coordinates:

    PJ* P = proj_create_crs_to_crs(PJ_DEFAULT_CTX, "+proj=krovak        +ellps=bessel +towgs84=485.021,169.465,483.839,7.786342,4.397554,4.102655,0", "+proj=utm +zone=34", NULL);
    res = proj_trans(P, PJ_FWD, proj_coord(-379033, -1208895, 2025.31, 0));
    

    I get invalid result -1406406.553884 5757982.001626 2025.310000
    I tried to use proj_normalize_for_visualization() and also tried swapping X/Y of the point without getting the right result.

    cs2cs behaves the same as my examples in 6.3.1 (cct reports 'latitude or longitude exceeded limits' for some reason):

    $ cs2cs +proj=krovak +czech +ellps=bessel +towgs84=485.021,169.465,483.839,7.786342,4.397554,4.102655,0 +to +proj=utm +zone=34
    379033 1208895 2025.31
    401375.88       5421243.68 2025.35
    $ cs2cs +proj=krovak +ellps=bessel +towgs84=485.021,169.465,483.839,7.786342,4.397554,4.102655,0 +to +proj=utm +zone=34
    -379033 -1208895 2025.31
    -1406406.55     5757982.00 2025.31
    

    In 5.2.0, the cs2cs behaviour is consistent (although it ignores +towgs parameter):

    $ cs2cs +proj=krovak +czech +ellps=bessel +towgs84=485.021,169.465,483.839,7.786342,4.397554,4.102655,0 +to +proj=utm +zone=34
    379033 1208895 2025.31
    401501.22       5421292.07 2025.31
    $ cs2cs +proj=krovak +ellps=bessel +towgs84=485.021,169.465,483.839,7.786342,4.397554,4.102655,0 +to +proj=utm +zone=34
    -379033 -1208895 2025.31
    401501.22       5421292.07 2025.31
    

    2) For how long it is expected that the proj_context_use_proj4_init_rules() will be supported?

    Currently I use proj_create() with +pipeline operator and need to support ESRI and EPSG codes. Is this a viable approch also in Proj 7+ or it will become necessary to use proj_create_crs_to_crs() to use ESRI/EPSG definition in the long run?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions