Commit dacfecb0 authored by Sonja Tripkovic's avatar Sonja Tripkovic

added geospatial: interpolate_series_at_distance; added plotting: plot_street_nodes

parent 754096cd
import overpy
import geopandas as gpd
from shapely.geometry import LineString, Polygon, box
from shapely.geometry import LineString, Polygon, Point, box
from scipy.spatial.distance import cdist
import numpy as np
import warnings
......@@ -157,3 +157,36 @@ def crop_geoseries_by_bounds(original_geoseries : gpd.GeoSeries, filter_geoserie
filtered_series = (gpd.overlay(gpd.GeoDataFrame(geometry = original_geoseries),bound_box, how='intersection')).geometry
return filtered_series
def interpolate_series_at_distance(street_series : gpd.GeoSeries, distance : int = 2) -> gpd.GeoSeries:
'''
Interpolates along the geoseries streets_series to get nodes at a distance 'distance' apart.
Make sure to pass data in Cartesian coordinates (e.g. use street_series.to_crs('EPSG:31287') for Austria).
:param street_series: geopandas geoseries
:param distance: integer determining the spacing between interpolation points, default=2
:return: geopandas geoseries overlapping with original geoseries containing more frequent nodes
'''
if street_series.crs == "EPSG:4326":
raise ValueError("Make sure to pass data in Cartesian coordinates (e.g. use street_series.to_crs('EPSG:31287') for Austria)")
if distance <= 0:
raise ValueError("Distance has to be a positive number.")
lines = []
for linestring in street_series:
list_points = [] # list to hold all the point coords
current_dist = distance # set the current distance to place the point
line_length = linestring.length # get the total length of the line (not number of points, but actual length)
list_points.append(Point(list( linestring.coords[0]))) # append the starting coordinate to the list
while current_dist < line_length: # while the current cumulative distance is less than the total length of the line
list_points.append(linestring.interpolate(current_dist)) # use interpolate and increase the current distance
current_dist += distance
list_points.append(Point(list( linestring.coords[-1]))) # append end coordinate to the list
line = LineString(list_points)
lines.append(line)
interpolated_street_series = gpd.GeoSeries(lines)
return interpolated_street_series
......@@ -34,3 +34,15 @@ def plot_series_osm(gps_series : gpd.GeoSeries, c_series : np.ndarray = None, zo
plt.savefig(save_path, bbox_inches="tight")
if show:
plt.show()
def plot_street_nodes(street_series : gpd.GeoSeries, axs, marker : str = 'o', color : str = 'blue'):
'''
Plots the street_series with nodes along each way.
:param street_series: geopandas series of LineStrings
:param axs: denotes the axis on which to plot
:param marker: marker for the nodes
:param color: color of the plotted lines and nodes
'''
for linestring in street_series:
axs.plot(np.array(linestring)[:,0],np.array(linestring)[:,1],marker = marker, color = color)
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment