Skip to content

Commit

Permalink
Merge pull request #471 from ungarj/handle_empty_vector_file
Browse files Browse the repository at this point in the history
enable handling empty vector files as input
  • Loading branch information
ungarj committed Jun 17, 2022
2 parents 663206d + ed96616 commit e511537
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 3 deletions.
2 changes: 1 addition & 1 deletion mapchete/commands/_convert.py
Expand Up @@ -421,7 +421,7 @@ def _input_fiona_info(inp):
crs=src.crs,
zoom_levels=None,
input_type="vector",
bounds=src.bounds,
bounds=src.bounds if len(src) else None,
)


Expand Down
7 changes: 5 additions & 2 deletions mapchete/formats/default/vector_file.py
Expand Up @@ -7,7 +7,7 @@
from cached_property import cached_property
import fiona
import logging
from shapely.geometry import box
from shapely.geometry import box, Point
from rasterio.crs import CRS

from mapchete.formats import base
Expand Down Expand Up @@ -173,8 +173,11 @@ def bbox(self, out_crs=None):
out_crs = self.pyramid.crs if out_crs is None else out_crs
if self._bbox_cache is None:
with fiona.open(self.path) as inp:
self._bbox_cache = CRS(inp.crs), tuple(inp.bounds)
self._bbox_cache = CRS(inp.crs), tuple(inp.bounds) if len(inp) else None
inp_crs, bounds = self._bbox_cache
if bounds is None:
# this creates an empty GeometryCollection object
return Point()
# TODO find a way to get a good segmentize value in bbox source CRS
return reproject_geometry(
box(*bounds), src_crs=inp_crs, dst_crs=out_crs, clip_to_crs_bounds=False
Expand Down
6 changes: 6 additions & 0 deletions test/conftest.py
Expand Up @@ -121,6 +121,12 @@ def s2_band_remote():
return "s3://mapchete-test/4band_test.tif"


@pytest.fixture
def empty_gpkg():
"""Fixture for HTTP raster."""
return os.path.join(TESTDATA_DIR, "empty.gpkg")


@pytest.fixture
def s3_metadata_json():
"""
Expand Down
10 changes: 10 additions & 0 deletions test/test_commands.py
Expand Up @@ -738,3 +738,13 @@ def test_index_text(cleantopo_br):
def test_index_errors(mp_tmpdir, cleantopo_br):
with pytest.raises(ValueError):
index(cleantopo_br.dict, zoom=5)


def test_convert_empty_gpkg(empty_gpkg, mp_tmpdir):
convert(
empty_gpkg,
mp_tmpdir,
output_pyramid="geodetic",
zoom=5,
output_format="GeoJSON",
)
Binary file added test/testdata/empty.gpkg
Binary file not shown.

0 comments on commit e511537

Please sign in to comment.