Shapely 1.8.5 中文文档

  1. 主页
  2. 文档
  3. Shapely 1.8.5 中文文档
  4. Migrating to Shapely 1.8 / 2.0(迁移到Shapely 1.8 / 2.0版本)
  5. Interopability with NumPy and the array interface(与NumPy和数组接口的互操作)
  6. Conversion of the coordinates to (NumPy) arrays(将坐标转换为NumPy数组)

Conversion of the coordinates to (NumPy) arrays(将坐标转换为NumPy数组)

Shapely provides an array interface to have easy access to the coordinates as, for example, NumPy arrays (manual section).

Shapely提供了一个数组接口,可以方便地访问坐标,例如,NumPy数组(手册部分)。

A small example:

一个小例子:

>>> line = LineString([(0, 0), (1, 1), (2, 2)])
>>> import numpy as np
>>> np.asarray(line)
array([[0., 0.],
       [1., 1.],
       [2., 2.]])

In addition, there are also the explicit array_interface() method and ctypes attribute to get access to the coordinates as array data:

此外,还有array_interface()方法和ctypes属性来获取作为数组数据的坐标:

>>> line.ctypes
<shapely.geometry.linestring.c_double_Array_6 at 0x7f75261eb740>
>>> line.array_interface()
{'version': 3,
 'typestr': '<f8',
 'data': <shapely.geometry.linestring.c_double_Array_6 at 0x7f752664ae40>,
 'shape': (3, 2)}

This functionality is available for Point, LineString, LinearRing and MultiPoint.

该功能适用于Point、LineString、LinearRing和MultiPoint。

For more robust interoperability with NumPy, this array interface will be removed from those geometry classes, and limited to the coords.

为了与NumPy有更强的互操作性,这个数组接口将从这些几何对象中移除,而只限于坐标。

Starting with Shapely 1.8, converting a geometry object to a NumPy array directly will start raising a warning:

从Shapely 1.8开始,将一个几何对象直接转换为NumPy数组将引发警告:

>>> np.asarray(line)
ShapelyDeprecationWarning: The array interface is deprecated and will no longer
work in Shapely 2.0. Convert the '.coords' to a NumPy array instead.
array([[0., 0.],
       [1., 1.],
       [2., 2.]])

How do I update my code? To convert a geometry to a NumPy array, you can convert the .coords attribute instead:

我如何更新我的代码?要将一个几何对象转换为NumPy数组,可以通过转换.coords属性来代替。

>>> line.coords
<shapely.coords.CoordinateSequence at 0x7f2e09e88d60>
>>> np.array(line.coords)
array([[0., 0.],
       [1., 1.],
       [2., 2.]])

The array_interface() method and ctypes attribute will be removed in Shapely 2.0, but since Shapely will start requiring NumPy as a dependency, you can use NumPy or its array interface directly. Check the NumPy docs on the ctypes attribute or the array interface for more details.

array_interface()方法和ctypes属性将在Shapely 2.0中被删除,但由于Shapely将开始使用NumPy作为依赖,可以直接使用NumPy或其数组接口。请查阅NumPy关于ctypes属性或数组接口的文档以了解更多细节。

标签 ,
这篇文章对您有用吗?

我们要如何帮助您?

欢迎留下您的宝贵建议

Please enter your comment!
Please enter your name here