Renaming and Copying Raster Data

1643
8
12-15-2017 11:57 AM
PeterBordokoff2
New Contributor II

I am working on a script to rename and copy a large raster data set, in folders containing .prj, .tfwx, .tif.aux.xml, .ovr, and .tif.xml files with each .tif image. In running a simple rename and shutil.copy script (below) I lose parts of the metadata:

...

filCnt = 0
for file in files:
# Slice off 1st 4 digits of file name
    num = file[0:4]
    if num == "1982":
        filCnt = filCnt + 1
        sStg = 0
        sStg = file.find('L') # A few files contain L
        if sStg > 8 and sStg < 15:
            newFile = file[sStg+2:]
            shutil.copy(file,outDir+newFile)
            print newFile

        sStg = file.find('l') # A few files contain l
        if sStg > 8 and sStg < 15:
            newFile = file[sStg+2:]
            shutil.copy(file,outDir+newFile)
            print newFile

How can I modify this to keep the renaming convention, but use another copying procedure? The Copy Raster tool ignores the associated files and I would prefer not creating a database for this, given the amount of data. Any ideas? Thank you!

Running 10.3 btw

0 Kudos
8 Replies
JakeSkinner
Esri Esteemed Contributor

Hi Peter,

The Copy Raster tool should retain this all associated files.  Before executing the tool go into Environments > Raster Storage.  Make sure 'Build Pyramids' and 'Calculate Statistics' are checked.  This should preserver the .aux.xml and .ovr files.  The projection information should be stored in the header of the TIFF so it will be okay if there is no .prj file.

PeterBordokoff2
New Contributor II

Thanks Jake,

what is the best order of operations to achieve this? Can I call the tool on the same line as the shutil.copy command? Or, do I need another block?

0 Kudos
JakeSkinner
Esri Esteemed Contributor

The Copy Raster tool will copy the raster to the new location, and you can rename it using this tool as well.  No need to implement the shutil.copy command.

PeterBordokoff2
New Contributor II

Getting an error now when calling the module. Runtime error  Traceback (most recent call last):   File "<string>", line 43, in <module>   File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\management.py", line 12583, in CopyRaster     raise e ExecuteError: ERROR 000732: Input Raster: Dataset newFile does not exist or is not supported

filCnt = 0
for file in files:
# Slice off 1st 4 digits of file name
num = file[0:4]
if num == "1982":
filCnt = filCnt + 1
sStg = 0
sStg = file.find('L') # A few files contain L
if sStg > 8 and sStg < 15:
newFile = file[sStg+2:]
arcpy.CopyRaster_management("newFile+.tif", "outDir", "DEFAULTS" , "" , "" , "" , "" , "")
print "Processing..." +newFile

sStg = file.find('l') # A few files contain l
if sStg > 8 and sStg < 15:
newFile = file[sStg+2:]
arcpy.CopyRaster_management("newFile+.tif", "outDir", "DEFAULTS" , "" , "" , "" , "" , "")
print "Processing..." +newFile

What do I need to do here??

0 Kudos
PeterBordokoff2
New Contributor II

I tried it without an extension, I realize above it should be "newFile"+.tif, but it is not working

0 Kudos
JakeSkinner
Esri Esteemed Contributor

Try the following:

arcpy.CopyRaster_management("Path to Original Raster + Raster name", "Path to New Location + New raster name")

Ex:

arcpy.CopyRaster_management(r"C:\Raster\Original.tif", r"D:\Raster\New.tif")
PeterBordokoff2
New Contributor II

I can successfully rename the .tif files, but when I call CopyRaster_management I get an error that the input Raster doesn't exist or is not compatible. Is there an order of operations I am unaware of? Do the files need to be written to a folder after renaming them before moving them? 

0 Kudos
UMUTÜÇOK
New Contributor II

I suggest you to use arcpy.Rename_management if your raster files don't contain .dre files. Also you can use fnmatch and os to list and find your raster with extension you specified. For example:

def Rasterlist(folder):
    rasterlarunisiz = []
    numberoffiles = len(fnmatch.filter(os.listdir(folder), '*.tif'))
    arcpy.AddMessage("count of files:%s" % numberoffiles)
    unilirasterlar = fnmatch.filter(os.listdir(folder), '*.tif')
    arcpy.AddMessage("unicode rasters:%s" % unilirasterlar)
    ranger = range(0, dosyasayisi)
    for i in ranger:
        rasterlarnameunisiz = unilirasterlar.encode('UTF-8')
        rasterlarunisiz.append(rasterlarnameunisiz)
    arcpy.AddMessage("rasters without unicode: %s " % rasterlarunisiz)

Besides, you shouldn't worry about .ovr and, .tif.aux.xml etc.. Because most of those files was created by making pyramids on associated rasters.

And use Pycharm, use def.

0 Kudos