def test_osr_ct_4D(): options = osr.CoordinateTransformationOptions() assert options.SetOperation( '+proj=pipeline +step +proj=unitconvert +xy_in=deg +xy_out=rad +step +proj=cart +step +proj=helmert +convention=position_vector +x=0.0127 +dx=-0.0029 +rx=-0.00039 +drx=-0.00011 +y=0.0065 +dy=-0.0002 +ry=0.00080 +dry=-0.00019 +z=-0.0209 +dz=-0.0006 +rz=-0.00114 +drz=0.00007 +s=0.00195 +ds=0.00001 +t_epoch=1988.0 +step +proj=cart +inv +step +proj=unitconvert +xy_in=rad +xy_out=deg' ) ct = osr.CoordinateTransformation(None, None, options) assert ct x, y, z, t = ct.TransformPoint(2, 49, 0, 2000) assert x == pytest.approx(2.0000005420366, abs=1e-10), x assert y == pytest.approx(49.0000003766711, abs=1e-10), y assert z == pytest.approx(-0.0222802283242345, abs=1e-8), z assert t == pytest.approx(2000, abs=1e-10), t ret = ct.TransformPoints([[2, 49, 0, 2000], [2, 49, 0, 1988]]) assert len(ret) == 2, ret assert len(ret[0]) == 4, ret x, y, z, t = ret[0] assert x == pytest.approx(2.0000005420366, abs=1e-10), x assert y == pytest.approx(49.0000003766711, abs=1e-10), y assert z == pytest.approx(-0.0222802283242345, abs=1e-8), z assert t == pytest.approx(2000, abs=1e-10), t assert len(ret[1]) == 4, ret x, y, z, t = ret[1] assert x == pytest.approx(1.9999998809056305, abs=1e-10), x assert y == pytest.approx(48.9999995630005, abs=1e-10), y assert z == pytest.approx(0.005032399669289589, abs=1e-8), z assert t == pytest.approx(1988, abs=1e-10), t
def pcs_2000_to_sz_local(self, points): helmert_para = helmert_para_dict(4547, 2435, "EAST") opt = osr.CoordinateTransformationOptions() opt.SetOperation(helmert_para) tr = osr.CreateCoordinateTransformation(None, None, opt) points = osr.CoordinateTransformation.TransformPoints(tr, points) return points
def test_osr_ct_options_area_of_interest(): srs_nad27 = osr.SpatialReference() srs_nad27.SetFromUserInput("NAD27") srs_wgs84 = osr.SpatialReference() srs_wgs84.SetFromUserInput("WGS84") options = osr.CoordinateTransformationOptions() assert not options.SetAreaOfInterest(-200, 40, -99, 41) assert not options.SetAreaOfInterest(-100, -100, -99, 41) assert not options.SetAreaOfInterest(-100, 40, 200, 41) assert not options.SetAreaOfInterest(-100, 40, -99, 100) assert options.SetAreaOfInterest(-100, 40, -99, 41) ct = osr.CoordinateTransformation(srs_nad27, srs_wgs84, options) assert ct x, y, z = ct.TransformPoint(40.5, -99.5, 0) assert x != 40.5 assert x == pytest.approx(40.5, abs=1e-3) x, y, z = ct.TransformPoint(0, 0, 0) if sys.platform == 'darwin': print( "ct.TransformPoint(0,0,0) doesn't return expected result on MacOSX. Not sure why." ) else: assert x == float('inf')
def test_osr_ct_options_operation(): options = osr.CoordinateTransformationOptions() assert options.SetOperation('+proj=affine +s11=-1') ct = osr.CoordinateTransformation(None, None, options) assert ct x, y, z = ct.TransformPoint(1, 2, 3) assert x == -1 assert y == 2 assert z == 3
def test_osr_ct_non_specified_time_with_time_dependent_transformation(): options = osr.CoordinateTransformationOptions() options.SetOperation( '+proj=pipeline +step +proj=axisswap +order=2,1 +step +proj=unitconvert +xy_in=deg +z_in=m +xy_out=rad +z_out=m +step +proj=cart +ellps=GRS80 +step +inv +proj=helmert +dx=0.0008 +dy=-0.0006 +dz=-0.0014 +drx=6.67e-05 +dry=-0.0007574 +drz=-5.13e-05 +ds=-7e-05 +t_epoch=2010 +convention=coordinate_frame +step +inv +proj=cart +ellps=GRS80 +step +proj=unitconvert +xy_in=rad +z_in=m +xy_out=deg +z_out=m +step +proj=axisswap +order=2,1' ) ct = osr.CoordinateTransformation(None, None, options) assert ct x, y, _ = ct.TransformPoint(50, -40, 0) assert x == pytest.approx(50, abs=1e-10) assert y == pytest.approx(-40, abs=1e-10)
def test_osr_ct_options_ballpark_disallowed(): s = osr.SpatialReference() s.SetFromUserInput("EPSG:4267") # NAD27 t = osr.SpatialReference() t.SetFromUserInput("EPSG:4258") # ETRS89 options = osr.CoordinateTransformationOptions() options.SetBallparkAllowed(False) with gdaltest.error_handler(): ct = osr.CoordinateTransformation(s, t, options) try: ct.TransformPoint(49, 2, 0) assert False except: pass
def test_osr_ct_options_accuracy(): s = osr.SpatialReference() s.SetFromUserInput("EPSG:4326") t = osr.SpatialReference() t.SetFromUserInput("EPSG:4258") # ETRS89 options = osr.CoordinateTransformationOptions() options.SetDesiredAccuracy(0.05) with gdaltest.error_handler(): ct = osr.CoordinateTransformation(s, t, options) try: ct.TransformPoint(49, 2, 0) assert False except: pass
def test_osr_ct_options_area_of_interest(): srs_nad27 = osr.SpatialReference() srs_nad27.SetFromUserInput("NAD27") srs_wgs84 = osr.SpatialReference() srs_wgs84.SetFromUserInput("WGS84") options = osr.CoordinateTransformationOptions() assert not options.SetAreaOfInterest(-200,40,-99,41) assert not options.SetAreaOfInterest(-100,-100,-99,41) assert not options.SetAreaOfInterest(-100,40,200,41) assert not options.SetAreaOfInterest(-100,40,-99,100) assert options.SetAreaOfInterest(-100,40,-99,41) ct = osr.CoordinateTransformation(srs_nad27, srs_wgs84, options) assert ct x, y, z = ct.TransformPoint(40.5,-99.5,0) assert x != 40.5 assert abs(x - 40.5) < 1e-3 x, y, z = ct.TransformPoint(0,0,0) assert x == float('inf')
def reproject(*args, **kwargs): """Transform coordinates from a source projection to a target projection. Call signatures:: reproject(C, **kwargs) reproject(X, Y, **kwargs) reproject(X, Y, Z, **kwargs) *C* is the np array of source coordinates. *X*, *Y* and *Z* specify arrays of x, y, and z coordinate values Parameters ---------- C : multidimensional :class:`numpy:numpy.ndarray` Array of shape (...,2) or (...,3) with coordinates (x,y) or (x,y,z) respectively X : :class:`numpy:numpy.ndarray` Array of x coordinates Y : :class:`numpy:numpy.ndarray` Array of y coordinates Z : :class:`numpy:numpy.ndarray` Array of z coordinates Keyword Arguments ----------------- projection_source : osr object defaults to EPSG(4326) projection_target : osr object defaults to EPSG(4326) area_of_interest : tuple tuple of floats (WestLongitudeDeg, SouthLatitudeDeg, EastLongitudeDeg, dfNorthLatitudeDeg), only gdal>=3 Returns ------- trans : :class:`numpy:numpy.ndarray` Array of reprojected coordinates x,y (...,2) or x,y,z (...,3) depending on input array. X, Y : :class:`numpy:numpy.ndarray` Arrays of reprojected x,y coordinates, shape depending on input array X, Y, Z: :class:`numpy:numpy.ndarray` Arrays of reprojected x,y,z coordinates, shape depending on input array Examples -------- See :ref:`/notebooks/georeferencing/wradlib_georef_example.ipynb`. """ if len(args) == 1: C = np.asanyarray(args[0]) cshape = C.shape numCols = C.shape[-1] C = C.reshape(-1, numCols) if numCols < 2 or numCols > 3: raise TypeError('Input Array column mismatch ' 'to %s' % ('reproject')) else: if len(args) == 2: X, Y = (np.asanyarray(arg) for arg in args) numCols = 2 elif len(args) == 3: X, Y, Z = (np.asanyarray(arg) for arg in args) zshape = Z.shape numCols = 3 else: raise TypeError('Illegal arguments to %s' % ('reproject')) xshape = X.shape yshape = Y.shape if xshape != yshape: raise TypeError('Incompatible X, Y inputs to %s' % ('reproject')) if 'Z' in locals(): if xshape != zshape: raise TypeError('Incompatible Z input to %s' % ('reproject')) C = np.concatenate( [X.ravel()[:, None], Y.ravel()[:, None], Z.ravel()[:, None]], axis=1) else: C = np.concatenate( [X.ravel()[:, None], Y.ravel()[:, None]], axis=1) projection_source = kwargs.get('projection_source', get_default_projection()) projection_target = kwargs.get('projection_target', get_default_projection()) area_of_interest = kwargs.get( 'area_of_interest', (np.float(C[..., 0].min()), np.float(C[..., 1].min()), np.float(C[..., 0].max()), np.float(C[..., 1].max()))) if gdal.VersionInfo()[0] >= '3': axis_order = osr.OAMS_TRADITIONAL_GIS_ORDER projection_source.SetAxisMappingStrategy(axis_order) projection_target.SetAxisMappingStrategy(axis_order) options = osr.CoordinateTransformationOptions() options.SetAreaOfInterest(*area_of_interest) ct = osr.CreateCoordinateTransformation(projection_source, projection_target, options) else: ct = osr.CoordinateTransformation(projection_source, projection_target) trans = np.array(ct.TransformPoints(C)) if len(args) == 1: # here we could do this one # return(np.array(ct.TransformPoints(C))[...,0:numCols])) # or this one trans = trans[:, 0:numCols].reshape(cshape) return trans else: X = trans[:, 0].reshape(xshape) Y = trans[:, 1].reshape(yshape) if len(args) == 2: return X, Y if len(args) == 3: Z = trans[:, 2].reshape(zshape) return X, Y, Z
UICore.coordTransform_table.coordTransform( r"D:\Codes\oneTools\data\基准数据\station_wgs84.csv", "ascii", False, 0, 1, 4326, 2435, r"D:\Codes\oneTools\res\station_sz_test.csv", "gb2312") with open("www.csv", "w", newline="") as f: writer = csv.writer(f) for i in range(10): writer.writerow([i, "xr,22", "www.yyy"]) sourceSRS = osr.SpatialReference() sourceSRS.ImportFromEPSG(2435) in_pt = [96070.547, 53474.857] point = ogr.CreateGeometryFromWkt("POINT({} {})".format(in_pt[0], in_pt[1])) para = "+proj=helmert +convention=position_vector +x={} +y={} +s={} +theta={}".format( 391090.578943, 2472660.600279, 0.999997415382, -3518.95267316) opt = osr.CoordinateTransformationOptions() opt.SetOperation(para) tr = osr.CreateCoordinateTransformation(sourceSRS, None, opt) point.Transform(tr) print(point) a = r"D:\Data\深圳坐标\配准中心线(深圳坐标).shp" b = r"D:\Data\深圳坐标\配准中心线(深圳坐标).gdb" c = r"D:\Data\深圳坐标\配准中心线(深圳坐标)" print([os.path.dirname(a), os.path.dirname(b), os.path.dirname(c)]) print([os.path.basename(a), os.path.basename(b), os.path.basename(c)]) if os.path.basename(c).find('.') > 0: print("yes") else: print("no")