找到图层中一个多边形到其他多边形的最小距离?

发布于 2024-11-26 15:06:34 字数 125 浏览 5 评论 0原文

我试图弄清楚如何找到ArcGIS图层(一个图层由许多多边形组成)中从一个多边形到其他多边形的最小距离。更具体地说,我想知道是否可以用 python 运行一个循环,它会找到每个多边形到其他多边形的最小距离?

谢谢, 拉吉卜

I tried to figure out how to find the min distances from one polygon to other polygons in a layer (a layer consists of many polygons) of ArcGIS. More specific, I was wondering if it is possible to run a loop with python, which will find the min distances from each polygon to others?

Thanks,
Rajib

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

唱一曲作罢 2024-12-03 15:06:34

如果您已经获得了多边形的中心坐标,那么您自己就可以轻松完成此操作。首先,您需要一个函数来查找相同维度的两点之间的距离:

def euclid(pt1, pt2):
    pairs = zip(pt1, pt2) # Form pairs in corresponding dimensions
    sum_sq_diffs = sum((a - b)**2 for a, b in pairs) # Find sum of squared diff
    return (sum_sq_diffs)**(float(1)/2) # Take sqrt to get euclidean distance

然后您可以创建一个函数来查找点向量(列表或其他)中最近的点。我只需将 min() 函数与快速自定义键函数一起应用:

# Returns the point in vec with minimum euclidean distance to pt
def closest_pt(pt, vec):
    return min(vec, key=lambda x: euclid(pt, x))

如果您有多边形的顶点,则这会更复杂一些,但如果您采取这一步,很容易弄清楚- 一步一步。最外层的循环应该迭代“基本”多边形中的点(您试图找到最小距离的点)。嵌套在其中的循环应该将您带到比较向量中的每个其他多边形。从这里您可以调用 closest_pt() 函数来将您的基点与另一个多边形中的所有点进行比较,找到最接近的点:

def closest_poly(basis, vec):
    closest = []
    for (i, pt) in basis:
        closer = []
        for poly in vec:
            closer.append(closest_pt(pt, poly))
        closest.append(closest_pt(pt, closer))
    best = min(enumerate(closest), key=lambda x: euclid(basis[x[0]], x[1]))
    return (best[0], best[1], [best[1] in poly for poly in vec])

它在结构上可能有点多余,但我认为它会起作用它提供了相当透明的逻辑。该函数返回一对 (vertex, close_pt, polys),其中: vertex 是基础中最接近另一个多边形的顶点的索引; close_pt 是另一个多边形中被发现包含最近点的点;并且 polys 是与 vec 中的多边形相对应的布尔值列表,这样每个 polys[i] == True 当且仅如果 close_ptvec[i] 的顶点。

希望这有帮助。

If you've got the center coordinates of your polygons, it's illustratably easy to do this on your own. First you need a function to find the distance between two points of the same dimensions:

def euclid(pt1, pt2):
    pairs = zip(pt1, pt2) # Form pairs in corresponding dimensions
    sum_sq_diffs = sum((a - b)**2 for a, b in pairs) # Find sum of squared diff
    return (sum_sq_diffs)**(float(1)/2) # Take sqrt to get euclidean distance

Then you can make a function to find the closest point among a vector (list or whatever) of points. I would simply apply the min() function with a quick custom key-function:

# Returns the point in vec with minimum euclidean distance to pt
def closest_pt(pt, vec):
    return min(vec, key=lambda x: euclid(pt, x))

If you have the vertices of the polygon this is a couple steps more complicated, but easy to figure out if you take it step-by-step. Your outer-most loop should iterate through the points in your "base" polygon (the one you are trying to find the minimum distance to). The loop nested within this should take you to each of the other polygons in your comparison vector. From here you can just call the closest_pt() function to compare your basis point to all the points in this other polygon, finding the closest one:

def closest_poly(basis, vec):
    closest = []
    for (i, pt) in basis:
        closer = []
        for poly in vec:
            closer.append(closest_pt(pt, poly))
        closest.append(closest_pt(pt, closer))
    best = min(enumerate(closest), key=lambda x: euclid(basis[x[0]], x[1]))
    return (best[0], best[1], [best[1] in poly for poly in vec])

It may be slightly redundant structurally, but I think it will work and it provides pretty transparent logic. The function returns a pair of (vertex, close_pt, polys), where: vertex is the index of the vertex in your basis which was found to be closest to another polygon; close_pt is the point in the other polygon which was found to contain the closest point; and polys is a list of Boolean values corresponding with the polygons in your vec, such that each polys[i] == True if and only if close_pt is a vertex of vec[i].

Hope this is helpful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文