Point in Polygon & Intersect
### Point in Polygon & Intersect
"""The fundamental geospatial operations that are often used to select data based on location
for doing spatial analysis are:
1. if a certain point is located inside or outside of an area
2. Finding out if a line intersects with another line or polygon
"""
## A. How to check if point is inside a polygon PIP?
""" There are two methods of conducting PIP in Shapely
1. using a function called .within() to check if the point is within a polygon
2. Using a function called .contains() that checks if a polygon contains a point
Note: We can also check if a LineString or Polygon is inside another Polygon.
"""
# Creating some points with shapely
from shapely.geometry import Point, Polygon
import matplotlib.pyplot as plt
p1 = Point(24.952242, 60.1696017)
p2 = Point(24.976567, 60.1612500)
# Create a Polygon using a coordinates of tuple, because we could just create our coordinates based on p1 and p2
coords = [(24.950899, 60.169158), (24.953492, 60.169158), (24.953510, 60.170104), (24.950958, 60.169990)]
poly = Polygon(coords)
""" Output:
>>> print(p1)
POINT (24.952242 60.1696017)
>>> print(p2)
POINT (24.976567 60.16125)
>>> print(coords)
[(24.950899, 60.169158), (24.953492, 60.169158), (24.95351, 60.170104), (24.950958, 60.16999)]
"""
# Check if those points are in (within) the polygon:
p1.within(poly)
p2.within(poly)
""" Output:
>>> p1.within(poly)
True
>>> p2.within(poly)
False
We can see clearly that the p1 is within the polygon and the p2 is not.
As we can see the first point p1 is very close to the polygon, that's mean a lot for us.
"""
# Let see if the p1 which is within the polygon it's situation with the centroid of our polygon
print(p1)
print(poly.centroid)
""" Output:
POINT (24.952242 60.1696017)
POINT (24.95224242849236 60.16960179038188)
With a simple eye observation, we can see how those numbers are close together
"""
# Now, we can check if Polygon contains a point different from PIP which is point in polygon
poly.contains(p1)
poly.contains(p2)
""" Output:
>>> poly.contains(p1)
True
>>> poly.contains(p2)
False
Our result from 'within' seems to be similar with 'contains'.
"""
## Task: You have many points, try to find which of them are in the Polygon
## Task2: You have many polygons and one point, and you want to find out which polygon contains the point.
### B. Intersect
""" Using intersect means we want to know if a geometry intersects or touches another one,
we can do it with:
1. intersects(): Two objects intersect if the boundary or interior of one object intersect
in anyway with boundary or interiro of the other object.
2. Touches(): Two objects touch if the objects have at least one point in common and their
interiors do not intersect with any part of the other object.
"""
# We will create two LineStrings
from shapely.geometry import LineString, MultiLineString
line_a = LineString([(0,0), (1,1)])
line_b = LineString([(1,1), (0,2)])
# Checking the intersection between line_a and line_b
"""
>>> line_a.intersects(line_b)
True
>>> plt.plot(line_a,line_b)
[<matplotlib.lines.Line2D object at 0x07938D60>, <matplotlib.lines.Line2D object at 0x07938DD8>]
>>> plt.show()
The figure is clearly showing one point of intersection between line_a and line_b and there interior do not touch with any other part
The figure is clearly showing the touch between two lines, we can make sure the touch:
"""
# Checking if the line_a touch the line_b, previously we sow the touch while visualizing with matplotlib
line_a.touches(line_b)
""" Output:
>>> line_a.touches(line_b)
True
"""
# To plot them together in shapely, we can create a MultiLineString from line_a and line_b
"""
>>> multi_line = MultiLineString([line_a,line_b])
>>> print(multi_line)
MULTILINESTRING ((0 0, 1 1), (1 1, 0 2))
>>> multi_line
The observation here is simple, we can see the line_b start at the end of line_a (1 1) and continues
"""
## C. Checking if a line can touch itself
"""
>>> line_a.touches(line_a)
False
>>> line_a.intersects(line_a)
True
>>> line_b.touches(line_b)
False
>>> line_b.intersects(line_b)
True
"""
## Conclusion
""" We sow how to check if our Point is in Polygon using .within() and our polygon contains a point
by using .contains().
We sow also, how to check the intersection of two line and if any line touches another one.
"""
# This tutorial is my practice from GIS self learning
Comments
Post a Comment