Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
elbeejay committed Jul 15, 2024
1 parent 7d2b7a0 commit 864347a
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/GOSTnets/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3615,7 +3615,7 @@ def project_gdf(gdf, to_crs=None, to_latlong=False):

# otherwise, automatically project the gdf to UTM
else:
if ox.CRS.from_user_input(gdf.crs).is_projected:
if ox.projection.is_projected(gdf.crs):
raise ValueError("Geometry must be unprojected to calculate UTM zone")

# calculate longitude of centroid of union of all geometries in gdf
Expand Down Expand Up @@ -3766,7 +3766,7 @@ def find_kne(point, lines, near_idx):
"""
# getting the distances between the point and the lines
dists = np.array(list(map(lambda line: line.distance(point), lines)))
dists = np.array(lines.distance(point))
kne_pos = dists.argsort()[0]
kne = lines.iloc[kne_pos]
kne_idx = near_idx[kne_pos]
Expand Down Expand Up @@ -3865,6 +3865,10 @@ def update_nodes(
The updated nodes GeoDataFrame.
"""
### NOTES
# node_highway_pp and node_highway_poi are not both needed, they are
# used conditionally depending on the ptype, so the parameters should be updated

# create gdf of new nodes (projected PAPs)
if ptype == "pp":
new_nodes = gpd.GeoDataFrame(new_points, columns=["geometry"], crs=measure_crs)
Expand Down Expand Up @@ -3918,6 +3922,12 @@ def update_edges(edges, new_lines, replace=True, nodes_meter=None, pois_meter=No
kne_idx refers to 'fid in Rtree'/'label'/'loc', not positional iloc
"""
### NOTES
# line_pps_dict is never defined, where did it come from?
# same for 'itertools', 'measure_crs', 'pois', 'poi_key_col', 'oneway_tag',
# 'road_col', 'edge_highway', 'factor', 'u_tag', 'v_tag', 'nodes_id_dict',
# 'threshold', and maybe additional variables... DID THIS FUNCTION EVER WORK?

# for interpolation (split by pp): replicate old line
if replace:
# create a flattened gdf with all line segs and corresponding kne_idx
Expand Down
46 changes: 46 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,13 @@ def test_project_gdf():
projected_gdf = core.project_gdf(gdf, to_crs="epsg:32633", to_latlong=False)
# check the output
assert projected_gdf.crs == "epsg:32633"
# with invalid CRS
gdf_invalid = gpd.GeoDataFrame({"geometry": [Point(0, 0), Point(1, 1)]}, crs=None)
with pytest.raises(ValueError):
core.project_gdf(gdf_invalid, to_crs="epsg:32633", to_latlong=False)
# with no "to_crs" parameter
utm_gdf = core.project_gdf(gdf, to_latlong=False)
assert "utm" in utm_gdf.crs.to_string()


def test_euclidean_distance():
Expand Down Expand Up @@ -857,3 +864,42 @@ def test_clip():
assert G_clip.nodes(data=True) != G.nodes(data=True)
assert G_clip.edges(data=True) != G.edges(data=True)
assert G_clip.number_of_nodes() < G.number_of_nodes()


def test_find_kne():
"""Test the find_kne function."""
# define a point
point = Point(0.5, 0.6)
# define some lines
line1 = LineString([(0, 0), (1, 1)])
line2 = LineString([(1, 1), (2, 2)])
line3 = LineString([(2, 2), (3, 3)])
# put lines in a geodataframe
gdf = gpd.GeoDataFrame({"geometry": [line1, line2, line3]})
# call function
kne_idx, kne = core.find_kne(point, gdf, [0, 1, 2])
assert isinstance(kne_idx, int)
assert isinstance(kne, pd.Series)
assert kne_idx == 0


def test_get_pp():
"""Test the get_pp function."""
# define a point
point = Point(0.5, 0.6)
# define the line
line = LineString([(0, 0), (1, 1)])
# call the function
pp = core.get_pp(point, line)
assert isinstance(pp, Point)
assert pp.x == 0.55
assert pp.y == 0.55


def test_split_line_error():
"""Test the split_line function error handling."""
# define line
line = LineString([(0, 0), (1, 1)])
# call function
with pytest.raises(TypeError):
core.split_line(line, "invalid")
14 changes: 14 additions & 0 deletions tests/test_core_integrative.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,17 @@ def test_edges_nodes_df_to_graph_03(self):
G = core.edges_and_nodes_gdf_to_graph(nodes_df, edges_df, largest_G=True)
# assert that the function returned a nx graph this time
assert isinstance(G, nx.Graph)

def test_edges_nodes_df_to_graph_04(self):
# read csv files as dfs
nodes_df = pd.read_csv(self.fpath_nodes)
edges_df = pd.read_csv(self.fpath_edges)
# switch stnode and endnode to floats
edges_df["stnode"] = edges_df["stnode"].astype(float)
edges_df["endnode"] = edges_df["endnode"].astype(float)
# call function with adding missing reflected edges and the oneway tag
G = core.edges_and_nodes_gdf_to_graph(
nodes_df, edges_df, add_missing_reflected_edges=True, oneway_tag="one_way"
)
# assert that the function returned a nx graph this time
assert isinstance(G, nx.Graph)

0 comments on commit 864347a

Please sign in to comment.