start_time = time.time()
// create a GeoDataFrame of the nodes, name it, convert osmid to str
nodes = {node:data for node, data in G_proj.nodes(data=True)}
gdf_nodes = gpd.GeoDataFrame(nodes).T
gdf_nodes.crs = G_proj.graph["crs"]
gdf_nodes.gdf_name = "{}_nodes".format(G_proj.name)
gdf_nodes["osmid"] = gdf_nodes["osmid"].astype(np.int64).map(make_str)
// create new lat/lon columns just to save that data for later, and create a
// geometry column from x/y
gdf_nodes["lon"] = gdf_nodes["x"]
gdf_nodes["lat"] = gdf_nodes["y"]
gdf_nodes["geometry"] = gdf_nodes.apply(lambda row: Point(row["x"], row["y"]), axis=1)
log("Created a GeoDataFrame from graph in {:,.2f} seconds".format(time.time()-start_time))
// project the nodes GeoDataFrame to UTM
gdf_nodes_utm = project_gdf(gdf_nodes, to_crs=to_crs)
// extract data for all edges that have geometry attribute
edges_with_geom = []
for u, v, key, data in G_proj.edges(keys=True, data=True):
if "geometry" in data:
edges_with_geom.append({"u":u, "v":v, "key":key, "geometry":data["geometry"]})
// create an edges GeoDataFrame and project to UTM, if there were any edges
// with a geometry attribute. geom attr only exists if graph has been
// simplified, otherwise you don"t have to project anything for the edges
// because the nodes still contain all spatial data
if len(edges_with_geom) > 0:
gdf_edges = gpd.GeoDataFrame(edges_with_geom)
gdf_edges.crs = G_proj.graph["crs"]
gdf_edges.gdf_name = "{}_edges".format(G_proj.name)
gdf_edges_utm = project_gdf(gdf_edges, to_crs=to_crs)
// extract projected x and y values from the nodes" geometry column
start_time = time.time()
gdf_nodes_utm["x"] = gdf_nodes_utm["geometry"].map(lambda point: point.x)
gdf_nodes_utm["y"] = gdf_nodes_utm["geometry"].map(lambda point: point.y)
gdf_nodes_utm = gdf_nodes_utm.drop("geometry", axis=1)
log("Extracted projected node geometries from GeoDataFrame in {:,.2f} seconds".format(time.time()-start_time))
After Change
start_time = time.time()
// create a GeoDataFrame of the nodes, name it, convert osmid to str
nodes_data = [data for _, data in G_proj.nodes(data=True)]
gdf_nodes = gpd.GeoDataFrame(nodes_data, index=G_proj.nodes(data=False))
gdf_nodes.crs = G_proj.graph["crs"]
gdf_nodes.gdf_name = "{}_nodes".format(G_proj.name)
// create new lat/lon columns just to save that data for later, and create a
// geometry column from x/y
gdf_nodes["lon"] = gdf_nodes["x"]
gdf_nodes["lat"] = gdf_nodes["y"]
gdf_nodes["geometry"] = gdf_nodes.apply(lambda row: Point(row["x"], row["y"]), axis=1)
log("Created a GeoDataFrame from graph in {:,.2f} seconds".format(time.time()-start_time))
// project the nodes GeoDataFrame to UTM
gdf_nodes_utm = project_gdf(gdf_nodes, to_crs=to_crs)
// extract data for all edges that have geometry attribute
edges_with_geom = []
for u, v, key, data in G_proj.edges(keys=True, data=True):
if "geometry" in data:
edges_with_geom.append({"u":u, "v":v, "key":key, "geometry":data["geometry"]})
// create an edges GeoDataFrame and project to UTM, if there were any edges
// with a geometry attribute. geom attr only exists if graph has been
// simplified, otherwise you don"t have to project anything for the edges
// because the nodes still contain all spatial data
if len(edges_with_geom) > 0:
gdf_edges = gpd.GeoDataFrame(edges_with_geom)
gdf_edges.crs = G_proj.graph["crs"]
gdf_edges.gdf_name = "{}_edges".format(G_proj.name)
gdf_edges_utm = project_gdf(gdf_edges, to_crs=to_crs)
// extract projected x and y values from the nodes" geometry column
start_time = time.time()
gdf_nodes_utm["x"] = gdf_nodes_utm["geometry"].map(lambda point: point.x)
gdf_nodes_utm["y"] = gdf_nodes_utm["geometry"].map(lambda point: point.y)
gdf_nodes_utm = gdf_nodes_utm.drop("geometry", axis=1)
log("Extracted projected node geometries from GeoDataFrame in {:,.2f} seconds".format(time.time()-start_time))