-1

How I can calculate if a given longitude and latitude belong to a multipolygon area in python?

Example:

My "coordinates": [-46.57421, -21.785741] # My location

I need to verify that the coordinates above are within the multipolygon region below:

"coverageArea": { 
    "type": "MultiPolygon", 
    "coordinates": [
      [[[30, 20], [45, 40], [10, 40], [30, 20]]], 
      [[[15, 5], [40, 10], [10, 20], [5, 10], [15, 5]]]
    ]
}

I have no idea where to start.

martineau
  • 119,623
  • 25
  • 170
  • 301

1 Answers1

1

The shapely library was made just for this!

Check out more here: https://automating-gis-processes.github.io/CSC18/lessons/L4/point-in-polygon.html

Per this example, you can do:

from shapely.geometry import Point, Polygon

# Create Point objects
point = Point(-46.57421, -21.785741)

# Create a Polygon
polygon1 = Polygon([(30, 20), (45, 40), (10, 40), (30, 20)])
polygon2 = Polygon([(15,  5), (40, 10), (10, 20), ( 5, 10), (15,  5)])

# Check if point is within either polygon:
point.within(polygon1)
point.within(polygon2)
Daniel
  • 3,228
  • 1
  • 7
  • 23