def find_closest_edge(polygons, edges, to_attr='index', column='nearest'): """Find closest edge for centroid of polygons. Args: polygons: a pandas DataFrame with geometry column of Polygons edges: a pandas DataFrame with geometry column of LineStrings to_attr: a column name in DataFrame edges (default: index) column: a column name to be added/overwrite in DataFrame polygons with the value of column to_attr from the nearest edge in edges Returns: a list of LineStrings connecting polygons' centroids with the nearest point in in edges. Side effect: polygons recieves new column with the attribute value of nearest edge. Warning: if column exists, it is overwritten. """ connecting_lines = [] nearest_indices = [] centroids = [b.centroid for b in polygons['geometry']] for centroid in centroids: nearest_edge, _, nearest_index = shapelytools.closest_object( edges['geometry'], centroid) nearest_point = shapelytools.project_point_to_object( centroid, nearest_edge) connecting_lines.append( LineString(tuple(centroid.coords) + tuple(nearest_point.coords))) nearest_indices.append(edges[to_attr][nearest_index]) polygons[column] = pd.Series(nearest_indices, index=polygons.index) return pd.DataFrame({'geometry': connecting_lines})
def find_closest_edge(polygons, edges, to_attr='index', column='nearest'): """Find closest edge for centroid of polygons. Args: polygons: a pandas DataFrame with geometry column of Polygons edges: a pandas DataFrame with geometry column of LineStrings to_attr: a column name in DataFrame edges (default: index) column: a column name to be added/overwrite in DataFrame polygons with the value of column to_attr from the nearest edge in edges Returns: a list of LineStrings connecting polygons' centroids with the nearest point in in edges. Side effect: polygons recieves new column with the attribute value of nearest edge. Warning: if column exists, it is overwritten. """ connecting_lines = [] nearest_indices = [] centroids = [b.centroid for b in polygons['geometry']] for centroid in centroids: nearest_edge, _, nearest_index = shapelytools.closest_object( edges['geometry'], centroid) nearest_point = shapelytools.project_point_to_object(centroid, nearest_edge) connecting_lines.append(LineString(tuple(centroid.coords) + tuple(nearest_point.coords))) nearest_indices.append(edges[to_attr][nearest_index]) polygons[column] = pd.Series(nearest_indices, index=polygons.index) return pd.DataFrame({'geometry': connecting_lines})
] cl = streets[streets["Edge ID"].isin(closest)] small_nw = {key: nw.network_dict[key] for key in cl["Edge ID"]} closest_nw = dict() for i in range(len(gps_p.points_list[1:gps_points])): closest_nw[i] = tuple(reversed(min((gps_p.points_list[i].distance(geom), k) for k, geom in small_nw.iteritems()))) point_edge_dict = {k: v[0] for k, v in closest_nw.iteritems()} print point_edge_dict nearest_points = [st.project_point_to_object(gps_p.points_list[p], small_nw[e]) for p, e in point_edge_dict.iteritems()] long_proj = [x.coords.xy[0][0] for x in nearest_points] lat_proj = [x.coords.xy[1][0] for x in nearest_points] proj = pyproj.Proj("+proj=utm +zone=10T, +north +ellps=WGS84 +datum=WGS84 +units=m") xx, yy = proj(long_proj, lat_proj, inverse=True) plt.figure() # project_point_to_object(point, geometry) # closest_object(geometries, point): for index, i in cl.iterrows(): plt.plot(i["Long"], i["Lat"]) plt.plot(xx, yy, "ro") plt.plot( gps.ix[1:gps_points, "Longitude"] + np.random.normal(0, 0.0001, gps_points),