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.

Taras
35.7k5 gold badges77 silver badges151 bronze badges
asked Oct 22, 2019 at 14:39

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)
Bjorn Svensson
3,58521 silver badges38 bronze badges
answered Oct 22, 2019 at 18:00
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.

answered Oct 22, 2019 at 15:52
1
  • this won't work, you need GPCs for that Commented Apr 1, 2021 at 9:34

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.