6

I have a big set of PNG files and EPSG:3006 coordinates of the edges of each file. (actually the coordinates of the edges are given in the file name, like this: 'image_666751_7023263_667007_7023519.png', so 666751, 7023263, 667007, 7023519 are the boundaries of the this image in EPSG:3006 )

How can I convert those PNG files to GeoTIFF files using Python so the TIFF files would contain the geo metadata. I guess it can be done with Rasterio lib, but I'm not sure how exactly.

2 Answers 2

6

eventually this worked for me:

dataset = rasterio.open(input_file_path, 'r')
bands = [1, 2, 3]
data = dataset.read(bands)
transform = rasterio.transform.from_bounds(west, south, east, north, data.shape[1], data.shape[2])
crs = {'init': 'epsg:3006'}

with rasterio.open(output_file_path, 'w', driver='GTiff',
                   width=data.shape[1], height=data.shape[2],
                   count=3, dtype=data.dtype, nodata=0,
                   transform=transform, crs=crs) as dst:
    dst.write(data, indexes=bands)
1
  • 1
    Can you write the Full code below for better understanding. Commented Oct 12, 2020 at 11:21
4

Check out gdalwarp that is part of the GDAL library. There are Python bindings available through the osgeo package.

This is untested, but it should get you close to what you need:

from osgeo import gdal

gdal.Warp(
  dstDS,
  srcDS,
  format='GTiff',
  dstSRS='EPSG:3006',
  outputBounds=[minX, minY, maxX, maxY]
)

You'll need to define the source and destination datasets, and fill in the extent for outputBounds. You can find more examples in the GDAL test suite.

1
  • this won't work, you need GPCs for that Commented Apr 1, 2021 at 9:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.