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

Add documentation, kwargs for Reader #2236

Merged
merged 5 commits into from
Aug 25, 2023
Merged
Changes from 1 commit
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
38 changes: 28 additions & 10 deletions lib/cartopy/io/shapereader.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,19 @@ def __init__(self, geometry, attributes):

class BasicReader:
"""
Provide an interface for accessing the contents of a shapefile.
Provide an interface for accessing the contents of a shapefile with the
Python Shapefile Library (PyShp). See the PyShp
`Readme <https://pypi.org/project/pyshp/>`_ for more information.

The primary methods used on a BasicReader instance are
:meth:`~cartopy.io.shapereader.BasicReader.records` and
:meth:`~cartopy.io.shapereader.BasicReader.geometries`.

"""

def __init__(self, filename, bbox=None):
def __init__(self, filename, **kwargs):
lgolston marked this conversation as resolved.
Show resolved Hide resolved
# Validate the filename/shapefile
self._reader = reader = shapefile.Reader(filename)
self._reader = reader = shapefile.Reader(filename, **kwargs)
lgolston marked this conversation as resolved.
Show resolved Hide resolved
if reader.shp is None or reader.shx is None or reader.dbf is None:
raise ValueError("Incomplete shapefile definition "
"in '%s'." % filename)
Expand Down Expand Up @@ -178,18 +180,21 @@ def records(self):
class FionaReader:
"""
Provides an interface for accessing the contents of a shapefile
with the fiona library, which has a much faster reader than pyshp.
with the fiona library, which has a much faster reader than PyShp. See
`fiona.open
<https://fiona.readthedocs.io/en/latest/fiona.html#fiona.open>`_
for additional information on supported kwargs.

The primary methods used on a FionaReader instance are
:meth:`~cartopy.io.shapereader.FionaReader.records` and
:meth:`~cartopy.io.shapereader.FionaReader.geometries`.

"""

def __init__(self, filename, bbox=None):
def __init__(self, filename, bbox=None, **kwargs):
self._data = []

with fiona.open(filename) as f:
with fiona.open(filename, **kwargs) as f:
if bbox is not None:
assert len(bbox) == 4
features = f.filter(bbox=bbox)
Expand Down Expand Up @@ -253,10 +258,23 @@ def records(self):
item.items() if key != 'geometry'})


if _HAS_FIONA:
Reader = FionaReader
else:
Reader = BasicReader
def Reader(filename, bbox=None, **kwargs):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason to make this change as well? Now Reader is a function rather than a class.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think Reader being an alias for BasicReader / FionaReader is ideal. Using a function avoids this and also adds the docstring.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think Reader being an alias for BasicReader / FionaReader is ideal.

I agree it isn't ideal. But, I personally would prefer that to a function that looks like a class, I won't block on it, but I also won't approve it either.

Another way of doing this would have been to create a class Reader that stores an instance of self._reader = FionaReader() if _HAS_FIONA else BasicReader() and then dispatches out the methods to whatever self._reader it contains, but that seems like more work than is necessary as well unless we start getting more readers.

Also, I think you can add a docstring right below the Reader definition like we do for the configuration dictionary:

config = {'pre_existing_data_dir': Path(os.environ.get('CARTOPY_DATA_DIR',
'')),
'data_dir': _data_dir,
'cache_dir': _cache_dir,
'repo_data_dir': Path(__file__).parent / 'data',
'downloaders': {},
}
"""
The config dictionary stores global configuration values for cartopy.

but maybe to get that to work you'd need to put it all on one line?

Reader = FionaReader if _HAS_FIONA else BasicReader
"""Explanation of Reader"""

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the tip on adding a docstring to the existing Reader line! I like that solution, which has now been made.

"""
Returns an instance of the default available shapereader interface.

Will either be :class:`~cartopy.io.shapereader.FionaReader` (if fiona
installed) or :class:`~cartopy.io.shapereader.BasicReader`
(based on PyShp). Note that FionaReader has greater speed and additional
functionality, including attempting to auto-detect source encoding and
support for bounding box filtering. Both libraries support the 'encoding'
keyword argument. Note that BasicReader and FionaReader instances can also
be created directly.

"""
if _HAS_FIONA:
return FionaReader(filename, bbox, **kwargs)
else:
return BasicReader(filename, **kwargs)


def natural_earth(resolution='110m', category='physical', name='coastline'):
Expand Down