给定外部坐标,提取多边形的内部点 [英] Extract interior points of polygon given the exterior coordinates

查看:52
本文介绍了给定外部坐标,提取多边形的内部点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个多边形的边界坐标列表.我想根据我可以选择的某种网格间距从该多边形中提取一些内部点.

I have a list of boundary coordinates for a polygon. I want to extract some interior points from this polygon based on some sort of grid spacing that I can choose.

例如,说我有这个:

from shapely.geometry import MultiPoint
Polygon([(0,0),
        (1,0),
        (1,1),
        (0,1)])

这将产生一个简单的正方形.但是,我们可以想象这个正方形实际上是一个充满无数个点的网格.对于此示例,假设我要在正方形内以0.1网格间距的点列表.有没有办法提取这些点的列表?

This yields a simple square. However, we can imagine this square is actually a grid filled with an infinite number of points. For this example, let's say I want a list of points inside the square at 0.1 grid spacing. Is there a way to extract a list of these points?

对于加分点,我们不仅要获得内部点,而且还要获得所有边界点(以0.1网格间距)?

For bonus points, how would we not only get the interior points, but also all of the boundary points at 0.1 grid spacing?

该问题主要是针对非矩形形状提出的,其目的是为县/省/区等的形状找到点.我仅使用矩形作为简单示例.但是,我接受了建议的解决方案.将为那些为非矩形物体提供更复杂解决方案的人表示感谢

This question was mainly posed for non-rectangular shapes and has the purpose of finding points for shapes of counties/provinces/districts, etc. I just used a rectangle as a simple example. However, I accepted the proposed solution. Will give out upvotes to anyone who provides a more complex solution for non-rectangular objects

推荐答案

  • 关于内部点的提取,基于网格形状,可以通过简单的数学来完成,就像这样:
    • Regarding to the extraction of the interior points, based on a grid shape, it can be done with simple math, like so :
    • from shapely.geometry import Polygon, MultiPoint
      from math import ceil
      
      # The setup
      pol = Polygon([(0,0), (1,0), (1,1), (0,1)])
      cell_size = 0.1
      
      # Extract coordinates of the bounding box:
      minx, miny, maxx, maxy = pol.bounds
      
      # How many points in each dimension ?
      rows = int(ceil((maxy - miny) / cell_size))
      cols = int(ceil((maxx - minx) / cell_size))
      
      # Actually generate the points:
      pts = []
      x, y = minx, miny
      for countcols in range(cols):
          for countrows in range(rows):
              pts.append((x, y))
              y += cell_size
          x += cell_size
          y = miny
      
      # Create the MultiPoint from this list
      result_pts_interior = MultiPoint(pts)
      

      结果:

      但是请注意,如果多边形不具有矩形形状,它还会生成位于多边形外部的点(然后应使用 pol.intersects(Point(x,y)),然后将其添加到列表中.

      But note that if the polygon does not have a rectangular shape, it will also generate points which will be outside the polygon (you should then test if they intersect the polygon with pol.intersects(Point(x, y)) before adding them to the list).

      • 关于提取边界点,可以使用整齐的LineString的 interpolate 方法来完成,就像这样:
      • Regarding to the extraction of boundary points, it can be done using the interpolate method of shapely LineStrings, like so :
      from shapely.geometry import Polygon, MultiPoint
      import numpy as np
      
      # The setup
      pol = Polygon([(0,0), (1,0), (1,1), (0,1)])
      distance_between_pts = 0.1
      
      boundary = pol.boundary # Boundary of polygon as a linestring
      boundary_length = boundary.length # Its length
      # Build a list of points spaced by 0.1 along this linestring:
      pts_boundary = [
          boundary.interpolate(n, False) for n
          in np.linspace(0, boundary_length, int(boundary_length / distance_between_pts) + 1)
      ]
      # Create the MultiPoint from this list
      result_pts_boundary = MultiPoint(pts_boundary)
      

      结果:

      这篇关于给定外部坐标,提取多边形的内部点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