Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""Convert masked array data into a proper array."""
# input is ma.masked_array
data = ma.empty((1, 1, 1))
# output ndarray
output = prepare_array(data, masked=False)
assert isinstance(output, np.ndarray)
assert not isinstance(output, ma.masked_array)
assert output.shape == (1, 1, 1)
# output masked array
output = prepare_array(data)
assert isinstance(output, ma.masked_array)
assert output.shape == (1, 1, 1)
# input is ma.masked_array with full mask
data = ma.masked_array(data=np.ones((1, 1, 1)), mask=np.ones((1, 1, 1)))
# output ndarray
output = prepare_array(data, masked=False)
assert isinstance(output, np.ndarray)
assert not isinstance(output, ma.masked_array)
assert output.shape == (1, 1, 1)
# output masked array
output = prepare_array(data)
assert isinstance(output, ma.masked_array)
assert output.shape == (1, 1, 1)
def test_prepare_array_errors():
"""Convert ndarray data into a proper array."""
# input is iterable
data = [None]
try:
prepare_array(data)
raise Exception()
except ValueError:
pass
# input is not array
data = 5
try:
prepare_array(data)
raise Exception()
except ValueError:
pass
def test_prepare_array_errors():
"""Convert ndarray data into a proper array."""
# input is iterable
data = [None]
try:
prepare_array(data)
raise Exception()
except ValueError:
pass
# input is not array
data = 5
try:
prepare_array(data)
raise Exception()
except ValueError:
pass
assert isinstance(output, np.ndarray)
assert not isinstance(output, ma.masked_array)
assert output.shape == (1, 1, 1)
# output masked array
output = prepare_array(data)
assert isinstance(output, ma.masked_array)
assert output.shape == (1, 1, 1)
# input is ma.masked_array with full mask
data = ma.masked_array(data=np.ones((1, 1, 1)), mask=np.ones((1, 1, 1)))
# output ndarray
output = prepare_array(data, masked=False)
assert isinstance(output, np.ndarray)
assert not isinstance(output, ma.masked_array)
assert output.shape == (1, 1, 1)
# output masked array
output = prepare_array(data)
assert isinstance(output, ma.masked_array)
assert output.shape == (1, 1, 1)
def test_prepare_array_maskedarrays():
"""Convert masked array data into a proper array."""
# input is ma.masked_array
data = ma.empty((1, 1, 1))
# output ndarray
output = prepare_array(data, masked=False)
assert isinstance(output, np.ndarray)
assert not isinstance(output, ma.masked_array)
assert output.shape == (1, 1, 1)
# output masked array
output = prepare_array(data)
assert isinstance(output, ma.masked_array)
assert output.shape == (1, 1, 1)
# input is ma.masked_array with full mask
data = ma.masked_array(data=np.ones((1, 1, 1)), mask=np.ones((1, 1, 1)))
# output ndarray
output = prepare_array(data, masked=False)
assert isinstance(output, np.ndarray)
assert not isinstance(output, ma.masked_array)
assert output.shape == (1, 1, 1)
# output masked array
output = prepare_array(data)
assert isinstance(output, ma.masked_array)
assert output.shape == (1, 1, 1)
def _extract(self, in_tile=None, in_data=None, out_tile=None):
"""Extract data from tile."""
if self.config.output.METADATA["data_type"] == "raster":
return raster.extract_from_array(
in_raster=raster.prepare_array(
in_data, nodata=self.config.output.nodata,
dtype=self.config.output.output_params["dtype"]
),
in_affine=in_tile.affine,
out_tile=out_tile
)
elif self.config.output.METADATA["data_type"] == "vector":
return [
feature
for feature in in_data
if shape(feature["geometry"]).intersects(out_tile.bbox)
]
def _prepare_array(self, data):
data = prepare_array(-(data - 255), dtype="uint8", masked=False, nodata=0)[0]
zeros = np.zeros(data.shape)
if self.old_band_num:
data = np.stack([zeros, zeros, zeros, data])
else:
data = np.stack([zeros, data])
return prepare_array(data, dtype="uint8", masked=True, nodata=255)
def extract_subset(self, input_data_tiles=None, out_tile=None):
"""
Extract subset from multiple tiles.
input_data_tiles : list of (``Tile``, process data) tuples
out_tile : ``Tile``
Returns
-------
NumPy array or list of features.
"""
if self.METADATA["data_type"] == "raster":
mosaic = create_mosaic(input_data_tiles)
return extract_from_array(
in_raster=prepare_array(
mosaic.data,
nodata=self.output_params["nodata"],
dtype=self.output_params["dtype"]
),
in_affine=mosaic.affine,
out_tile=out_tile
)
elif self.METADATA["data_type"] == "vector":
return [
feature for feature in list(
chain.from_iterable([features for _, features in input_data_tiles])
)
if shape(feature["geometry"]).intersects(out_tile.bbox)
]
def _prepare_array(self, data):
data = prepare_array(-(data - 255), dtype="uint8", masked=False, nodata=0)[0]
zeros = np.zeros(data.shape)
if self.old_band_num:
data = np.stack([zeros, zeros, zeros, data])
else:
data = np.stack([zeros, data])
return prepare_array(data, dtype="uint8", masked=True, nodata=255)
def _prepare_array_for_png(self, data):
data = prepare_array(data, dtype=np.uint8)
# Create 3D NumPy array with alpha channel.
if len(data) == 1:
rgba = np.stack((
data[0], data[0], data[0],
np.where(
data[0].data == self.output_params["nodata"], 0, 255)
.astype("uint8")
))
elif len(data) == 2:
rgba = np.stack((data[0], data[0], data[0], data[1]))
elif len(data) == 3:
rgba = np.stack((
data[0], data[1], data[2], np.where(
data[0].data == self.output_params["nodata"], 0, 255
).astype("uint8", copy=False)
))