Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def polygonize(imageArray, transformAffineObject, maskValue=0):
mask = imageArray!=maskValue
featureGenerator = features.shapes(imageArray,
transform=transformAffineObject,
mask=mask)
return featureGenerator
mask = None
for fname in images:
logging.info("Valid regions for %s", fname)
# ensure formats match
with rasterio.open(str(fname), 'r') as dataset:
transform = dataset.transform
img = dataset.read(1)
if mask_value is not None:
new_mask = img & mask_value == mask_value
else:
new_mask = img != 0
if mask is None:
mask = new_mask
else:
mask |= new_mask
shapes = rasterio.features.shapes(mask.astype('uint8'), mask=mask)
shape = shapely.ops.unary_union([shapely.geometry.shape(shape) for shape, val in shapes if val == 1])
type(shapes)
# convex hull
geom = shape.convex_hull
# buffer by 1 pixel
geom = geom.buffer(1, join_style=3, cap_style=3)
# simplify with 1 pixel radius
geom = geom.simplify(1)
# intersect with image bounding box
geom = geom.intersection(shapely.geometry.box(0, 0, mask.shape[1], mask.shape[0]))
# transform from pixel space into CRS space
geom = shapely.affinity.affine_transform(geom, (transform.a, transform.b, transform.d,
transform.e, transform.xoff, transform.yoff))
return geom
:type crs: sentinelhub.CRS
:param timestamp: Time of the data slice
:type timestamp: datetime.datetime
:return: Vectorized data
:rtype: geopandas.GeoDataFrame
"""
mask = None
if self.values:
mask = np.zeros(raster.shape, dtype=np.bool)
for value in self.values:
mask[raster == value] = True
geo_list = []
value_list = []
for idx in range(raster.shape[-1]):
for geojson, value in rasterio.features.shapes(raster[..., idx],
mask=None if mask is None else mask[..., idx],
transform=affine_transform, **self.rasterio_params):
geo_list.append(shapely.geometry.shape(geojson))
value_list.append(value)
series_dict = {
self.values_column: GeoSeries(value_list),
'geometry': GeoSeries(geo_list)
}
if timestamp is not None:
series_dict['TIMESTAMP'] = GeoSeries([timestamp] * len(geo_list))
vector_data = GeoDataFrame(series_dict, crs={'init': 'epsg:{}'.format(crs.value)})
if not vector_data.geometry.is_valid.all():
vector_data.geometry = vector_data.geometry.buffer(0)
for fname in images:
# ensure formats match
with rasterio.open(str(fname), 'r') as ds:
transform = ds.transform
img = ds.read(1)
if mask_value is not None:
new_mask = img & mask_value == mask_value
else:
new_mask = img != ds.nodata
if mask is None:
mask = new_mask
else:
mask |= new_mask
shapes = rasterio.features.shapes(mask.astype('uint8'), mask=mask)
shape = shapely.ops.unary_union([shapely.geometry.shape(shape) for shape, val in shapes if val == 1])
# convex hull
geom = shape.convex_hull
# buffer by 1 pixel
geom = geom.buffer(1, join_style=3, cap_style=3)
# simplify with 1 pixel radius
geom = geom.simplify(1)
# intersect with image bounding box
geom = geom.intersection(shapely.geometry.box(0, 0, mask.shape[1], mask.shape[0]))
# transform from pixel space into CRS space
geom = shapely.affinity.affine_transform(geom, (transform.a, transform.b, transform.d, transform.e, transform.xoff,
for fname in images:
# ensure formats match
with rasterio.open(str(fname), 'r') as ds:
transform = ds.transform
img = ds.read(1)
if mask_value is not None:
new_mask = img & mask_value == mask_value
else:
new_mask = img != ds.nodata
if mask is None:
mask = new_mask
else:
mask |= new_mask
shapes = rasterio.features.shapes(mask.astype('uint8'), mask=mask)
shape = shapely.ops.unary_union([shapely.geometry.shape(shape) for shape, val in shapes if val == 1])
# convex hull
geom = shape.convex_hull
# buffer by 1 pixel
geom = geom.buffer(1, join_style=3, cap_style=3)
# simplify with 1 pixel radius
geom = geom.simplify(1)
# intersect with image bounding box
geom = geom.intersection(shapely.geometry.box(0, 0, mask.shape[1], mask.shape[0]))
# transform from pixel space into CRS space
geom = shapely.affinity.affine_transform(geom, (transform.a, transform.b, transform.d,
# ensure formats match
with rasterio.open(str(fname), 'r') as ds:
transform = ds.transform
img = ds.read(1)
if mask_value is not None:
new_mask = img & mask_value == mask_value
else:
# new_mask = img != ds.nodata
new_mask = img != 0
if mask is None:
mask = new_mask
else:
mask |= new_mask
shapes = rasterio.features.shapes(mask.astype('uint8'), mask=mask)
shape = shapely.ops.unary_union([shapely.geometry.shape(shape) for shape, val in shapes if val == 1])
type(shapes)
# convex hull
geom = shape.convex_hull
# buffer by 1 pixel
geom = geom.buffer(1, join_style=3, cap_style=3)
# simplify with 1 pixel radius
geom = geom.simplify(1)
# intersect with image bounding box
geom = geom.intersection(shapely.geometry.box(0, 0, mask.shape[1], mask.shape[0]))
# transform from pixel space into CRS space
geom = shapely.affinity.affine_transform(geom, (transform.a, transform.b, transform.d,
def array_to_polygons(array, affine=None):
"""
Returns a geopandas dataframe of polygons as deduced from an array.
:param array: The 2D numpy array to polygonize.
:param affine: The affine transformation.
:return:
"""
if affine == None:
results = [
{'properties': {'raster_val': v}, 'geometry': s}
for i, (s, v)
in enumerate(shapes(array))
]
else:
results = [
{'properties': {'raster_val': v}, 'geometry': s}
for i, (s, v)
in enumerate(shapes(array, transform=affine))
]
tops_df = geopandas.GeoDataFrame({'geometry': [shape(results[geom]['geometry']) for geom in range(len(results))],
'raster_val': [results[geom]['properties']['raster_val'] for geom in range(len(results))]})
return(tops_df)
for fname in images:
# ensure formats match
with rasterio.open(str(fname), 'r') as ds:
transform = ds.transform
img = ds.read(1)
if mask_value is not None:
new_mask = img & mask_value == mask_value
else:
new_mask = img != ds.nodata
if mask is None:
mask = new_mask
else:
mask |= new_mask
shapes = rasterio.features.shapes(mask.astype('uint8'), mask=mask)
shape = shapely.ops.unary_union([shapely.geometry.shape(shape) for shape, val in shapes if val == 1])
# convex hull
geom = shape.convex_hull
# buffer by 1 pixel
geom = geom.buffer(1, join_style=3, cap_style=3)
# simplify with 1 pixel radius
geom = geom.simplify(1)
# intersect with image bounding box
geom = geom.intersection(shapely.geometry.box(0, 0, mask.shape[1], mask.shape[0]))
# transform from pixel space into CRS space
geom = shapely.affinity.affine_transform(geom, (transform.a, transform.b, transform.d,
with rasterio.open(str(fname), 'r') as ds:
transform = ds.affine
img = ds.read(1)
if mask_value is not None:
new_mask = img & mask_value == mask_value
else:
# TODO update when sen2cor format write finalised new_mask = img != ds.nodata
new_mask = img != 0
if mask is None:
mask = new_mask
else:
mask |= new_mask
shapes = rasterio.features.shapes(mask.astype('uint8'), mask=mask)
shape = shapely.ops.unary_union([shapely.geometry.shape(shape) for shape, val in shapes if val == 1])
type(shapes)
geom = shape.convex_hull
# buffer by 1 pixel
geom = geom.buffer(1, join_style=3, cap_style=3)
# simplify with 1 pixel radius
geom = geom.simplify(1)
# intersect with image bounding box
geom = geom.intersection(shapely.geometry.box(0, 0, mask.shape[1], mask.shape[0]))
# transform from pixel space into CRS space
geom = shapely.affinity.affine_transform(geom, (transform.a, transform.b, transform.d,