allenai / satlas

Apache License 2.0
184 stars 19 forks source link

Georeference output png #27

Closed oguzhannysr closed 7 months ago

oguzhannysr commented 7 months ago

Hello, I created my output.png file according to the example here https://github.com/allenai/satlas/blob/main/Normalization.md#sentinel-2-images, according to the sentinel-2 framework I specified. But I need to geo coordinate this image. How do you do this, is there an example?

favyen2 commented 7 months ago

That example inputs a GeoTIFF, so the output is in the same coordinate system as the input, which is geo-referenced.

Here is an example of using GDAL to transform coordinates from pixel (col, row) in a GeoTIFF to (longitude, latitude) WGS-84 coordinates. Use conda install gdal to install gdal.

from osgeo import gdal
raster = gdal.Open('stack.tif')
transformer = gdal.Transformer(raster, None, ['DST_SRS=WGS84'])
col = 1234
row = 5678
success, point = transformer.TransformPoint(0, col, row, 0)
assert success == 1
print(point)

Or you can produce output GeoTIFF by copying the geo-reference data from the input, something like this should work:

from osgeo import gdal, osr
import skimage.io
raster = gdal.Open('stack.tif')
image = skimage.io.imread('output.png')
driver = gdal.GetDriverByName("GTiff")
dataset = driver.Create('output.tif', image.shape[1], image.shape[0], 1, gdal.GDT_Byte, options=['COMPRESS=DEFLATE'])
dataset.SetProjection(raster.GetProjection())
dataset.SetGeoTransform(raster.GetGeoTransform())
band = dataset.GetRasterBand(1)
band.WriteArray(image)
band.FlushCache()