Skip to content
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

Fix bounds error in shapereader for null records #2188

Merged
merged 2 commits into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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