Shapely supports map projections and other arbitrary transformations of geometric objects.
Shapely支持地图投影和其他对几何对象的变换操作。
- shapely.ops.transform(func, geom)
Applies func to all coordinates of geom and returns a new geometry of the same type from the transformed coordinates.
将func应用于geom的所有坐标,并根据转换后的坐标返回一个相同类型的新几何对象。
func maps x, y, and optionally z to output xp, yp, zp. The input parameters may be iterable types like lists or arrays or single values. The output shall be of the same type: scalars in, scalars out; lists in, lists out.
func将x, y,和可选的z映射到输出xp, yp, zp。输入参数可以是可迭代的类型,如列表或数组或单个值。输出应是相同的类型:标量输入,标量输出;列表输入,列表输出。
transform tries to determine which kind of function was passed in by calling func first with n iterables of coordinates, where n is the dimensionality of the input geometry. If func raises a TypeError when called with iterables as arguments, then it will instead call func on each individual coordinate in the geometry.
transform试图通过首先调用func的n个坐标迭代变量来确定传递的是哪种函数,其中n是输入几何对象的维度。如果func在以迭代变量作为参数调用时引发了TypeError,那么它将在几何对象中的每个坐标上调用func。
New in version 1.2.18.
For example, here is an identity function applicable to both types of input (scalar or array).
例如,这里有一个适用于两种输入类型(标量或数组)的函数。
def id_func(x, y, z=None):
return tuple(filter(None, [x, y, z]))
g2 = transform(id_func, g1)
If using pyproj>=2.1.0, the preferred method to project geometries is:
如果使用pyproj>=2.1.0,投影几何图形的首选方法是:
import pyproj
from shapely.geometry import Point
from shapely.ops import transform
wgs84_pt = Point(-72.2495, 43.886)
wgs84 = pyproj.CRS('EPSG:4326')
utm = pyproj.CRS('EPSG:32618')
project = pyproj.Transformer.from_crs(wgs84, utm, always_xy=True).transform
utm_point = transform(project, wgs84_pt)
It is important to note that in the example above, the always_xy kwarg is required as Shapely only supports coordinates in X,Y order, and in PROJ 6 the WGS84 CRS uses the EPSG-defined Lat/Lon coordinate order instead of the expected Lon/Lat.
需要注意的是,在上面的例子中,需要always_xy参数,因为Shapely只支持X、Y顺序的坐标,而在PROJ6中,WGS84 CRS使用EPSG定义的Lat/Lon坐标顺序,而不是预期的Lon/Lat。
If using pyproj < 2.1, then the canonical example is:
如果使用 pyproj < 2.1,那么典型的例子是:
from functools import partial
import pyproj
from shapely.ops import transform
wgs84 = pyproj.Proj(init='epsg:4326')
utm = pyproj.Proj(init='epsg:32618')
project = partial(
pyproj.transform,
wgs84,
utm)
utm_point = transform(project, wgs84_pt)
Lambda expressions such as the one in
Lambda表达式
g2 = transform(lambda x, y, z=None: (x+1.0, y+1.0), g1)
also satisfy the requirements for func.
也满足func的要求。