5

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?

KarateKid
  • 3,138
  • 4
  • 20
  • 39

1 Answers1

10

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
jdmcbr
  • 5,964
  • 6
  • 28
  • 38