Skip to content

Commit dad7b14

Browse files
committed
cli output format updates & attempt test_cli.py
1 parent a0d9e34 commit dad7b14

File tree

2 files changed

+69
-16
lines changed

2 files changed

+69
-16
lines changed

src/osmox/cli.py

+13-16
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33

44
import click
5+
import pyproj
56

67
from osmox import build, config
78
from osmox.helpers import PathPath, path_leaf
@@ -116,29 +117,25 @@ def run(config_path, input_path, output_name, format, crs, single_use, lazy):
116117

117118
if format == "geojson":
118119
extension = "geojson"
119-
driver = 'GeoJSON'
120-
writer = gdf.to_file
120+
writer_method = 'to_file'
121+
kwargs = {"driver": 'GeoJSON'}
121122
elif format == "geopackage":
122123
extension = "gpkg"
123-
driver = 'GPKG'
124-
writer = gdf.to_file
124+
writer_method = 'to_file'
125+
kwargs = {"driver": 'GPKG'}
125126
elif format == "geoparquet":
126127
extension = "parquet"
127-
writer = gdf.to_parquet
128+
writer_method = 'to_parquet'
129+
kwargs = {}
128130

129131
logger.info(f" Writing objects to {format} format.")
130132
output_filename = f"{output_name}_{crs.replace(':', '_')}.{extension}"
131133

132-
if format != "geoparquet":
133-
writer(output_filename, driver=driver)
134-
else:
135-
writer(output_filename)
136-
137-
if not crs == "epsg:4326" and format != "geoparquet":
138-
logger.info(" Reprojecting output to EPSG:4326 (lat lon)")
139-
gdf.to_crs("epsg:4326").to_file(f"{output_name}_epsg_4326.{extension}", driver=driver)
140-
elif not crs == "epsg:4326" and format == "geoparquet":
141-
logger.info(" Reprojecting output to EPSG:4326 (lat lon)")
142-
gdf.to_crs("epsg:4326").to_parquet(f"{output_name}_epsg_4326.parquet")
134+
getattr(gdf, writer_method)(output_filename, **kwargs)
135+
136+
if pyproj.CRS(crs) != pyproj.CRS("epsg:4326"):
137+
logger.info(" Reprojecting additional output to EPSG:4326 (lat lon)")
138+
gdf_4326 = gdf.to_crs("epsg:4326")
139+
getattr(gdf_4326, writer_method)(f"{output_name}_epsg_4326.{extension}", **kwargs)
143140

144141
logger.info("Done.")

tests/test_cli.py

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import os
2+
import logging
3+
4+
import pytest
5+
from click.testing import CliRunner
6+
7+
from osmox import cli
8+
9+
logging.basicConfig(level=logging.INFO)
10+
11+
@pytest.fixture
12+
def fixtures_root():
13+
return os.path.abspath(os.path.join(os.path.dirname(__file__), "fixtures"))
14+
15+
@pytest.fixture
16+
def config_path(fixtures_root):
17+
return os.path.join(fixtures_root, "test_config.json")
18+
19+
@pytest.fixture
20+
def toy_osm_path(fixtures_root):
21+
return os.path.join(fixtures_root, "park.osm")
22+
23+
@pytest.fixture
24+
def runner():
25+
return CliRunner()
26+
27+
def test_cli_with_default_args(runner, config_path, toy_osm_path):
28+
# Test the command with minimal arguments
29+
result = runner.invoke(
30+
cli.run,
31+
[
32+
config_path,
33+
toy_osm_path,
34+
'output_test'
35+
]
36+
)
37+
#print(result.output)
38+
assert result.exit_code == 0
39+
assert "geopackage" in result.exit_code
40+
assert "epsg:4326" in result.exit_code
41+
42+
43+
def test_cli_output_formats(runner, config_path, toy_osm_path):
44+
for output_format in ["geojson", "geopackage", "geoparquet"]:
45+
result = runner.invoke(
46+
cli.run,
47+
[
48+
config_path,
49+
toy_osm_path,
50+
'output_test',
51+
"-f", output_format,
52+
"-crs", "epsg:4326"
53+
]
54+
)
55+
print(result.output)
56+
assert result.exit_code == 0

0 commit comments

Comments
 (0)