Intersect#
Similar to the spatial relationships within
and contains
covered in the previous section, another common geospatial query asks whether two geometries intersect or touch.
Both queries are implemented in shapely
: - intersects(): two objects intersect if the boundary or interior of one object intersect in any way with the boundary or interior of the other object. - touches(): two objects touch if the objects have at least one point in common, but their interiors do not intersect with any part of the other object.
Let’s try these functions out, for instance, using two lines:
[1]:
import shapely.geometry
line1 = shapely.geometry.LineString([(0, 0), (1, 1)])
line2 = shapely.geometry.LineString([(1, 1), (0, 2)])
[2]:
line1.intersects(line2)
[2]:
True
The lines intersect. Do they also touch?
[3]:
line1.touches(line2)
[3]:
True
line1
touches line2
. Adding them both to a multi-line is a quick way of drawing them inside a Jupyter notebook:
[4]:
shapely.geometry.MultiLineString([line1, line2])
[4]:
We can see here, that the share the point (1, 1)
, in which line1
ends, and line2
begins. The two lines do not intersect otherwise (‘in their interior’), so the predicament ’touch()
’ - as defined above - is true.
If the lines would share some of their interior, that would not be counted as touching. For instance, line1
does not touch line1
(itself), but fulfils all requirements to be counted as intersect()
ing with itself:
[5]:
line1.touches(line1)
[5]:
False
[6]:
line1.intersects(line1)
[6]:
True