Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get list of points inside a polygon in python?

Tags:

I searched a lot and cant find any practical answer to my question. I have a polygon. For example:

    [(86, 52), (85, 52), (81, 53), (80, 52), (79, 48), (81, 49), (86, 53),      (85, 51), (82, 54), (84, 54), (83, 49), (81, 52), (80, 50), (81, 48),      (85, 50), (86, 54), (85, 54), (80, 48), (79, 50), (85, 49), (80, 51),      (85, 53), (82, 49), (83, 54), (82, 53), (84, 49), (79, 49)] 

I want to get a list of all the points inside this border polygon. I heard alot about polygon triangulation techniques or linear/flood/intersection/... filling algorithms. but i cant really come up with an efficient way of implementing this. This poly is small, imagine a polygon with 1 billion points. I am now using PIL draw polygon to fill the poly with red color and loop inside it to find red points. This is a horribly slow technique:

def render(poly, z):     xs = [i[0] for i in poly]     ys = [i[1] for i in poly]     minx, maxx = min(xs), max(xs)     miny, maxy = min(ys), max(ys)     X = maxx - minx + 1     Y = maxy - miny + 1     newPoly = [(x - minx, y - miny) for (x, y) in polygons]     i = Image.new("RGB", (X, Y))     draw = ImageDraw.Draw(i)     draw.polygon(newPoly, fill="red")     # i.show()     tiles = list()     w, h = i.size     print w, h     for x in range(w):         for y in range(h):             data = i.getpixel((x, y))             if data != (0, 0, 0):                 tiles.append((x + minx, y + miny))      return tiles 

I am searching for a Pythonic way of solving this problem. Thank you all.

like image 575
Farshid Ashouri Avatar asked Jan 24 '14 18:01

Farshid Ashouri


People also ask

How do you find a point inside a polygon in Python?

How to check if a point is inside a polygon in Python. To perform a Point in Polygon (PIP) query in Python, we can resort to the Shapely library's functions . within(), to check if a point is within a polygon, or . contains(), to check if a polygon contains a point.

How do you find a point inside a polygon?

Draw a horizontal line to the right of each point and extend it to infinity. Count the number of times the line intersects with polygon edges. A point is inside the polygon if either count of intersections is odd or point lies on an edge of polygon.

How do you check if a point is inside a polygon in Matlab?

in = inpolygon( xq , yq , xv , yv ) returns in indicating if the query points specified by xq and yq are inside or on the edge of the polygon area defined by xv and yv . [ in , on ] = inpolygon( xq , yq , xv , yv ) also returns on indicating if the query points are on the edge of the polygon area.


1 Answers

I suggest to use matplotlib contains_points()

from matplotlib.path import Path  tupVerts=[(86, 52), (85, 52), (81, 53), (80, 52), (79, 48), (81, 49), (86, 53),  (85, 51), (82, 54), (84, 54), (83, 49), (81, 52), (80, 50), (81, 48),  (85, 50), (86, 54), (85, 54), (80, 48), (79, 50), (85, 49), (80, 51),  (85, 53), (82, 49), (83, 54), (82, 53), (84, 49), (79, 49)]   x, y = np.meshgrid(np.arange(300), np.arange(300)) # make a canvas with coordinates x, y = x.flatten(), y.flatten() points = np.vstack((x,y)).T   p = Path(tupVerts) # make a polygon grid = p.contains_points(points) mask = grid.reshape(300,300) # now you have a mask with points inside a polygon 
like image 120
Stanpol Avatar answered Sep 19 '22 11:09

Stanpol