0

I am trying to manually set the CRS of my rasterio object after reading in a png/pgw file.

Of course, the pgw file contains the transformation for the raster but does not include any georeferencing/CRS information. So, I am trying to manually set it afterwards, it seems to work sometimes but not others... this is what I'm currently doing:

from rasterio.crs import CRS
import rasterio as rio

r = rio.open(file, 'r')
print(r.crs)
#prints nothing
crs = CRS.from_epsg(32611)
r._crs = crs

Now weirdly, sometimes it works..and sometimes even if I run this command without error, the r.crs will still be None.

I tried using:

r.crs = crs #read only attribute, cant do this
r._crs = crs #works sometimes but not others??
r._set_crs(crs) #read only attribute
setattr(r, '_crs', to_crs) #seems to work the same as r._crs

What is the proper way to manually define the CRS of an unknown raster?

1 Answer 1

1

You need r+ mode:

from rasterio.crs import CRS
import rasterio

with rasterio.open(file, 'r+') as rds:
    rds.crs = CRS.from_epsg(32611)
2
  • lol for the love of god.. thanks
    – Derek Eden
    Sep 3, 2021 at 15:39
  • Not a problem. Good luck!
    – snowman2
    Sep 3, 2021 at 16:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.