Using GDAL utilities within ArcGIS python scripting

9075
14
03-09-2012 10:03 PM
MichaelStead
Regular Contributor
I have GDAL installed with the python bindings. Does anyone know how I use this to build some tools in ArcGIS? I didn't check before the GDAL install, but I can import gdal in the python command line window now.
Tags (2)
14 Replies
StacyRendall1
Occasional Contributor III
Not really sure if it will work, but I can't see why it would not... You need to look into the normal method of making a script tool and using it within Arc (i.e. try this) for starters.

Then make a script like normal and add the 'from osgeo import gdal' line to import GDAL.

Arc really just uses paths for the inputs and outputs from script tools, which GDAL can then use to load files, so you can make use of the handy Arc interface, so your script might be like:
import arcpy
from osgeo import gdal

inputPath = arcpy.GetParamaterAsText(0)

inputRaster = gdal.Open(inputPath)

# do operations, etc., etc., etc.


Let me know how you get on, or if you have other questions!
Luke_Pinner
MVP Regular Contributor
Yes you can build script tools with GDAL just like other python packages. Make sure you install the appropriate GDAL bindings for the version of python you use with ArcGIS. Also, don't run the scripts 'in process', see the GDAL Python "Gotchas" wiki page.
0 Kudos
curtvprice
MVP Esteemed Contributor

Luke, now in 10x/Pro we have RasterToNumpyArray - does that provide a method to get our rasters from the numpy array to gdal in-process without the kludgy workaround detailed here?

0 Kudos
Luke_Pinner
MVP Regular Contributor

Curtis, I would think so, but haven't specifically tested.  I haven't had any issues with a number of python toolboxes I've written that use gdal. If I get a chance, I'll test.

0 Kudos
Luke_Pinner
MVP Regular Contributor

Kludgy workaround no longer required (tested on ArcGIS 10.2.2). This simple script works fine as a script tool run multiple times in-process from a binary (tbx) toolbox:

import arcpy
from osgeo import gdal
path = r'/path/to/some/raster'
ds = gdal.Open(path)
ras = arcpy.Raster(path)

arr = ds.ReadAsArray()
arcpy.AddMessage(repr(arr))
arr = ds.GetRasterBand(1).ReadAsArray()
arcpy.AddMessage(repr(arr))
arr = arcpy.RasterToNumPyArray(path)
arcpy.AddMessage(repr(arr))
0 Kudos
MichaelStead
Regular Contributor
I was originally just wanting a tool to create vrt's within ArcMap from folders of rasters. I just figured out how to call the built in GDAL command line scripts last night. Here is my super simple script for a raster conversion tool that just uses ArcGIS for inputs and outputs. I stuck a list of raster type codes into the 3rd parameter so you can pick them from a dropdown. Not all that useful as ArcMap could already do most common formats, but it gets me started.

This is all it is:
import arcpy
from subprocess import call
Input = arcpy.GetParameterAsText(0)
Output = arcpy.GetParameterAsText(1)
OutType = arcpy.GetParameterAsText(2)
String = "gdal_translate -of " + OutType + " " + Input + " " + Output
call(String)


here's all I did to be able to build VRT's from a folder of tiled images....

import arcpy
from subprocess import call
InputFolder = arcpy.GetParameterAsText(0)
InType = arcpy.GetParameterAsText(1)
OutputFolder = arcpy.GetParameterAsText(2)
VRTName = arcpy.GetParameterAsText(3)
String = "gdalbuildvrt " + OutputFolder + "\\" + VRTName + ".vrt " + InputFolder + "\\*." + InType
call(String)


I would really like to see an example of something that is using GDAL from python in the way it is meant to be incorporated, not using the command line scripts. I have found some very specific examples online that are from a tutorial for a class somewhere, but they refer to specific data and other scripts that I don't have access to.
0 Kudos
DougKnight
New Contributor II
Do you have this working?  I followed your example, but I am not using getparamsastext() as a input at this time. I tried to use the ogr2ogr utility in my snake script and I got it pretty far, but when I call it, basically nothing happens.  I suspect it may be syntax errors for the ogr2ogr inputs, but I am not too sure?  When I run this script subprocess.check_output I get the following

CalledProcessError: Command 'ogr2ogr -f "KML" throwthisaway.kml test.dgn' returned non-zero exit status 1

Here is my code:
import os.path
import subprocess
import os
import sys
import osgeo
import gdal
from osgeo import ogr
import ogr2ogr
import xml.parsers.expat
import ogrinfo
import subprocess
from subprocess import call

log=open(r'C:\Users\E186854\Desktop\logs\log.txt', 'a')

#Set this directory to your source folder1 declaration
folder1 =r"C:\Users\E186854\Desktop\dgn"

#get into the working folder
os.chdir('C:\Users\E186854\Desktop\dgn')

#make sure I am working in the proper folder at first
print os.getcwd()

string=str("ogr2ogr -f"+ ' "KML"'+" "+ 'throwthisaway.kml'+" "+'test.dgn')
subprocess.call (string, shell=True)
out=subprocess.check_output(string, shell=True)


log.close()
0 Kudos
Luke_Pinner
MVP Regular Contributor
import osgeo
import gdal
from osgeo import ogr
This is unnecessary , as you're not using the GDAL/OGR python bindings. Note the correct way to import gdal is "from osgeo import gdal"

import ogr2ogr
import ogrinfo
ogr2ogr and ogrinfo are commandline programs not python modules. This should fail with an ImportError exception.

string=str("ogr2ogr -f"+ ' "KML"'+" "+ 'throwthisaway.kml'+" "+'test.dgn')
Use a list instead, it's easier to read:
cmd=["ogr2ogr", "-f", "KML", "throwthisaway.kml", "test.dgn"]

You can use subprocess.Popen if you want to get the ogr2ogr error message:
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,stderr=subprocess.PIPE)
stdout,stderr=proc.communicate()
exit_code=proc.wait()
if exit_code: print stderr
else: print stdout
0 Kudos
curtvprice
MVP Esteemed Contributor

Hi Luke. Today I tried ArcGIS Pro's Conda setup to do this:

conda install Python=3.4.4 gdal

and it seemed to work, the python gdal tools seemed to all be there. Have tried this new approach?

0 Kudos