I have two polygons and I want to find the intersection line length between these polygons.

for geom1, geom2 in itertools.permutations(selection, r=2):
    intersection =geom1.geometry().intersection(geom2.geometry())

It gives the boolean values as true/false. But I want to find the intersection length. How can I do that using Python?


1 Answer 1


intersects gives the boolean value True, False, intersection gives the geometry

If you use the dir function of Python

['Error', '__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__geo_interface__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'addPart', 'addPartGeometry', 'addRing', 'adjacentVertices', 'area', 'asGeometryCollection', 'asMultiPoint', 'asMultiPolygon', 'asMultiPolyline', 'asPoint', 'asPolygon', 'asPolyline', 'asQPointF', 'asQPolygonF', 'asWkb', 'avoidIntersections', 'boundingBox', 'buffer', 'centroid', 'closestSegmentWithContext', 'closestVertex', 'closestVertexWithContext', 'combine', 'compare', 'contains', 'convertToMultiType', 'convertToType', 'convexHull', 'crosses', 'deletePart', 'deleteRing', 'deleteVertex', 'difference', 'disjoint', 'distance', 'equals', 'exportToGeoJSON', 'exportToWkt', 'fromMultiPoint', 'fromMultiPolygon', 'fromMultiPolyline', 'fromPoint', 'fromPolygon', 'fromPolyline', 'fromQPointF', 'fromQPolygonF', 'fromRect', 'fromWkb', 'fromWkt', 'insertVertex', 'interpolate', 'intersection', 'intersects', 'isGeosEmpty', 'isGeosEqual', 'isGeosValid', 'isMultipart', 'length', 'makeDifference', 'moveVertex', 'offsetCurve', 'overlaps', 'pointOnSurface', 'reshapeGeometry', 'rotate', 'simplify', 'smooth', 'splitGeometry', 'sqrDistToVertexAt', 'symDifference', 'touches', 'transform', 'translate', 'type', 'unaryUnion', 'validateGeometry', 'vertexAt', 'within', 'wkbSize', 'wkbType']

You find the length function (look also at Using OGR Intersection Method Python for the function get_patt or use the see module)

from see import see 
b = see(intersection)
import re
[item for item in b if re.search("length",item)]

So the command is


for geom1,geom2 in  itertools.combinations(selection, 2):
    if geom1.intersects(geom2):
       intersection = geom1.intersection(geom2)
       print intersection.exportToWkt()
u'MULTILINESTRING((481.58899999999999864 -222.97899999999992815, 481.58899999999999864 -265.54699999999996862), (481.58899999999999864 -265.54699999999996862, 433.38699999999994361 -386.36500000000000909), (433.38699999999994361 -386.36500000000000909, 319.91949270774443903 -369.28386986998305019))'
print intersection.length()

But be careful
- the intersection of two Polygons is never a simple LineString but a (Multi)Polygon
- if you want (Multi)LineStrings, you need to use the intersection of the Polygons Rings (my reply in Finding the common borders between polygons in the same shapefile)
- in most cases,the result is a heterogeneous collections of geometric objects (look at Shapely: Collections)

  • from the MULTILINESTRING i want to get the coordinates. I want like this (3.24934170594707172 6.67189348518093706, 3.24929629918317087 6.67176343113283998)(3.24929629918317087 6.67176343113283998, 3.24925621546922283 6.67149461722516701).How can i do that.
    – user99
    Sep 2, 2015 at 9:15

Your Answer

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

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