Skip to content

Commit

Permalink
Merge pull request #2188 from lgolston/fiona-record-bug
Browse files Browse the repository at this point in the history
Fix bounds error in shapereader for null records
  • Loading branch information
dopplershift authored Jun 13, 2023
2 parents 3ebd4a4 + 7982056 commit 2abf4fd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
8 changes: 6 additions & 2 deletions lib/cartopy/io/shapereader.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def bounds(self):
The bounds of this Record's :meth:`~Record.geometry`.
"""
if self._bounds is None:
if self._bounds is None and self.geometry is not None:
self._bounds = self.geometry.bounds
return self._bounds

Expand Down Expand Up @@ -114,7 +114,11 @@ class FionaRecord(Record):
def __init__(self, geometry, attributes):
self._geometry = geometry
self.attributes = attributes
self._bounds = geometry.bounds

if geometry is not None:
self._bounds = geometry.bounds
else:
self._bounds = None


class BasicReader:
Expand Down
34 changes: 21 additions & 13 deletions lib/cartopy/tests/test_shapereader.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,17 @@


class TestLakes:
def setup_class(self):
@pytest.fixture(autouse=True, params=[0, 1])
def setup_class(self, request):
LAKES_PATH = (Path(__file__).parent / 'lakes_shapefile'
/ 'ne_110m_lakes.shp')
self.reader = shp.Reader(LAKES_PATH)
# run tests with both available Readers
if request.param == 0:
self.reader = shp.BasicReader(LAKES_PATH)
elif not shp._HAS_FIONA:
pytest.skip("Fiona library not available")
else:
self.reader = shp.FionaReader(LAKES_PATH)
names = [record.attributes['name'] for record in self.reader.records()]
# Choose a nice small lake
self.lake_name = 'Lago de\rNicaragua'
Expand Down Expand Up @@ -56,18 +63,19 @@ def test_record(self):
assert actual == expected
assert lake_record.geometry == self.test_lake_geometry

@pytest.mark.skipif(shp._HAS_FIONA,
reason="Fiona reader doesn't support lazy loading.")
def test_bounds(self):
# tests that a file which has a record with a bbox can
# use the bbox without first creating the geometry
record = next(self.reader.records())
assert not record._geometry, \
'The geometry was loaded before it was needed.'
assert len(record._bounds) == 4
assert record._bounds == record.bounds
assert not record._geometry, \
'The geometry was loaded in order to create the bounds.'
if isinstance(self.reader, shp.BasicReader):
# tests that a file which has a record with a bbox can
# use the bbox without first creating the geometry
record = next(self.reader.records())
assert not record._geometry, \
'The geometry was loaded before it was needed.'
assert len(record._bounds) == 4
assert record._bounds == record.bounds
assert not record._geometry, \
'The geometry was loaded in order to create the bounds.'
else:
pytest.skip("Fiona reader doesn't support lazy loading")


@pytest.mark.filterwarnings("ignore:Downloading")
Expand Down

0 comments on commit 2abf4fd

Please sign in to comment.