TransWikia.com

How to save a new raster with the projections of the previous raster

Geographic Information Systems Asked by Leonardo Moreira on July 15, 2021

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:UsersdatasetS30W051.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:UsersdatasetS30W051_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)?

One Answer

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:UsersdatasetS30W051.tif'
pathout = 'C:UsersdatasetS30W051_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:
    dst.write(data)

Answered by user2856 on July 15, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP