I tried to make an algorithm in Python where I entered a georeferenced raster (known coordinate system), all its negative values were transformed to zero, and then a new image was saved with the georeference of the initial image.

import skimage.io
import pandas as pd
import numpy as np

pathhr = 'C:\\Users\\dataset\\S30W051.tif'
HR = skimage.io.imread(pathhr)
df1 = pd.DataFrame(HR)
HR_changed = df1[df1 < 0] = 0

#save function
savedata = df1.to_numpy()
skimage.io.imsave('C:\\Users\\dataset\\S30W051_TEST.tif', savedata)

But when I save my raster at the end of this script, I get a non-georeferenced TIFF raster.

How do I keep the same coordinate system as the initial raster (without transforming the output raster into local coordinates)?


It's very easy using a library that's designed to handle georeferenced raster data like GDAL or rasterio (based on GDAL).

import rasterio as rio

pathhr = 'C:\\Users\\dataset\\S30W051.tif'
pathout = 'C:\\Users\\dataset\\S30W051_TEST.tif'

with rio.open(pathhr) as src:
    profile = src.profile
    data = src.read()

data[data < 0] = 0

with rio.open(pathout, 'w', **profile) as dst:

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.