3

Does anyone know how to get the affine transformation from a geopandas dataframe?

I need this for rasterising geospatial vector data with rasterio.

shapes = ((geom,value) for geom, value in zip(geodataframe.geometry, len(geodataframe)*[255]))

numpy_array = features.rasterize(
    shapes,
    out_shape=self.tile_size,
    transform=geodataframe.transform???,
    all_touched=True)

I am however still missing the transform part.

1

1 Answer 1

4

The solution I found to work for anybody else searching is to create the transform yourself.

import affine
transform = affine.Affine(res, 0, minx, 0, -res, maxy)

shapes = ((geom,value) for geom, value in zip(dataset.geometry, len(dataset)*[val]))

raster = rasterio.features.rasterize(shapes,
                                     out_shape=tile_size,
                                     transform=transform)

whereby res is the spatial resolution (ie 1pixel = 2m). minx and maxy correspond to the geographic position and the y axis is chosen negative to match the different coordinate system of the another raster I am using.

1
  • just what I needed
    – GeoMonkey
    Commented Mar 11, 2023 at 2:48

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.