import geopandas as gpd
import osmnx as ox
import networkx as nx
from glob import glob
#Read in the full NC graph
G_nc = ox.load_graphml(filename='NC_highways_all.graphml',
folder='./Data/OSM')
#Read in a subgraph
G_sub = ox.load_graphml(filename='gml_0',folder='./Data/subgraphs')
#Create a set of end nodes in NC graph
ends_NC = {n for n in G_nc.edges if G_nc.out_degree(n)==0}
len(ends_NC)
#Create a set of end nodes in the subgraph
ends_sub_all = {n for n in G_sub.nodes if G_sub.out_degree(n)==0}
len(ends_sub_all)
#Subtract the NC end nodes from the subgraph end nodes
ends_sub = ends_sub_all.difference(ends_NC)
print(ends_sub)
#Create a geodataframe of all the nodes
gdf_nodes, gdf_edges = ox.graph_to_gdfs(G_sub)
#Mask out just the end nodes in the subgraph
gdf_ends = gdf_nodes.loc[gdf_nodes.osmid.isin(ends_sub)]
out_edges = [e for e in G_sub.out_edges(data=True,keys=True)]# if e[2] == 1]
df = gpd.GeoDataFrame(out_edges)
df[2].unique()
out_edges[0]
gdf=gpd.GeoDataFrame(out_edges)
gdf[['length','geometry']].to_file('./scratch/edgetemp.shp')