Skip to content

Commit

Permalink
Merge efceb5b into 2b5f319
Browse files Browse the repository at this point in the history
  • Loading branch information
sgillies committed Sep 13, 2019
2 parents 2b5f319 + efceb5b commit e2d9564
Show file tree
Hide file tree
Showing 19 changed files with 374 additions and 303 deletions.
8 changes: 4 additions & 4 deletions rasterio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def emit(self, record):

@ensure_env_with_credentials
def open(fp, mode='r', driver=None, width=None, height=None, count=None,
crs=None, transform=None, dtype=None, nodata=None, sharing=True,
crs=None, transform=None, dtype=None, nodata=None, sharing=False,
**kwargs):
"""Open a dataset for reading or writing.
Expand Down Expand Up @@ -110,9 +110,9 @@ def open(fp, mode='r', driver=None, width=None, height=None, count=None,
sharing : bool; optional
To reduce overhead and prevent programs from running out of file
descriptors, rasterio maintains a pool of shared low level
dataset handles. When `True` (the default), this function will
use a shared handle if one is available. Multithreaded programs
must avoid sharing and should set *sharing* to `False`.
dataset handles. When `True` this function will use a shared
handle if one is available. Multithreaded programs must avoid
sharing and should set *sharing* to `False`.
kwargs : optional
These are passed to format drivers as directives for creating or
interpreting datasets. For example: in 'w' or 'w+' modes
Expand Down
7 changes: 4 additions & 3 deletions rasterio/_base.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def get_dataset_driver(path):
path = path.encode('utf-8')

try:
dataset = exc_wrap_pointer(GDALOpenShared(<const char *>path, <GDALAccess>0))
dataset = exc_wrap_pointer(GDALOpen(<const char *>path, <GDALAccess>0))
driver = GDALGetDatasetDriver(dataset)
drivername = get_driver_name(driver)

Expand Down Expand Up @@ -174,7 +174,7 @@ cdef class DatasetBase(object):
Photometric interpretation's short name
"""

