From 808d1045d7d47dff62cedbc88efe2ccfadfd5bb6 Mon Sep 17 00:00:00 2001 From: "Michael T. Kelbaugh" Date: Mon, 2 Mar 2020 12:41:26 -0500 Subject: [PATCH] Dockerize the graph construction tool Former-commit-id: 23f78dba4890c89fa77f93e57584f20b0932b335 --- Makefile | 3 +++ graphs/Dockerfile | 12 ++++++++++++ graphs/build.py | 20 ++++++++++++-------- graphs/config.json | 19 +++++++++++++++++++ 4 files changed, 46 insertions(+), 8 deletions(-) create mode 100644 graphs/Dockerfile create mode 100644 graphs/config.json diff --git a/Makefile b/Makefile index 45bbfdf..8b19e17 100644 --- a/Makefile +++ b/Makefile @@ -18,3 +18,6 @@ clean: purge: cd build && docker-compose -p simon down --rmi all + +graph: + cd graphs && docker build -t simon-graph:latest . && docker run -v `pwd`:/opt -d simon-graph:latest diff --git a/graphs/Dockerfile b/graphs/Dockerfile new file mode 100644 index 0000000..553ef47 --- /dev/null +++ b/graphs/Dockerfile @@ -0,0 +1,12 @@ +FROM ubuntu:18.04 +RUN apt-get update && apt-get install -y python3-dev=3.6.7-1~18.04 python3-pip=9.0.1-2.3~ubuntu1.18.04.1 cmake=3.10.2-1ubuntu2.18.04.1 wget && apt-get -qy autoremove && apt-get clean && rm -r /var/lib/apt/lists/* +RUN wget https://github.com/libspatialindex/libspatialindex/releases/download/1.9.3/spatialindex-src-1.9.3.tar.gz -P / +RUN tar xfvz /spatialindex-src-1.9.3.tar.gz +WORKDIR /spatialindex-src-1.9.3 +RUN cmake -DCMAKE_INSTALL_PREFIX=/libspatialindex . +RUN make +RUN make install +ENV LD_LIBRARY_PATH=/libspatialindex/lib +COPY ./requirements.txt / +RUN pip3 install -r /requirements.txt +ENTRYPOINT ["python3", "/opt/build.py"] diff --git a/graphs/build.py b/graphs/build.py index 1610049..bb455c9 100644 --- a/graphs/build.py +++ b/graphs/build.py @@ -16,6 +16,10 @@ import itertools from collections import defaultdict import uuid import os +import json + +with open(os.path.join(os.path.dirname(__file__), "config.json")) as f: + config = json.load(f) # where to save the graphs save_dir = os.path.join(os.path.dirname(__file__), "out") @@ -24,19 +28,19 @@ save_dir = os.path.join(os.path.dirname(__file__), "out") shapefile_dir = os.path.join(os.path.dirname(__file__), "shapefiles") # the coordinate reference system the shapefiles are defined on -projection = 3085 +projection = config["projection"] # scale the areas calculated for shapefile geometries from square kilometers to square meters -scale_factor = 10**6 +scale_factor = config["scale_factor"] # the minimum area of a meet node (a node formed by the interestion of two disparate geograophic granularities), in square kilometers -minimum_intersection_area = 1 +minimum_intersection_area = config["minimum_intersection_area"] # Define the abstract graph with a list of tuples with the form (source, destination), where source is a higher lower resolution granularity that encompasses destination, a higher resolution granularity. -abstract_edges = [("usa48", "state"), ("state", "county"), ("usa48", "nerc"), ("usa48", "huc8"), ("usa48", "latlon")] +abstract_edges = config["abstract_edges"] # Save the instance graph with its geometries included. This will create a very large graph. -save_shapes = False +save_shapes = config["save_shapes"] # open the shapefiles for each granularity states = read_file(f"{shapefile_dir}/state.shp").to_crs(epsg=projection) @@ -257,7 +261,7 @@ print(metadata) # save the instance graph with its geometries (very large) if save_shapes: - with open("{}/{}-official-instance-graph-{}-shapes_{}km_simple1km.geojson".format(save_dir, "-".join(abstract_nodes), projection, minimum_intersection_area), mode='w') as outfile: + with open("{}/instance-graph_{}_{}_{}_{}_shapes.geojson".format(save_dir, "-".join(abstract_nodes), projection, minimum_intersection_area, config["tag"]), mode='w') as outfile: geojson.dump(json_graph.node_link_data(instance_graph), outfile) # remove geometries from the instance graph (much smaller) @@ -267,9 +271,9 @@ for node in instance_graph_noshapes['nodes']: del node['shape'] # save graphs to JSON files -with open("{}/abstract-graph_{}_{}_{}km_simple1km.geojson".format(save_dir, "-".join(abstract_nodes), projection, minimum_intersection_area), mode='w') as outfile: +with open("{}/abstract-graph_{}_{}_{}_{}.geojson".format(save_dir, "-".join(abstract_nodes), projection, minimum_intersection_area, config["tag"]), mode='w') as outfile: geojson.dump(json_graph.node_link_data(abstract_graph), outfile) -with open("{}/instance-graph_{}_{}_{}km_simple1km.geojson".format(save_dir, "-".join(abstract_nodes), projection, minimum_intersection_area), mode='w') as outfile: +with open("{}/instance-graph_{}_{}_{}_{}.geojson".format(save_dir, "-".join(abstract_nodes), projection, minimum_intersection_area, config["tag"]), mode='w') as outfile: geojson.dump(instance_graph_noshapes, outfile) print("done building graphs") diff --git a/graphs/config.json b/graphs/config.json new file mode 100644 index 0000000..3b40971 --- /dev/null +++ b/graphs/config.json @@ -0,0 +1,19 @@ +{ + "projection": 3085, + + "scale_factor": 1000000, + + "minimum_intersection_area": 1, + + "abstract_edges": [ + ["usa48", "state"], + ["state", "county"], + ["usa48", "nerc"], + ["usa48", "huc8"], + ["usa48", "latlon"] + ], + + "save_shapes": false, + + "tag": "latest" +}