Skip to content

Commit

Permalink
Fix for #417
Browse files Browse the repository at this point in the history
  • Loading branch information
peterrrock2 committed Apr 28, 2024
1 parent a833b5c commit 2a30296
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion gerrychain/graph/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ def from_geodataframe(
cols_to_add: Optional[List[str]] = None,
reproject: bool = False,
ignore_errors: bool = False,
crs_override: Optional[str | int] = None,
) -> "Graph":
"""
Creates the adjacency :class:`Graph` of geometries described by `dataframe`.
Expand Down Expand Up @@ -214,6 +215,9 @@ def from_geodataframe(
:param ignore_errors: Whether to ignore all invalid geometries and
attept to create the graph anyway. Default is ``False``.
:type ignore_errors: bool, optional
:param crs_override: Value to override the CRS of the GeoDataFrame.
Default is None.
:type crs_override: Optional[str | int], optional
:returns: The adjacency graph of the geometries from `dataframe`.
:rtype: Graph
Expand Down Expand Up @@ -263,7 +267,21 @@ 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()

if crs_override is not None:
df.set_crs(crs_override, inplace=True)

if df.crs is None:
warnings.warn(
"GeoDataFrame has no CRS. Did you forget to set it? "
"If you're sure this is correct, you can ignore this warning. "
"Otherwise, please set the CRS using the `crs_override` parameter. "
"Attempting to proceed without a CRS."
)
graph.graph["crs"] = None
else:
graph.graph["crs"] = df.crs.to_json()

return graph

def lookup(self, node: Any, field: Any) -> Any:
Expand Down

0 comments on commit 2a30296

Please sign in to comment.