def test_catchments_property(): # also vary previous test with 2D lines n = swn.SurfaceWaterNetwork.from_lines(force_2d(valid_lines)) assert n.catchments is None n.catchments = valid_polygons assert n.catchments is not None np.testing.assert_array_almost_equal(n.catchments.area, [800.0, 875.0, 525.0]) np.testing.assert_array_almost_equal(n.segments['upstream_area'], [2200.0, 875.0, 525.0]) np.testing.assert_array_almost_equal(n.segments['width'], [1.4615, 1.4457, 1.4397], 4) assert repr(n) == dedent('''\ <SurfaceWaterNetwork: with catchment polygons 3 segments: [0, 1, 2] 2 headwater: [1, 2] 1 outlets: [0] no diversions />''') if matplotlib: _ = n.plot() plt.close() # unset property n.catchments = None assert n.catchments is None # check errors with pytest.raises(ValueError, match='catchments must be a GeoSeries or None'): n.catchments = 1.0 with pytest.raises( ValueError, match=r'catchments\.index is different than for segments'): n.catchments = valid_polygons.iloc[1:]
def test_init_2D_geom(): lines = force_2d(valid_lines) n = swn.SurfaceWaterNetwork.from_lines(lines) assert len(n.warnings) == 0 assert len(n.errors) == 0 assert len(n) == 3 assert n.has_z is False assert list(n.segments.index) == [0, 1, 2] assert list(n.segments['to_segnum']) == [-1, 0, 0] assert list(n.segments['cat_group']) == [0, 0, 0] assert list(n.segments['num_to_outlet']) == [1, 2, 2] np.testing.assert_allclose(n.segments['dist_to_outlet'], [20.0, 56.05551, 51.622776]) assert list(n.segments['sequence']) == [3, 1, 2] assert list(n.segments['stream_order']) == [2, 1, 1] np.testing.assert_allclose(n.segments['upstream_length'], [87.67828936, 36.05551275, 31.6227766]) assert list(n.headwater) == [1, 2] assert list(n.outlets) == [0] assert repr(n) == dedent('''\ <SurfaceWaterNetwork: 3 segments: [0, 1, 2] 2 headwater: [1, 2] 1 outlets: [0] no diversions />''') if matplotlib: _ = n.plot() plt.close()
def get_basic_swn(has_z: bool = True, has_diversions: bool = False): if has_z: n = swn.SurfaceWaterNetwork.from_lines(n3d_lines) else: n = swn.SurfaceWaterNetwork.from_lines(force_2d(n3d_lines)) if has_diversions: diversions = geopandas.GeoDataFrame(geometry=[ Point(58, 97), Point(62, 97), Point(61, 89), Point(59, 89) ]) n.set_diversions(diversions=diversions) return n
def test_adjust_elevation_profile_errors(valid_n): with pytest.raises(ValueError, match='min_slope must be greater than zero'): valid_n.adjust_elevation_profile(0.0) n2d = swn.SurfaceWaterNetwork.from_lines(force_2d(valid_lines)) with pytest.raises(AttributeError, match='line geometry does not have Z dimension'): n2d.adjust_elevation_profile() min_slope = pd.Series(2. / 1000, index=valid_n.segments.index) min_slope[1] = 3. / 1000 min_slope.index += 1 with pytest.raises(ValueError, match='index is different'): valid_n.adjust_elevation_profile(min_slope)