0

in Arcpy trying to create a subset of rasters from a list of rasters.

Basically the idea, run arcpy.ListRasters on a geodatabase, check all raster names for a user input value, then add those rasters that match to a new list. ideally, the user would be able to add the values they want as a list of values and the code iterates through each one. And actually I'm trying to get it so it will refine the new list further. but I only really need the code to do the first one.

Code so far

 listvals = arcpy.GetParameterAsText(0)
 listrasters = arcpy.ListRasters
 for raster in listrasters:
 rastname = str(raster)
 for val in listvals
 a = rastname.rfind(val,7,10)
 if a > 0:
 #add raster to newlist
 else:
 #do nothing
asked Feb 22, 2017 at 22:55
7
  • Are you just interested in how to create and modify a collection in Python? Commented Feb 22, 2017 at 23:45
  • What happens when you run this code? What gets printed when you make print listrasters your second line of code? Have you configured a tool dialog? Commented Feb 22, 2017 at 23:45
  • @AHigh That sounds exactly like what I'm trying to do. Commented Feb 22, 2017 at 23:46
  • @PolyGeo It returns the ListRaster function. <function ListRasters at 0x19AA96B0>. but this portion of code I know works to iterate through the rasters in the workspace (for my purposes a geodatabase). Yes I've got a tool dialog. Commented Feb 22, 2017 at 23:50
  • 1
    Your code will fail on lines 3 and 5 as For should be lowercase for Commented Feb 22, 2017 at 23:53

1 Answer 1

1

You should be able to just initialize an empty list and then append the raster to it if it matches your criteria. See below. I've also removed the unused else block and cleaned up some syntax issues.

listvals = arcpy.GetParameterAsText(0)
listrasters = arcpy.ListRasters
#Initialize an empty list
matchedRasters = []
for raster in listrasters:
 rastname = str(raster)
 for val in listvals:
 a = rastname.rfind(val,7,10)
 if a > 0:
 #add raster to newlist
 matchedRasters.append(raster)

Here's some material on collections and lists in Python that you may find useful: https://docs.python.org/3/tutorial/datastructures.html

answered Feb 22, 2017 at 23:50
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.