def __init__(self, path=None, driver=None, sharing=True, **kwargs):
def __init__(self, path=None, driver=None, sharing=False, **kwargs):
"""Construct a new dataset
Parameters
Expand All @@ -185,7 +185,7 @@ cdef class DatasetBase(object):
A single driver name or a list of driver names to consider when
opening the dataset.
sharing : bool, optional
Whether to share underlying GDAL dataset handles (default: True).
Whether to share underlying GDAL dataset handles (default: False).
kwargs : dict
GDAL dataset opening options.
Expand Down Expand Up @@ -314,6 +314,7 @@ cdef class DatasetBase(object):
if self._hds != NULL:
GDALClose(self._hds)
self._hds = NULL
self._closed = True

def close(self):
self.stop()
Expand Down
224 changes: 117 additions & 107 deletions rasterio/_features.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -73,74 +73,80 @@ def _shapes(image, mask, connectivity, transform):
if connectivity not in (4, 8):
raise ValueError("Connectivity Option must be 4 or 8")

if dtypes.is_ndarray(image):
mem_ds = InMemoryRaster(image=image, transform=transform)
band = mem_ds.band(1)
elif isinstance(image, tuple):
rdr = image.ds
band = (<DatasetReaderBase?>rdr).band(image.bidx)
else:
raise ValueError("Invalid source image")

if mask is not None:
if mask.shape != image.shape:
raise ValueError("Mask must have same shape as image")

if np.dtype(mask.dtype).name not in ('bool', 'uint8'):
raise ValueError("Mask must be dtype rasterio.bool_ or "
"rasterio.uint8")

if dtypes.is_ndarray(mask):
# A boolean mask must be converted to uint8 for GDAL
mask_ds = InMemoryRaster(image=mask.astype('uint8'),
transform=transform)
maskband = mask_ds.band(1)
elif isinstance(mask, tuple):
mrdr = mask.ds
maskband = (<DatasetReaderBase?>mrdr).band(mask.bidx)

# Create an in-memory feature store.
driver = OGRGetDriverByName("Memory")
if driver == NULL:
raise ValueError("NULL driver")
fs = OGR_Dr_CreateDataSource(driver, "temp", NULL)
if fs == NULL:
raise ValueError("NULL feature dataset")

# And a layer.
layer = OGR_DS_CreateLayer(fs, "polygons", NULL, 3, NULL)
if layer == NULL:
raise ValueError("NULL layer")

fielddefn = OGR_Fld_Create("image_value", fieldtp)
if fielddefn == NULL:
raise ValueError("NULL field definition")
OGR_L_CreateField(layer, fielddefn, 1)
OGR_Fld_Destroy(fielddefn)

if connectivity == 8:
options = CSLSetNameValue(options, "8CONNECTED", "8")

if is_float:
GDALFPolygonize(band, maskband, layer, 0, options, NULL, NULL)
else:
GDALPolygonize(band, maskband, layer, 0, options, NULL, NULL)
try:

if dtypes.is_ndarray(image):
mem_ds = InMemoryRaster(image=image, transform=transform)
band = mem_ds.band(1)
elif isinstance(image, tuple):
rdr = image.ds
band = (<DatasetReaderBase?>rdr).band(image.bidx)
else:
raise ValueError("Invalid source image")

if mask is not None:
if mask.shape != image.shape:
raise ValueError("Mask must have same shape as image")

if np.dtype(mask.dtype).name not in ('bool', 'uint8'):
raise ValueError("Mask must be dtype rasterio.bool_ or "
"rasterio.uint8")

if dtypes.is_ndarray(mask):
# A boolean mask must be converted to uint8 for GDAL
mask_ds = InMemoryRaster(image=mask.astype('uint8'),
transform=transform)
maskband = mask_ds.band(1)
elif isinstance(mask, tuple):
mrdr = mask.ds
maskband = (<DatasetReaderBase?>mrdr).band(mask.bidx)

# Create an in-memory feature store.
driver = OGRGetDriverByName("Memory")
if driver == NULL:
raise ValueError("NULL driver")
fs = OGR_Dr_CreateDataSource(driver, "temp", NULL)
if fs == NULL:
raise ValueError("NULL feature dataset")

# And a layer.
layer = OGR_DS_CreateLayer(fs, "polygons", NULL, 3, NULL)
if layer == NULL:
raise ValueError("NULL layer")

fielddefn = OGR_Fld_Create("image_value", fieldtp)
if fielddefn == NULL:
raise ValueError("NULL field definition")
OGR_L_CreateField(layer, fielddefn, 1)
OGR_Fld_Destroy(fielddefn)

if connectivity == 8:
options = CSLSetNameValue(options, "8CONNECTED", "8")

if is_float:
GDALFPolygonize(band, maskband, layer, 0, options, NULL, NULL)
else:
GDALPolygonize(band, maskband, layer, 0, options, NULL, NULL)

finally:
if mem_ds is not None:
mem_ds.close()
if mask_ds is not None:
mask_ds.close()
if options:
CSLDestroy(options)

# Yield Fiona-style features
shape_iter = ShapeIterator()
shape_iter.layer = layer
shape_iter.fieldtype = fieldtp
for s, v in shape_iter:
yield s, v
try:
# Yield Fiona-style features
shape_iter = ShapeIterator()
shape_iter.layer = layer
shape_iter.fieldtype = fieldtp
for s, v in shape_iter:
yield s, v

if mem_ds is not None:
mem_ds.close()
if mask_ds is not None:
mask_ds.close()
if fs != NULL:
OGR_DS_Destroy(fs)
if options:
CSLDestroy(options)
finally:
if fs != NULL:
OGR_DS_Destroy(fs)


def _sieve(image, size, out, mask, connectivity):
Expand Down Expand Up @@ -199,54 +205,58 @@ def _sieve(image, size, out, mask, connectivity):
if np.dtype(image.dtype).name != np.dtype(out.dtype).name:
raise ValueError('out raster must match dtype of image')

if dtypes.is_ndarray(image):
in_mem_ds = InMemoryRaster(image=image)
in_band = in_mem_ds.band(1)
elif isinstance(image, tuple):
rdr = image.ds
in_band = (<DatasetReaderBase?>rdr).band(image.bidx)
else:
raise ValueError("Invalid source image")

if dtypes.is_ndarray(out):
log.debug("out array: %r", out)
out_mem_ds = InMemoryRaster(image=out)
out_band = out_mem_ds.band(1)
elif isinstance(out, tuple):
udr = out.ds
out_band = (<DatasetReaderBase?>udr).band(out.bidx)
else:
raise ValueError("Invalid out image")
try:

if mask is not None:
if mask.shape != image.shape:
raise ValueError("Mask must have same shape as image")
if dtypes.is_ndarray(image):
in_mem_ds = InMemoryRaster(image=image)
in_band = in_mem_ds.band(1)
elif isinstance(image, tuple):
rdr = image.ds
in_band = (<DatasetReaderBase?>rdr).band(image.bidx)
else:
raise ValueError("Invalid source image")

if dtypes.is_ndarray(out):
log.debug("out array: %r", out)
out_mem_ds = InMemoryRaster(image=out)
out_band = out_mem_ds.band(1)
elif isinstance(out, tuple):
udr = out.ds
out_band = (<DatasetReaderBase?>udr).band(out.bidx)
else:
raise ValueError("Invalid out image")

if np.dtype(mask.dtype) not in ('bool', 'uint8'):
raise ValueError("Mask must be dtype rasterio.bool_ or "
"rasterio.uint8")
if mask is not None:
if mask.shape != image.shape:
raise ValueError("Mask must have same shape as image")

if dtypes.is_ndarray(mask):
# A boolean mask must be converted to uint8 for GDAL
mask_mem_ds = InMemoryRaster(image=mask.astype('uint8'))
mask_band = mask_mem_ds.band(1)
if np.dtype(mask.dtype) not in ('bool', 'uint8'):
raise ValueError("Mask must be dtype rasterio.bool_ or "
"rasterio.uint8")

elif isinstance(mask, tuple):
mask_reader = mask.ds
mask_band = (<DatasetReaderBase?>mask_reader).band(mask.bidx)
if dtypes.is_ndarray(mask):
# A boolean mask must be converted to uint8 for GDAL
mask_mem_ds = InMemoryRaster(image=mask.astype('uint8'))
mask_band = mask_mem_ds.band(1)

GDALSieveFilter(in_band, mask_band, out_band, size, connectivity,
NULL, NULL, NULL)
elif isinstance(mask, tuple):
mask_reader = mask.ds
mask_band = (<DatasetReaderBase?>mask_reader).band(mask.bidx)

# Read from out_band into out
io_auto(out, out_band, False)
GDALSieveFilter(in_band, mask_band, out_band, size, connectivity,
NULL, NULL, NULL)

if in_mem_ds is not None:
in_mem_ds.close()
if out_mem_ds is not None:
out_mem_ds.close()
if mask_mem_ds is not None:
mask_mem_ds.close()
else:
# Read from out_band into out
io_auto(out, out_band, False)

finally:
if in_mem_ds is not None:
in_mem_ds.close()
if out_mem_ds is not None:
out_mem_ds.close()
if mask_mem_ds is not None:
mask_mem_ds.close()


def _rasterize(shapes, image, transform, all_touched, merge_alg):
Expand Down
7 changes: 0 additions & 7 deletions rasterio/_io.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,6 @@ cdef class BufferedDatasetWriterBase(DatasetWriterBase):
pass


cdef class WarpedVRTReaderBase(DatasetReaderBase):
cdef GDALDatasetH _hds_source
cdef object dst_crs
cdef object resampling
cdef object tolerance


cdef class InMemoryRaster:
cdef GDALDatasetH _hds
cdef double gdal_transform[6]
Expand Down
Loading

0 comments on commit e2d9564

Please sign in to comment.