Share via

Point is inside polygon in python excel

Anonymous
2024-01-11T15:31:37+00:00

"Hi there,


I have a question about Python and Excel. Is it possible to check if a point is inside a polygon and return the name of the polygon? I've tried many methods, but when I run the script, it displays a ModuleNotFoundError: No module named 'shapely,' even though I have already installed it. The script runs without any issues in Visual Studio."

from shapely.geometry import Point, Polygon

Define your polygons

polygon1 = Polygon([(0, 0), (0, 1), (1, 1), (1, 0)])

polygon2 = Polygon([(1, 0), (1, 1), (2, 1), (2, 0)])

polygon3 = Polygon([(2, 0), (2, 1), (3, 1), (3, 0)])

List of polygons and their names

polygons = {'Polygon1': polygon1, 'Polygon2': polygon2}

Function to check which polygon a point belongs to

def point_in_polygons(point, polygons):

for poly\_name, poly in polygons.items(): 

    if poly.contains(point): 

        return poly\_name 

return None 

Example points

points_to_check = [Point(0.5, 0.5), Point(0.5, 0.5), Point(1.5, 0.5),Point (1.8, 0.8)]

Check each point

for point in points_to_check:

polygon\_name = point\_in\_polygons(point, polygons) 

print(f"Point {point} is inside {polygon\_name}." if polygon\_name else f"Point {point} is outside all polygons.")

Actually, I want to check if the point is inside any of the polygons and return the name of the corresponding polygon. I don't know if this is possible in Python with Excel."

PointID PointX PointY InsidePolygon
1 2 2
2 2 3
3 5 10
4 20 10
5 80 10
6 15 5
7 25 5
8 35 5
9 25 15
PolygonName VertexX VertexY
Poly1 0 0
Poly1 0 4
Poly1 4 4
Poly1 4 0
Poly2 5 10
Poly2 5 5
Poly2 10 5
Poly2 10 10
Poly3 20 0
Poly3 20 10
Poly3 30 10
Poly3 30 0
Microsoft 365 Insider | Excel | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

1 answer

Sort by: Most helpful
  1. Anonymous
    2024-01-12T14:28:27+00:00

    Hi Kittipun,

    Thank you for posting here in Microsoft Community.

    While I would love to assist you with your concern about defining Excel Python to point inside a polygon and return the name of the polygon, I am not an expert in Python coding. However, I can suggest that you post your concern on our Excel Tech Community and Microsoft Q&A.

    The Excel Tech Community and Microsoft Q&A are great resources for users to connect with experts and other users who may have experience with similar issues. By posting your concern there, you can get help from a wider range of people who may be able to provide you with a solution or point you in the right direction.

    Also to set your expectations that Excel Python is still on preview which it may have less feature than expected or you may encounter issues on it, which our engineering team are continuously working on improving it. I also highly suggest on sending a feedback about it as they are gathering data and feedbacks on this feature. You can just simply click on Help > Feedback.

    I hope this information helps! I will also keep this thread open so that other community members and provide their input or if they have a workaround for it.

    Best regards,

    Arquel C.

    Microsoft Moderator

    0 comments No comments