1

I am having issues with calculating an intersection of two LineStrings using Shapely. Such an intersection should return a point, yet my code returns a LineString. I found a simple example of an intersection of two lines, and it works as expected:

from shapely.geometry import LineString

line1 = LineString([(0,0), (2,2)])
line2 = LineString([(0,2), (2,0)])
print(line1.intersection(line2))

The resulting printout is:

POINT (1 1)

However, here is my code (with everything stripped out except for just enough to replicate the issue):

import fiona
from shapely.geometry import LineString

sch = {}
sch["properties"] = {}
sch["geometry"] = "LineString"
file = fiona.open("d:/tmp/test.gpkg", "w", driver="GPKG", schema=sch)

line1 = LineString([(0,0), (2,2)])
line2 = LineString([(0,2), (2,0)])
print(line1.intersection(line2))

The (incorrect) intersection result now becomes:

LINESTRING (2 2, 0 0)

So, something about fiona is changing the behavior of intersection. If I comment out the call to fiona.open (but leave the import fiona) the result is an OSError:

Traceback (most recent call last):
  File "test2.py", line 11, in <module>
    print(line1.intersection(line2))
  File "C:\OSGEO4~1\apps\Python37\lib\site-packages\shapely\geometry\base.py", line 620, in intersection
    return geom_factory(self.impl['intersection'](self, other))
  File "C:\OSGEO4~1\apps\Python37\lib\site-packages\shapely\topology.py", line 65, in __call__
    self._validate(other, stop_prepared=True)
  File "C:\OSGEO4~1\apps\Python37\lib\site-packages\shapely\topology.py", line 19, in _validate
    if stop_prepared and not hasattr(ob, 'type'):
  File "C:\OSGEO4~1\apps\Python37\lib\site-packages\shapely\geometry\base.py", line 352, in type
    return self.geometryType()
  File "C:\OSGEO4~1\apps\Python37\lib\site-packages\shapely\geometry\base.py", line 348, in geometryType
    return geometry_type_name(self._geom)
  File "C:\OSGEO4~1\apps\Python37\lib\site-packages\shapely\geometry\base.py", line 70, in geometry_type_name
    return GEOMETRY_TYPES[lgeos.GEOSGeomTypeId(g)]
OSError: exception: access violation reading 0x0000000000000000

Any idea what is going on?

1 Answer 1

1

In case this affects anyone else: I didn't identify the exact problem, but I believe it was somehow related to dependencies, in my case to an installation of OSGeo4W64.

I had OSGeoW64 on my machine so that I can use QGIS. I am using Windows 7. The example above was running using Python 3.7, which came with that OSGeo installation. The fiona and shapely wheels (from here) were installed using that version of Python.

I have a separate python standalone installation (3.6) on the same machine. I uninstalled fiona and shapely from the 3.7, then I reinstalled them (different wheels for the different python version) for 3.6. There was some hassling with GDAL, but with some help from here I got it running. (note that I am not using GeoPandas, which that link discusses - I am just using gdal, fiona, and shapely).

Now, the problem from my original post is solved: LineString intersection works properly whether or not fiona is involved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.