Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set CRS for a file read with rasterio

I am reading a jpg image and its associated world file in Python with Rasterio like this:

import rasterio
with rasterio.open('/path/to/file.jpg') as src:
    print(src.width, src.height)
    print(src.crs)
    print(src.indexes)

The image file and its associated world file are correctly read, however the CRS is undefined (I guess that's because world file doesn't contain the CRS). Here is the output:

5000 5000
None
(1, 2, 3)

How to set the CRS manually in Rasterio after reading the file?

like image 858
KarateKid Avatar asked Jan 27 '23 06:01

KarateKid


1 Answers

Without seeing the world file, I don't know for sure if this is correct in detail, but I've used the following to add the tranform and CRS to a raster after reading in a file with a world file:

from affine import Affine
import rasterio.crs

a, d, b, e, c, f = np.loadtxt(world_filename)    # order depends on convention
transform = Affine(a, b, c, d, e, f)
crs = rasterio.crs.CRS({"init": "epsg:4326"})    # or whatever CRS you know the image is in    
with rasterio.open('/path/to/file.jpg', mode='r+') as src:
    src.transform = transform
    src.crs = crs
like image 152
jdmcbr Avatar answered Jan 29 '23 18:01

jdmcbr