Skip to content

Commit

Permalink
CLN: InMemoryRaster initialize values with cinit & other cython fixes (
Browse files Browse the repository at this point in the history
…#1964)

Co-authored-by: Sean Gillies <sean@mapbox.com>
  • Loading branch information
snowman2 and Sean Gillies committed Dec 2, 2020
1 parent a5da8c4 commit 669d86b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 16 deletions.
24 changes: 14 additions & 10 deletions rasterio/_fill.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,27 @@ def _fillnodata(image, mask, double max_search_distance=100.0,
cdef GDALRasterBandH image_band = NULL
cdef GDALRasterBandH mask_band = NULL
cdef char **alg_options = NULL
cdef InMemoryRaster image_dataset = None
cdef InMemoryRaster mask_dataset = None

# copy numpy ndarray into an in-memory dataset.
image_dataset = InMemoryRaster(image)
image_band = image_dataset.band(1)
try:
# copy numpy ndarray into an in-memory dataset.
image_dataset = InMemoryRaster(image)
image_band = image_dataset.band(1)

if mask is not None:
mask_cast = mask.astype('uint8')
mask_dataset = InMemoryRaster(mask_cast)
mask_band = mask_dataset.band(1)
if mask is not None:
mask_cast = mask.astype('uint8')
mask_dataset = InMemoryRaster(mask_cast)
mask_band = mask_dataset.band(1)

try:
alg_options = CSLSetNameValue(alg_options, "TEMP_FILE_DRIVER", "MEM")
exc_wrap_int(
GDALFillNodata(image_band, mask_band, max_search_distance, 0,
smoothing_iterations, alg_options, NULL, NULL))
return image_dataset.read()
finally:
image_dataset.close()
mask_dataset.close()
if image_dataset is not None:
image_dataset.close()
if mask_dataset is not None:
mask_dataset.close()
CSLDestroy(alg_options)
16 changes: 12 additions & 4 deletions rasterio/_io.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1685,7 +1685,14 @@ cdef class InMemoryRaster:
This class is only intended for internal use within rasterio to support
IO with GDAL. Other memory based operations should use numpy arrays.
"""
def __cinit__(self, image=None, dtype='uint8', count=1, width=None,
def __cinit__(self):
self._hds = NULL
self.band_ids = NULL
self._image = None
self.crs = None
self.transform = None

def __init__(self, image=None, dtype='uint8', count=1, width=None,
height=None, transform=None, gcps=None, rpcs=None, crs=None):
"""
Create in-memory raster dataset, and fill its bands with the
Expand Down Expand Up @@ -1794,7 +1801,6 @@ cdef class InMemoryRaster:
if options != NULL:
CSLDestroy(options)

self._image = None
if image is not None:
self.write(image)

Expand All @@ -1805,7 +1811,9 @@ cdef class InMemoryRaster:
self.close()

def __dealloc__(self):
CPLFree(self.band_ids)
if self.band_ids != NULL:
CPLFree(self.band_ids)
self.band_ids = NULL

cdef GDALDatasetH handle(self) except NULL:
"""Return the object's GDAL dataset handle"""
Expand All @@ -1829,7 +1837,7 @@ cdef class InMemoryRaster:
def close(self):
if self._hds != NULL:
GDALClose(self._hds)
self._hds = NULL
self._hds = NULL

def read(self):

Expand Down
4 changes: 2 additions & 2 deletions rasterio/_warp.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,8 @@ def _reproject(
in_transform = in_transform.translation(eps, eps)
return in_transform

mem_raster = None
src_mem = None
cdef InMemoryRaster mem_raster = None
cdef InMemoryRaster src_mem = None

try:

Expand Down

0 comments on commit 669d86b

Please sign in to comment.