How to use the libpysal.weights.Queen.from_dataframe function in libpysal

To help you get started, we’ve selected a few libpysal examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github martinfleis / momepy / tests / test_intensity.py View on Github external
def test_Courtyards(self):
        courtyards = mm.Courtyards(self.df_buildings, "bID").series
        sw = Queen.from_dataframe(self.df_buildings)
        courtyards_wm = mm.Courtyards(
            self.df_buildings, self.df_buildings.bID, sw
        ).series
        check = 0.6805555555555556
        assert courtyards.mean() == check
        assert courtyards_wm.mean() == check
github martinfleis / momepy / momepy / dimension.py View on Github external
def __init__(self, gdf, spatial_weights=None, mean=False, verbose=True):
        self.gdf = gdf

        if spatial_weights is None:
            print("Calculating spatial weights...") if verbose else None
            from libpysal.weights import Queen

            spatial_weights = Queen.from_dataframe(gdf, silence_warnings=True)
            print("Spatial weights ready...") if verbose else None
        self.sw = spatial_weights

        lenghts = gdf.geometry.length

        sums = []
        means = []
        for index in tqdm(gdf.index, total=gdf.shape[0], disable=not verbose):
            neighbours = spatial_weights.neighbors[index].copy()
            if neighbours:
                neighbours.append(index)
            else:
                neighbours = [index]

            dims = lenghts.iloc[neighbours]
            if mean:
github martinfleis / momepy / momepy / distribution.py View on Github external
def __init__(
        self, gdf, spatial_weights_higher, unique_id, spatial_weights=None, verbose=True
    ):
        self.gdf = gdf
        self.sw_higher = spatial_weights_higher
        self.id = gdf[unique_id]
        results_list = []

        # if weights matrix is not passed, generate it from gdf
        if spatial_weights is None:
            print("Calculating spatial weights...") if verbose else None
            from libpysal.weights import Queen

            spatial_weights = Queen.from_dataframe(
                gdf, silence_warnings=True, ids=unique_id
            )
            print("Spatial weights ready...") if verbose else None

        self.sw = spatial_weights
        patches = dict(zip(gdf[unique_id], spatial_weights.component_labels))

        for uid in tqdm(
            self.id,
            total=gdf.shape[0],
            disable=not verbose,
            desc="Calculating adjacency",
        ):
            if uid in spatial_weights_higher.neighbors.keys():
                neighbours = spatial_weights_higher.neighbors[uid].copy()
                if neighbours:
github martinfleis / momepy / momepy / elements.py View on Github external
cells_copy.geometry.iteritems(),
            total=cells_copy.shape[0],
            disable=not verbose,
        ):
            if GPD_08:
                possible_matches_index = streets_index.query(cell)
            else:
                possible_matches_index = list(streets_index.intersection(cell.bounds))
            possible_matches = street_buff.iloc[possible_matches_index]
            new_geom.append(cell.difference(possible_matches.unary_union))

        print("Defining adjacency...") if verbose else None
        blocks_gdf = gpd.GeoDataFrame(geometry=new_geom)
        blocks_gdf = blocks_gdf.explode().reset_index(drop=True)

        spatial_weights = libpysal.weights.Queen.from_dataframe(
            blocks_gdf, silence_warnings=True
        )

        patches = {}
        jID = 1
        for idx in tqdm(
            blocks_gdf.index, total=blocks_gdf.shape[0], disable=not verbose
        ):

            # if the id is already present in courtyards, continue (avoid repetition)
            if idx in patches:
                continue
            else:
                to_join = [idx]  # list of indices which should be joined together
                neighbours = []  # list of neighbours
                neighbours += spatial_weights.neighbors[