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?