1

I use python and gdal to initate some empty rasters at given locations. When using "GTIFF" as driver the script successfully creates a tif file. In order to optimize the storage I want to initate the files as MRF files instead. Is that possible?

When I try to initate MRF files instead of tif files using the same script but changing Driver from "GTIFF" to "MRF" and the filename to out.mrf the only output is an out.mrf.aux.xml file.

Any ideas why the following code just creates the above mentioned xml file but no mrf? I've spent a few hours on this with no luck.

import gdal, osr
geotransform=[500000, 2, 0, 7050000, 0, -2] 
cols=2500
rows=2500
bands=1
outfile = 'out.mrf'
driver = gdal.GetDriverByName("MRF")
outdata = driver.Create(outfile, cols, rows, bands, gdal.GDT_UInt16)
outdata.SetGeoTransform(geotransform)
srs = osr.SpatialReference()
srs.ImportFromEPSG(3006)
outdata.SetProjection(srs.ExportToWkt())
outdata=None
driver=None

If I ran gdal from commandline

gdal_create -of MRF -bands 1 -ot UINT16 -a_srs EPSG:3006 -outsize 2500 2500 -a_ullr 500000 7050000 550000 7000000 ./test2.mrf

it all works as expected with created idx,mrf,mrf.aux.xml and ppg.

gdal version is 3.6.4 and python is 3.8

asked May 15, 2023 at 12:48

1 Answer 1

1

OK, this was maybe a bit embarrising. But unlike tif MRF needs some data in order to be initated correctly. So the improvements that i needed was. import numpy and setting the raster data of the band.

import gdal, osr
import numpy as np
geotransform=[500000, 2, 0, 7050000, 0, -2] 
cols=2500
rows=2500
bands=1
outfile = 'out2.mrf'
driver = gdal.GetDriverByName("MRF")
outdata = driver.Create(outfile, cols, rows, bands, gdal.GDT_UInt16)
outdata.SetGeoTransform(geotransform)
srs = osr.SpatialReference()
srs.ImportFromEPSG(3006)
outdata.GetRasterBand(1).WriteArray(np.zeros((2500,2500)))
outdata.SetProjection(srs.ExportToWkt())
outdata=None
driver=None
answered May 15, 2023 at 13:03
2
  • There is also a special utility for creating files gdal.org/programs/gdal_create.html but I guess that you are happy now once you managed to do it with Python. Commented May 15, 2023 at 13:59
  • Yes, I'm aware of that and have used it some cases. In this project there are a lot of updates of rasters later on that are handled with python. Commented May 16, 2023 at 4:54

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.