Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using qgis and shaply error: GEOSGeom_createLinearRing_r returned a NULL pointer

I tried to create a polygon shapefile in QGIS and read it in python by shapely. An example code looks like this:

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

The EOSGeom_createLinearRing_r returned a NULL pointer I checked if the polygon is valid in QGIS and no error was reported. Actually, it does not work for even a simple triangle generated in QGIS. Anyone knows how to solve it?

Thank you

like image 378
jjk Avatar asked May 28 '20 22:05

jjk


3 Answers

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)'

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

Some other issues in the Shapely repository to look at

  • 553 for import order issues on a Mac
  • 887 (same reverse-import-order trick with osgeo and shapely)
  • 919
like image 192
Jeremiah England Avatar answered Nov 11 '22 02:11

Jeremiah England


I had a similar problem but with the shapely.geometry.LineString. The error I got was

ValueError: GEOSGeom_createLineString_r returned a NULL pointer

I don't know the reason behind this message, but there are two ways, how to avoid it:

  1. Do the following:

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

    Import the speedups module and disable the speedups. This needs to be done, since they are enabled by default. From shapelys speedups init method:

    """
    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.
    """
    

    If you disable them, you won't get the NULL Pointer Exception, because you don't use the C implementation, rather than the usual implementation.

  2. If you call python in a command shell, type:

    from shapely.geometry import shape
    

    this loads your needed shape. Then load your program

    import yourscript
    

    then run your script.

    yourscript.main()
    

    This should also work. I think in this variant the C modules get properly loaded and therefore you don't get the NULL Pointer Exception. But this only works, if you open a python terminal by hand and import the needed shape by hand. If you import the shape with your program, you will run into the same error again.

like image 8
J. P. Avatar answered Nov 11 '22 04:11

J. P.


Face the same issue and this work for me

import shapely

shapely.speedups.disable()

like image 1
MD Gazuruddin Avatar answered Nov 11 '22 03:11

MD Gazuruddin