Skip to content

Commit

Permalink
Update tutorials (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
elbeejay authored Jun 13, 2024
1 parent 811bd6f commit 3e26232
Show file tree
Hide file tree
Showing 17 changed files with 112 additions and 135 deletions.
17 changes: 11 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,17 @@ venv.bak/

.DS_Store

tutorial/tutorial_outputs/*
Tutorial/tutorial_data/rek_grid_pop_gravity.cpg
Tutorial/tutorial_data/rek_grid_pop_gravity.dbf
Tutorial/tutorial_data/rek_grid_pop_gravity.prj
Tutorial/tutorial_data/rek_grid_pop_gravity.shp
Tutorial/tutorial_data/rek_grid_pop_gravity.shx
Tutorials/tutorial_outputs/*
Tutorials/tutorial_data/rek_grid_pop_gravity.cpg
Tutorials/tutorial_data/rek_grid_pop_gravity.dbf
Tutorials/tutorial_data/rek_grid_pop_gravity.prj
Tutorials/tutorial_data/rek_grid_pop_gravity.shp
Tutorials/tutorial_data/rek_grid_pop_gravity.shx
Tutorials/tutorial_data/rek_grid_pop_gravity2.cpg
Tutorials/tutorial_data/rek_grid_pop_gravity2.dbf
Tutorials/tutorial_data/rek_grid_pop_gravity2.prj
Tutorials/tutorial_data/rek_grid_pop_gravity2.shp
Tutorials/tutorial_data/rek_grid_pop_gravity2.shx

# ignore the version file
*_version.py*
Expand Down
28 changes: 20 additions & 8 deletions Tutorials/EXAMPLE_Finding_links_between_pairs.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
"outputs": [],
"source": [
"import os\n",
"import sys\n",
"\n",
"import pickle as pkl\n",
"import networkx as nx\n",
"import geopandas as gpd\n",
"import pandas as pd\n",
Expand All @@ -31,8 +30,7 @@
"metadata": {},
"outputs": [],
"source": [
"# add to your system path the location of the LoadOSM.py and GOSTnet.py scripts\n",
"sys.path.append(\"../\")\n",
"# import the GOSTnet library\n",
"import GOSTnets as gn"
]
},
Expand All @@ -45,8 +43,8 @@
"# Define input data\n",
"pth = \"./\"\n",
"# Read in cleaned pickle from earlier analysis and convert to time\n",
"G = nx.read_gpickle(\n",
" os.path.join(pth, \"tutorial_outputs\", r\"iceland_network_clean.pickle\")\n",
"G = pkl.load(\n",
" open(os.path.join(pth, \"tutorial_outputs\", r\"iceland_network_clean.pickle\"), \"rb\")\n",
")\n",
"G_time = gn.convert_network_to_time(\n",
" G, distance_tag=\"length\", road_col=\"infra_type\", factor=1000\n",
Expand Down Expand Up @@ -117,6 +115,18 @@
"## Calculate line strings connecting all origins to all destinations"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# for the sake of the example we truncate the number of origins and destinations\n",
"# to make the computation faster. We will only use the first 10 of each\n",
"origins = origins[:10]\n",
"destinations = destinations[:10]"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -153,7 +163,7 @@
"source": [
"# Write all connections to file\n",
"all_results = pd.DataFrame(all_res, columns=[\"O\", \"D\", \"geometry\"])\n",
"all_results.to_csv(os.path.join(pth, \"tutorial_data\", \"all_OD_links.csv\"))"
"all_results.to_csv(os.path.join(pth, \"tutorial_outputs\", \"all_OD_links.csv\"))"
]
},
{
Expand All @@ -177,7 +187,9 @@
"metadata": {},
"outputs": [],
"source": [
"all_connections_first.to_csv(os.path.join(pth, \"tutorial_data\", \"OD_links_usage.csv\"))"
"all_connections_first.to_csv(\n",
" os.path.join(pth, \"tutorial_outputs\", \"OD_links_usage.csv\")\n",
")"
]
},
{
Expand Down
59 changes: 8 additions & 51 deletions Tutorials/EXAMPLE_Fixing_your_road_network.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
"outputs": [],
"source": [
"import os\n",
"import sys\n",
"import networkx as nx\n",
"import osmnx as ox"
"import osmnx as ox\n",
"import pickle as pkl"
]
},
{
Expand All @@ -27,8 +27,7 @@
"metadata": {},
"outputs": [],
"source": [
"# add to your system path the location of the LoadOSM.py and GOSTnet.py scripts\n",
"sys.path.append(\"../\")\n",
"# import the GOSTnets library\n",
"import GOSTnets as gn"
]
},
Expand All @@ -41,8 +40,8 @@
"pth = \"./\" # change this path to your working folder\n",
"data_pth = os.path.join(pth, \"tutorial_outputs\")\n",
"\n",
"# read back your graph from step 1 from you saved pickle\n",
"G = nx.read_gpickle(os.path.join(data_pth, \"iceland_network_clean.pickle\"))"
"# read back your graph from step 2 from you saved pickle\n",
"G = pkl.load(open(os.path.join(data_pth, \"iceland_network_clean.pickle\"), \"rb\"))"
]
},
{
Expand All @@ -64,8 +63,7 @@
"metadata": {},
"outputs": [],
"source": [
"G_largest = list_of_subgraphs[0]\n",
"G_second_largest = list_of_subgraphs[1]"
"G_largest = list_of_subgraphs[0]"
]
},
{
Expand All @@ -74,25 +72,7 @@
"metadata": {},
"outputs": [],
"source": [
"print(nx.info(G_largest))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(nx.info(G_second_largest))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(nx.info(list_of_subgraphs[2]))"
"print(G_largest)"
]
},
{
Expand All @@ -106,20 +86,7 @@
"G_largest.graph[\"name\"] = \"Iceland\"\n",
"\n",
"# largest connected subgraph\n",
"ox.plot_graph(G_largest, fig_width=10, edge_linewidth=1, node_size=7)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"G_second_largest.graph[\"crs\"] = \"epsg:32646\"\n",
"G_second_largest.graph[\"name\"] = \"Iceland\"\n",
"\n",
"# second largest connected subgraph\n",
"ox.plot_graph(G_second_largest, fig_width=10, edge_linewidth=1, node_size=7)"
"ox.plot_graph(G_largest, figsize=(10, 14), edge_linewidth=1, node_size=7)"
]
},
{
Expand All @@ -132,16 +99,6 @@
"edges_largest.to_csv(os.path.join(data_pth, \"edges_largest.csv\"))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"edges_second = gn.edge_gdf_from_graph(G_second_largest)\n",
"edges_second.to_csv(os.path.join(data_pth, \"edges_second.csv\"))"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down
23 changes: 15 additions & 8 deletions Tutorials/EXAMPLE_Gravity_Calculations.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@
"outputs": [],
"source": [
"import os\n",
"import sys\n",
"import rasterio\n",
"\n",
"import networkx as nx\n",
"import geopandas as gpd\n",
"import pandas as pd"
"import pandas as pd\n",
"import pickle as pkl"
]
},
{
Expand All @@ -21,8 +20,7 @@
"metadata": {},
"outputs": [],
"source": [
"# add to your system path the location of the LoadOSM.py and GOSTnet.py scripts\n",
"sys.path.append(\"../\")\n",
"# import the GOSTnets library\n",
"import GOSTnets as gn"
]
},
Expand All @@ -35,8 +33,11 @@
"# Define input data\n",
"pth = \"./\"\n",
"# Read in cleaned pickle from earlier analysis and convert to time\n",
"G = nx.read_gpickle(\n",
" os.path.join(pth, \"tutorial_outputs\", r\"iceland_network_clean.pickle\")\n",
"G = pkl.load(\n",
" open(\n",
" os.path.join(pth, \"tutorial_outputs\", r\"iceland_network_clean.pickle\"),\n",
" \"rb\",\n",
" )\n",
")\n",
"G_time = gn.convert_network_to_time(\n",
" G, distance_tag=\"length\", road_col=\"infra_type\", factor=1000\n",
Expand Down Expand Up @@ -177,9 +178,15 @@
"OD = gn.calculate_OD(G_time, origins, destinations, fail_value=9999999)\n",
"gravity = calcOD.calculate_gravity(\n",
" OD, oWeight=origins_df[\"Pop\"], dWeight=destinations_df[\"Pop\"]\n",
")"
")\n",
"gravity[\"NN\"] = origins"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "code",
"execution_count": null,
Expand Down
28 changes: 14 additions & 14 deletions Tutorials/Step_1-Extract_From_OSM.ipynb
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## note: As of Jan 21, 2020 This tutorial is using the GOSTnets version from this repo's master branch. Make sure that the GOSTnets version from PyPi is uninstalled and that you are pointing your path to the correct location of the library."
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand All @@ -23,8 +16,7 @@
"outputs": [],
"source": [
"import geopandas as gpd\n",
"import os\n",
"import sys"
"import os"
]
},
{
Expand All @@ -43,8 +35,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Add to your system path the location of the LoadOSM.py and GOSTnet.py scripts. \n",
"In this case, we use relative paths (\"..\") to locate the parent directory (one level up). One dot (\".\") means current directory."
"Import the GOSTnets library as gn."
]
},
{
Expand All @@ -53,7 +44,6 @@
"metadata": {},
"outputs": [],
"source": [
"sys.path.append(\"../\")\n",
"import GOSTnets as gn"
]
},
Expand Down Expand Up @@ -216,7 +206,7 @@
"outputs": [],
"source": [
"clip_shp = gpd.read_file(os.path.join(pth, \"tutorial_data\", \"rek2.shp\"))\n",
"clip_shp = clip_shp.to_crs({\"init\": \"epsg:4326\"})\n",
"clip_shp = clip_shp.to_crs(\"epsg:4326\")\n",
"clip_shp_obj = clip_shp.geometry.iloc[0]"
]
},
Expand Down Expand Up @@ -271,7 +261,7 @@
"outputs": [],
"source": [
"iceland.roads_raw = iceland.roads_raw.loc[\n",
" iceland.roads_raw.geometry.intersects(clip_shp_obj) is True\n",
" iceland.roads_raw.geometry.intersects(clip_shp_obj) == True # noqa: E712\n",
"]"
]
},
Expand All @@ -291,6 +281,16 @@
"iceland.generateRoadsGDF(verbose=False)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# view the first 5 rows of the newly created roads GeoDataFrame\n",
"iceland.roadsGPD.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down
Loading

0 comments on commit 3e26232

Please sign in to comment.