Skip to content

Commit 3fec8f7

Browse files
committed
Test matching_gdf_from_geoms
- initialise GeoDataFrame with geometry=[...] everywhere - ensures that "geometry" column is set and active by default - copy CRS when creating matching GeoDataFrame
1 parent e686e90 commit 3fec8f7

File tree

2 files changed

+56
-45
lines changed

2 files changed

+56
-45
lines changed

src/snkit/network.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ class Network:
6363
def __init__(self, nodes=None, edges=None):
6464
""" """
6565
if nodes is None:
66-
nodes = GeoDataFrame()
66+
nodes = GeoDataFrame(geometry=[])
6767
self.nodes = nodes
6868

6969
if edges is None:
70-
edges = GeoDataFrame()
70+
edges = GeoDataFrame(geometry=[])
7171
self.edges = edges
7272

7373
def set_crs(self, crs=None, epsg=None):
@@ -518,7 +518,9 @@ def matching_gdf_from_geoms(gdf, geoms):
518518
"""Create a geometry-only GeoDataFrame with column name to match an existing GeoDataFrame"""
519519
geom_col = geometry_column_name(gdf)
520520
geom_arr = geoms_to_array(geoms)
521-
return GeoDataFrame(geom_arr, columns=[geom_col])
521+
matching_gdf = GeoDataFrame(geometry=geom_arr, crs=gdf.crs)
522+
matching_gdf.geometry.name = geom_col
523+
return matching_gdf
522524

523525

524526
def geoms_to_array(geoms):

tests/test_init.py

+51-42
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def edge_only():
2727
|
2828
"""
2929
edge = LineString([(0, 0), (0, 2)])
30-
edges = GeoDataFrame([{"geometry": edge}])
30+
edges = GeoDataFrame(geometry=[edge])
3131
return snkit.Network(edges=edges)
3232

3333

@@ -40,7 +40,7 @@ def nodes_only():
4040
"""
4141
a = Point((0, 0))
4242
b = Point((0, 2))
43-
nodes = GeoDataFrame([{"geometry": a}, {"geometry": b}])
43+
nodes = GeoDataFrame(geometry=[a, b])
4444
return snkit.Network(nodes=nodes)
4545

4646

@@ -54,8 +54,8 @@ def connected():
5454
a = Point((0, 0))
5555
b = Point((0, 2))
5656
edge = LineString([a, b])
57-
edges = GeoDataFrame([{"geometry": edge}])
58-
nodes = GeoDataFrame([{"geometry": a}, {"geometry": b}])
57+
edges = GeoDataFrame(geometry=[edge])
58+
nodes = GeoDataFrame(geometry=[a, b])
5959
return snkit.Network(edges=edges, nodes=nodes)
6060

6161

@@ -69,8 +69,8 @@ def misaligned():
6969
edge = LineString([(0, 0), (0, 2)])
7070
a = Point((0.5, 0))
7171
b = Point((-0.5, 2))
72-
edges = GeoDataFrame([{"geometry": edge}])
73-
nodes = GeoDataFrame([{"geometry": a}, {"geometry": b}])
72+
edges = GeoDataFrame(geometry=[edge])
73+
nodes = GeoDataFrame(geometry=[a, b])
7474
return snkit.Network(edges=edges, nodes=nodes)
7575

7676

@@ -89,8 +89,8 @@ def unsplit():
8989
d = Point((1, 1))
9090
ab = LineString([a, b])
9191
cd = LineString([c, d])
92-
edges = GeoDataFrame([ab, cd], columns=["geometry"])
93-
nodes = GeoDataFrame([a, b, c, d], columns=["geometry"])
92+
edges = GeoDataFrame(geometry=[ab, cd])
93+
nodes = GeoDataFrame(geometry=[a, b, c, d])
9494
return snkit.Network(edges=edges, nodes=nodes)
9595

9696

@@ -107,11 +107,11 @@ def split():
107107
b = Point((0, 2))
108108
c = Point((0, 1))
109109
d = Point((1, 1))
110-
nodes = GeoDataFrame([a, b, c, d], columns=["geometry"])
110+
nodes = GeoDataFrame(geometry=[a, b, c, d])
111111
ac = LineString([a, c])
112112
cb = LineString([c, b])
113113
cd = LineString([c, d])
114-
edges = GeoDataFrame([ac, cb, cd], columns=["geometry"])
114+
edges = GeoDataFrame(geometry=[ac, cb, cd])
115115
return snkit.Network(edges=edges, nodes=nodes)
116116

