1

I have a TIFF file with imperviousness levels and a Geoseries object containing the boundary coordinates of circles around 9 points. I want to find the mean value using zonal_stats but I don't know how to pass the circle geometry objects.

geometry=[Point(xy) for xy in zip(df['longitude'], df['latitude'])]
geodata=gpd.GeoDataFrame(df,crs=crs, geometry=geometry)
buf_df = gpd.GeoSeries(geometry)
buff_circle= buf_df.buffer(0.001, cap_style = 1)

The buff_circle has the boundary coordinates of the circles

4
  • gis.stackexchange.com/a/421774/2856
    – user2856
    Commented Feb 4, 2022 at 23:48
  • I don't have my polygon in a shapefile. Just this geoseries variable 'buff_circle' How do I save my circle polygon in a shapefile? Commented Feb 5, 2022 at 1:34
  • The answer linked to uses geopandas geoseries just like you, shapefile is irrelevant.
    – user2856
    Commented Feb 5, 2022 at 3:12
  • When I pass my Geoseries object "buff_circle" I get this warning /usr/local/lib/python3.7/dist-packages/rasterstats/io.py:313: UserWarning: Setting nodata to -999; specify nodata explicitly warnings.warn("Setting nodata to -999; specify nodata explicitly") /usr/local/lib/python3.7/dist-packages/rasterstats/io.py:313: UserWarning: Setting nodata to -999; specify nodata explicitly warnings.warn("Setting nodata to -999; specify nodata explicitly") Commented Feb 5, 2022 at 15:54

1 Answer 1

2

Something like the following that buffers, runs zonal stats and joins back to the original df ought to work. Obviously you'll need to fill out the df creation etc.

from rasterstats import zonal_stats
import pandas as pd
import geopandas as gpd

your_raster = "/path/to/raster"
df = ...?
crs = ...?

geometry=[Point(xy) for xy in zip(df['longitude'], df['latitude'])]
buf_df = gpd.GeoSeries(geometry)
buff_circle= buf_df.buffer(0.001, cap_style = 1)

# zonal stats
stats = zonal_stats(buff_circle, your_raster, stats="mean")

# Join stats to gdf 
sdf = pd.DataFrame(stats)
sdf = pd.concat([df, sdf], axis=1)
gdf = gpd.GeoDataFrame(df, crs=crs, geometry=buff_circle)
2
  • from rasterstats import zonal_stats, point_query rf = '/content/drive/MyDrive/GIS/Coding_task/imperviousness_raster_data/NLCD_2016_Impervious_L48_20190405_aeCiajHeNAvXB2754sYm.tiff' rds = rasterio.open(rf) stats = zonal_stats(buff_circle, rds, stats="mean") sdf = pd.DataFrame(stats) I get this error while executing the stats = .... line AttributeError: '_GeneratorContextManager' object has no attribute 'transform' Commented Feb 5, 2022 at 15:59
  • @KarthikEswar edit your question to include this error and the output of either gdalinfo NLCD_2016_Impervious_L48_20190405_aeCiajHeNAvXB2754sYm.tiff or rio info NLCD_2016_Impervious_L48_20190405_aeCiajHeNAvXB2754sYm.tiff
    – user2856
    Commented Feb 13, 2022 at 6:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.