How can I get every pixel separated? When I calc unique values, if two pixels have the same value, it converts in one row in the final shape. I need every single pixel converted into one unique geometry.
Any ideas?
My code is:
def geojsonizer(ndvi_path, downloads_path, ndvi_date, UHC_name):
entry_date = datetime.today().strftime('%Y%m%d')
# Read input band with Rasterio
with rasterio.open(ndvi_path) as raster_ndvi:
crs = raster_ndvi.crs
huso = str(crs).replace(':', '')
src_band = raster_ndvi.read(1)
# Keep track of unique pixel values in the input band
unique_values = np.unique(src_band)
# Polygonize with Rasterio. `shapes()` returns an iterable
# of (geom, value) as tuples
shapes = list(rasterio.features.shapes(src_band, transform=raster_ndvi.transform))
geojson_schema = {
'geometry': 'MultiPolygon',
'properties': {'name': 'str', 'pixel_id': 'int', 'ndvi_value': 'float', 'ndvi_date': 'str',
'entry_date': 'str'}
}
with fiona.open(downloads_path + '\\' + UHC_name + '_' + huso + '_ndvi.geojson', 'w', driver='GeoJSON',
schema=geojson_schema, crs=crs) as geojson:
for pixel_value in unique_values:
if pixel_value != 999 and pixel_value != 0:
polygons = [shape(geom) for geom, value in shapes
if value == pixel_value]
multipolygon = MultiPolygon(polygons)
pixel_centroid = (str(multipolygon.centroid).strip('POINT ()')).replace(" ", "")
geojson.write({
'geometry': mapping(multipolygon),
'properties': {'name': 'nombre_finca',
'pixel_id': pixel_centroid,
'ndvi_value': float(pixel_value),
'ndvi_date': str(ndvi_date),
'entry_date': entry_date,
}})