117117

@@ -128,11 +128,11 @@ def split_with_ids():
128128
b = Point((0, 2))
129129
c = Point((0, 1))
130130
d = Point((1, 1))
131-
nodes = GeoDataFrame(data={"geometry": [a, b, c, d], "id": ["a", "b", "c", "d"]})
131+
nodes = GeoDataFrame(data={"id": ["a", "b", "c", "d"]}, geometry=[a, b, c, d])
132132
ac = LineString([a, c])
133133
cb = LineString([c, b])
134134
cd = LineString([c, d])
135-
edges = GeoDataFrame(data={"geometry": [ac, cb, cd], "id": [1, 2, 3]})
135+
edges = GeoDataFrame(data={"id": [1, 2, 3]}, geometry=[ac, cb, cd])
136136
return snkit.Network(edges=edges, nodes=nodes)
137137

138138

@@ -149,10 +149,10 @@ def unsplit_intersection():
149149
b = Point((1, 2))
150150
c = Point((0, 1))
151151
d = Point((2, 1))
152-
nodes = GeoDataFrame(data={"geometry": [a, b, c, d]})
152+
nodes = GeoDataFrame(geometry=[a, b, c, d])
153153
ab = LineString([a, b])
154154
cd = LineString([c, d])
155-
edges = GeoDataFrame(data={"geometry": [ab, cd]})
155+
edges = GeoDataFrame(geometry=[ab, cd])
156156
return snkit.Network(edges=edges, nodes=nodes)
157157

158158

@@ -170,12 +170,12 @@ def split_intersection():
170170
c = Point((0, 1))
171171
d = Point((2, 1))
172172
x = Point((1, 1))
173-
nodes = GeoDataFrame(data={"geometry": [a, b, c, d, x]})
173+
nodes = GeoDataFrame(geometry=[a, b, c, d, x])
174174
ax = LineString([a, x])
175175
xb = LineString([x, b])
176176
cx = LineString([c, x])
177177
xd = LineString([x, d])
178-
edges = GeoDataFrame(data={"geometry": [ax, xb, cx, xd]})
178+
edges = GeoDataFrame(geometry=[ax, xb, cx, xd])
179179
return snkit.Network(edges=edges, nodes=nodes)
180180

181181

@@ -194,11 +194,11 @@ def unsplit_multiple_intersections():
194194
d = Point((3, 1))
195195
e = Point((2, 0))
196196
f = Point((2, 2))
197-
nodes = GeoDataFrame(data={"geometry": [a, b, c, d, e, f]})
197+
nodes = GeoDataFrame(geometry=[a, b, c, d, e, f])
198198
ab = LineString([a, b])
199199
cd = LineString([c, d])
200200
ef = LineString([e, f])
201-
edges = GeoDataFrame(data={"geometry": [ab, cd, ef]})
201+
edges = GeoDataFrame(geometry=[ab, cd, ef])
202202
return snkit.Network(edges=edges, nodes=nodes)
203203

204204

@@ -219,15 +219,15 @@ def split_multiple_intersections():
219219
f = Point((2, 2))
220220
x = Point((1, 1))
221221
y = Point((2, 1))
222-
nodes = GeoDataFrame(data={"geometry": [a, b, c, d, e, f, x, y]})
222+
nodes = GeoDataFrame(geometry=[a, b, c, d, e, f, x, y])
223223
ax = LineString([a, x])
224224
xb = LineString([x, b])
225225
cx = LineString([c, x])
226226
xy = LineString([x, y])
227227
yd = LineString([y, d])
228228
ey = LineString([e, y])
229229
yf = LineString([y, f])
230-
edges = GeoDataFrame(data={"geometry": [ax, xb, cx, xy, yd, ey, yf]})
230+
edges = GeoDataFrame(geometry=[ax, xb, cx, xy, yd, ey, yf])
231231
return snkit.Network(edges=edges, nodes=nodes)
232232

233233

@@ -246,10 +246,10 @@ def unsplit_overlapping_lines():
246246
d = Point((2, 2))
247247
# x is just a construction point
248248
x = Point((1, 1))
249-
nodes = GeoDataFrame(data={"geometry": [a, b, c, d]})
249+
nodes = GeoDataFrame(geometry=[a, b, c, d])
250250
ac = LineString([a, c])
251251
bd = LineString([b, x, c, d])
252-
edges = GeoDataFrame(data={"geometry": [ac, bd]})
252+
edges = GeoDataFrame(geometry=[ac, bd])
253253
return snkit.Network(edges=edges, nodes=nodes)
254254

255255

@@ -267,13 +267,13 @@ def split_overlapping_lines():
267267
c = Point((1, 2))
268268
d = Point((2, 2))
269269
x = Point((1, 1))
270-
nodes = GeoDataFrame(data={"geometry": [a, b, c, d, x]})
270+
nodes = GeoDataFrame(geometry=[a, b, c, d, x])
271271
ax = LineString([a, x])
272272
bx = LineString([b, x])
273273
xc = LineString([x, c])
274274
cd = LineString([c, d])
275275
# note that there are two edges 'xc'
276-
edges = GeoDataFrame(data={"geometry": [ax, xc, bx, xc, cd]})
276+
edges = GeoDataFrame(geometry=[ax, xc, bx, xc, cd])
277277
return snkit.Network(edges=edges, nodes=nodes)
278278

279279

@@ -296,10 +296,10 @@ def unsplit_heterogeneous_intersection():
296296
d = Point((3, 1))
297297
e = Point((0, 1))
298298
f = Point((4, 1))
299-
nodes = GeoDataFrame(data={"geometry": [a, b, c, d, e, f]})
299+
nodes = GeoDataFrame(geometry=[a, b, c, d, e, f])
300300
ad = LineString([a, b, c, y, d])
301301
ef = LineString([e, f])
302-
edges = GeoDataFrame(data={"geometry": [ad, ef]})
302+
edges = GeoDataFrame(geometry=[ad, ef])
303303
return snkit.Network(edges=edges, nodes=nodes)
304304

305305

@@ -321,7 +321,7 @@ def split_heterogeneous_intersection():
321321
x = Point((1, 1))
322322
y = Point((2, 1))
323323
# note: this is order sensitive although it shouldn't matter
324-
nodes = GeoDataFrame(data={"geometry": [a, b, c, d, e, f, y, x]})
324+
nodes = GeoDataFrame(geometry=[a, b, c, d, e, f, y, x])
325325
ax = LineString([a, x])
326326
xb = LineString([x, b])
327327
bc = LineString([b, c])
@@ -331,7 +331,7 @@ def split_heterogeneous_intersection():
331331
xy = LineString([x, y])
332332
df = LineString([d, f])
333333
# note that there are two edges 'yd'
334-
edges = GeoDataFrame(data={"geometry": [ax, xb, bc, cy, yd, ex, xy, yd, df]})
334+
edges = GeoDataFrame(geometry=[ax, xb, bc, cy, yd, ex, xy, yd, df])
335335
return snkit.Network(edges=edges, nodes=nodes)
336336

337337

@@ -350,9 +350,9 @@ def unsplit_self_intersection():
350350
b = Point((1, 2))
351351
c = Point((2, 2))
352352
d = Point((2, 1))
353-
nodes = GeoDataFrame(data={"geometry": [a, e]})
353+
nodes = GeoDataFrame(geometry=[a, e])
354354
ae = LineString([a, b, c, d, e])
355-
edges = GeoDataFrame(data={"geometry": [ae]})
355+
edges = GeoDataFrame(geometry=[ae])
356356
return snkit.Network(edges=edges, nodes=nodes)
357357

358358

@@ -372,11 +372,11 @@ def split_self_intersection():
372372
b = Point((1, 2))
373373
c = Point((2, 2))
374374
d = Point((2, 1))
375-
nodes = GeoDataFrame(data={"geometry": [a, e, x]})
375+
nodes = GeoDataFrame(geometry=[a, e, x])
376376
ax = LineString([a, x])
377377
xx = LineString([x, b, c, d, x])
378378
xe = LineString([x, e])
379-
edges = GeoDataFrame(data={"geometry": [ax, xx, xe]})
379+
edges = GeoDataFrame(geometry=[ax, xx, xe])
380380
return snkit.Network(edges=edges, nodes=nodes)
381381

