From 0023592bb8bd9b0e66f6fe7623c575b7d48b5591 Mon Sep 17 00:00:00 2001 From: val-ismaili Date: Tue, 10 Jan 2023 14:34:59 +0000 Subject: [PATCH 1/5] simplify outputs --- README.md | 22 +++++++++++----------- osmox/helpers.py | 5 +++++ osmox/main.py | 21 +++++++++++---------- 3 files changed, 27 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index a7dc9a2..dc97840 100644 --- a/README.md +++ b/README.md @@ -62,10 +62,10 @@ Extract `home`, `work`, `education`, `shop` and various other activity locations First download `isle-of-man-latest.osm.pbf` from [geobabrik](https://download.geofabrik.de/europe/isle-of-man.html) and place in an `example` directory. ```{sh} -osmox run configs/example.json example/isle-of-man-latest.osm.pbf example -crs epsg:27700 -l +osmox run configs/example.json example/isle-of-man-latest.osm.pbf isle-of-man -crs epsg:27700 -l ``` -After about 30 seconds, you should find the outputs in geojson format in the specified `example` directory. The geojson file contains locations for the extracted facilities, and each facility includes a number of features with coordinates given in WGS-84 (EPSG:4326) coordinate reference system (CRS), so that they can be quickly inspected via [kepler](https://kepler.gl) or equivalent. +After about 30 seconds, you should find the outputs in geojson format in the same `example` directory as your OSM input file. The geojson file contains locations for the extracted facilities, and each facility includes a number of features with coordinates given in WGS-84 (EPSG:4326) coordinate reference system (CRS), so that they can be quickly inspected via [kepler](https://kepler.gl) or equivalent. `-l` is short for `--lazy` which helps osmox run a little faster. @@ -105,12 +105,12 @@ After about 30 seconds, you should find the outputs in geojson format in the spe ## OSMOX Run -`osmox run ` is the main entry point for OSMOX: +`osmox run ` is the main entry point for OSMOX: ```{sh} osmox run --help -Usage: osmox run [OPTIONS] CONFIG_PATH INPUT_PATH OUTPUT_PATH +Usage: osmox run [OPTIONS] CONFIG_PATH INPUT_PATH OUTPUT_NAME Options: -crs, --crs TEXT crs string eg (default): 'epsg:27700' (UK grid) @@ -119,7 +119,7 @@ Options: --help Show this message and exit. ``` -Configs are described below. The `` should point to an OSM map dataset (`osm.xml` and `osm.pbf` are supported). The `` should point to an exiting or new output directory. +Configs are described below. The `` should point to an OSM map dataset (`osm.xml` and `osm.pbf` are supported). The `` should be the desired name of the geojson output file i.e. `isle-of-man`. ### Using Docker #### Build the image @@ -155,7 +155,7 @@ OSMOX features and associated configurations are described in the sections below ## Output -After running `osmox run ` you should see something like the following (slowly if you are processing a large map) appear in your terminal: +After running `osmox run ` you should see something like the following (slowly if you are processing a large map) appear in your terminal: ```{sh} Loading config from 'configs/config_NTS_UK.json'. @@ -179,15 +179,15 @@ INFO:osmox.main: Assigning distances to nearest shop. Progress: |██████████████████████████████████████████████████| 100.0% Complete INFO:osmox.main: Assigning distances to nearest medical. Progress: |██████████████████████████████████████████████████| 100.0% Complete -INFO:osmox.main: Writting objects to: suffolk2/epsg_27700.geojson +INFO:osmox.main: Writting objects to: suffolk2/suffolk_epsg_27700.geojson INFO:osmox.main: Reprojecting output to epsg:4326 (lat lon) -INFO:osmox.main: Writting objects to: suffolk2/epsg_4326.geojson +INFO:osmox.main: Writting objects to: suffolk2/suffolk_epsg_4326.geojson INFO:osmox.main:Done. ``` -Once completed, you will find OSMOX has outputted file(s) in `geojson` format in the specified ``. If you have specified a CRS, you will find two output files, named as follows: -1) `.geojson` -2) `epsg_4326.geojson` +Once completed, you will find OSMOX has outputted file(s) in `geojson` format in the same folder as the OSM input file. If you have specified a CRS, you will find two output files, named as follows: +1) `_.geojson` +2) `_epsg_4326.geojson` We generally refer to the outputs collectively as `facilities` and their properties as `features`. Note that each facility has a unique id, a number of features (depending on the configuration) and a point geometry. In the case of areas or polygons, such as buildings, the point represents the centroid. diff --git a/osmox/helpers.py b/osmox/helpers.py index 55a3486..4eb61b9 100644 --- a/osmox/helpers.py +++ b/osmox/helpers.py @@ -1,5 +1,6 @@ import logging import click +import ntpath from pathlib import Path from rtree import index from shapely.geometry import Polygon, Point @@ -190,3 +191,7 @@ def fill_object(i, point, size, new_osm_tags, new_tags, required_acts): object = build.Object(idx=idx, osm_tags=new_osm_tags, activity_tags=new_tags, geom=geom) object.activities = list(required_acts) return object + +def path_leaf(filepath): + head, tail = ntpath.split(filepath) + return Path(head) or ntpath.basename(head) \ No newline at end of file diff --git a/osmox/main.py b/osmox/main.py index d24ffda..fd1d2c5 100644 --- a/osmox/main.py +++ b/osmox/main.py @@ -5,6 +5,7 @@ from osmox import config, build from osmox.helpers import PathPath +from osmox.helpers import path_leaf default_config_path = os.path.abspath( os.path.join(os.path.dirname(__file__), "../configs/config.json") @@ -12,9 +13,9 @@ default_input_path = os.path.abspath( os.path.join(os.path.dirname(__file__), "../tests/data/isle-of-man-latest.osm.pbf") ) -default_output_path = os.path.abspath( - os.path.join(os.path.dirname(__file__), "../outputs/example.geojson") -) +#default_output_path = os.path.abspath( +# os.path.join(os.path.dirname(__file__), "../outputs/example.geojson") +#) logger = logging.getLogger(__name__) @@ -40,21 +41,21 @@ def validate(config_path): @osmox.command() @click.argument('config_path', type=PathPath(exists=True), nargs=1, required=True) @click.argument('input_path', type=PathPath(exists=True), nargs=1, required=True) -@click.argument('output_path', type=PathPath(exists=False), nargs=1, required=True) +@click.argument('output_name', nargs=1, required=True) @click.option("-crs", "--crs", type=str, default="epsg:27700", help="crs string eg (default): 'epsg:27700' (UK grid)") @click.option("-s", "--single_use", is_flag=True, help="split multi-activity facilities into multiple single-activity facilities") @click.option("-l", "--lazy", is_flag=True, help="if filtered object already has a label, do not search for more (supresses multi-use)") -def run(config_path, input_path, output_path, crs, single_use, lazy): +def run(config_path, input_path, output_name, crs, single_use, lazy): logger.info(f" Loading config from {config_path}") cnfg = config.load(config_path) config.validate_activity_config(cnfg) - if not os.path.exists(output_path): - logger.info(f'Creating output directory: {output_path}') - os.mkdir(output_path) + #if not os.path.exists(output_path): + # logger.info(f'Creating output directory: {output_path}') + # os.mkdir(output_path) logger.info(f"Creating handler with crs: {crs}.") if single_use: @@ -103,7 +104,7 @@ def run(config_path, input_path, output_path, crs, single_use, lazy): gdf = handler.geodataframe(single_use=single_use) - path = output_path / f"{crs.replace(':', '_')}.geojson" + path = path_leaf(input_path) / f"{output_name}_{crs.replace(':', '_')}.geojson" logger.info(f" Writting objects to: {path}") with open(path, "w") as file: file.write(gdf.to_json()) @@ -111,7 +112,7 @@ def run(config_path, input_path, output_path, crs, single_use, lazy): if not crs == "epsg:4326": logger.info(" Reprojecting output to epsg:4326 (lat lon)") gdf.to_crs("epsg:4326", inplace=True) - path = output_path / "epsg_4326.geojson" + path = path_leaf(input_path) / f"{output_name}_epsg_4326.geojson" logger.info(f" Writting objects to: {path}") with open(path, "w") as file: file.write(gdf.to_json()) From 29967588e01e43888330b26208cbffbad339d9a0 Mon Sep 17 00:00:00 2001 From: val-ismaili Date: Tue, 10 Jan 2023 14:46:31 +0000 Subject: [PATCH 2/5] linting fix --- osmox/helpers.py | 4 +++- osmox/main.py | 8 +------- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/osmox/helpers.py b/osmox/helpers.py index 4eb61b9..279cf0b 100644 --- a/osmox/helpers.py +++ b/osmox/helpers.py @@ -192,6 +192,8 @@ def fill_object(i, point, size, new_osm_tags, new_tags, required_acts): object.activities = list(required_acts) return object + def path_leaf(filepath): head, tail = ntpath.split(filepath) - return Path(head) or ntpath.basename(head) \ No newline at end of file + return Path(head) or ntpath.basename(head) + \ No newline at end of file diff --git a/osmox/main.py b/osmox/main.py index fd1d2c5..1d35ea4 100644 --- a/osmox/main.py +++ b/osmox/main.py @@ -13,9 +13,7 @@ default_input_path = os.path.abspath( os.path.join(os.path.dirname(__file__), "../tests/data/isle-of-man-latest.osm.pbf") ) -#default_output_path = os.path.abspath( -# os.path.join(os.path.dirname(__file__), "../outputs/example.geojson") -#) + logger = logging.getLogger(__name__) @@ -53,10 +51,6 @@ def run(config_path, input_path, output_name, crs, single_use, lazy): cnfg = config.load(config_path) config.validate_activity_config(cnfg) - #if not os.path.exists(output_path): - # logger.info(f'Creating output directory: {output_path}') - # os.mkdir(output_path) - logger.info(f"Creating handler with crs: {crs}.") if single_use: logger.info("Handler is single-use, activities will get unique locations.") From 03bda3f2fdc3d791ebfe1caa5eb993b288a1faf7 Mon Sep 17 00:00:00 2001 From: val-ismaili Date: Tue, 10 Jan 2023 14:51:23 +0000 Subject: [PATCH 3/5] linting fix --- osmox/helpers.py | 1 - 1 file changed, 1 deletion(-) diff --git a/osmox/helpers.py b/osmox/helpers.py index 279cf0b..89b070f 100644 --- a/osmox/helpers.py +++ b/osmox/helpers.py @@ -196,4 +196,3 @@ def fill_object(i, point, size, new_osm_tags, new_tags, required_acts): def path_leaf(filepath): head, tail = ntpath.split(filepath) return Path(head) or ntpath.basename(head) - \ No newline at end of file From b9e42d56e64b1367e51eeba821151ed418338817 Mon Sep 17 00:00:00 2001 From: val-ismaili Date: Tue, 17 Jan 2023 16:01:52 +0000 Subject: [PATCH 4/5] ntpath changed to pathlib --- osmox/helpers.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/osmox/helpers.py b/osmox/helpers.py index 89b070f..574891a 100644 --- a/osmox/helpers.py +++ b/osmox/helpers.py @@ -1,6 +1,5 @@ import logging import click -import ntpath from pathlib import Path from rtree import index from shapely.geometry import Polygon, Point @@ -194,5 +193,5 @@ def fill_object(i, point, size, new_osm_tags, new_tags, required_acts): def path_leaf(filepath): - head, tail = ntpath.split(filepath) - return Path(head) or ntpath.basename(head) + folder_path = Path(filepath).parent + return folder_path From 1dd82734ba8ea227d20e15d2c451a861565e34ad Mon Sep 17 00:00:00 2001 From: val-ismaili Date: Tue, 17 Jan 2023 16:06:47 +0000 Subject: [PATCH 5/5] docker run instruction change --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index dc97840..ffde3a6 100644 --- a/README.md +++ b/README.md @@ -130,11 +130,11 @@ Configs are described below. The `` should point to an OSM map datas Once you have built the image, the only thing you need to do is add the path to the folder where your inputs are stored to the command, in order to mount that folder (i.e. give the container access to the data in this folder): - docker run -v DATA_FOLDER_PATH:/MOUNT_PATH osmox CONFIG_PATH INPUT_PATH OUTPUT_PATH -crs epsg:27700 -l + docker run -v DATA_FOLDER_PATH:/MOUNT_PATH osmox CONFIG_PATH INPUT_PATH OUTPUT_NAME -crs epsg:27700 -l For example, if your input data and config is stored on your machine in `/Users/user_1/mydata`, and this is also the directoy where you wish to place the outputs: - docker run -v /Users/user_1/mydata:/mnt/mydata osmox /mnt/mydata/example_config.json /mnt/mydata/isle-of-man-latest.osm.pbf /mnt/mydata/ -crs epsg:27700 -l + docker run -v /Users/user_1/mydata:/mnt/mydata osmox /mnt/mydata/example_config.json /mnt/mydata/isle-of-man-latest.osm.pbf isle-of-man -crs epsg:27700 -l ## Options