Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gdf2osmnx #545

Closed
wants to merge 5 commits into from
Closed

Conversation

anastassiavybornova
Copy link
Contributor

Added the osmnx_like keyword to gdf_to_nx(). If osmnx_like=True, within _generate_primal:

  • first, add all unique node coordinates as nodes to net with unique dummy IDs
  • break each linestring in gdf into two-point linestrings
  • for each two-point linestring, find corresponding node IDs (u,v)
  • only then, add edges as (u,v) pairs to graph iteratively, at each step checking whether (u,v) already exists in graph (if so, key+=1)
  • the output MultiDiGraph from gdf_to_nx()now works as input for osmnx.simplify_graph()

Copy link

codecov bot commented Feb 2, 2024

Codecov Report

Attention: 4 lines in your changes are missing coverage. Please review.

Comparison is base (2ceac22) 97.4% compared to head (2d54382) 97.3%.
Report is 1 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##            main    #545     +/-   ##
=======================================
- Coverage   97.4%   97.3%   -0.1%     
=======================================
  Files         26      26             
  Lines       4317    4358     +41     
=======================================
+ Hits        4203    4240     +37     
- Misses       114     118      +4     
Files Coverage Δ
momepy/coins.py 95.3% <ø> (ø)
momepy/distribution.py 97.4% <ø> (ø)
momepy/preprocessing.py 93.3% <100.0%> (ø)
momepy/shape.py 91.5% <ø> (ø)
momepy/tests/test_preprocess.py 100.0% <ø> (ø)
momepy/tests/test_utils.py 99.2% <100.0%> (+<0.1%) ⬆️
momepy/utils.py 97.0% <92.5%> (-1.8%) ⬇️

@jGaboardi jGaboardi added the enhancement New feature or request label Feb 2, 2024
Copy link
Member

@jGaboardi jGaboardi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking very good @anastassiavybornova.

  • break each linestring in gdf into two-point linestrings
  • for each two-point linestring, find corresponding node IDs (u,v)

I am curious, does this approach handle loops (e.g., $[u,u]$ one-point linestrings)? Or does "two-point linestrings" simply imply both endpoints of the linestring (in which case ignore my question)?

blg.loc[
blg.loc[blg["mm_uid"] == key].index[0], blg.geometry.name
] = new_geom
blg.loc[blg.loc[blg["mm_uid"] == key].index[0], blg.geometry.name] = (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this extra indexing needed due to an update in Pandas/GeoPandas?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

haven't touched that one (except for running black on it) i'll leave the question to @martinfleis

momepy/utils.py Outdated Show resolved Hide resolved
momepy/utils.py Outdated Show resolved Hide resolved
Copy link
Member

@martinfleis martinfleis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did only a partial review as I need to clarify some bits.

What exactly is the difference between the two options? Assuming both are MultiDiGraphs. As far as I can tell, the only difference is that the original uses tuple (x, y) to encode node with no attributes, the osmnx-like encodes nodes to integers and adds x and y as attributes. Is there anything else I don't see?

If I am right, then my suggestion would be to adapt our original function to add {'x': 1, 'y': 2} attributes to each node as osmnx-like version do and if osmnx_like=True, call convert_node_labels_to_integers and be done with it. Am I wrong?

momepy/utils.py Outdated Show resolved Hide resolved
else:
graph.add_edge(first, last, **attributes)
gdf_network["geometry"] = gdf_network["geometry"].apply(
lambda x: linemerge(x) if x.geom_type == "MultiLineString" else x
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you doing this? If it is a pre-processing step, than a user should do it themselves. Otherwise this does not round-trip as it should.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_generate_primal already checks for some potential non-linestring geometries, so i thought i'd add this one as well - but yes it is a pre-processing step indeed. fine to leave it out of the function.

@martinfleis
Copy link
Member

Looking into it a bit further, it seems that the only change we need to do is to add the attributes with coordinates to nodes. Adding these two lines results in no issue on a small sample dataset.

node_attrs = {node: {"x": node[0], "y": node[1]} for node in graph.nodes}
nx.set_node_attributes(graph, node_attrs)

anastassiavybornova and others added 2 commits February 3, 2024 17:24
Co-authored-by: James Gaboardi <[email protected]>
Co-authored-by: James Gaboardi <[email protected]>
@martinfleis
Copy link
Member

@anastassiavybornova maybe have a look at #546 before spending more time on this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants