Skip to content

Commit

Permalink
Add projection information to serialized dual graph. (#388)
Browse files Browse the repository at this point in the history
  • Loading branch information
jenni-niels committed Feb 23, 2022
1 parent 17fa8bf commit ae7e776
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
3 changes: 2 additions & 1 deletion gerrychain/graph/graph.py
Expand Up @@ -85,7 +85,7 @@ def from_file(
reproject=reproject,
ignore_errors=ignore_errors
)

graph.graph["crs"] = df.crs.to_json()
return graph

@classmethod
Expand Down Expand Up @@ -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):
Expand Down
11 changes: 11 additions & 0 deletions tests/test_make_graph.py
Expand Up @@ -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
Expand Down Expand Up @@ -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)

0 comments on commit ae7e776

Please sign in to comment.