Exemple #1
0
def test_osr_ct_lon_wrap():

    if osr.GetPROJVersionMajor() * 10000 + osr.GetPROJVersionMinor(
    ) * 100 + osr.GetPROJVersionMicro() < 70001:
        # Issue before PROJ 7.0.1
        pytest.skip()

    s = osr.SpatialReference()
    s.SetFromUserInput("+proj=longlat +ellps=GRS80")
    t = osr.SpatialReference()
    t.SetFromUserInput("+proj=longlat +ellps=GRS80 +lon_wrap=180")
    ct = osr.CoordinateTransformation(s, t)
    assert ct

    x, y, _ = ct.TransformPoint(-25, 60, 0)
    assert x == pytest.approx(-25 + 360, abs=1e-12)
    assert y == pytest.approx(60, abs=1e-12)
Exemple #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]))
Exemple #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))