(k, gdf=None, weights=None, ids=None, contiguity="queen", silent=True)
return first_order
Generate spatial weights based on Queen or Rook contiguity of order k.

    Adjacent are all features within <= k steps. Pass either ``gdf`` or ``weights``.
    If both are passed, ``weights`` is used.
    If ``weights`` are passed, ``contiguity`` is
    ignored and high order spatial weights based on ``weights`` are computed.

    Parameters
    ----------
    k : int
        order of contiguity
    gdf : GeoDataFrame
        GeoDataFrame containing objects to analyse. Index has to be
        consecutive range ``0:x``.
        Otherwise, spatial weights will not match objects.
    weights : libpysal.weights
        libpysal.weights of order 1
    contiguity : str (default 'queen')
        type of contiguity weights. Can be ``'queen'`` or ``'rook'``.
    silent : bool (default True)
        silence libpysal islands warnings

    Returns
    -------
    libpysal.weights
        libpysal.weights object

    Examples
    --------
    >>> first_order = libpysal.weights.Queen.from_dataframe(geodataframe)
    >>> first_order.mean_neighbors
    5.848032564450475
    >>> fourth_order = sw_high(k=4, gdf=geodataframe)
    >>> fourth.mean_neighbors
    85.73188602442333
Generate spatial weights based on Queen or Rook contiguity of order k.
Generate spatial weights based on Queen or Rook contiguity of order k .
def sw_high(k, gdf=None, weights=None, ids=None, contiguity="queen", silent=True):
    """
    Generate spatial weights based on Queen or Rook contiguity of order k.

    Adjacent are all features within <= k steps. Pass either ``gdf`` or ``weights``.
    If both are passed, ``weights`` is used.
    If ``weights`` are passed, ``contiguity`` is
    ignored and high order spatial weights based on ``weights`` are computed.

    Parameters
    ----------
    k : int
        order of contiguity
    gdf : GeoDataFrame
        GeoDataFrame containing objects to analyse. Index has to be
        consecutive range ``0:x``.
        Otherwise, spatial weights will not match objects.
    weights : libpysal.weights
        libpysal.weights of order 1
    contiguity : str (default 'queen')
        type of contiguity weights. Can be ``'queen'`` or ``'rook'``.
    silent : bool (default True)
        silence libpysal islands warnings

    Returns
    -------
    libpysal.weights
        libpysal.weights object

    Examples
    --------
    >>> first_order = libpysal.weights.Queen.from_dataframe(geodataframe)
    >>> first_order.mean_neighbors
    5.848032564450475
    >>> fourth_order = sw_high(k=4, gdf=geodataframe)
    >>> fourth.mean_neighbors
    85.73188602442333

    """
    if weights is not None:
        first_order = weights
    elif gdf is not None:
        if contiguity == "queen":
            first_order = libpysal.weights.Queen.from_dataframe(
                gdf, ids=ids, silence_warnings=silent
            )
        elif contiguity == "rook":
            first_order = libpysal.weights.Rook.from_dataframe(
                gdf, ids=ids, silence_warnings=silent
            )
        else:
            raise ValueError(f"{contiguity} is not supported. Use 'queen' or 'rook'.")
    else:
        raise AttributeError("GeoDataFrame or spatial weights must be given.")

    if k > 1:
        id_order = first_order.id_order
        w = first_order.sparse
        wk = sum(map(lambda x: w ** x, range(2, k + 1)))
        rk, ck = wk.nonzero()
        sk = set(zip(rk, ck))
        sk = set([(i, j) for i, j in sk if i != j])
        d = dict([(i, []) for i in id_order])
        for pair in sk:
            k, v = pair
            k = id_order[k]
            v = id_order[v]
            d[k].append(v)
        return libpysal.weights.W(neighbors=d, silence_warnings=silent)
    return first_order
def sw_high ( k , gdf = None , weights = None , ids = None , contiguity = "queen" , silent = True ) : if weights is not None : first_order = weights elif gdf is not None : if contiguity == "queen" : first_order = libpysal . weights . Queen . from_dataframe ( gdf , ids = ids , silence_warnings = silent ) elif contiguity == "rook" : first_order = libpysal . weights . Rook . from_dataframe ( gdf , ids = ids , silence_warnings = silent ) else : raise ValueError ( f"{contiguity} is not supported. Use 'queen' or 'rook'." ) else : raise AttributeError ( "GeoDataFrame or spatial weights must be given." ) if k > 1 : id_order = first_order . id_order w = first_order . sparse wk = sum ( map ( lambda x : w ** x , range ( 2 , k + 1 ) ) ) rk , ck = wk . nonzero ( ) sk = set ( zip ( rk , ck ) ) sk = set ( [ ( i , j ) for i , j in sk if i != j ] ) d = dict ( [ ( i , [ ] ) for i in id_order ] ) for pair in sk : k , v = pair k = id_order [ k ] v = id_order [ v ] d [ k ] . append ( v ) return libpysal . weights . W ( neighbors = d , silence_warnings = silent ) return first_order