|
1 | 1 | from pathlib import Path
|
2 |
| - |
3 | 2 | import geopandas as gpd
|
4 |
| - |
| 3 | +import argparse |
5 | 4 | from nrel.hive.model.roadnetwork.osm.osm_roadnetwork import OSMRoadNetwork
|
6 | 5 |
|
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", |
9 | 24 | )
|
10 |
| -OUTFILE = Path("denver_demo_road_network.json") |
11 |
| - |
12 | 25 |
|
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 | + """ |
14 | 37 | df = gpd.read_file(geojson_file)
|
15 |
| - |
16 |
| - polygon = df.iloc[0].geometry |
17 |
| - |
| 38 | + polygon = df.geometry.unary_union |
18 | 39 | rn = OSMRoadNetwork.from_polygon(polygon)
|
19 | 40 |
|
20 |
| - rn.to_file(OUTFILE) |
| 41 | + rn.to_file(outfile) |
21 | 42 |
|
22 | 43 |
|
23 | 44 | 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