Ejemplo n.º 1
0
 def test_Tessellation(self):
     tes = mm.Tessellation(self.df_buildings, "uID", self.limit, segment=2)
     tessellation = tes.tessellation
     assert len(tessellation) == len(self.df_tessellation)
     bands = mm.Tessellation(
         self.df_streets, "nID", mm.buffered_limit(self.df_streets, 50), segment=5
     ).tessellation
     assert len(bands) == len(self.df_streets)
     queen_corners = tes.queen_corners(2)
     w = libpysal.weights.Queen.from_dataframe(queen_corners)
     assert w.neighbors[14] == [35, 36, 13, 15, 26, 27, 28, 30, 31]
Ejemplo n.º 2
0
    def test_get_node_id(self):
        nx = mm.gdf_to_nx(self.df_streets)
        nodes, edges = mm.nx_to_gdf(nx)
        self.df_buildings["nID"] = mm.get_network_id(self.df_buildings,
                                                     self.df_streets, "nID")
        ids = mm.get_node_id(self.df_buildings, nodes, edges, "nodeID", "nID")
        assert not ids.isna().any()

        convex_hull = edges.unary_union.convex_hull
        enclosures = mm.enclosures(edges, limit=gpd.GeoSeries([convex_hull]))
        enclosed_tess = mm.Tessellation(self.df_buildings,
                                        unique_id="uID",
                                        enclosures=enclosures).tessellation
        links = mm.get_network_ratio(enclosed_tess, edges)
        enclosed_tess[links.columns] = links

        ids = mm.get_node_id(
            enclosed_tess,
            nodes,
            edges,
            node_id="nodeID",
            edge_keys="edgeID_keys",
            edge_values="edgeID_values",
        )

        assert not ids.isna().any()
Ejemplo n.º 3
0
    def test_get_network_ratio(self):
        convex_hull = self.df_streets.unary_union.convex_hull
        enclosures = mm.enclosures(self.df_streets, limit=gpd.GeoSeries([convex_hull]))
        enclosed_tess = mm.Tessellation(
            self.df_buildings, unique_id="uID", enclosures=enclosures
        ).tessellation
        links = mm.get_network_ratio(enclosed_tess, self.df_streets, initial_buffer=10)

        assert links.edgeID_values.apply(lambda x: sum(x)).sum() == len(enclosed_tess)
        assert links.loc[149, "edgeID_keys"] == [13, 30, 27, 29, 28]
Ejemplo n.º 4
0
    def test_Tessellation(self):
        tes = mm.Tessellation(self.df_buildings, "uID", self.limit, segment=2)
        tessellation = tes.tessellation
        assert len(tessellation) == len(self.df_tessellation)
        bands = mm.Tessellation(self.df_streets,
                                "nID",
                                mm.buffered_limit(self.df_streets, 50),
                                segment=5).tessellation
        assert len(bands) == len(self.df_streets)

        #  test_enclosed_tessellation
        enc1 = mm.Tessellation(self.df_buildings,
                               "uID",
                               enclosures=self.enclosures).tessellation
        assert len(enc1) == 155
        assert isinstance(enc1, gpd.GeoDataFrame)

        enc1_loop = mm.Tessellation(self.df_buildings,
                                    "uID",
                                    enclosures=self.enclosures,
                                    use_dask=False).tessellation
        assert len(enc1) == 155
        assert isinstance(enc1, gpd.GeoDataFrame)

        assert len(enc1_loop) == 155
        assert isinstance(enc1_loop, gpd.GeoDataFrame)

        assert_geodataframe_equal(enc1, enc1_loop)

        with pytest.raises(ValueError):
            mm.Tessellation(self.df_buildings,
                            "uID",
                            limit=self.limit,
                            enclosures=self.enclosures)

        enc1_loop = mm.Tessellation(self.df_buildings,
                                    "uID",
                                    enclosures=self.enclosures,
                                    use_dask=False).tessellation
        assert len(enc1) == 155
        assert isinstance(enc1, gpd.GeoDataFrame)

        # erroneous geometry
        df = self.df_buildings
        b = df.total_bounds
        x = np.mean([b[0], b[2]])
        y = np.mean([b[1], b[3]])

        df.loc[144] = [145, Polygon([(x, y), (x, y + 1), (x + 1, y)])]
        df.loc[145] = [146, MultiPoint([(x, y), (x + 1, y)]).buffer(0.55)]
        df.loc[146] = [147, affinity.rotate(df.geometry.iloc[0], 12)]

        tess = mm.Tessellation(df, "uID", self.limit)
        assert tess.collapsed == {145}
        assert len(tess.multipolygons) == 3
Ejemplo n.º 5
0
def tessellate(
    bldgs: MultiPolygon,
    block: Polygon,
) -> Tuple[gpd.GeoDataFrame, gpd.GeoDataFrame]:
    """ Use momepy to do basic tesselation """

    bldgs_gdf = gpd.GeoDataFrame({'geometry': bldgs})
    bldgs_gdf['uID'] = np.arange(bldgs_gdf.shape[0])

    tess_gdf = momepy.Tessellation(bldgs_gdf, unique_id='uID',
                                   limit=block).tessellation

    return tess_gdf, bldgs_gdf
Ejemplo n.º 6
0
buildings = mm.preprocess(buildings, size=30, compactness=False, islands=True)

buildings['uID'] = range(len(buildings))

buildings['blg_area'] = buildings.area
buildings.to_file('data/zurich.gpkg', layer='buildings', driver='GPKG')

buildings['geometry'] = buildings.simplify(0.2)
print('simplified')

buffers = [300, 10, 15, 20, 25, 30, 40, 50, 60, 70, 80, 90, 100, 150, 200]
for buf in buffers:
    print('Generating', buf)
    limit = mm.buffered_limit(buildings, buf)
    tessellation = mm.Tessellation(buildings, 'uID', limit).tessellation
    tessellation.to_file('data/tessellation/{0}_tessellation.shp'.format(buf))

# In[ ]:


def gini(vals):
    """Calculate the Gini coefficient of a numpy array."""
    # based on bottom eq:
    # http://www.statsdirect.com/help/generatedimages/equations/equation154.svg
    # from:
    # http://www.statsdirect.com/help/default.htm#nonparametric_methods/gini.htm
    # All values are treated equally, arrays must be 1d:
    vals = vals.flatten()
    if np.amin(vals) < 0:
        # Values cannot be negative:
Ejemplo n.º 7
0
 def time_Tessellation(self):
     mm.Tessellation(self.df_buildings, "uID", self.limit, segment=2)