2

I'm trying to use and recreate this code from Using GDAL polygonize from Python?

import subprocess, glob

script = "/usr/bin/gdal_polygonize.py"
for raster in glob.glob("/data/brent/GAUP/Jul21_17-Apr27_18_results/imp_malaybalay/comp/*.tif"):
    outshp = raster[:-4] + ".shp"
    subprocess.call(["python", script, raster, "-f", "shp", outshp])

The only difference is that I am specifying an ESRI shapefile format as an output vector type (the link above used GeoJSON instead).

After I run the code, there were no results in my output folder. Is there something I missed out?

I also tried this code. However, I don't have any idea on how to edit the code in a batch processing form.

4
  • 1
    Try subprocess.Popen to return an object you can poll for error messages.. it's possible the process has errors you can't see. The driver for shapefiles is -f "ESRI Shapefile" so you might need to change your list to ["python", script, raster, "-f", "\"ESRI Shapefile\"", outshp].. or not, OGR will try to guess based on your file extension if -f is omitted. If you run python followed by a script in your shell does that work for you? In Windows I would omit the "python" because the script is associated with python.exe. Commented May 29, 2018 at 3:55
  • Hello, I changed the "shp" into "ESRI Shapefile" from the subprocess. call line. Maybe you can edit my code and accept it as an answer. Thanks a lot! Commented May 29, 2018 at 4:12
  • If my comment has helped you come to an answer feel free to answer your own question with your updated code.. I don't know much about the Linux (or is it Unix) operating system you're using so I'd be interested to see what works for you. Commented May 29, 2018 at 4:15
  • 1
    It is documented in gdal.org/gdal_polygonize.html how to define the output format. The list of the short format names is in gdal.org/ogr_formats.html.
    – user30184
    Commented May 29, 2018 at 4:46

1 Answer 1

4

As said by Michael Stimson from the comments, here's the answer for my own question:

import subprocess, glob

script = "/usr/bin/gdal_polygonize.py"
for raster in glob.glob("/data/brent/GAUP/Jul21_17-Apr27_18_results/imp_malaybalay/comp/*.tif"):
    outshp = raster[:-4] + ".shp"
    subprocess.call(["python", script, raster, "-f", "ESRI Shapefile", outshp])

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.