コード例 #1
0
def test_osr_ct_OGR_CT_PREFER_OFFICIAL_SRS_DEF():

    # Not sure about the minimal version, but works as expected with 7.2.1
    if osr.GetPROJVersionMajor() * 100 + osr.GetPROJVersionMinor() < 702:
        pytest.skip('requires PROJ 7.2 or later')

    wkt = "PROJCS[\"OSGB 1936 / British National Grid\",GEOGCS[\"OSGB 1936\",DATUM[\"OSGB_1936\",SPHEROID[\"Airy 1830\",6377563.396,299.3249646,AUTHORITY[\"EPSG\",\"7001\"]],TOWGS84[446.448,-125.157,542.06,0.15,0.247,0.842,-20.489],AUTHORITY[\"EPSG\",\"6277\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4277\"]],PROJECTION[\"Transverse_Mercator\"],PARAMETER[\"latitude_of_origin\",49],PARAMETER[\"central_meridian\",-2],PARAMETER[\"scale_factor\",0.9996012717],PARAMETER[\"false_easting\",400000],PARAMETER[\"false_northing\",-100000],UNIT[\"metre\",1,AUTHORITY[\"EPSG\",\"9001\"]],AXIS[\"Easting\",EAST],AXIS[\"Northing\",NORTH],AUTHORITY[\"EPSG\",\"27700\"]]"
    s = osr.SpatialReference()
    s.SetFromUserInput(wkt)

    t = osr.SpatialReference()
    t.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER)
    t.ImportFromEPSG(4258)  # ETRS 89

    # No datum shift
    ct = osr.CoordinateTransformation(s, t)
    x, y, _ = ct.TransformPoint(826158.063, 2405844.125, 0)
    assert abs(x - 9.873) < 0.001, x
    assert abs(y - 71.127) < 0.001, y

    # Datum shift implied by the TOWGS4 clause
    with gdaltest.config_option('OGR_CT_PREFER_OFFICIAL_SRS_DEF', 'NO'):
        ct = osr.CoordinateTransformation(s, t)
        x, y, _ = ct.TransformPoint(826158.063, 2405844.125, 0)
        assert abs(x - 9.867) < 0.001, x
        assert abs(y - 71.125) < 0.001, y
コード例 #2
0
ファイル: tiff_srs.py プロジェクト: luzpaz/gdal
def test_tiff_srs_write_vertical_perspective():

    if osr.GetPROJVersionMajor() * 100 + osr.GetPROJVersionMinor() < 700:
        pytest.skip('requires PROJ 7 or later')

    ds = gdal.GetDriverByName('GTiff').Create('/vsimem/src.tif', 1, 1)
    sr = osr.SpatialReference()
    sr.SetGeogCS("GEOG_NAME", "D_DATUM_NAME", "", 3000000, 0)
    sr.SetVerticalPerspective(1, 2, 0, 1000, 0, 0)
    gdal.ErrorReset()
    ds.SetSpatialRef(sr)
    assert gdal.GetLastErrorMsg() == ''
    ds = None

    src_ds = gdal.Open('/vsimem/src.tif')
    # First is PROJ 7
    assert src_ds.GetSpatialRef().ExportToProj4() in (
        '+proj=nsper +lat_0=1 +lon_0=2 +h=1000 +x_0=0 +y_0=0 +R=3000000 +units=m +no_defs',
        '+proj=nsper +R=3000000 +lat_0=1 +lon_0=2 +h=1000 +x_0=0 +y_0=0 +wktext +no_defs'
    )
    gdal.ErrorReset()
    gdal.GetDriverByName('GTiff').CreateCopy('/vsimem/dst.tif', src_ds)
    assert gdal.GetLastErrorMsg() == ''

    ds = gdal.Open('/vsimem/dst.tif')
    assert ds.GetSpatialRef().ExportToProj4() == src_ds.GetSpatialRef(
    ).ExportToProj4()

    src_ds = None
    ds = None

    gdal.GetDriverByName('GTiff').Delete('/vsimem/src.tif')
    gdal.GetDriverByName('GTiff').Delete('/vsimem/dst.tif')
