def shapes(image, mask=None, connectivity=4, transform=IDENTITY): """Yields a (shape, image_value) pair for each feature in the image. The shapes are GeoJSON-like dicts and the image values are ints. Features are found using a connected-component labeling algorithm. The image must be of unsigned 8-bit integer (rasterio.byte or numpy.uint8) data type. If a mask is provided, pixels for which the mask is `False` will be excluded from feature generation. """ if np.dtype(image.dtype) != np.dtype(rasterio.ubyte): raise ValueError("Image must be dtype uint8/ubyte") if mask is not None and np.dtype(mask.dtype) != np.dtype(rasterio.bool_): raise ValueError("Mask must be dtype rasterio.bool_") if connectivity not in (4, 8): raise ValueError("Connectivity Option must be 4 or 8") transform = guard_transform(transform) with rasterio.drivers(): for s, v in _shapes(image, mask, connectivity, transform.to_gdal()): yield s, v
def shapes(image, mask=None, connectivity=4, transform=IDENTITY): """Yields a (shape, image_value) pair for each feature in the image. The shapes are GeoJSON-like dicts and the image values are ints or floats depending on the data type of the image. Features are found using a connected-component labeling algorithm. The image must be one of int16, int32, uint8, uint16, float32 data types. Note: due to floating point precision issues, the floating point values returned from a floating point image may not exactly match the original values. If a mask is provided, pixels for which the mask is `False` will be excluded from feature generation. """ valid_dtypes = ('int16', 'int32', 'uint8', 'uint16', 'float32') if np.dtype(image.dtype).name not in valid_dtypes: raise ValueError('image dtype must be one of: %s' % (', '.join(valid_dtypes))) if mask is not None and np.dtype(mask.dtype) != np.dtype(rasterio.bool_): raise ValueError("Mask must be dtype rasterio.bool_") if connectivity not in (4, 8): raise ValueError("Connectivity Option must be 4 or 8") transform = guard_transform(transform) with rasterio.drivers(): for s, v in _shapes(image, mask, connectivity, transform.to_gdal()): yield s, v
def shapes(image, mask=None, connectivity=4, transform=IDENTITY): """ Return a generator of (polygon, value) for each each set of adjacent pixels of the same value. Parameters ---------- image : numpy ndarray or rasterio Band object (RasterReader, bidx namedtuple). Data type must be one of rasterio.int16, rasterio.int32, rasterio.uint8, rasterio.uint16, or rasterio.float32. mask : numpy ndarray or rasterio Band object, optional Values of False or 0 will be excluded from feature generation Must evaluate to bool (rasterio.bool_ or rasterio.uint8) connectivity : int, optional Use 4 or 8 pixel connectivity for grouping pixels into features transform : Affine transformation, optional If not provided, feature coordinates will be generated based on pixel coordinates Returns ------- Generator of (polygon, value) Yields a pair of (polygon, value) for each feature found in the image. Polygons are GeoJSON-like dicts and the values are the associated value from the image, in the data type of the image. Note: due to floating point precision issues, values returned from a floating point image may not exactly match the original values. Notes ----- The amount of memory used by this algorithm is proportional to the number and complexity of polygons produced. This algorithm is most appropriate for simple thematic data. Data with high pixel-to-pixel variability, such as imagery, may produce one polygon per pixel and consume large amounts of memory. """ valid_dtypes = ('int16', 'int32', 'uint8', 'uint16', 'float32') if np.dtype(image.dtype).name not in valid_dtypes: raise ValueError('image dtype must be one of: %s' % (', '.join(valid_dtypes))) if mask is not None and np.dtype(mask.dtype).name not in ('bool', 'uint8'): raise ValueError("Mask must be dtype rasterio.bool_ or rasterio.uint8") if connectivity not in (4, 8): raise ValueError("Connectivity Option must be 4 or 8") transform = guard_transform(transform) with rasterio.drivers(): for s, v in _shapes(image, mask, connectivity, transform.to_gdal()): yield s, v
def shapes(source, mask=None, connectivity=4, transform=IDENTITY): """Get shapes and values of connected regions in a dataset or array. Parameters ---------- source : array, dataset object, Band, or tuple(dataset, bidx) Data type must be one of rasterio.int16, rasterio.int32, rasterio.uint8, rasterio.uint16, or rasterio.float32. mask : numpy ndarray or rasterio Band object, optional Must evaluate to bool (rasterio.bool_ or rasterio.uint8). Values of False or 0 will be excluded from feature generation. Note well that this is the inverse sense from Numpy's, where a mask value of True indicates invalid data in an array. If `source` is a Numpy masked array and `mask` is None, the source's mask will be inverted and used in place of `mask`. connectivity : int, optional Use 4 or 8 pixel connectivity for grouping pixels into features transform : Affine transformation, optional If not provided, feature coordinates will be generated based on pixel coordinates Yields ------- polygon, value A pair of (polygon, value) for each feature found in the image. Polygons are GeoJSON-like dicts and the values are the associated value from the image, in the data type of the image. Note: due to floating point precision issues, values returned from a floating point image may not exactly match the original values. Notes ----- The amount of memory used by this algorithm is proportional to the number and complexity of polygons produced. This algorithm is most appropriate for simple thematic data. Data with high pixel-to-pixel variability, such as imagery, may produce one polygon per pixel and consume large amounts of memory. Because the low-level implementation uses either an int32 or float32 buffer, uint32 and float64 data cannot be operated on without truncation issues. """ if hasattr(source, 'mask') and mask is None: mask = ~source.mask source = source.data transform = guard_transform(transform) for s, v in _shapes(source, mask, connectivity, transform): yield s, v
def shapes(source, mask=None, connectivity=4, transform=IDENTITY): """Yield (polygon, value for each set of adjacent pixels of the same value. Parameters ---------- source : array or dataset object opened in 'r' mode or Band or tuple(dataset, bidx) Data type must be one of rasterio.int16, rasterio.int32, rasterio.uint8, rasterio.uint16, or rasterio.float32. mask : numpy ndarray or rasterio Band object, optional Must evaluate to bool (rasterio.bool_ or rasterio.uint8). Values of False or 0 will be excluded from feature generation. Note well that this is the inverse sense from Numpy's, where a mask value of True indicates invalid data in an array. If `source` is a Numpy masked array and `mask` is None, the source's mask will be inverted and used in place of `mask`. connectivity : int, optional Use 4 or 8 pixel connectivity for grouping pixels into features transform : Affine transformation, optional If not provided, feature coordinates will be generated based on pixel coordinates Yields ------- tuple A pair of (polygon, value) for each feature found in the image. Polygons are GeoJSON-like dicts and the values are the associated value from the image, in the data type of the image. Note: due to floating point precision issues, values returned from a floating point image may not exactly match the original values. Notes ----- The amount of memory used by this algorithm is proportional to the number and complexity of polygons produced. This algorithm is most appropriate for simple thematic data. Data with high pixel-to-pixel variability, such as imagery, may produce one polygon per pixel and consume large amounts of memory. """ if hasattr(source, 'mask') and mask is None: mask = ~source.mask source = source.data transform = guard_transform(transform) for s, v in _shapes(source, mask, connectivity, transform): yield s, v
def shapes(image, mask=None, connectivity=4, transform=None): """Yields a (shape, image_value) pair for each feature in the image. The shapes are GeoJSON-like dicts and the image values are ints. Features are found using a connected-component labeling algorithm. The image must be of unsigned 8-bit integer (rasterio.byte or numpy.uint8) data type. If a mask is provided, pixels for which the mask is `True` will be excluded from feature generation. """ if image.dtype.type != rasterio.ubyte: raise ValueError("Image must be dtype uint8/ubyte") if mask is not None and mask.dtype.type != rasterio.bool_: raise ValueError("Mask must be dtype rasterio.bool_") with rasterio.drivers(): for s, v in _shapes(image, mask, transform): yield s, v
def shapes(image, mask=None, connectivity=4, transform=IDENTITY): """Yield (polygon, value for each set of adjacent pixels of the same value. Parameters ---------- image : numpy ndarray or rasterio Band object (RasterReader, bidx namedtuple). Data type must be one of rasterio.int16, rasterio.int32, rasterio.uint8, rasterio.uint16, or rasterio.float32. mask : numpy ndarray or rasterio Band object, optional Values of False or 0 will be excluded from feature generation Must evaluate to bool (rasterio.bool_ or rasterio.uint8) connectivity : int, optional Use 4 or 8 pixel connectivity for grouping pixels into features transform : Affine transformation, optional If not provided, feature coordinates will be generated based on pixel coordinates Yields ------- tuple A pair of (polygon, value) for each feature found in the image. Polygons are GeoJSON-like dicts and the values are the associated value from the image, in the data type of the image. Note: due to floating point precision issues, values returned from a floating point image may not exactly match the original values. Notes ----- The amount of memory used by this algorithm is proportional to the number and complexity of polygons produced. This algorithm is most appropriate for simple thematic data. Data with high pixel-to-pixel variability, such as imagery, may produce one polygon per pixel and consume large amounts of memory. """ transform = guard_transform(transform) rasterio.env.setenv() for s, v in _shapes(image, mask, connectivity, transform.to_gdal()): yield s, v
def shapes(image, mask=None, connectivity=4, transform=IDENTITY): """Yield (polygon, value for each set of adjacent pixels of the same value. Parameters ---------- image : numpy ndarray or rasterio Band object (RasterReader, bidx namedtuple). Data type must be one of rasterio.int16, rasterio.int32, rasterio.uint8, rasterio.uint16, or rasterio.float32. mask : numpy ndarray or rasterio Band object, optional Values of False or 0 will be excluded from feature generation Must evaluate to bool (rasterio.bool_ or rasterio.uint8) connectivity : int, optional Use 4 or 8 pixel connectivity for grouping pixels into features transform : Affine transformation, optional If not provided, feature coordinates will be generated based on pixel coordinates Yields ------- tuple A pair of (polygon, value) for each feature found in the image. Polygons are GeoJSON-like dicts and the values are the associated value from the image, in the data type of the image. Note: due to floating point precision issues, values returned from a floating point image may not exactly match the original values. Notes ----- The amount of memory used by this algorithm is proportional to the number and complexity of polygons produced. This algorithm is most appropriate for simple thematic data. Data with high pixel-to-pixel variability, such as imagery, may produce one polygon per pixel and consume large amounts of memory. """ transform = guard_transform(transform) for s, v in _shapes(image, mask, connectivity, transform.to_gdal()): yield s, v