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.

asked Jul 13, 2015 at 9:10
7
  • 2
    os.system is deprecated, use the standard module subprocess. For a pure Python solution, look at Python GDAL/OGR Cookbook: Polygonize a Raster Band Commented Jul 13, 2015 at 9:20
  • Yes, I have seen pcjericks.github.io/py-gdalogr-cookbook/…. gdal_polygonize is fine. It would be especially great if you could give me a python implementation or syntax for use of gdal_calc.py. Then rest of the utilities i'll figure out accordingly. Thank you for you response anyway! :) Commented Jul 13, 2015 at 9:25
  • look at Error 193 while running gdal_calc.py using subprocess.call in Python for example Commented Jul 13, 2015 at 9:48
  • 1
    @gene I tried to run the command given in the answer on your mentioned link and it does run without error but the output file is not created. Same thing happened with the other commands which I mentioned in the question. Any idea why is that happening? Commented Jul 13, 2015 at 9:56
  • 2
    Or use rasterio, easier: polygonizing with rasterio Commented Jul 13, 2015 at 9:57

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.

answered Jul 13, 2015 at 9:38
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? 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. 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. Commented Jul 13, 2015 at 11:11
  • Can you upload the source raster, or use something that is available from the web for free? 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/… Commented Jul 13, 2015 at 13:12

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.