简体   繁体   English

移除多个多边形内的点

[英]Removing points inside several polygons

THE DATA: I have co-ordinates of two variables a and b of length 100,000 each and I have a text file containing the co-ordinates of several polygons. 数据:我有两个长度分别为100,000的变量ab的坐标,并且我有一个文本文件,其中包含多个多边形的坐标。

I would like to now remove all those points of a and b that are inside the different polygons. 我现在想删除不同多边形内的所有a和b点。

To do so, I am trying to use THE CODE FROM THIS ANSWER IN STACKOVERFLOW which does it for one point and one polygon. 为此,我正在尝试使用此答案在STACKOVERFLOW中的代码,该代码可对一个点和一个多边形进行处理。

The method I have chalked out to go about the problem for several points and several polygons is this: 我想出的方法来解决几个点和几个多边形的问题是这样的:

  • Take the co-ordinates of the first polygon 第一个多边形的坐标
  • Run the function for all the 100,000 points of a and b and if they are inside, then append them to a list, which I can use later to compare with the original a and b a and b所有100,000个点运行该函数, 如果它们在内部,则将它们附加到列表中,以后可以用来与原始a和b比较
  • Perform the above two steps with the co-ordinates of the second polygon and so on... 使用第二个多边形的坐标执行以上两个步骤,依此类推...

Now I have two problems facing me, which I don't know how to proceed with. 现在我面临两个问题,我不知道该如何处理。

  1. The text file containing the co-ordinates of the polygons looks like this: 包含多边形坐标的文本文件如下所示:

    020241-041200 4 30.83 -3.69 30.82 -3.69 30.82 -3.73 30.83 -3.73 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 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) is the ID of the polygon, and (4) is the number of corners the polygon has, 30.83 is the X co-ordinate of the first corner and -3.69 is the Y co-ordinate of the first corner and so on. 这里(020241-041200)是多边形的ID ,而(4)是多边形具有的角数, 30.83是第一个角的X坐标, -3.69是 第一个角 的Y坐标等等。

    I want to skip the first two columns so that I can only consider the X,Y co-ordinates of the polygons. 我想跳过前两列,以便仅考虑多边形的X,Y坐标。 How do I do that? 我怎么做?

  2. The polygons are not of the same shape , as you can see, the second polygon has 12 corners compared to 4 in the first one. 多边形的形状不同 ,如您所见, 第二个多边形具有12个角 ,而第一个多边形为4个。

THE 100,000 POINTS OF a and b LOOK LIKE THIS a and b 100,000点a and b 像这样的100,000点

If there is any convenient way, other than the solution I have given above, it would also be useful. 如果除了我上面给出的解决方案之外,还有任何方便的方法,它也将很有用。

All I want are, those points of a and b that are outside the polygons. 我只想要a和b在多边形之外的那些点。

When you say "those points of a and b that are outside the polygons" do you mean outside all of the polygons or outside any of the polygons? 当您说“ a和b的那些在多边形之外的点”时,是指在所有多边形之外还是在任何多边形之外?

Here is: 这是:

  1. A routine to read in the polygon points and create the appropriate data structure for use with the point_in_poly function. 读取多边形点并创建适当的数据结构以与point_in_poly函数一起使用的point_in_poly
  2. A routine to check if a point is in any of the polygons. 用于检查点是否在任何多边形中的例程。

Here is the routine to read the polygon points from a file: 这是从文件中读取多边形点的例程:

def readPolygons(path):
  polygons = []
  with open(path) as f:
    for line in f:                                         # (1)
      words = [ float(y) for 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 is represented as a list of pairs of floats, and the routine returns a list of polygons. 每个多边形都表示为成对的浮点列表,并且例程返回一个多边形列表。

Notes: 笔记:

  1. Iterate over all of the lines in the file. 遍历文件中的所有行。
  2. Split the line into words, drop the first two words with [2:] and convert each word to a float. 将行拆分为单词,使用[2:]删除前两个单词,然后将每个单词转换为浮点数。
  3. Create a list of pairs, taking the 1st, 3rd, 5th, etc as the x coordinates and the 2nd, 4th, 6th, etc as the y coordinates. 创建一个成对的列表,以1、3、5等为x坐标,以2、4、6等为y坐标。
  4. Ignore blank lines. 忽略空行。

Here is the routine to check if a point is in any polygon: 这是检查点是否在任何多边形中的例程:

def inAnyPolygon(x,y,polygons):
  for p in polygons:
    if point_in_poly(x,y,p):
      return True
  return False

If your criteria is "in all the polygons", then use: 如果您的条件是“在所有多边形中”,则使用:

def inAllPolygons(x,y,polygons):
  for p in polygons:
    if not point_in_poly(x,y,p):
      return False
  return True

Update: if you have a list of points points , you can create another list containing those points which are not in any of the polygons with: 更新:如果你有一个点列表points ,你可以创建一个包含那些不以任何与多边形的点另一个列表:

outliers = []
for p in points:
  (x,y) = p
  if not inAnyPolygons(x,y,polygons):
    outliers.append(p)
return outliers

If a and b are lists of numbers representing the x and y coordinates respectively of the 100000 points, here is the code to find the outliers: 如果ab是分别表示100000点的x和y坐标的数字列表,则下面是查找异常值的代码:

outliers = []
for (x,y) in zip(a,b):
  if not inAnyPolygons(x,y,polygons):
    outliers.append((x,y))
return outliers

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM