perrygeo/python-rasterstats

Adding CRS information to output of zonal stats

AlexanderJuestel opened this issue · 0 comments

Hello,

I am using rasterstats and its zonal statistics where the output of everything is a GeoPandas GeoDataFrame containing the results. I am doing the following as shown below. However, the list of dicts (geojson-like) that zonal_stats returns does not contain the CRS information of the vector data and in return, a GeoDataFrame is created without CRS.

I would like to propose adding the CRS information to the output of zonal_stats so that GeoPandas can create a GeoDataFrame with a valid CRS from it. I could of course just open the vector data using GeoPandas but I like the way it is accomplished below.

I could try working on a PR if that feature is desired for rasterstats.

stats = zonal_stats("path_to_vector.shp", 
                             "path_to_raster.tif",
                             stats="count min mean max median sum std",
                             geojson_out=True)

gdf = gpd.GeoDataFrame.from_features(stats)

A place to add the CRS would be somewhere in the fiona_generator, I guess:

def fiona_generator(obj, layer=0):
with fiona.open(obj, "r", layer=layer) as src:
for feat in src:
yield fiona.model.to_dict(feat)