3

I borrowed a simple script I found online to clip a shapefile from the extent of a raster. It works perfectly from the ArcMap built in python command prompt. I modified is slightly to use in a tool. It's having issues accepting the raster (inraster) in:

extent = arcpy.Raster(inRaster).extent

I get the following error:

extent = arcpy.Raster(inRaster).extent TypeError: expected a raster or layer name

I'm using "Raster Layer" as my inRaster parameter type. The raster type I am using is an ERDAS IMAGINE .img file. I'm thinking it must be conflicting with the parameter type and the .img input raster, but I can't figure out what it is. As I mentioned, it worked from the command prompt, just not in the tool. Any suggestions are welcome.

Code is as follows:

import arcpy

inLines = arcpy.GetParameter(0)
inRaster = arcpy.GetParameter(1)
outRaster = arcpy.GetParameter(2)

pnt_array = arcpy.Array()
extent = arcpy.Raster(inRaster).extent
pnt_array.add(extent.lowerLeft)
pnt_array.add(extent.lowerRight)
pnt_array.add(extent.upperRight)
pnt_array.add(extent.upperLeft)

poly = arcpy.Polygon(pnt_array)

arcpy.Clip_analysis(inLines, poly, outRaster)

2 Answers 2

2

Try changing the inRaster data type to "Raster Dataset" rather than "Raster Layer". Unless you have created a raster layer (usually with Make Raster Layer (Data Management)), you will not be able to call it in the script. If you are displaying a raster in Arc's table of contents, you've also created a raster layer and you will be able to call it as such within the command prompt. However, if you are running a tool without first creating the raster layer, you will get an error.

1
  • Thanks Aaron. I tested this and it wasn't my issue. Good suggestion on the 'Make Raster Layer' function. Thanks for taing the time. I found my answer and I will post it.
    – Mike
    Apr 15, 2013 at 15:52
0

The issue I had was that I was using a file path that contained spaces. Python interpreted it as a string instead of a file path I believe. There's a few ways to fix this, but I ran a loop and tested for an absolute path and parsed the input file name accordingly. For anyone who may get caught in the same scenario, try something like this after you have built the polygon from the array:

for shp in inLines.split(';'):

if os.path.isabs(shp) == True:
    (filename, ext) = os.path.splitext(os.path.basename(shp))
    arcpy.Clip_analysis(shp, poly, outRaster + "\\" + str(filename) + "_Clip")

elif os.path.isabs(shp[1:-1]) == True:
    (filename, ext) = os.path.splitext(os.path.basename(shp))
    arcpy.Clip_analysis(shp[1:-1], poly, outRaster + "\\" + str(filename[0:-1]) + "_Clip")

else:
    shpStrip = shp.strip("'")
    arcpy.Clip_analysis(shpStrip, poly, outRaster + "\\" + str(shpStrip) + "_Clip")
1
  • 2
    You could also use os.path.join() to append you filepaths together instead of using + "\\" +. Apr 15, 2013 at 16:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.