import arcpy from osgeo import gdal inputPath = arcpy.GetParamaterAsText(0) inputRaster = gdal.Open(inputPath) # do operations, etc., etc., etc.
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?
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.
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))
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)
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)
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 osgeo import gdal from osgeo import ogr
ogr2ogr and ogrinfo are commandline programs not python modules. This should fail with an ImportError exception.import ogr2ogr import ogrinfo
Use a list instead, it's easier to read:string=str("ogr2ogr -f"+ ' "KML"'+" "+ 'throwthisaway.kml'+" "+'test.dgn')
cmd=["ogr2ogr", "-f", "KML", "throwthisaway.kml", "test.dgn"]
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
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?