示例#1
0
def test_proj_transformation_grid():
    import kart  # noqa
    from osgeo import osr

    # note kart.__init__ sets PROJ_NETWORK=ON and PROJ_LIB to the proj data files
    # during tests $HOME is changed to a temporary dir, so downloaded grids in the
    # user proj data dir will be empty/missing
    # TODO: except for windows where somehow it's buggy and we get a network error
    print("PROJ/GDAL-related environment variables:")
    for k, v in os.environ.items():
        if re.match(r"^(GDAL|PROJ|CPL)_", k):
            print(f"${k}={v}")

    print("osr.GetPROJSearchPaths():", osr.GetPROJSearchPaths())
    print("osr.GetPROJAuxDbPaths():", osr.GetPROJAuxDbPaths())
    # print("osr.GetPROJEnableNetwork():", osr.GetPROJEnableNetwork())  # not in the Python bindings yet

    # Do a test conversion to check the transformation grids are available
    nzgd49 = osr.SpatialReference()
    nzgd49.ImportFromEPSG(4272)  # NZGD1949
    nzgd2k = osr.SpatialReference()
    nzgd2k.ImportFromEPSG(4167)  # NZGD2000
    ct = osr.CreateCoordinateTransformation(nzgd49, nzgd2k)
    # Test point from: https://www.linz.govt.nz/data/geodetic-system/coordinate-conversion/geodetic-datum-conversions/datum-transformation-examples
    pt = ct.TransformPoint(-36.5, 175.0)

    # Equivalent commands:
    # $ echo 175.0 -36.5 | PROJ_DEBUG=2 CPL_DEBUG=ON gdaltransform -s_srs EPSG:4272 -t_srs EPSG:4167
    # $ echo -36.5 175.0 0 | PROJ_DEBUG=2 cs2cs -v -f "%.8f" EPSG:4272 EPSG:4167

    if not is_windows:
        # This is the (desired) accurate result expected when the transformation grid is available:
        assert pt == pytest.approx((-36.49819023, 175.00019297, 0.0), abs=1e-8)
    else:
        # This is the less accurate result which uses the 7-parameter transform,
        # which indicates that the transformation grid is not available
        #
        # Currently the Windows proj libraries are built without network
        # support, so we can't auto-fetch grids
        assert pt == pytest.approx((-36.49819267, 175.00018527, 0.0), abs=1e-8)
示例#2
0
def test_proj(src_srs, src_xyz, src_error, dst_srs, dst_xyz, dst_error,
              unit_name, options, grid_req, proj_version_req):

    if grid_req is not None:
        grid_name = grid_req
        assert grid_name in map_old_grid_name_to_tif_ones
        search_paths = osr.GetPROJSearchPaths()
        found = False
        if search_paths:
            for path in search_paths:
                if os.path.exists(os.path.join(path, grid_name)) or \
                   os.path.exists(os.path.join(path, map_old_grid_name_to_tif_ones[grid_name])):
                    found = True
                    break
        if not found:
            pytest.skip(f'Did not find GRID:{grid_name}')

    if proj_version_req is not None:
        major, minor, micro = proj_version_req.split('.')
        major, minor, micro = int(major), int(minor), int(micro)
        if osr.GetPROJVersionMajor(
        ) * 10000 + osr.GetPROJVersionMinor() * 100 + osr.GetPROJVersionMicro(
        ) <= major * 10000 + minor * 100 + micro:
            pytest.skip(f'PROJ version < {proj_version_req}')

    src = osr.SpatialReference()
    src.SetAxisMappingStrategy(osr.OAMS_AUTHORITY_COMPLIANT)
    assert src.SetFromUserInput(src_srs) == 0, \
        ('SetFromUserInput(%s) failed.' % src_srs)

    dst = osr.SpatialReference()
    dst.SetAxisMappingStrategy(osr.OAMS_AUTHORITY_COMPLIANT)
    assert dst.SetFromUserInput(dst_srs) == 0, \
        ('SetFromUserInput(%s) failed.' % dst_srs)

    has_built_ct = False
    if options and '=' in options:
        tokens = options.split('=')
        if len(tokens) == 2:
            key = tokens[0]
            value = tokens[1]
            with gdaltest.config_option(key, value):
                has_built_ct = True
                ct = osr.CoordinateTransformation(src, dst)
    if not has_built_ct:
        ct = osr.CoordinateTransformation(src, dst)

    ######################################################################
    # Transform source point to destination SRS.

    result = ct.TransformPoint(src_xyz[0], src_xyz[1], src_xyz[2])

    error = abs(result[0] - dst_xyz[0]) \
        + abs(result[1] - dst_xyz[1]) \
        + abs(result[2] - dst_xyz[2])

    assert error <= dst_error, \
        ('Dest error is %g, got (%.15g,%.15g,%.15g)'
                             % (error, result[0], result[1], result[2]))

    ######################################################################
    # Now transform back.

    has_built_ct = False
    if options and '=' in options:
        tokens = options.split('=')
        if len(tokens) == 2:
            key = tokens[0]
            value = tokens[1]
            with gdaltest.config_option(key, value):
                has_built_ct = True
                ct = osr.CoordinateTransformation(dst, src)
    if not has_built_ct:
        ct = osr.CoordinateTransformation(dst, src)

    result = ct.TransformPoint(result[0], result[1], result[2])

    error = abs(result[0] - src_xyz[0]) \
        + abs(result[1] - src_xyz[1]) \
        + abs(result[2] - src_xyz[2])

    assert error <= src_error, \
        ('Back to source error is %g got (%.15g,%.15g,%.15g)'
                            % (error, result[0], result[1], result[2]))
