I'm trying to reproduce the R rasterize function in python to streamline my code and am having issues with summing multiple points in the raster. Essentially I want to sum the value of my variable for every point in the grid I create. However currently it only appears to be returning the value of the first point in the grid it sees.

import rasterio
import geopandas as gpd

def create_raster_defined_res(geodataset, output, out_var, resolution):  
    res = (resolution, resolution)
    bounds = geodataset.total_bounds
    kwargs = {
            'count': 1,
            'crs': geodataset.crs['init'],
            'bounds': bounds,
            'width': max(int(ceil((bounds[2] - bounds[0]) / float(res[0]))), 1),
            'height': max(int(ceil((bounds[3] - bounds[1]) / float(res[1]))), 1),
            'affine': Affine(res[0], 0, bounds[0], 0, -res[1], bounds[3]),
            'transform': (bounds[0], res[0], 0, bounds[3], 0, -res[1]),
            'compress': 'lzw',
            'driver': u'GTiff',
            'dtype': 'float64',
            #'nodata': -1e+308

    with rasterio.open(output, 'w', **kwargs) as out:
        out_arr = np.empty([kwargs['height'],kwargs['width']])

        # this is where we create a generator of geom, value pairs to use in rasterizing
        shapes = ((geom,value) for geom, value in zip(geodataset['geometry'], geodataset[out_var]))

        burned = rasterize(shapes=shapes, fill=0, out=out_arr, transform=kwargs['affine'])
        out.write_band(1, burned)

create_raster_defined_res(geopandas_dataset, output_tiff, output_var, 2500)

Is there a way to add a parameter to sum the value of all the points that enter into the grid or tell it to add instead of just taking the value of the first point?

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.