コード例 #3
0
def test_osr_unknown_member_id_in_datum_ensemble():

    if not(osr.GetPROJVersionMajor() > 6 or osr.GetPROJVersionMinor() >= 2):
        pytest.skip()

    # Test workaround fix for https://github.com/OSGeo/PROJ/pull/3221

    projjson = '{"$schema":"https://proj.org/schemas/v0.4/projjson.schema.json","type":"GeographicCRS","name":"WGS 84","datum_ensemble":{"name":"World Geodetic System 1984 ensemble","members":[{"name":"World Geodetic System 1984 (Transit)","id":{"authority":"EPSG","code":1166}},{"name":"World Geodetic System 1984 (G730)","id":{"authority":"EPSG","code":1152}},{"name":"World Geodetic System 1984 (G873)","id":{"authority":"EPSG","code":1153}},{"name":"World Geodetic System 1984 (G1150)","id":{"authority":"EPSG","code":1154}},{"name":"World Geodetic System 1984 (G1674)","id":{"authority":"EPSG","code":1155}},{"name":"World Geodetic System 1984 (G1762)","id":{"authority":"EPSG","code":1156}},{"name":"World Geodetic System 1984 (G2139)","id":{"authority":"EPSG","code":1309}},{"name":"unknown datum","id":{"authority":"UNKNOW?","code":1234}}],"ellipsoid":{"name":"WGS 84","semi_major_axis":6378137,"inverse_flattening":298.257223563},"accuracy":"2.0","id":{"authority":"EPSG","code":6326}},"coordinate_system":{"subtype":"ellipsoidal","axis":[{"name":"Geodetic latitude","abbreviation":"Lat","direction":"north","unit":"degree"},{"name":"Geodetic longitude","abbreviation":"Lon","direction":"east","unit":"degree"}]},"scope":"Horizontal component of 3D system.","area":"World.","bbox":{"south_latitude":-90,"west_longitude":-180,"north_latitude":90,"east_longitude":180},"id":{"authority":"EPSG","code":4326}}'
    sr = osr.SpatialReference()
    assert sr.SetFromUserInput(projjson) == 0
コード例 #4
0
ファイル: osr_compd.py プロジェクト: pciphelippeneveu/gdal-1
def test_osr_compd_vert_datum_2002():

    if osr.GetPROJVersionMajor() * 10000 + osr.GetPROJVersionMinor() * 100 < 70100:
        # Not supported before PROJ 7.1
        pytest.skip()

    sr = osr.SpatialReference()
    sr.SetFromUserInput('COMPD_CS["NAD83 / Alabama West + Ellipsoidal height",PROJCS["NAD83 / Alabama West",GEOGCS["NAD83",DATUM["North_American_Datum_1983",SPHEROID["GRS 1980",6378137,298.257222101,AUTHORITY["EPSG","7019"]],TOWGS84[0,0,0,0,0,0,0],AUTHORITY["EPSG","6269"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4269"]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",30],PARAMETER["central_meridian",-87.5],PARAMETER["scale_factor",0.999933333],PARAMETER["false_easting",600000],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["X",EAST],AXIS["Y",NORTH],AUTHORITY["EPSG","26930"]],VERT_CS["Ellipsoidal height",VERT_DATUM["Ellipsoid",2002],UNIT["metre",1.0,AUTHORITY["EPSG","9001"]],AXIS["Up",UP]]]')
    assert sr.IsProjected()
    assert sr.GetAuthorityCode('PROJCS') == '26930'
    assert sr.GetAuthorityName('PROJCS') == 'EPSG'
    assert sr.GetAuthorityCode(None) is None
    assert sr.GetAuthorityName(None) is None
