3

I am trying to use rasterio to load in an image, modify the ndarray, then write out using the same spatial reference system as the original image. The below function is my attempt to do this. But the spatial reference system is missing from the output geotiff. Any suggestions on what I am doing wrong?

I have checked the input geotiff crs is valid ('epsg:32611').

# Function to write out an ndarry as a GeoTIFF using the spatial references of a sample geotif file
def write_GeoTif_like(templet_tif_file, output_ndarry, output_tif_file):
    import rasterio
    orig = rasterio.open(templet_tif_file)
    with rasterio.open(output_tif_file, 'w', driver='GTiff', height=output_ndarry.shape[0],
                       width=output_ndarry.shape[1], count=1, dtype=output_ndarry.dtype,
                       crs=orig.crs, transform=orig.transform, nodata=-9999) as dst:
        dst.write(output_ndarry, 1)
nicway
  • 538
  • 1
  • 6
  • 16

1 Answers1

2

Having been bitten by this issue before, I'd guess that your GDAL_DATA environment variable is not being set correctly (see https://github.com/conda/conda/issues/4050 for more detail). Without knowing more about your installation/OS, I cannot say for sure, but if gdal (and rasterio) are unable to find the location with metadata files such as those that support operations involving coordinate references systems, you'll lose the CRS in the output tif.

jdmcbr
  • 5,964
  • 6
  • 28
  • 38
  • Was on Windows and GDAL_DATA was not set. Setting it manually worked. I am surprised that no warning was thrown in rasterio when GDAL_DATA is not set. – nicway Sep 15 '17 at 19:19
  • Yeah, that's a good point. I haven't looked into how hard it would be to implement that. – jdmcbr Sep 15 '17 at 23:28