-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
29 clip osm #34
29 clip osm #34
Changes from all commits
7b138f1
6b29978
7eaffe9
5bf382f
ac0ae02
2a4a433
f7c2844
e8f5607
5acc6e4
a29ea96
bcfd84c
f91f508
ac0c179
ca73900
3652ae7
ddfe587
62d2925
fbf32d8
a63a51c
c50c266
c168573
ab205bf
40186dc
c20a94a
2ed8ec3
75f8c77
953e1b2
26dc30c
3d443ec
1650c90
dfc3b21
1c06fd0
5a56e6f
8555382
37cb97b
ad778bb
a39fc34
a987d72
3568572
e895a95
e2b031f
61f9afa
7fbcd49
04dd63f
15c8ef1
fabbbbe
c5fd6c1
e593b3d
9d57d9f
42f7180
7320cee
3778229
e773ca4
a2278a3
6484092
de72c6b
fc0dfc6
102f812
a4ddb9e
1e83b01
10bacf6
dbf76cc
7fc2cd9
9d9881e
184ccde
b0aa256
62b60ab
f00dabb
50a8987
703140c
de17234
85c771f
67f1b4e
c2554fd
691d88d
b2ac376
5341729
212be70
cae3757
f9308cf
616c3e7
c17668e
23467cd
b2fff03
e52a6c4
bb886d5
8b809e8
6215a1e
55698ff
4989f78
434b962
5628d50
9f6db33
4b93c34
8e677b7
ba4465d
646fae3
040635a
73ec169
f054283
8ade074
cfa9717
38c6018
3f4c81d
277ab35
5d1e0cd
892b70e
5a08467
883874d
8f50c65
7561bc6
e36db0f
6dea0e5
efbd3be
124d239
696c8cd
402d27b
9cb601d
91158fe
91bbdb4
a0dbea9
a6d6fe6
872b045
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,10 @@ | |
from shapely.geometry import box | ||
from pyprojroot import here | ||
|
||
from transport_performance.utils.defence import _is_gtfs_pth, _check_list | ||
from transport_performance.utils.defence import ( | ||
_is_expected_filetype, | ||
_check_list, | ||
) | ||
|
||
|
||
def bbox_filter_gtfs( | ||
|
@@ -38,8 +41,10 @@ def bbox_filter_gtfs( | |
None | ||
|
||
""" | ||
_is_gtfs_pth(pth=in_pth, param_nm="in_pth") | ||
_is_gtfs_pth(pth=out_pth, param_nm="out_pth", check_existing=False) | ||
_is_expected_filetype(pth=in_pth, param_nm="in_pth") | ||
_is_expected_filetype( | ||
pth=out_pth, param_nm="out_pth", check_existing=False | ||
) | ||
_check_list(ls=bbox_list, param_nm="bbox_list", exp_type=float) | ||
for param in [units, crs]: | ||
if not isinstance(param, str): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this function is going to be used to crop GTFS based on a provided box, we should rename the local variable |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
"""Utility functions for OSM files.""" | ||
import subprocess | ||
from pyprojroot import here | ||
|
||
from transport_performance.utils.defence import ( | ||
_bool_defence, | ||
_check_list, | ||
_check_parent_dir_exists, | ||
_is_expected_filetype, | ||
) | ||
|
||
|
||
def filter_osm( | ||
pbf_pth=here("tests/data/newport-2023-06-13.osm.pbf"), | ||
out_pth="filtered.osm.pbf", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The default of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If using a path in the Will add to #61 as it's related to helper function |
||
bbox=[-3.01, 51.58, -2.99, 51.59], | ||
tag_filter=True, | ||
install_osmosis=False, | ||
): | ||
"""Filter an osm.pbf file to a bbox. Relies on homebrew with osmosis. | ||
|
||
Parameters | ||
---------- | ||
pbf_pth: ((str, pathlib.PosixPath), optional) | ||
Path to the open street map pbf to be filtered. Defaults to | ||
here("tests/data/newport-2023-06-13.osm.pbf"). | ||
out_pth: ((str, pathlib.PosixPath), optional) | ||
Path to write to. Defaults to "filtered.osm.pbf". | ||
bbox: (list, optional) | ||
Bounding box used to perform the filter, in left, bottom, right top | ||
order. Defaults to [-3.01, 51.58, -2.99, 51.59]. | ||
tag_filter: (bool, optional) | ||
Should non-highway ways be filtered? Excludes waterway, landuse & | ||
natural. Defaults to True. | ||
install_osmosis: (bool, optional) | ||
Should brew be used to install osmosis if not found. Defaults to False. | ||
|
||
Raises | ||
------ | ||
Exception: Subprocess error. | ||
Exception: Osmosis not found. Will not raise if install_osmosis=True. | ||
|
||
Returns | ||
------- | ||
None | ||
|
||
""" | ||
# defence | ||
_is_expected_filetype(pbf_pth, param_nm="pbf_pth", exp_ext=".pbf") | ||
_is_expected_filetype( | ||
out_pth, param_nm="out_pth", exp_ext=".pbf", check_existing=False | ||
) | ||
for nm, val in { | ||
"tag_filter": tag_filter, | ||
"install_osmosis": install_osmosis, | ||
}.items(): | ||
_bool_defence(val, param_nm=nm) | ||
# check bbox values makes sense, else osmosis will error | ||
if not bbox[0] < bbox[2]: | ||
raise ValueError( | ||
( | ||
f"Bounding box longitude West {bbox[0]}" | ||
f" is not smaller than East {bbox[2]}" | ||
) | ||
) | ||
elif not bbox[1] < bbox[3]: | ||
raise ValueError( | ||
( | ||
f"Bounding box latitude South {bbox[1]}" | ||
f" is not smaller than North {bbox[3]}" | ||
) | ||
) | ||
|
||
_check_list(bbox, param_nm="bbox", check_elements=True, exp_type=float) | ||
_check_parent_dir_exists(out_pth, param_nm="out_pth", create=True) | ||
# Compile the osmosis command | ||
cmd = [ | ||
"osmosis", | ||
"--read-pbf", | ||
pbf_pth.as_posix(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PosixPath() will likely not work in Windows machines. According to the pathlib documentation, the method |
||
"--bounding-box", | ||
f"left={bbox[0]}", | ||
f"bottom={bbox[1]}", | ||
f"right={bbox[2]}", | ||
f"top={bbox[3]}", | ||
] | ||
if tag_filter: # optionaly filter ways | ||
print("Rejecting ways: waterway, landuse & natural.") | ||
cmd.extend(["--tf", "reject-ways", "waterway=* landuse=* natural=*"]) | ||
|
||
cmd.extend(["--used-node", "--write-pbf", out_pth]) | ||
|
||
try: | ||
subprocess.run(cmd, check=True) | ||
print(f"Filter completed. Written to {out_pth}") | ||
except subprocess.CalledProcessError as e1: | ||
raise Exception(f"Error executing osmosis command: {e1}") | ||
except FileNotFoundError as e2: | ||
if install_osmosis: | ||
print(f"osmosis command was not recognised: {e2}. Trying install.") | ||
subprocess.run(["brew", "install", "osmosis"]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
print("Installation of `osmosis successful.`") | ||
subprocess.run(cmd, check=True) | ||
print(f"Retry filter pbf completed. Written to {out_pth}") | ||
else: | ||
raise Exception("`osmosis` is not found. Please install.") | ||
|
||
return None |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,8 @@ def _is_path_like(pth, param_nm): | |
|
||
def _check_parent_dir_exists(pth, param_nm, create=False): | ||
_is_path_like(pth, param_nm) | ||
# realpath helps to catch cases where relative paths are passed in main | ||
pth = os.path.realpath(pth) | ||
parent = os.path.dirname(pth) | ||
if not os.path.exists(parent): | ||
if create: | ||
|
@@ -43,8 +45,8 @@ def _check_parent_dir_exists(pth, param_nm, create=False): | |
return None | ||
|
||
|
||
def _is_gtfs_pth(pth, param_nm, check_existing=True): | ||
"""Handle file paths that should be existing GTFS feeds. | ||
def _is_expected_filetype(pth, param_nm, check_existing=True, exp_ext=".zip"): | ||
"""Handle file paths that should be existing filetypes. | ||
|
||
Parameters | ||
---------- | ||
|
@@ -53,13 +55,15 @@ def _is_gtfs_pth(pth, param_nm, check_existing=True): | |
param_nm : str | ||
The name of the parameter being tested. Helps with debugging. | ||
check_existing : bool | ||
Whether to check if the GTFS file already exists. Defaults to True. | ||
Whether to check if the filetype file already exists. Defaults to True. | ||
exp_ext: str | ||
The expected filetype. | ||
|
||
Raises | ||
------ | ||
TypeError: `pth` is not either of string or pathlib.PosixPath. | ||
FileExistsError: `pth` does not exist on disk. | ||
ValueError: `pth` does not have a `.zip` file extension. | ||
ValueError: `pth` does not have the expected file extension. | ||
|
||
Returns | ||
------- | ||
|
@@ -71,9 +75,9 @@ def _is_gtfs_pth(pth, param_nm, check_existing=True): | |
_, ext = os.path.splitext(pth) | ||
if check_existing and not os.path.exists(pth): | ||
raise FileExistsError(f"{pth} not found on file.") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This error message is unclear.
Something like |
||
if ext != ".zip": | ||
if ext != exp_ext: | ||
raise ValueError( | ||
f"`gtfs_pth` expected a zip file extension. Found {ext}" | ||
f"`{param_nm}` expected file extension {exp_ext}. Found {ext}" | ||
) | ||
|
||
return None | ||
|
@@ -109,11 +113,11 @@ def _url_defence(url): | |
return None | ||
|
||
|
||
def _bool_defence(some_bool): | ||
def _bool_defence(some_bool, param_nm): | ||
"""Defence checking. Not exported.""" | ||
if not isinstance(some_bool, bool): | ||
raise TypeError( | ||
f"`extended_schema` expected boolean. Got {type(some_bool)}" | ||
f"`{param_nm}` expected boolean. Got {type(some_bool)}" | ||
) | ||
|
||
return None | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instructions to install osmosis need to be added to
CONTRIBUTING.md
.#79