コード例 #5
0
    def pts(self, pts):
        if len(pts) == 0:
            return []

        if len(pts[0]) != 2:
            raise ReprojError('Points must be [ (x,y) ]')

        if self.iproj == 'NO_REPROJ':
            return pts

        if self.iproj == 'GDAL':
            #Since PROJ 6, the order of coordinates for geographic crs is latitude first, longitude second.
            if hasattr(osr, 'GetPROJVersionMajor'):
                projVersion = osr.GetPROJVersionMajor()
            else:
                projVersion = 4
            if projVersion >= 6 and self.crs1.IsGeographic():
                pts = [(pt[1], pt[0]) for pt in pts]
            if self.crs2.IsGeographic():
                ys, xs, _zs = zip(*self.osrTransfo.TransformPoints(pts))
            else:
                xs, ys, _zs = zip(*self.osrTransfo.TransformPoints(pts))
            return list(zip(xs, ys))

        elif self.iproj == 'PYPROJ':
            if self.crs1.crs.is_geographic:
                ys, xs = zip(*pts)
            else:
                xs, ys = zip(*pts)
            transformer = pyproj.Transformer.from_proj(self.crs1, self.crs2)
            if self.crs2.crs.is_geographic:
                ys, xs = transformer.transform(xs, ys)
            else:
                xs, ys = transformer.transform(xs, ys)
            return list(zip(xs, ys))

        elif self.iproj == 'EPSGIO':
            return EPSGIO.reprojPts(self.crs1, self.crs2, pts)

        elif self.iproj == 'BUILTIN':
            #Web Mercator
            if self.crs1 == 4326 and self.crs2 == 3857:
                return [lonLatToWebMerc(*pt) for pt in pts]
            elif self.crs1 == 3857 and self.crs2 == 4326:
                return [webMercToLonLat(*pt) for pt in pts]
            #UTM
            if self.crs1 == 4326 and self.crs2 in UTM_EPSG_CODES:
                return [self.utm.lonlat_to_utm(*pt) for pt in pts]
            elif self.crs1 in UTM_EPSG_CODES and self.crs2 == 4326:
                return [self.utm.utm_to_lonlat(*pt) for pt in pts]
コード例 #6
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)
コード例 #7
0
ファイル: tiff_srs.py プロジェクト: kongdd/gdal
def test_tiff_srs_dynamic_geodetic_crs():

    if osr.GetPROJVersionMajor() < 8:
        pytest.skip()

    srs = osr.SpatialReference()
    srs.ImportFromEPSG(8999) # ITRF2008
    ds = gdal.GetDriverByName('GTiff').Create(
        '/vsimem/test_tiff_srs_dynamic_geodetic_crs.tif', 1, 1)
    ds.SetSpatialRef(srs)
    ds = None
    ds = gdal.Open('/vsimem/test_tiff_srs_dynamic_geodetic_crs.tif')
    gdal.ErrorReset()
    srs = ds.GetSpatialRef()
    assert gdal.GetLastErrorMsg() == '', srs.ExportToWkt(['FORMAT=WKT2_2019'])
    assert srs.GetAuthorityCode(None) == '8999'
    ds = None
    gdal.Unlink('/vsimem/test_tiff_srs_dynamic_geodetic_crs.tif')
コード例 #8
0
def test_osr_ct_take_into_account_srs_coordinate_epoch():

    if osr.GetPROJVersionMajor() * 100 + osr.GetPROJVersionMinor() < 702:
        pytest.skip('requires PROJ 7.2 or later')

    s = osr.SpatialReference()
    s.SetFromUserInput("EPSG:7844")  # GDA2020
    s.SetAxisMappingStrategy(osr.OAMS_AUTHORITY_COMPLIANT)

    t_2020 = osr.SpatialReference()
    t_2020.SetAxisMappingStrategy(osr.OAMS_AUTHORITY_COMPLIANT)
    t_2020.SetFromUserInput("EPSG:9000")  # ITRF2014
    t_2020.SetCoordinateEpoch(2020)

    # 2020 is the central epoch of the transformation, so no coordinate
    # change is expected
    ct = osr.CoordinateTransformation(s, t_2020)
    x, y, _ = ct.TransformPoint(-30, 150, 0)
    assert x == pytest.approx(-30, abs=1e-10)
    assert y == pytest.approx(150, abs=1e-10)

    t_2030 = osr.SpatialReference()
    t_2030.SetAxisMappingStrategy(osr.OAMS_AUTHORITY_COMPLIANT)
    t_2030.SetFromUserInput("EPSG:9000")  # ITRF2014
    t_2030.SetCoordinateEpoch(2030)

    ct = osr.CoordinateTransformation(s, t_2030)
    x, y, _ = ct.TransformPoint(-30, 150, 0)
    assert x == pytest.approx(-29.9999950478, abs=1e-10)
    assert y == pytest.approx(150.0000022212, abs=1e-10)

    ct = osr.CoordinateTransformation(t_2030, s)
    x, y, _ = ct.TransformPoint(-29.9999950478, 150.0000022212, 0)
    assert x == pytest.approx(-30, abs=1e-10)
    assert y == pytest.approx(150, abs=1e-10)

    # Not properly supported currently
    gdal.ErrorReset()
    with gdaltest.error_handler():
        ct = osr.CoordinateTransformation(t_2020, t_2030)
    assert gdal.GetLastErrorMsg() != ''
