python-rasterstats
python-rasterstats copied to clipboard
Progress Bar on Zonal_Stats
Is there any plan or interest to add a progress bar to zonal_stats? When working with large data-sets, the function can take a long time to complete, sometimes hanging part of the way through.
A progress bar would be a good way to see that the function is still running and has not crashed.
tqdm could be a good candidate for this, it would just wrap around the feature iteration in this line of code.
@mataslauzadis I definitely see the need for better feedback while iterating through input features.
I'm not sure the best way to implement it though - consider that tqdm
and similar approaches are designed for interactive use where you want to get regular feedback printed to the screen . This library can be used in a web server or batch processing context where the additional noise from such frequent logging would be a problem. IOW I hesitate to add progress reporting directly into the library because it would disrupt some use cases.
However, there are some solutions you can use in your own Python code by using a tqdm context manager as in https://stackoverflow.com/a/51083782/519385 . Since the zonal_stats
function is a generator, it should be possible to implement a progress bar in the application code.
I would consider adding something like that to the rio zonalstats
command line interface.
Having a built-in progress bar would be great! Maybe there could be a show_progress=False
arg so interactive users could enable a progress bar without adding noise for other users? tqdm
takes a disable
arg, so both options could be implemented just by changing that, e.g. wrap the iteration in tqdm
and set disable=not show_progress
to hide by default.
For now, here's a workaround I used in case anyone wants to create their own progress bar.
import rasterstats
from tqdm.auto import tqdm
import geopandas as gpd
# Load geometry
geom = gpd.read_file("geom.gpkg").geometry
# Create a zonal stats generator
gen = rasterstats.gen_zonal_stats(vectors=geom, raster="img.tif")
# Display a progress bar while converting the generator into a list of zonal stats
stats = [n for n in tqdm(gen, total=geom.shape[0])]
Thanks for building and maintaining rasterstats
! It's an awesome tool.