3

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?

  • what happens if you try it without the --optfile setting? I've seen a number of threads describing issues with _retile and --optfile. – elrobis Dec 4 '12 at 13:06
  • same issue.. so I dont think it's the "--optfile" – Katie E. Dec 4 '12 at 13:48
  • It seems to compain at the first - no matter what option it is. Anyone know why it would do this? – Katie E. Dec 4 '12 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 – elrobis Dec 4 '12 at 15:30
  • nope, it works fine in my DOS window – Katie E. Dec 4 '12 at 15:55
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.

1

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.

  • use tiler.main() to actually run the tiler. – Michael Feb 21 at 8:14
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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.