コード例 #9
0
ファイル: osr_ct.py プロジェクト: pciphelippeneveu/gdal-1
def test_osr_ct_transformpointwitherrorcode():

    if osr.GetPROJVersionMajor() < 8:
        # Issue before PROJ 8
        pytest.skip()

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

    x, y, z, t, error_code = ct.TransformPointWithErrorCode(1, 2, 3, 4)
    assert x == pytest.approx(111257.80439304397, rel=1e-10)
    assert y == pytest.approx(221183.3401672801, rel=1e-10)
    assert z == 3
    assert t == 4
    assert error_code == 0

    x, y, z, t, error_code = ct.TransformPointWithErrorCode(90, 0, 0, 0)
    assert math.isinf(x)
    assert error_code == osr.PROJ_ERR_COORD_TRANSFM_OUTSIDE_PROJECTION_DOMAIN
コード例 #10
0
ファイル: test_ogr2ogr_lib.py プロジェクト: tnixeu/gdal
def test_ogr2ogr_t_coord_epoch():

    if osr.GetPROJVersionMajor() * 100 + osr.GetPROJVersionMinor() < 702:
        pytest.skip('requires PROJ 7.2 or later')

    src_ds = gdal.GetDriverByName('Memory').Create('', 0, 0, 0,
                                                   gdal.GDT_Unknown)
    src_lyr = src_ds.CreateLayer('layer')
    f = ogr.Feature(src_lyr.GetLayerDefn())
    f.SetGeometry(ogr.CreateGeometryFromWkt('POINT(120 -40)'))
    src_lyr.CreateFeature(f)

    # GDA2020 to ITRF2014
    ds = gdal.VectorTranslate(
        '',
        src_ds,
        options=
        '-f Memory -t_srs EPSG:9000 -t_coord_epoch 2030 -s_srs EPSG:7844')
    lyr = ds.GetLayer(0)
    f = lyr.GetNextFeature()
    g = f.GetGeometryRef()
    assert g.GetX(0) != 120 and abs(g.GetX(0) - 120) < 1e-5
    assert g.GetY(0) != -40 and abs(g.GetY(0) - -40) < 1e-5
コード例 #11
0
ファイル: tiff_srs.py プロジェクト: luzpaz/gdal
def test_tiff_srs_try_write_derived_geographic():

    if osr.GetPROJVersionMajor() < 7:
        pytest.skip()

    tmpfile = '/vsimem/tmp.tif'
    ds = gdal.GetDriverByName('GTiff').Create(tmpfile, 1, 1)
    wkt = 'GEOGCRS["Coordinate System imported from GRIB file",BASEGEOGCRS["Coordinate System imported from GRIB file",DATUM["unnamed",ELLIPSOID["Sphere",6367470,0,LENGTHUNIT["metre",1,ID["EPSG",9001]]]],PRIMEM["Greenwich",0,ANGLEUNIT["degree",0.0174532925199433,ID["EPSG",9122]]]],DERIVINGCONVERSION["Pole rotation (GRIB convention)",METHOD["Pole rotation (GRIB convention)"],PARAMETER["Latitude of the southern pole (GRIB convention)",-30,ANGLEUNIT["degree",0.0174532925199433,ID["EPSG",9122]]],PARAMETER["Longitude of the southern pole (GRIB convention)",-15,ANGLEUNIT["degree",0.0174532925199433,ID["EPSG",9122]]],PARAMETER["Axis rotation (GRIB convention)",0,ANGLEUNIT["degree",0.0174532925199433,ID["EPSG",9122]]]],CS[ellipsoidal,2],AXIS["latitude",north,ORDER[1],ANGLEUNIT["degree",0.0174532925199433,ID["EPSG",9122]]],AXIS["longitude",east,ORDER[2],ANGLEUNIT["degree",0.0174532925199433,ID["EPSG",9122]]]]'
    ds.SetProjection(wkt)
    ds = None

    assert gdal.VSIStatL(tmpfile + '.aux.xml')
    ds = gdal.Open(tmpfile)
    srs = ds.GetSpatialRef()
    assert srs is not None
    assert srs.IsDerivedGeographic()
    ds = None

    gdal.Unlink(tmpfile + '.aux.xml')
    ds = gdal.Open(tmpfile)
    assert ds.GetSpatialRef() is None
    ds = None

    gdal.Unlink(tmpfile)
