I use the Rasterio to cut out roofs of buildings, roofs are polygons. I would like to set up crs of felled roofs. Is it possible to do that by Rasterio?

I can set up crs of layer, layer.crs = 'epsg:2180', but how can I do that for raster file?

My code to cutting out roofs of buildings:

def cutting_out(df, start, end):
    for index, row in df.iloc[start:end].iterrows():
        if len(row['1_5000_pi025']) == 1:
            coo = shapely.wkt.loads(row['geometry'])
            multiplier = int(row['liczba_kon'])
            coo_buffer = coo.buffer(3 * multiplier)
            lokalnyid = row['lokalnyid']
            tiff = str(row['1_5000_pi025'][0])
            with rasterio.open(f'/mnt/qnap/geo_tiff_1_5000_p025/{tiff}') as src:
                out_image, out_transform = rasterio.mask.mask(src, [coo_buffer], crop=True)
                out_meta = src.meta
                        "driver": "GTiff",
                        "height": out_image.shape[1],
                        "width": out_image.shape[2],
                        "transform": out_transform,
                with rasterio.open(f'/mnt/qnap/geo_tiff_roofs_testy3/{lokalnyid}'+'_025'+'.tif', "w", **out_meta) as dest:
  • If src.crs is not None it will automatically get written to dest.crs, if src.crs is None and you know it is epsg:2180, just add "crs": "epsg:2180" to out_meta.update
    – user2856
    Apr 23, 2021 at 12:15

1 Answer 1


As user2856 commented, when opening a new file for writing you can just add "crs" argument in "rasterio.open":

with rasterio.open(f'/.../{lokalnyid}'+'_025'+'.tif', "w", crs='EPSG:2180', **out_meta) as dest:

An example from rasterio documentation:

profile = {'driver': 'GTiff', 'height': 100, 'width': 100, 'count': 1, 'dtype': rasterio.uint8}
with rasterio.open('/tmp/foo.tif', 'w', crs='EPSG:3857', **profile) as dst:
...     pass # write data to this Web Mercator projection dataset.

Source: https://rasterio.readthedocs.io/en/latest/topics/georeferencing.html?highlight=CRS#coordinate-reference-system

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.