jjk*_*jjk 7 python qgis shapely
我试图在 QGIS 中创建一个多边形 shapefile 并通过 shapely 在 python 中读取它。示例代码如下所示:
import fiona
from shapely.geometry import shape
multipolys = fiona.open(somepath)
multi = multipolys[0]
coord = shape(multi['geometry'])
Run Code Online (Sandbox Code Playgroud)
EOSGeom_createLinearRing_r 返回了一个空指针,我检查了多边形在 QGIS 中是否有效并且没有报告错误。实际上,它甚至不适用于 QGIS 中生成的简单三角形。有谁知道如何解决它?
谢谢
小智 8
我有一个类似的问题,但与 shapely.geometry.LineString。我得到的错误是
ValueError: GEOSGeom_createLineString_r returned a NULL pointer
Run Code Online (Sandbox Code Playgroud)
我不知道这条消息背后的原因,但有两种方法,如何避免它:
请执行下列操作:
...
from shapely import speedups
...
speedups.disable()
Run Code Online (Sandbox Code Playgroud)
导入加速模块并禁用加速。这需要完成,因为它们默认启用。从 shapelys 加速初始化方法:
"""
The shapely.speedups module contains performance enhancements written in C.
They are automaticaly installed when Python has access to a compiler and
GEOS development headers during installation, and are enabled by default.
"""
Run Code Online (Sandbox Code Playgroud)
如果您禁用它们,您将不会得到 NULL 指针异常,因为您不使用 C 实现,而不是通常的实现。
如果在命令 shell 中调用 python,请键入:
from shapely.geometry import shape
Run Code Online (Sandbox Code Playgroud)
这会加载您需要的形状。然后加载你的程序
import yourscript
Run Code Online (Sandbox Code Playgroud)
然后运行你的脚本。
yourscript.main()
Run Code Online (Sandbox Code Playgroud)
这也应该有效。我认为在这个变体中,C 模块被正确加载,因此你不会得到 NULL 指针异常。但这仅适用于,如果您手动打开 python 终端并手动导入所需的形状。如果您使用程序导入形状,您将再次遇到相同的错误。
Like J. P., I had this issue with creating LineStrings as well. There is an old issue (2016) in the Shapely github repository that seems related. Changing the order of the imports solved the problem for me:
from shapely.geometry import LineString
import fiona
LineString([[0, 0], [1, 1]]).to_wkt()
# 'LINESTRING (0.0000000000000000 0.0000000000000000, 1.0000000000000000 1.0000000000000000)'
Run Code Online (Sandbox Code Playgroud)
whereas
import fiona
from shapely.geometry import LineString
LineString([[0, 0], [1, 1]]).to_wkt()
# Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
# File "C:\Users\xxxxxxx\AppData\Roaming\Python\Python37\site-packages\shapely\geometry\linestring.py", line 48, in __init__
# self._set_coords(coordinates)
# File "C:\Users\xxxxxxx\AppData\Roaming\Python\Python37\site-packages\shapely\geometry\linestring.py", line 97, in _set_coords
# ret = geos_linestring_from_py(coordinates)
# File "shapely\speedups\_speedups.pyx", line 208, in shapely.speedups._speedups.geos_linestring_from_py
# ValueError: GEOSGeom_createLineString_r returned a NULL pointer
Run Code Online (Sandbox Code Playgroud)
Some other issues in the Shapely repository to look at
osgeo and shapely)| 归档时间: |
|
| 查看次数: |
2804 次 |
| 最近记录: |