コード例 #12
0
def proj_version() -> Tuple[int, int]:
    """Returns PROJ library version """
    major: int = osr.GetPROJVersionMajor()
    minor: int = osr.GetPROJVersionMinor()
    return major, minor
コード例 #13
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]))
コード例 #14
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))
コード例 #15
0
    def transform_to_WGS84(self, geo_transform: tuple, image_size_x: int, image_size_y: int, projection: str) -> CJson:
        """
        wgs84坐标系转换结果(wgs84节点)
        :param geo_transform:
        :param image_size_x:
        :param image_size_y:
        :param projection:
        :return:
        """
        json_wgs84 = CJson()
        spatial_ref = osr.SpatialReference()
        spatial_ref.SetWellKnownGeogCS('WGS84')
        wgs84_wkt = spatial_ref.ExportToWkt()
        wgs84_proj4 = spatial_ref.ExportToProj4()
        spatial_ref.MorphToESRI()
        wgs84_esri = spatial_ref.ExportToWkt()
        json_wgs84_coordinate = CJson()
        json_wgs84_coordinate.set_value_of_name('wkt', wgs84_wkt)
        json_wgs84_coordinate.set_value_of_name('proj4', wgs84_proj4)
        json_wgs84_coordinate.set_value_of_name('esri', wgs84_esri)
        json_wgs84.set_value_of_name('coordinate', json_wgs84_coordinate.json_obj)

        if geo_transform is not None:
            point_left_top_x = geo_transform[0]
            point_left_top_y = geo_transform[3]
            point_right_bottom_x = geo_transform[0] + image_size_x * geo_transform[1] + image_size_y * geo_transform[2]
            point_right_bottom_y = geo_transform[3] + image_size_x * geo_transform[4] + image_size_y * geo_transform[5]
            rb = (0, 0)
            lu = (0, 0)
            if not CUtils.equal_ignore_case(projection, ''):
                source_projection = osr.SpatialReference(wkt=projection)
                source = source_projection.GetAttrValue('GEOGCS', 0)
                prosrs = osr.SpatialReference()
                prosrs.ImportFromWkt(projection)
                geosrs = prosrs.CloneGeogCS()

                if "GetPROJVersionMajor" in dir(osr) and osr.GetPROJVersionMajor() >= 6:  # gdal>=3.0
                    prosrs.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER)
                    geosrs.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER)

                ct = osr.CreateCoordinateTransformation(prosrs, geosrs)
                if ct is not None:
                    rb = ct.TransformPoint(point_right_bottom_x, point_right_bottom_y)
                    lu = ct.TransformPoint(point_left_top_x, point_left_top_y)

                    json_bounding = CJson()
                    json_bounding.set_value_of_name('left', lu[0])
                    json_bounding.set_value_of_name('top', lu[1])
                    json_bounding.set_value_of_name('right', rb[0])
                    json_bounding.set_value_of_name('bottom', rb[1])
                    json_wgs84.set_value_of_name('boundingbox', json_bounding.json_obj)
                    json_wgs84.set_value_of_name('msg', 'boundingbox四至范围从{0}坐标系转wgs_84坐标系转换成功!'.format(source))
                    json_wgs84.set_value_of_name('result', self.Success)
                else:
                    json_wgs84.set_value_of_name('msg',
                                                 'boundingbox四至范围从{0}坐标系转wgs_84坐标系转换失败!失败原因:构建坐标转换关系失败!可能是地方坐标系,无法转换。'.format(
                                                     source))
                    json_wgs84.set_value_of_name('result', self.Failure)
            else:
                json_wgs84.set_value_of_name('msg',
                                             'boundingbox四至范围从原坐标系转wgs_84坐标系转换失败!失败原因:文件不存在coordinate信息!')
                json_wgs84.set_value_of_name('result', self.Failure)
        else:
            json_wgs84.set_value_of_name('msg',
                                         'boundingbox四至范围从原坐标系转wgs_84坐标系转换失败!失败原因:文件不存在仿射变换信息!')
            json_wgs84.set_value_of_name('result', self.Failure)
        return json_wgs84