示例#3
0
def test_proj(src_srs, src_xyz, src_error,
             dst_srs, dst_xyz, dst_error, unit_name, options, requirements):

    if requirements is not None and requirements[:5] == 'GRID:':
        grid_name = requirements[5:]
        if grid_name == 'egm96_15.gtx' and \
           osr.GetPROJVersionMajor() * 10000 + osr.GetPROJVersionMinor() * 100 + osr.GetPROJVersionMicro() < 60201:
            # Issues before PROJ 6.2.1
            pytest.skip()
        search_paths = osr.GetPROJSearchPaths()
        found = False
        for path in search_paths:
            if os.path.exists(os.path.join(path, grid_name)):
                found = True
                break
        if not found:
            #print( 'Did not find GRID:%s' % grid_name )
            pytest.skip()

    src = osr.SpatialReference()
    assert src.SetFromUserInput(src_srs) == 0, \
        ('SetFromUserInput(%s) failed.' % src_srs)

    dst = osr.SpatialReference()
    assert dst.SetFromUserInput(dst_srs) == 0, \
        ('SetFromUserInput(%s) failed.' % dst_srs)

    if requirements is not None and requirements[0] != 'G':
        additionnal_error_str = ' Check that proj version is >= %s ' % requirements
    else:
        additionnal_error_str = ''

    has_built_ct = False
    if options and '=' in options:
        tokens = options.split('=')
        if len(tokens) == 2:
            key = tokens[0]
            value = tokens[1]
            with gdaltest.config_option(key, value):
                has_built_ct = True
                ct = osr.CoordinateTransformation(src, dst)
    if not has_built_ct:
        ct = osr.CoordinateTransformation(src, dst)

    ######################################################################
    # Transform source point to destination SRS.

    result = ct.TransformPoint(src_xyz[0], src_xyz[1], src_xyz[2])

    error = abs(result[0] - dst_xyz[0]) \
        + abs(result[1] - dst_xyz[1]) \
        + abs(result[2] - dst_xyz[2])

    assert error <= dst_error, \
        ('Dest error is %g, got (%.15g,%.15g,%.15g)%s'
                             % (error, result[0], result[1], result[2], additionnal_error_str))

    ######################################################################
    # Now transform back.

    has_built_ct = False
    if options and '=' in options:
        tokens = options.split('=')
        if len(tokens) == 2:
            key = tokens[0]
            value = tokens[1]
            with gdaltest.config_option(key, value):
                has_built_ct = True
                ct = osr.CoordinateTransformation(dst, src)
    if not has_built_ct:
        ct = osr.CoordinateTransformation(dst, src)

    result = ct.TransformPoint(result[0], result[1], result[2])

    error = abs(result[0] - src_xyz[0]) \
        + abs(result[1] - src_xyz[1]) \
        + abs(result[2] - src_xyz[2])

    assert error <= src_error, \
        ('Back to source error is %g got (%.15g,%.15g,%.15g)%s'
                            % (error, result[0], result[1], result[2], additionnal_error_str))