I'm attempting to batch project .sid rasters using python. I'm able to successfully use the tool in arcmap so I know the software is capable of doing it. Here is my code:
try:
import arcpy, sys
InFolder = r"C:\Original"
OutFolder = r"C:\Reproject"
OutSR = arcpy.SpatialReference(26918) # NAD83 / UTM zone 18N
arcpy.env.workspace = InFolder
for Ras in arcpy.ListRasters():
arcpy.AddMessage("Projecting " + Ras)
arcpy.ProjectRaster_management (InFolder + "\\" + Ras, OutFolder + "\\" + Ras,OutSR)
arcpy.AddMessage("Projecting complete")
except:
print "Project Raster failed"
print arcpy.GetMessages()
I'm getting the following: "ERROR 000445: Extension is invalid for the output raster format."
I'm thinking it has to do with the .sid format being turned into a .tiff but I'm unsure about how to get the program to output .tiff format.
1 Answer 1
Your code is trying to output .sid
files and not .tif
files. ArcGIS won't create .sid
files which is why you get the invalid output raster format error.
You need to specify that the output raster needs to be a .tif
. Something like the following should get you in the right direction:
import arcpy, sys, os
InFolder = r"C:\Original"
OutFolder = r"C:\Reproject"
OutSR = arcpy.SpatialReference(26918) # NAD83 / UTM zone 18N
arcpy.env.workspace = InFolder
rasterList = arcpy.ListRasters()
if not rasterList:
arcpy.AddWarning("No Rasters found in {}".format(InFolder))
else:
for Ras in rasterList :
inRaster = os.path.join(InFolder, Ras) # Path and Filename of input raster (.sid)
outFileName = "{}.{}".format(os.path.splitext(Ras)[0], "tif") # Remove .sid and add .tif to Ras filename
outRaster = os.path.join(OutFolder, outFileName) # Path and Filename of output raster (.tif)
arcpy.AddMessage("Projecting ", + Ras)
arcpy.ProjectRaster_management (inRaster, outRaster, OutSR)
arcpy.AddMessage("Projecting complete")
This will remove the .sid
from your filename to allow you to add .tif
on the output filename.
It appears that .sid
is not a supported format for arcpy.ListRasters()
- see the list of supported raster types at ListRasters - ArcGIS Desktop Help. You may need to change the arcpy.ListRasters()
to something like arcpy.ListFiles('*.sid')
instead.
-
I ran the code and I'm getting: "line 9, in <module> for Ras in arcpy.ListRasters(): TypeError: 'NoneType' object is not iterable". Ras should be referring to the rasters in the infolder so I'm wondering how it's a NoneType.Anthony Stokes– Anthony Stokes2016年09月12日 00:13:54 +00:00Commented Sep 12, 2016 at 0:13
-
@AnthonyStokes I've modified my code sample slightly, to check that
arcpy.ListRasters()
actually finds some rasters. If not it will give a message and not proceed through the loop.2016年09月12日 01:02:25 +00:00Commented Sep 12, 2016 at 1:02 -
It works wonderfully. My other question would be whether there is a process to compress the .tif files? The original sids are 24mb but the newly created .tifs are 240mb. Is there a way to work compression into this script?Anthony Stokes– Anthony Stokes2016年09月12日 02:01:48 +00:00Commented Sep 12, 2016 at 2:01
-
Take a look at
arcpy.env.compression
-> something likearcpy.env.compression = 'LZ77'
or one of the other options if preferred2016年09月12日 02:24:00 +00:00Commented Sep 12, 2016 at 2:24 -
The script runs successfully if all rasters have projection information. How would you suggest adding code that basically says, if input projection is none, move onto next raster?Anthony Stokes– Anthony Stokes2016年09月17日 22:08:25 +00:00Commented Sep 17, 2016 at 22:08
try
/except
statements so that you can see the error messages, including line number, from Python. To figure out what syntax to use to get TIFF output I would run the tool once via its tool dialog, and then use the Geoprocessing | Results window to Copy As Python Snippet.try
/except
statements) and any error and printed messages that you receive.