python - Removing points inside several polygons -
the data: have co-ordinates of 2 variables a
, b
of length 100,000 each , have text file containing co-ordinates of several polygons.
i remove points of , b inside different polygons.
to so, trying use the code answer in stackoverflow 1 point , 1 polygon.
the method have chalked out go problem several points , several polygons this:
- take co-ordinates of first polygon
- run function 100,000 points of
a , b
, if inside, append them list, can use later compare original , b - perform above 2 steps co-ordinates of second polygon , on...
now have 2 problems facing me, don't know how proceed with.
the text file containing co-ordinates of polygons looks this:
020241-041200 4 30.83 -3.69 30.82 -3.69 30.82 -3.73 30.83 -3.73
020241-041200 12 30.91 -4.03 30.89 -4.03 30.85 -4.05 30.83 -4.07 30.82 -4.09 30.84 -4.16 30.89 -4.19 30.96 -4.16 30.97 -4.13 30.97 -4.08 30.95 -4.05 30.93 -4.04
here (020241-041200) id of polygon, , (4) number of corners polygon has, 30.83 x co-ordinate of first corner , -3.69 y co-ordinate of first corner , on.
i want skip first 2 columns can consider x,y co-ordinates of polygons. how do that?
the polygons not of same shape, can see, the second polygon has 12 corners compared 4 in first one.
the 100,000 points of a , b
if there convenient way, other solution have given above, useful.
all want are, points of , b outside polygons.
when "those points of , b outside polygons" mean outside all of polygons or outside any of polygons?
here is:
- a routine read in polygon points , create appropriate data structure use
point_in_poly
function. - a routine check if point in of polygons.
here routine read polygon points file:
def readpolygons(path): polygons = [] open(path) f: line in f: # (1) words = [ float(y) y in (line.split())[2:] ] # (2) poly = zip (words[::2], words[1::2]) # (3) if len(poly): # (4) polygons.append(poly) return polygons
each polygon represented list of pairs of floats, , routine returns list of polygons.
notes:
- iterate on of lines in file.
- split line words, drop first 2 words
[2:]
, convert each word float. - create list of pairs, taking 1st, 3rd, 5th, etc x coordinates , 2nd, 4th, 6th, etc y coordinates.
- ignore blank lines.
here routine check if point in polygon:
def inanypolygon(x,y,polygons): p in polygons: if point_in_poly(x,y,p): return true return false
if criteria "in polygons", use:
def inallpolygons(x,y,polygons): p in polygons: if not point_in_poly(x,y,p): return false return true
update: if have list of points points
, can create list containing points not in of polygons with:
outliers = [] p in points: (x,y) = p if not inanypolygons(x,y,polygons): outliers.append(p) return outliers
if a
, b
lists of numbers representing x , y coordinates respectively of 100000 points, here code find outliers:
outliers = [] (x,y) in zip(a,b): if not inanypolygons(x,y,polygons): outliers.append((x,y)) return outliers
Comments
Post a Comment