开发者

find the min distances from one polygon to other polygons in a layer?

开发者 https://www.devze.com 2023-03-23 10:29 出处:网络
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 开发者_StackOverflow社区specific, I was wondering

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 开发者_StackOverflow社区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


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.


There is a tool in arcgis toolbox called: http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00080000001q000000.htm

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号