Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Link node to nearest edge (which it does not already touch) #19

Open
tomalrussell opened this issue Jan 16, 2019 · 1 comment
Open

Link node to nearest edge (which it does not already touch) #19

tomalrussell opened this issue Jan 16, 2019 · 1 comment

Comments

@tomalrussell
Copy link
Owner

For example, given a set of edges, add endpoints, then connect those endpoints to other edges (maybe given some condition).

Notes:

  • nearest_edge returns nearest single edge
  • edges_within can return set of incident edges, which can then be excluded from the edges passed to nearest_edge
  • suggest (thanks @ElcoK) low-level d_outside or disjoint to complement d_within or intersects
@ElcoK
Copy link
Contributor

ElcoK commented Jan 17, 2019

Suggested function to add (or at least a first version for a new one):

def nearest_not_intersecting(node, gdf, condition,threshold=0.01):
    """Find the element of a GeoDataFrame nearest a shapely geometry
    """
    geom = node.geometry
    n = 10
    step = 10
    while True:
        matches_idx = gdf.sindex.nearest(geom.bounds, n)
        candidate_idxs = []
        k = 0
        for match_idx in matches_idx:
            edge = gdf.iloc[match_idx]
            distance = geom.distance(edge.geometry) 
            if (edge.infra_type == node.infra_type) & (distance == 0):
                k += 1
            if not condition(node, edge):
                continue
            if 0 < distance <= threshold:
                candidate_idxs.append(match_idx)
                
        if k > 2:
            break
            
        if candidate_idxs:            
            nearest_geom = min(
                [gdf.iloc[match_idx] for match_idx in candidate_idxs],
                key=lambda match: geom.distance(match.geometry)
            )
            return nearest_geom
        n = n + step
        if n > 10:
            break

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants