Skip to content

Commit 75b0985

Browse files
Merge pull request #138 from NREL/rjf/doc-road-network-example
update example documentation + CLI
2 parents 31039d6 + 0519f4f commit 75b0985

File tree

1 file changed

+34
-12
lines changed

1 file changed

+34
-12
lines changed

examples/download_road_network.py

+34-12
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,46 @@
11
from pathlib import Path
2-
32
import geopandas as gpd
4-
3+
import argparse
54
from nrel.hive.model.roadnetwork.osm.osm_roadnetwork import OSMRoadNetwork
65

7-
GEOJSON_PATH = Path(
8-
"../nrel/hive/resources/scenarios/denver_downtown/geofence/downtown_denver.json"
6+
# this example script builds a HIVE road network using OSMNx and GeoPandas
7+
# and can be called from the command line via
8+
# `$ python download_road_network.py some_boundary.geojson`
9+
# under the hood, HIVE will call OSMNx and build the road network with some
10+
# simple rules for downloading and reading the OSM data. for more complex
11+
# network parsing rules you will need to build your own process.
12+
13+
parser = argparse.ArgumentParser(description="network builder")
14+
parser.add_argument(
15+
"boundary",
16+
type=Path,
17+
help='GeoJSON boundary file describing the extent of the network to load',
18+
)
19+
parser.add_argument(
20+
"--outfile",
21+
type=Path,
22+
default="network.json",
23+
help="file path to use when writing the HIVE network",
924
)
10-
OUTFILE = Path("denver_demo_road_network.json")
11-
1225

13-
def build_road_network(geojson_file: Path, outfile: Path):
26+
def import_network(geojson_file: Path, outfile: Path):
27+
"""builds a road network for HIVE from the provided source GeoJSON.
28+
a simple wrapper around reading the GeoJSON via GeoPandas and then
29+
calling HIVE's OSM network reader.
30+
depends on OSMNx: https://github.com/gboeing/osmnx
31+
32+
:param geojson_file: file containing network boundary
33+
:type geojson_file: Path
34+
:param outfile: _description_
35+
:type outfile: Path
36+
"""
1437
df = gpd.read_file(geojson_file)
15-
16-
polygon = df.iloc[0].geometry
17-
38+
polygon = df.geometry.unary_union
1839
rn = OSMRoadNetwork.from_polygon(polygon)
1940

20-
rn.to_file(OUTFILE)
41+
rn.to_file(outfile)
2142

2243

2344
if __name__ == "__main__":
24-
build_road_network(GEOJSON_PATH, OUTFILE)
45+
args = parser.parse_args()
46+
import_network(args.boundary, args.outfile)

0 commit comments

Comments
 (0)