0

I'm trying to get the intersection geometry result of two polygons using the python OGR library.

I have verified that each polygon is a geometry and that they intersect. However, intersects returns true while Intersection returns None. Any ideas?

# First polygon
ringA = ogr.Geometry(ogr.wkbLinearRing)
ringA.AddPoint(1179091.1646903288, 712782.8838459781)
...
polyA = ogr.Geometry(ogr.wkbPolygon)
polyA.AddGeometry(ringA)

# Second polygon
ringB = ogr.Geometry(ogr.wkbLinearRing)
ringB.AddPoint(1179091.1646903288, 712782.8838459781)
...
polyB = ogr.Geometry(ogr.wkbPolygon)
polyB.AddGeometry(ringB)

if (polyA.Intersects(polyB)) # returns True
    return polyA.Intersection(polyB) # returns None
todoroki
  • 11
  • 5

1 Answers1

0

I had a similar problem. According to Enabling GEOS for GDAL/OGR and the documentation, without GEOS, only the envelope of the two geometries is compared. I couldn't find out how to enable GEOS without compiling GDAL from source code (in Ubuntu 18.04, any help welcome!).

However, as a workaround I suggest

    intersect_geom = polyA.Intersection(polyB)
    if(intersect_geom is not None and intersect_geom.Area()>0):
        return intersect_geom

In my case, intersect_geom is None in most cases where the geometries don't intersect. In some cases, however, intersect_geom was GEOMETRYCOLLECTION EMPTY, which I check by Area()>0.

I hope this helps, even if the question is already 6 months old.

Ludwig
  • 123
  • 3