determine if a line segment is within a polygon
Question:
I am trying to find out if a given line segment consisting of two or more points is inside a polygon here is a drawing to help capture the idea:
picture to help visualize the problem
All I found on the internet was a code that accepted a line passing through a polygon (could be only inside or just passing through a polygon) not exclusively inside a polygon here is the code mentioned:
import numpy as np
import matplotlib.pyplot as plt
import shapely.geometry
import descartes
circle = shapely.geometry.Point(5.0, 0.0).buffer(10.0)
clip_poly = shapely.geometry.Polygon([[-9.5, -2], [2, 2], [3, 4], [-1, 3]])
clipped_shape = circle.difference(clip_poly)
line = shapely.geometry.LineString([[-10, -5], [15, 5]])
line2 = shapely.geometry.LineString([[-10, -5], [-5, 0], [2, 3]])
print 'Blue line intersects clipped shape:', line.intersects(clipped_shape)
print 'Green line intersects clipped shape:', line2.intersects(clipped_shape)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(*np.array(line).T, color='blue', linewidth=3, solid_capstyle='round')
ax.plot(*np.array(line2).T, color='green', linewidth=3, solid_capstyle='round')
ax.add_patch(descartes.PolygonPatch(clipped_shape, fc='blue', alpha=0.5))
ax.axis('equal')
plt.show()
Answers:
To check if the segment completely lies within the polygon you can follow these two-step procedure:
- Look for segment-polygon intersections. If intersections are found then the segment does not lie within the polygon and the procedure stops here.
from shapely.geometry import LineString, Point, Polygon
point_a = Point(7, 2)
point_b = Point(10, 6)
segment = LineString([point_a, point_b])
polygon = Polygon([(1, 0), (4, 1), (5, 4), (3, 5), (3, 2)])
polygon_ext = LineString(list(polygon.exterior.coords))
intersections = polygon_ext.intersection(segment)
- If no intersection is found, then we need to check if the segment is actually lying within the polygon. So let’s pick one point from the segment and verify if it is contained within the polygon.
if intersections.is_empty:
if polygon.contains(point_a):
print("The segment completely lies within the polygon.")
else:
print("The segment does not lies within the polygon.")
else:
print("Segment-polygon intersections are found.")
For this case, no intersection is found but the line is outside the polygon, as you can see in the image below:
If we modify the line, for instance:
point_a = Point(2, 3)
point_b = Point(10, 6)
polygon-segment intersections are found (see image below):
If:
point_a = Point(2, 0.5)
point_b = Point(3.3, 4)
again intersections are found:
Finally, for the case:
point_a = Point(2, 3)
point_b = Point(10, 6)
the segment completely lies within the polygon as per image below:
I am trying to find out if a given line segment consisting of two or more points is inside a polygon here is a drawing to help capture the idea:
picture to help visualize the problem
All I found on the internet was a code that accepted a line passing through a polygon (could be only inside or just passing through a polygon) not exclusively inside a polygon here is the code mentioned:
import numpy as np
import matplotlib.pyplot as plt
import shapely.geometry
import descartes
circle = shapely.geometry.Point(5.0, 0.0).buffer(10.0)
clip_poly = shapely.geometry.Polygon([[-9.5, -2], [2, 2], [3, 4], [-1, 3]])
clipped_shape = circle.difference(clip_poly)
line = shapely.geometry.LineString([[-10, -5], [15, 5]])
line2 = shapely.geometry.LineString([[-10, -5], [-5, 0], [2, 3]])
print 'Blue line intersects clipped shape:', line.intersects(clipped_shape)
print 'Green line intersects clipped shape:', line2.intersects(clipped_shape)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(*np.array(line).T, color='blue', linewidth=3, solid_capstyle='round')
ax.plot(*np.array(line2).T, color='green', linewidth=3, solid_capstyle='round')
ax.add_patch(descartes.PolygonPatch(clipped_shape, fc='blue', alpha=0.5))
ax.axis('equal')
plt.show()
To check if the segment completely lies within the polygon you can follow these two-step procedure:
- Look for segment-polygon intersections. If intersections are found then the segment does not lie within the polygon and the procedure stops here.
from shapely.geometry import LineString, Point, Polygon
point_a = Point(7, 2)
point_b = Point(10, 6)
segment = LineString([point_a, point_b])
polygon = Polygon([(1, 0), (4, 1), (5, 4), (3, 5), (3, 2)])
polygon_ext = LineString(list(polygon.exterior.coords))
intersections = polygon_ext.intersection(segment)
- If no intersection is found, then we need to check if the segment is actually lying within the polygon. So let’s pick one point from the segment and verify if it is contained within the polygon.
if intersections.is_empty:
if polygon.contains(point_a):
print("The segment completely lies within the polygon.")
else:
print("The segment does not lies within the polygon.")
else:
print("Segment-polygon intersections are found.")
For this case, no intersection is found but the line is outside the polygon, as you can see in the image below:
If we modify the line, for instance:
point_a = Point(2, 3)
point_b = Point(10, 6)
polygon-segment intersections are found (see image below):
If:
point_a = Point(2, 0.5)
point_b = Point(3.3, 4)
again intersections are found:
Finally, for the case:
point_a = Point(2, 3)
point_b = Point(10, 6)
the segment completely lies within the polygon as per image below: