swn.spatial.location_pair_geoms#

swn.spatial.location_pair_geoms(pairs, loc_df, n)#

Extract linestring geometry for location pairs, that follow segments.

Parameters:
pairsiterable

Collection of location pair tuples (upstream, downstream), indexed by loc_df from find_location_pairs().

loc_dfgeopandas.GeoDataFrame or pandas.DataFrame

Location [geo] dataframe, usually created by SurfaceWaterNetwork.locate_geoms().

nSurfaceWaterNetwork

A surface water network.

Returns:
dict

keys are the pair tuple, and values are the linestring geometry.

Examples

>>> import geopandas
>>> from shapely import wkt
>>> import swn
>>> lines = geopandas.GeoSeries(list(wkt.loads('''\
... MULTILINESTRING(
...     (380 490, 370 420), (300 460, 370 420), (370 420, 420 330),
...     (190 250, 280 270), (225 180, 280 270), (280 270, 420 330),
...     (420 330, 584 250), (520 220, 584 250), (584 250, 710 160),
...     (740 270, 710 160), (735 350, 740 270), (880 320, 740 270),
...     (925 370, 880 320), (974 300, 880 320), (760 460, 735 350),
...     (650 430, 735 350), (710 160, 770 100), (700  90, 770 100),
...     (770 100, 820  40))''').geoms))
>>> lines.index += 100
>>> n = swn.SurfaceWaterNetwork.from_lines(lines)
>>> obs_gs = geopandas.GeoSeries.from_wkt([
...    "POINT (370 400)",
...    "POINT (720 230)",
...    "POINT (780 70)",
...    "POINT (606 256)",
...    "POINT (690 170)"])
>>> obs_gs.index += 11
>>> obs_match = n.locate_geoms(obs_gs)
>>> pairs = swn.spatial.find_location_pairs(obs_match, n)
>>> sorted(pairs)
[(11, 14), (12, 13), (14, 15), (15, 13)]
>>> pair_geoms = swn.spatial.location_pair_geoms(pairs, obs_match, n)
>>> pair_gs = geopandas.GeoSeries(pair_geoms)
>>> pair_gdf = geopandas.GeoDataFrame(geometry=pair_gs)
>>> pair_gdf["length"] = pair_gdf.length
>>> pair_gdf.sort_values("length", ascending=False, inplace=True)
>>> pair_gdf
                                                geometry      length
11 14  LINESTRING (378.491 404.717, 420 330, 584 250,...  282.359779
12 13  LINESTRING (728.462 227.692, 710 160, 770 100,...  184.465938
15 13  LINESTRING (692.027 172.838, 710 160, 770 100,...  136.388347
14 15       LINESTRING (595.73 241.622, 692.027 172.838)  118.340096