I am trying to call gdal_retile
from my Python code like this:
import gdal_retile
and in main:
gdal_retile.main("-v -r bilinear -levels 4 -ps 2048 2048 -co \"tiled=YES\" -targetDir pyramid --optfile files.txt")
but I get this error in my Debug Probe window:
Unrecognised command option: -
Usage: gdal_retile.py
[-v] [-co NAME=VALUE]* [-of out_format]
[-ps pixelWidth pixelHeight]
[-ot {Byte/Int16/UInt16/UInt32/Int32/Float32/Float64/
CInt16/CInt32/CFloat32/CFloat64}]
[ -tileIndex tileIndexName [-tileIndexField fieldName]]
[ -csv fileName [-csvDelim delimiter]]
[-s_srs srs_def] [-pyramidOnly] -levels numberoflevels
[-r {near/bilinear/cubic/cubicspline/lanczos}]
[-useDirForEachRow]
-targetDir TileDirectory input_files
Any idea what I am doing wrong?
4 Answers 4
So I have been trying to to do this for a while now. The main reason I want to run gdal_retile from python is so that i can run it in multiple process.
To answer your question: Take the gdal_retile.py file and copy it to the site-packages of your python install. C:\Python27\Lib\site-packages your path might differ.
Next in python you can just import gdal_retile. Then make gdal_retile a object. You then set the parameters for each of the options in gdal_retile. See the below snippet from my script.
When setting the paths to files of your target dir use \ and not r"path\path" for windows. Also for the targetDir place \ and the end of your path.
import gdal_retile
import gdal
import ogr
import os
from multiprocessing.dummy import Pool
def retile(inputList):
tiler = gdal_retile
tiler.Verbose = True
tiler.TileHeight = 2046
tiler.TileWidth = 2046
tiler.TargetDir = inputList[1]
tiler.Levels = inputList[2]
tiler.Names = inputList[0]
tiler.CreateOptions = ["COMPRESS=JPEG","TILED=YES"]
Also take note that when you create a loop in python over files and execute gdal_retile.py using sys.cmd() or subprocess. Python will not wait till the process is done. The reason for this is that cmd starts the python process and closes. So even if you use subprocess and use wait() on the subprocess object it will not stop because the cmd process closed. You will in the end start x amount of gdal_retile processes by just looping over files.
Hope this helps.
-
1use
tiler.main()
to actually run the tiler.Michael– Michael2019年02月21日 08:14:28 +00:00Commented Feb 21, 2019 at 8:14 -
Good answer, important to document that
tiler.TargetDir="path/to/output_folder/"
whiletiler.Names=["path/to/input.tif"]
See thatNames
is surrounded by a list of strings vs. TargetDir is one output folder.Binx– Binx2022年04月25日 23:43:15 +00:00Commented Apr 25, 2022 at 23:43
I am not certain, but I am guessing gdal_retile.main() wants a list of arguments, not a string. A small example to show what may be happening:
>>> def test(args):
for i in args:
print i
>>> a = 'a string of arguments'
>>> b = ['a', 'list', 'of', 'arguments']
>>> test(a)
a
s
t
r
i
n
g
o
f
a
r
g
u
m
e
n
t
s
>>> test(b)
a
list
of
arguments
If a string is passed, each character would be considered an argument. Try:
gdal_retile.main(['-v', '-r', 'bilinear', '-levels', '4', '-ps', '2048', '2048', '-co', '\"tiled=YES\"', '-targetDir', 'pyramid', '--optfile', 'files.txt'])
Like I said, I am just guessing.
Maybe it will work like this. When I want to call ogr2ogr
from inside a Python script, I use the os.system()
function to execute my ogr2ogr
instruction---oftentimes in a loop. But I wonder if this would also work on another .py
script. I don't 'see why not (just make sure it can qualify your file paths).
import os
cmd = 'gdal_retile.py -v -r bilinear -levels 4 -ps 2048 2048 -co "tiled=YES" -targetDir pyramid --optfile files.txt'
os.system(cmd)
Create a folder with a custom library name. Copy gdal_retile.py
into this folder. Within this folder, create an empty Python file __init__.py
.
Start using this library in your code by importing it. The example below is for the simple task of generating tiles.
from mygdal import my_gdal_retile
from mygdal.my_gdal_retile import *
import pandas as pd
from osgeo import gdal
command="gdal_retile.py -ps 512 512 -overlap 128 \
-targetDir MyTiles MyGeotiff.tif -v"
my_gdal_retile.main(command.split())
--optfile
setting? I've seen a number of threads describing issues with_retile
and--optfile
.gdal_retile
instruction in a terminal does it behave the same way? That is, running this in a cmd:gdal_retile.py -v -r bilinear -levels 4 -ps 2048 2048 -co "tiled=YES" -targetDir pyramid --optfile files.txt