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
1 Answer 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
-
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.user30184– user301842023年05月15日 13:59:44 +00:00Commented 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.Simon Johansson– Simon Johansson2023年05月16日 04:54:19 +00:00Commented May 16, 2023 at 4:54