diff --git a/gerrychain/graph/graph.py b/gerrychain/graph/graph.py index da03ec10..bfbecfc2 100644 --- a/gerrychain/graph/graph.py +++ b/gerrychain/graph/graph.py @@ -85,7 +85,7 @@ def from_file( reproject=reproject, ignore_errors=ignore_errors ) - + graph.graph["crs"] = df.crs.to_json() return graph @classmethod @@ -170,6 +170,7 @@ def from_geodataframe( networkx.set_node_attributes(graph, name="area", values=areas) graph.add_data(df, columns=cols_to_add) + graph.graph["crs"] = df.crs.to_json() return graph def lookup(self, node, field): diff --git a/tests/test_make_graph.py b/tests/test_make_graph.py index f798cc9d..e7c1a5c8 100644 --- a/tests/test_make_graph.py +++ b/tests/test_make_graph.py @@ -6,6 +6,7 @@ import pandas import pytest from shapely.geometry import Polygon +from pyproj import CRS from gerrychain.graph import Graph from gerrychain.graph.geo import GeometryError @@ -245,3 +246,13 @@ def test_data_and_geometry(gdf_with_data): assert (graph.data["data"] == df["data"]).all() #graph.add_data(df[["data2"]]) assert list(graph.data.columns) == ["data", "data2"] + + +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) + +def test_make_graph_from_shapefile_has_crs(shapefile): + graph = Graph.from_file(shapefile) + df = gp.read_file(shapefile) + assert CRS.from_json(graph.graph["crs"]).equals(df.crs) \ No newline at end of file