使用qgis和shaply错误:GeoGeom_createLinearRing_r返回空指针

2024-06-10 04:27:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在QGIS中创建一个多边形shapefile,并通过shapely在python中阅读它。示例代码如下所示:

import fiona
from shapely.geometry import shape
multipolys = fiona.open(somepath)
multi = multipolys[0]
coord = shape(multi['geometry'])

EOSGeom_createLinearRing_r返回空指针 我检查了多边形在QGIS中是否有效,没有报告错误。实际上,即使是QGIS中生成的简单三角形,它也不起作用。有人知道怎么解决吗

多谢各位


Tags: 代码fromimport示例open多边形multishapefile
3条回答

为我面对同样的问题和工作

import shapely

shapely.speedups.disable()

和J.p.一样,我也有创建线字符串的问题。Shapely github存储库中有an old issue(2016)似乎是相关的。更改导入顺序为我解决了问题:

from shapely.geometry import LineString
import fiona 

LineString([[0, 0], [1, 1]]).to_wkt()
# 'LINESTRING (0.0000000000000000 0.0000000000000000, 1.0000000000000000 1.0000000000000000)'

鉴于

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

Shapely存储库中的一些其他问题

  • 553用于Mac上的导入订单问题
  • 887(与osgeoshapely相同的反向导入顺序技巧)
  • 919

我有一个类似的问题,但与shapely.geometry.LineString。我犯的错误是

ValueError: GEOSGeom_createLineString_r returned a NULL pointer

我不知道这条消息背后的原因,但有两种方法,如何避免它:

  1. 请执行以下操作:

    ...
    from shapely import speedups
    ...
    
    speedups.disable()
    

    导入加速模块并禁用加速。这需要完成,因为它们在默认情况下处于启用状态。 从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.
    """
    

    如果禁用它们,就不会出现空指针异常,因为您不使用C实现,而不是通常的实现

  2. 如果在命令shell中调用python,请键入:

    from shapely.geometry import shape
    

    这将加载所需的形状。然后加载你的程序

    import yourscript
    

    然后运行脚本

    yourscript.main()
    

    这也应该起作用。我认为在这个变体中,C模块得到了正确的加载,因此不会出现空指针异常。但这只适用于手动打开python终端并手动导入所需形状的情况。如果使用程序导入形状,则会再次遇到相同的错误

相关问题 更多 >