382382

@@ -393,10 +393,10 @@ def gap():
393393
b = Point((0, 2))
394394
c = Point((0.1, 1))
395395
d = Point((1, 1))
396-
nodes = GeoDataFrame([a, b, c, d], columns=["geometry"])
396+
nodes = GeoDataFrame(geometry=[a, b, c, d])
397397
ab = LineString([a, b])
398398
cd = LineString([c, d])
399-
edges = GeoDataFrame([ab, cd], columns=["geometry"])
399+
edges = GeoDataFrame(geometry=[ab, cd])
400400
return snkit.Network(edges=edges, nodes=nodes)
401401

402402

@@ -414,12 +414,12 @@ def bridged():
414414
c = Point((0.1, 1))
415415
d = Point((1, 1))
416416
e = Point((0, 1))
417-
nodes = GeoDataFrame([a, b, c, d, e], columns=["geometry"])
417+
nodes = GeoDataFrame(geometry=[a, b, c, d, e])
418418
ae = LineString([a, e])
419419
eb = LineString([e, b])
420420
cd = LineString([c, d])
421421
ce = LineString([c, e])
422-
edges = GeoDataFrame([ae, eb, cd, ce], columns=["geometry"])
422+
edges = GeoDataFrame(geometry=[ae, eb, cd, ce])
423423
return snkit.Network(edges=edges, nodes=nodes)
424424

425425

@@ -437,11 +437,11 @@ def two_components(gap):
437437
c = Point((1, 1))
438438
d = Point((2, 1))
439439
e = Point((3, 1))
440-
nodes = GeoDataFrame([a, b, c, d, e], columns=["geometry"])
440+
nodes = GeoDataFrame(geometry=[a, b, c, d, e])
441441
ab = LineString([a, b])
442442
cd = LineString([c, d])
443443
de = LineString([d, e])
444-
edges = GeoDataFrame([ab, cd, de], columns=["geometry"])
444+
edges = GeoDataFrame(geometry=[ab, cd, de])
445445
network = snkit.Network(edges=edges, nodes=nodes)
446446
network = snkit.network.add_ids(network)
447447
network = snkit.network.add_topology(network)
@@ -723,7 +723,8 @@ def test_passing_slice():
723723
ac = LineString([a, c])
724724
cb = LineString([c, b])
725725
cd = LineString([c, d])
726-
edges = GeoDataFrame([ac, cb, cd], columns=["geometry"])
726+
edges = GeoDataFrame(geometry=[ac, cb, cd])
727+
727728
network = snkit.Network(edges=edges[1:])
728729
with_endpoints = snkit.network.add_endpoints(network)
729730
with_ids = snkit.network.add_ids(with_endpoints)
@@ -745,9 +746,7 @@ def test_drop_duplicate_geometries():
745746
cb = LineString([c, b])
746747
# use an index that doesn't start from 0 to check our indexing hygiene
747748
index = pd.Index([2, 3, 5, 7, 11, 13])
748-
gdf_with_dupes = GeoDataFrame(
749-
index=index, data=[a, a, b, ac, ac, cb], columns=["geometry"]
750-
)
749+
gdf_with_dupes = GeoDataFrame(index=index, geometry=[a, a, b, ac, ac, cb])
751750
deduped = snkit.network.drop_duplicate_geometries(gdf_with_dupes)
752751
# we should have just the first of each duplicate item
753752
assert (deduped.index == pd.Index([2, 5, 7, 13])).all()
@@ -771,3 +770,13 @@ def test_to_networkx(connected):
771770
def test_add_component_ids(two_components):
772771
labelled = snkit.network.add_component_ids(two_components)
773772
assert all(labelled.edges.component_id == pd.Series([2, 1, 1]))
773+
774+
775+
def test_matching_gdf_from_geoms(edge_only):
776+
expected = edge_only.edges.copy()
777+
gdf = edge_only.edges.copy()
778+
geoms = gdf.geometry
779+
gdf["a"] = range(len(gdf))
780+
gdf["b"] = "abc"
781+
actual = snkit.network.matching_gdf_from_geoms(gdf, geoms)
782+
assert_frame_equal(actual, expected)

0 commit comments

Comments
 (0)