5

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?

Taras
35.7k5 gold badges77 silver badges151 bronze badges
asked Dec 4, 2012 at 12:53
6
  • what happens if you try it without the --optfile setting? I've seen a number of threads describing issues with _retile and --optfile. Commented Dec 4, 2012 at 13:06
  • same issue.. so I dont think it's the "--optfile" Commented Dec 4, 2012 at 13:48
  • It seems to compain at the first - no matter what option it is. Anyone know why it would do this? Commented Dec 4, 2012 at 15:25
  • If you execute your 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 Commented Dec 4, 2012 at 15:30
  • nope, it works fine in my DOS window Commented Dec 4, 2012 at 15:55

4 Answers 4

3

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.

answered May 18, 2018 at 11:32
2
  • 1
    use tiler.main() to actually run the tiler. Commented Feb 21, 2019 at 8:14
  • Good answer, important to document that tiler.TargetDir="path/to/output_folder/" while tiler.Names=["path/to/input.tif"] See that Names is surrounded by a list of strings vs. TargetDir is one output folder. Commented Apr 25, 2022 at 23:43
1

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.

answered Jan 11, 2013 at 16:34
0

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)
answered Dec 4, 2012 at 16:25
0

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())
Taras
35.7k5 gold badges77 silver badges151 bronze badges
answered Jul 12, 2023 at 5:15

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.