Skip to content

Commit

Permalink
Refactor function to use vectorised version
Browse files Browse the repository at this point in the history
  • Loading branch information
reyery committed Oct 15, 2023
1 parent 8a27a7a commit 48f0eb9
Showing 1 changed file with 8 additions and 18 deletions.
26 changes: 8 additions & 18 deletions cea/technologies/network_layout/connectivity_potential.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,24 +270,14 @@ def nearest_neighbor_within(others, point, max_distance):


def near_analysis(building_centroids, street_network, crs):
near_point = []
building_name = []
for point, name in zip(building_centroids.geometry, building_centroids.Name):
point._crs = crs
distance = 10e10
for line in street_network.geometry:
line._crs = crs
nearest_point_candidate = line.interpolate(line.project(point))
distance_candidate = point.distance(nearest_point_candidate)
if distance_candidate < distance:
distance = distance_candidate
nearest_point = nearest_point_candidate
building_name.append(name)
near_point.append(nearest_point)

geometry = near_point
df = gdf(geometry=geometry, crs=crs)
df["Name"] = building_name
# Get the nearest edge for each building centroid
nearest_indexes = street_network.sindex.nearest(building_centroids.geometry, return_all=False)[1]
nearest_lines = street_network.iloc[nearest_indexes].reset_index(drop=True) # reset index so vectorization works

# Find length along line that is closest to the point (project) and get interpolated point on the line (interpolate)
nearest_points = nearest_lines.interpolate(nearest_lines.project(building_centroids))

df = gdf({"Name": building_centroids["Name"]}, geometry=nearest_points, crs=crs)
return df


Expand Down

0 comments on commit 48f0eb9

Please sign in to comment.