{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import geopandas as gpd\n", "import osmnx as ox\n", "import networkx as nx\n", "from glob import glob" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Read in the full NC graph\n", "G_nc = ox.load_graphml(filename='NC_highways_all.graphml',\n", " folder='./Data/OSM')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Read in a subgraph\n", "G_sub = ox.load_graphml(filename='gml_0',folder='./Data/subgraphs')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Create a set of end nodes in NC graph\n", "ends_NC = {n for n in G_nc.edges if G_nc.out_degree(n)==0}\n", "len(ends_NC)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Create a set of end nodes in the subgraph\n", "ends_sub_all = {n for n in G_sub.nodes if G_sub.out_degree(n)==0}\n", "len(ends_sub_all)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Subtract the NC end nodes from the subgraph end nodes\n", "ends_sub = ends_sub_all.difference(ends_NC)\n", "print(ends_sub)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Create a geodataframe of all the nodes\n", "gdf_nodes, gdf_edges = ox.graph_to_gdfs(G_sub)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Mask out just the end nodes in the subgraph\n", "gdf_ends = gdf_nodes.loc[gdf_nodes.osmid.isin(ends_sub)]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "out_edges = [e for e in G_sub.out_edges(data=True,keys=True)]# if e[2] == 1]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df = gpd.GeoDataFrame(out_edges)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df[2].unique()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "out_edges[0]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "gdf=gpd.GeoDataFrame(out_edges)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "gdf[['length','geometry']].to_file('./scratch/edgetemp.shp')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.6" } }, "nbformat": 4, "nbformat_minor": 4 }