perrygeo/python-rasterstats

Using a GeoPandas frame in zonal_stats results in OverflowError: cannot convert float infinity to integer

lauzadis opened this issue · 3 comments

When I use a pre-loaded shapefile dataframe in the zonal_stats as follows,

output_columns = ["count", "mean", "max", "min"]
shape = gpd.read_file("./one_county.shp")
file = "./data/geotiff/masked/2015/1.geotiff"
stats = rs.zonal_stats(shape, file, stats=output_columns, all_touched=True)

I receive the error:

---------------------------------------------------------------------------
OverflowError                             Traceback (most recent call last)
<ipython-input-11-3cc4d775d264> in <module>
     13         print(file)
     14         name = file[file.rfind('/')+1:file.rfind('.')]
---> 15         stats = rs.zonal_stats(shape, file, stats=output_columns, all_touched=True)
     16         frame = pd.DataFrame.from_dict(stats)
     17 

~/anaconda3/envs/gis/lib/python3.8/site-packages/rasterstats/main.py in zonal_stats(*args, **kwargs)
     29     The only difference is that ``zonal_stats`` will
     30     return a list rather than a generator."""
---> 31     return list(gen_zonal_stats(*args, **kwargs))
     32 
     33 

~/anaconda3/envs/gis/lib/python3.8/site-packages/rasterstats/main.py in gen_zonal_stats(vectors, raster, layer, band, nodata, affine, stats, all_touched, categorical, category_map, add_stats, zone_func, raster_out, prefix, geojson_out, **kwargs)
    154             geom_bounds = tuple(geom.bounds)
    155 
--> 156             fsrc = rast.read(bounds=geom_bounds)
    157 
    158             # rasterized geometry

~/anaconda3/envs/gis/lib/python3.8/site-packages/rasterstats/io.py in read(self, bounds, window, masked)
    286 
    287         if bounds:
--> 288             win = bounds_window(bounds, self.affine)
    289         elif window:
    290             win = window

~/anaconda3/envs/gis/lib/python3.8/site-packages/rasterstats/io.py in bounds_window(bounds, affine)
    148     """
    149     w, s, e, n = bounds
--> 150     row_start, col_start = rowcol(w, n, affine)
    151     row_stop, col_stop = rowcol(e, s, affine, op=math.ceil)
    152     return (row_start, row_stop), (col_start, col_stop)

~/anaconda3/envs/gis/lib/python3.8/site-packages/rasterstats/io.py in rowcol(x, y, affine, op)
    139     """ Get row/col for a x/y
    140     """
--> 141     r = int(op((y - affine.f) / affine.e))
    142     c = int(op((x - affine.c) / affine.a))
    143     return r, c

OverflowError: cannot convert float infinity to integer

But, when I simply provide the path to the shapefile, instead of the geopandas object, it works fine. Is this intended? I remember zonal_stats working fine with Geopandas objects just a few months ago.

Here is the minimum data required to reproduce the error:
https://drive.google.com/drive/folders/1lJe50IVsTLVAX7G9w7_9mm4HGX4gfSpc?usp=sharing

@mataslauzadis Thanks for a great bug report! Code and data is always appreciated!

However, I can't reproduce this case. Using Python 3.8 in a clean virutalenv:

$ pip install -U rasterstats geopandas

Running against the sample data you provided

import geopandas as gpd
import rasterstats as rs

output_columns = ["count", "mean", "max", "min"]
shape = gpd.read_file("one_county/one_county.shp")
path = "1.geotiff.tif"
stats = rs.zonal_stats(shape, path, stats=output_columns, all_touched=True)
print(stats)

gives me the presumably expected output of

[{'min': 62.27641296386719, 'max': 87.83891296386719, 'mean': 79.12016296386719, 'count': 4}]

I'm inclined to think this is a bug specific to your installation and/or one of this libraries many dependencies. Can you try to install in a clean environment and see if the problem persists? What versions are you using and how are you installing them?

I can no longer replicate the bug either on Python 3.7.8.
I've changed my working environment since this was first reported, so I can't debug the libraries / dependencies.
Thank you for the reply!