(left, right, network_id, min_size=100, verbose=True)
return series
Snap each element (preferably building) to the closest street network segment,
    saves its id.

    Adds network ID to elements.

    Parameters
    ----------
    left : GeoDataFrame
        GeoDataFrame containing objects to snap
    right : GeoDataFrame
        GeoDataFrame containing street network with unique network ID.
        If there is none, it could be generated by :func:`momepy.unique_id`.
    network_id : str, list, np.array, pd.Series (default None)
        the name of the streets dataframe column, ``np.array``, or ``pd.Series``
        with network unique id.
    min_size : int (default 100)
        min_size should be a vaule such that if you build a box centered in each
        building centroid with edges of size ``2*min_size``, you know a priori that at
        least one
        segment is intersected with the box.
    verbose : bool (default True)
        if True, shows progress bars in loops and indication of steps

    Returns
    -------
    elements_nID : Series
        Series containing network ID for elements

    Examples
    --------
    >>> buildings_df['nID'] = momepy.get_network_id(buildings_df, streets_df, 'nID')
    Generating centroids...
    Generating rtree...
    Snapping: 100%|██████████| 144/144 [00:00<00:00, 2718.98it/s]
    >>> buildings_df['nID'][0]
    1

    See also
    --------
    momepy.get_network_ratio
    momepy.get_node_id
Snap each element (preferably building) to the closest street network segment,
    saves its id.
Snap each element ( preferably building ) to the closest street network segment saves its id .
def get_network_id(left, right, network_id, min_size=100, verbose=True):
    """
    Snap each element (preferably building) to the closest street network segment,
    saves its id.

    Adds network ID to elements.

    Parameters
    ----------
    left : GeoDataFrame
        GeoDataFrame containing objects to snap
    right : GeoDataFrame
        GeoDataFrame containing street network with unique network ID.
        If there is none, it could be generated by :func:`momepy.unique_id`.
    network_id : str, list, np.array, pd.Series (default None)
        the name of the streets dataframe column, ``np.array``, or ``pd.Series``
        with network unique id.
    min_size : int (default 100)
        min_size should be a vaule such that if you build a box centered in each
        building centroid with edges of size ``2*min_size``, you know a priori that at
        least one
        segment is intersected with the box.
    verbose : bool (default True)
        if True, shows progress bars in loops and indication of steps

    Returns
    -------
    elements_nID : Series
        Series containing network ID for elements

    Examples
    --------
    >>> buildings_df['nID'] = momepy.get_network_id(buildings_df, streets_df, 'nID')
    Generating centroids...
    Generating rtree...
    Snapping: 100%|██████████| 144/144 [00:00<00:00, 2718.98it/s]
    >>> buildings_df['nID'][0]
    1

    See also
    --------
    momepy.get_network_ratio
    momepy.get_node_id
    """
    INFTY = 1000000000000
    left = left.copy()
    right = right.copy()

    if not isinstance(network_id, str):
        right["mm_nid"] = network_id
        network_id = "mm_nid"

    buildings_c = left.copy()

    buildings_c[buildings_c.geometry.name] = buildings_c.centroid  # make centroids

    idx = right.sindex

    # TODO: use sjoin nearest once done
    result = []
    for p in tqdm(
        buildings_c.geometry,
        total=buildings_c.shape[0],
        desc="Snapping",
        disable=not verbose,
    ):
        pbox = (p.x - min_size, p.y - min_size, p.x + min_size, p.y + min_size)
        hits = list(idx.intersection(pbox))
        d = INFTY
        nid = None
        for h in hits:
            new_d = p.distance(right.geometry.iloc[h])
            if d >= new_d:
                d = new_d
                nid = right[network_id].iloc[h]
        if nid is None:
            result.append(np.nan)
        else:
            result.append(nid)

    series = pd.Series(result, index=left.index)

    if series.isnull().any():
        warnings.warn(
            "Some objects were not attached to the network. "
            "Set larger min_size. {} affected elements".format(sum(series.isnull()))
        )
    return series
def get_network_id ( left , right , network_id , min_size = 100 , verbose = True ) : INFTY = 1000000000000 left = left . copy ( ) right = right . copy ( ) if not isinstance ( network_id , str ) : right [ "mm_nid" ] = network_id network_id = "mm_nid" buildings_c = left . copy ( ) buildings_c [ buildings_c . geometry . name ] = buildings_c . centroid # make centroids idx = right . sindex # TODO: use sjoin nearest once done result = [ ] for p in tqdm ( buildings_c . geometry , total = buildings_c . shape [ 0 ] , desc = "Snapping" , disable = not verbose , ) : pbox = ( p . x - min_size , p . y - min_size , p . x + min_size , p . y + min_size ) hits = list ( idx . intersection ( pbox ) ) d = INFTY nid = None for h in hits : new_d = p . distance ( right . geometry . iloc [ h ] ) if d >= new_d : d = new_d nid = right [ network_id ] . iloc [ h ] if nid is None : result . append ( np . nan ) else : result . append ( nid ) series = pd . Series ( result , index = left . index ) if series . isnull ( ) . any ( ) : warnings . warn ( "Some objects were not attached to the network. " "Set larger min_size. {} affected elements" . format ( sum ( series . isnull ( ) ) ) ) return series