I am using the shapes interface of rasterio to extract polygons from a GeoTIFF image. rasterio returns an excessive number of polygons which is not expected. It is taking a long time to extract a relatively very simple raster. I did additional processing of the resulting shapefiles, namely Dissolve and Multipart to a single part, and eventually, get the correct polygons.
Can any experts in rasterio can shed some light on what is happening? For the sample raster, I am expecting fewer polygons whereas rasterio shapes are 77562 in number. File shared here
import pandas as pd
import geopandas
import matplotlib.pyplot as plt
import numpy as np
import os
import rasterio
from rasterio.features import shapes
mask = None
results=None
with rasterio.Env():
with rasterio.open('/content/small_area.tif') as src:
image = src.read(1) # first band
results = (
{'properties': {'raster_val': v}, 'geometry': s}
for i, (s, v)
in enumerate(
shapes(image, mask=mask, transform=src.transform)))
geoms=list(results)
import geopandas as gp
gdf = geopandas.GeoDataFrame.from_features(geoms) #Convert to Geopandas dataframe
gdf.insert(0, 'New_ID', range(0, 0 + len(gdf)))
gdf.head()
gdf.set_crs(epsg=3857, inplace=True)
gdf = gdf.to_crs(epsg=3857)
gdf.set_geometry(col='geometry', inplace=True)
gdf.to_file("small_area.shp")