2

I am trying to use gdal utilities like gdal_polygonize in python to polygonize my raster image but I'm having trouble understanding the commands. What I mean is in some places the command is of the given form say:

gdal.Polygonize( srcband, None, dst_layer, -1, [], callback=None )

I found it here

And the command is also called in python with os.system().

I read and tried to implement commands from Gdal_polygonize: How to filter pixels above a given value (elevation)?

gdal_polygonize.py result.tif result.shp

and python gdal_polygonize error

os.system('gdal_polygonize.py ' + filename + ' -f "ESRI Shapefile" ' + shapefile)

but no shapefile is created although there was no error while running the code.

I am stuck and having great trouble learning to use gdal with python as I don't understand the need or difference between the two types of commands. Please address my question.

What is the proper syntax for using (lets say) gdal_calc.py when called with os.system() and also when used as gdal_calc() ?

P.s I use python 2.7 on win 8.1 platform.

EDIT: With gdal.polygonize() I get the output shapefile, but when I try to view it in qgis, I get nothing but white screen. And in the case where I try to call utilities with os.system(), I don't get an output file only. Please tell me what is going wrong.

7

1 Answer 1

2

In your first example you have a simple script, where all modules are imported, parameters are created and your function is executed with these parameters. Your vector data are written to dst_layer.

There are few ways to execute your script. You can do this in command line (if you have enviromental variables for python with gdal):

$ python script_name.py

You also can execute your scripts and commands in Python shell. os.system() Executes the command (a string) in a subshell. More about that. So in your example, there is a string inside os.system(). You can execute all commands you want, they must be windows cmd commands.

In Python shell you also can execute files with execfile('path/to/file'), for example you can run your first script and new shapefile will be created.

I hope it will help.

5
  • Hi, thank you for your response. Your answer gives me slight ides of the commands. I tried to run scripts with os.system() and they seem to run without error but do no create the output file. Could you tell me what's wrong?
    – rach
    Commented Jul 13, 2015 at 10:37
  • It might help to add full pathname to your input and output files. And the input files must share the same CRS.
    – AndreJ
    Commented Jul 13, 2015 at 10:58
  • @AndreJ I tried that already, but didn't work. See with gdal.polygonize() I get the output shapefile, but when I try to view it in qgis, I get nothing but white screen. And in case of where I try to run call utilities with os.system(), I don't get an output file.
    – rach
    Commented Jul 13, 2015 at 11:11
  • Can you upload the source raster, or use something that is available from the web for free?
    – AndreJ
    Commented Jul 13, 2015 at 11:14
  • here is a link to one of the images that I am using ftp.glcf.umd.edu/glcf/Global_LNDCVR/Global_5min_Rev1/GeoTIFF/…
    – rach
    Commented Jul 13, 2015 at 13:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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