Select to view content in your preferred language

Using Python to copy more than one raster at a time

1583
4
Jump to solution
03-27-2014 06:33 AM
GiovanniOcchiali
Emerging Contributor
Hi everyone,

sorry if I am cross posting but I am relatively new to the ArcGIS world.
I think my problem will be easily solvable by somebody with a little knowledge of ArcGIS/Programming.
Basically, I need to transform a few thousands of .tiff raster into .bil and I was trying to run a simple Python command along the line of:

import arcpy
arcpy.CopyRaster_management("e:/Pyton/prova/tif/(africa_arc.1983*)","e:/Pyton/prova/bil/(1983*.bil)")

because all the input file starts with the same name and I don't particularly care about the output name as long as the file are distinguishable from one another. There is obviously a lot wrong with my command line, the only small experience I have in programming is in STATA which works very differently (I am an economist and there is one person in my whole department knowing what ArcGIS is, let alone Python).
If this is as easy as I imagine having a little knowledge on the language and anybody feels like giving me an answer this will save me a lot of time!
Thanks in advance

Giovanni
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
Zeke
by
Honored Contributor
In your first example, you should pass CopyRaster dataset, rather than datasetList. Also not a good idea to use the word dataset as a variable name, since that has a specific meaning in ArcGIS and may be a reserved word in arcpy as well. You could just use d instead. Then, you're correct that you need to give an actual path & raster name.
In your ListRasters call, "Raster" is an incorrect specification. In your case you want "TIF". I've also never seen a wildcard listed with a letter in List type functions, but don't know that it's incorrect. In any case, it would only get tifs starting with 'a'.

Here's your first code corrected:
import arcpy  arcpy.env.workspace = "E:/Pyton/Prova/Tif"  datasetList = arcpy.ListDatasets("*", "TIF")  for d in datasetList:          arcpy.CopyRaster_management(d,"e:/Pyton/prova/bil/new_rastername.bil") 

See here  .

As for your second example, I didn't go through it closely, but generally,
it's not a good idea to end paths with a slash. Also, you don't want two
slashes if you're using '/'. You'd only use two with '\' because '\' is
an escape character, or if you were starting a UNC path.

View solution in original post

0 Kudos
4 Replies
Zeke
by
Honored Contributor
You want to use ListRasters() and a for loop. An example from ESRI:

import arcpy 
from arcpy import env  

# Set the current workspace  
env.workspace = "C:/Data/DEMS" 
 
# Get a list of ESRI GRIDs from the workspace and print 
rasterList = arcpy.ListRasters("*", "GRID") 
for raster in rasterList:     
    print raster # replace this with your copy code


The ()'s and *'s in your file names might be a problem, though. Also, a few thousand will take a long time, and might overwhelm your system. You might also want to look at the Raster to Other Format tool.
0 Kudos
GiovanniOcchiali
Emerging Contributor
Thank you very much,

that hasn't work but it was useful in pointing me towards the right direction, but unfortunately I'm still not there.
When trying with arcpy.ListRasters I could not seen anything happening (not even the print line was actually returning the raster list) so I have tried with arcpy.ListDataset specifying the "Raster" wildcard

import arcpy
arcpy.env.workspace = "E:/Pyton/Prova/Tif"
datasetList = arcpy.ListDatasets("a*", "Raster")
for dataset in datasetList:
    arcpy.CopyRaster_management(datasetList,"e:/Pyton/prova/bil/")


but the return code I was then having is a general "Runtime error <type 'exceptions.RuntimeError'>: Object: Error in executing tool" which I guess might depend on the fact that I haven't specified the name of the file to be saved.
After a further look around Help and Forum I am now trying something along the line of:

import arcpy, os
from arcpy import env
InputLoc = raw_input("E:/Pyton/Prova/tif/")
arcpy.env.workspace = InputLoc
targetDatasetList=arcpy.ListDatasets("a*", "Raster")
outLoc = raw_input("e:/Pyton/prova/bil/")
outDirectory = outLoc
for dataset in targetDatasetList:
    tif = str(dataset)
    outRast = tif[:-4] + "_jul.bil"
    outDataset = outDirectory + "//" + outRast
    arcpy.CopyRaster_management(tif,outDataset)


which though returns "Runtime error <type 'exceptions.EOFError'>: EOF when reading a line"
Any idea of what might be wrong?
Again, thank you very much in advance to whoever is going to dedicate me sometime.
🙂

Giovanni
0 Kudos
Zeke
by
Honored Contributor
In your first example, you should pass CopyRaster dataset, rather than datasetList. Also not a good idea to use the word dataset as a variable name, since that has a specific meaning in ArcGIS and may be a reserved word in arcpy as well. You could just use d instead. Then, you're correct that you need to give an actual path & raster name.
In your ListRasters call, "Raster" is an incorrect specification. In your case you want "TIF". I've also never seen a wildcard listed with a letter in List type functions, but don't know that it's incorrect. In any case, it would only get tifs starting with 'a'.

Here's your first code corrected:
import arcpy  arcpy.env.workspace = "E:/Pyton/Prova/Tif"  datasetList = arcpy.ListDatasets("*", "TIF")  for d in datasetList:          arcpy.CopyRaster_management(d,"e:/Pyton/prova/bil/new_rastername.bil") 

See here  .

As for your second example, I didn't go through it closely, but generally,
it's not a good idea to end paths with a slash. Also, you don't want two
slashes if you're using '/'. You'd only use two with '\' because '\' is
an escape character, or if you were starting a UNC path.
0 Kudos
GiovanniOcchiali
Emerging Contributor
Thanks,
I did now get to a working code:

import arcpy 
arcpy.env.workspace = "E:/Pyton/Prova/Tif" 
outLoc = ("e:/Pyton/prova/bil")
outDirectory = outLoc
datasetList = arcpy.ListDatasets("*", "Raster") 
for d in datasetList:
 tif = str(d)
 outRast = tif + "_jul.bil"
 outD = outDirectory + "/" + outRast
 arcpy.CopyRaster_management(tif,outD) 


Apparently though TIF is a specification only for RasterList (which for some reason appears not to work for me) but not for DatasetList (while the a* depended on the fact that all my .tif start with a 🙂 )
Thanks again!
0 Kudos