def test_can_pass_queen_or_rook_strings_to_control_adjacency(geodataframe): df = geodataframe.set_index("ID") graph = Graph.from_geodataframe(df, adjacency="queen") assert edge_set_equal( set(graph.edges), {("a", "b"), ("a", "c"), ("b", "d"), ("c", "d"), ("a", "d"), ("b", "c")}, )
def test_make_graph_works_with_queen_adjacency(geodataframe): df = geodataframe.set_index("ID") graph = Graph.from_geodataframe(df, adjacency="queen") assert edge_set_equal( set(graph.edges), {("a", "b"), ("a", "c"), ("b", "d"), ("c", "d"), ("a", "d"), ("b", "c")}, )
def test_computes_boundary_perims(geodataframe_with_boundary): df = geodataframe_with_boundary.set_index("ID") graph = Graph.from_geodataframe(df, reproject=False) expected = {"a": 5, "e": 5, "b": 1, "c": 1} for node, value in expected.items(): assert graph.nodes[node]["boundary_perim"] == value
def test_data_and_geometry(gdf_with_data): df = gdf_with_data graph = Graph.from_geodataframe(df, cols_to_add=["data", "data2"]) assert graph.geometry is df.geometry #graph.add_data(df[["data"]]) assert (graph.data["data"] == df["data"]).all() #graph.add_data(df[["data2"]]) assert list(graph.data.columns) == ["data", "data2"]
def test_does_not_reproject_by_default(geodataframe): df = geodataframe.set_index("ID") graph = Graph.from_geodataframe(df) for node in ("a", "b", "c", "d"): assert graph.nodes[node]["area"] == 1.0 for edge in graph.edges: assert graph.edges[edge]["shared_perim"] == 1.0
def test_can_insist_on_not_reprojecting(geodataframe): df = geodataframe.set_index("ID") graph = Graph.from_geodataframe(df, reproject=False) for node in ("a", "b", "c", "d"): assert graph.nodes[node]["area"] == 1 for edge in graph.edges: assert graph.edges[edge]["shared_perim"] == 1
def test_reproject(geodataframe): # I don't know what the areas and perimeters are in UTM for these made-up polygons, # but I'm pretty sure they're not 1. df = geodataframe.set_index("ID") graph = Graph.from_geodataframe(df, reproject=True) for node in ("a", "b", "c", "d"): assert graph.nodes[node]["area"] != 1 for edge in graph.edges: assert graph.edges[edge]["shared_perim"] != 1
def test_graph_raises_if_crs_is_missing_when_reprojecting(geodataframe): geodataframe.crs = None with pytest.raises(ValueError): Graph.from_geodataframe(geodataframe, reproject=True)
def test_make_graph_from_dataframe_gives_correct_graph(geodataframe): df = geodataframe.set_index("ID") graph = Graph.from_geodataframe(df) assert edge_set_equal(set(graph.edges), {("a", "b"), ("a", "c"), ("b", "d"), ("c", "d")})
def test_make_graph_from_dataframe_preserves_df_index(geodataframe): df = geodataframe.set_index("ID") graph = Graph.from_geodataframe(df) assert set(graph.nodes) == {"a", "b", "c", "d"}
def test_make_graph_from_dataframe_creates_graph(geodataframe): graph = Graph.from_geodataframe(geodataframe) assert isinstance(graph, Graph)
def test_graph_raises_if_crs_is_missing(geodataframe): del geodataframe.crs with pytest.raises(ValueError): Graph.from_geodataframe(geodataframe)
def test_make_graph_from_dataframe_has_crs(gdf_with_data): graph = Graph.from_geodataframe(gdf_with_data) assert CRS.from_json(graph.graph["crs"]).equals(gdf_with_data.crs)