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 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
40 changes: 28 additions & 12 deletions lib/cartopy/io/shapereader.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,20 @@ 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, bbox=None, **kwargs):
# 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
self._bbox = bbox
if reader.shp is None or reader.shx is None or reader.dbf is None:
raise ValueError("Incomplete shapefile definition "
"in '%s'." % filename)
Expand All @@ -158,7 +161,7 @@ def geometries(self):
:meth:`~Record.geometry` method.

"""
for shape in self._reader.iterShapes():
for shape in self._reader.iterShapes(bbox=self._bbox):
# Skip the shape that can not be represented as geometry.
if shape.shapeType != shapefile.NULL:
yield sgeom.shape(shape)
Expand All @@ -168,28 +171,32 @@ def records(self):
Return an iterator of :class:`~Record` instances.

"""

# Ignore the "DeletionFlag" field which always comes first
fields = self._reader.fields[1:]
for shape_record in self._reader.iterShapeRecords():
for shape_record in self._reader.iterShapeRecords(bbox=self._bbox):
attributes = shape_record.record.as_dict()
yield Record(shape_record.shape, attributes, fields)


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 +260,19 @@ def records(self):
item.items() if key != 'geometry'})


if _HAS_FIONA:
Reader = FionaReader
else:
Reader = BasicReader
Reader = FionaReader if _HAS_FIONA else BasicReader
"""
Alias 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 different format drivers. Both libraries support the 'encoding'
and 'bbox' keyword arguments. If specific functionality is needed,
BasicReader and FionaReader instances can also be created directly.

"""


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