Repository: martinfleis/momepy
Pull request #100 (2019-10-23 16:00:50)

BUG: generating angular graph with non-default index
`_generate_dual()` within `gdf_to_nx()` expected index to be consecutive integer 0-n. That is no longer true and every kind of index should be okay.

codecov[bot]: # [Codecov](https://codecov.io/gh/martinfleis/momepy/pull/100?src=pr&el=h1) Report
> Merging [#100](https://codecov.io/gh/martinfleis/momepy/pull/100?src=pr&el=desc) into [master](https://codecov.io/gh/martinfleis/momepy/commit/f895914de027d94578a8f8521e074947c7256ac0?src=pr&el=desc) will **not change** coverage.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/martinfleis/momepy/pull/100/graphs/tree.svg?width=650&token=i04999UxkK&height=150&src=pr)](https://codecov.io/gh/martinfleis/momepy/pull/100?src=pr&el=tree)

```diff
@@           Coverage Diff           @@
##           master     #100   +/-   ##
=======================================
  Coverage   93.27%   93.27%           
=======================================
  Files           9        9           
  Lines        2439     2439           
=======================================
  Hits         2275     2275           
  Misses        164      164
```


| [Impacted Files](https://codecov.io/gh/martinfleis/momepy/pull/100?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [momepy/utils.py](https://codecov.io/gh/martinfleis/momepy/pull/100/diff?src=pr&el=tree#diff-bW9tZXB5L3V0aWxzLnB5) | `81.5% <100%> (ø)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/martinfleis/momepy/pull/100?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/martinfleis/momepy/pull/100?src=pr&el=footer). Last update [f895914...da8a194](https://codecov.io/gh/martinfleis/momepy/pull/100?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/momepy/utils.py b/momepy/utils.py
index a04c289..b4ad077 100644
--- a/momepy/utils.py
+++ b/momepy/utils.py
@@ -151,16 +151,16 @@ def _generate_dual(G, gdf_network, fields):
     sw = libpysal.weights.Queen.from_dataframe(gdf_network)
     gdf_network["mm_cent"] = gdf_network.geometry.centroid
 
-    for index, row in gdf_network.iterrows():
+    for i, (index, row) in enumerate(gdf_network.iterrows()):
         centroid = (row.mm_cent.x, row.mm_cent.y)
         data = [row[f] for f in fields]
         attributes = dict(zip(fields, data))
         G.add_node(centroid, **attributes)
 
-        if sw.cardinalities[index] > 0:
-            for n in sw.neighbors[index]:
+        if sw.cardinalities[i] > 0:
+            for n in sw.neighbors[i]:
                 start = centroid
-                end = list(gdf_network.loc[n, "mm_cent"].coords)[0]
+                end = list(gdf_network.iloc[n]["mm_cent"].coords)[0]
                 p0 = row.geometry.coords[0]
                 p1 = row.geometry.coords[-1]
                 p2 = gdf_network.iloc[n]["geometry"].coords[0]


Test patch:
diff --git a/tests/test_utils.py b/tests/test_utils.py
index 0f6f6da..8816124 100644
--- a/tests/test_utils.py
+++ b/tests/test_utils.py
@@ -42,6 +42,11 @@ class TestUtils:
         dual = mm.gdf_to_nx(self.df_streets, approach="dual")
         assert dual.number_of_nodes() == 35
         assert dual.number_of_edges() == 148
+        self.df_streets["ix"] = np.arange(0, len(self.df_streets) * 2, 2)
+        self.df_streets.set_index("ix", inplace=True)
+        dual2 = mm.gdf_to_nx(self.df_streets, approach="dual")
+        assert dual2.number_of_nodes() == 35
+        assert dual2.number_of_edges() == 148
         with pytest.raises(ValueError):
             mm.gdf_to_nx(self.df_streets, approach="nonexistent")