Similar to the spatial relationships
contains covered in the previous section, another common geospatial query asks whether two geometries intersect or touch.
Both queries are implemented in
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:
import shapely.geometry line1 = shapely.geometry.LineString([(0, 0), (1, 1)]) line2 = shapely.geometry.LineString([(1, 1), (0, 2)])
The lines intersect. Do they also touch?
line2. Adding them both to a multi-line is a quick way of
drawing them inside a Jupyter notebook:
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: