Help with rasterio.transfrom.xy

Cooney,Tom (ECCC)


We are working on a process that extracts values from raster data and returns them as GeoJSON geometries. Our use cases are point, line, and polygon. 

For the line use case, we are trying to return the values and coordinates for each raster cell intersected by the line. We would also like to have the coordinates returned in geographic coordinates. To do this we are trying to use rasterio.transform.xy.


The problem we are encountering is that rasterio.transform.xy is not returning the coordinates in lat/long but rather in their native projection. We think this is caused by using the incorrect transform (Affine.affine) parameter.


Our setup is below. Note in this case shapes is a GeoJSON LineString:

with as src:

       out_image, out_transform = rasterio.mask.mask(src, shapes, crop=True)

       with MemoryFile() as memfile:

               with"GTiff", height=out_image.shape[1], width=out_image.shape[2], count=1,

                                      dtype=rasterio.float64, transform=out_transform) as dataset:


                        ds =

                        xs_to_mod = list(range(0, ds.shape[1]))

                        ys_to_mod = list(range(0, ds.shape[2]))

                        xs, ys = rasterio.transform.xy(out_transform, xs_to_mod, ys_to_mod, offset='center')

Thank you

Join to automatically receive all group messages.