简体   繁体   中英

Finding points of a cascaded union of polygons in python

I am trying to find the closely lying points and remove duplicate points for some shape data (co-ordinates) in Python. I name the co-ordinates nodes as 1,2,3.. and so on and I'm using the shapely package and creating polygons around the node points 1,2,3.. by saying

polygons = [Point([nodes[i]).buffer(1) for i in range(len(nodes))]

and to find the cascading ones I use

cascade = cascaded_union(polygons)

the cascade which is returned is a multipolygon and has many co-ordinates listed, I want to exactly know which of the points from my nodes are cascaded (based on the buffer value of 1) so that I can replace them by a new node. How can I know this??

Instead of using the cascaded_union method, it might be easier to write your own method to check if any two polygons intersect. If I'm understanding what you want to do correctly, you want to find if two polygons overlap, and then delete one of them and edit another accordingly.

You could so something like this (not the best solution, I'll explain why):

def clean_closely_lying_points(nodes):
    polygons = [Point([nodes[i]).buffer(1) for i in range(len(nodes))]
    for i in range(len(polygons) - 1):
        if polygons[i] is None:
            continue
        for j in range(i + 1, len(polygons)):
            if polygons[j] is None:
                continue
            if polygons[i].intersects(polygons[j]):
                polygons[j] = None
                nodes[j] = None
                # now overwrite 'i' so that it's whatever you want it to be, based on the fact that polygons[i] and polygons[j] intersect
                polygons[i] = 
                nodes[i] = 

However, overall, I feel like the creation of polygons is time intensive and unnecessary. It's also tedious to update the polygons list and the nodes list together. Instead, you could just use the nodes themselves, and use shapely's distance method to check if two points are within 2 units of each other. This should be mathematically equivalent since the intersection between two circles both of radius 1 means that their center points are at most distance 2 away. In this scenario, your for loops would take a similar structure except they would iterate over the nodes.

def clean_closely_lying_points(nodes):
    point_nodes = [Point(node) for node in nodes]  # Cast each of the nodes (which I assume are in tuple form like (x,y), to shapely Points)
    for i in range(len(point_nodes) - 1):
        if point_nodes[i] is None:
            continue
        for j in range(i + 1, len(point_nodes)):
            if point_nodes[j] is None:
                continue
            if point_nodes[i].distance(point_nodes[j]) < 2:
                point_nodes[j] = None
                point_nodes[i] = # Whatever you want point_nodes[i] to be now that you know that point_nodes[j] was within a distance of 2 (could remain itself)
     return [node for node in point_nodes if node is not None]

The result of this method would be a list of shapely point objects, with closely lying points eliminated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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