Select to view content in your preferred language

Generating new output names after each iteration

757
4
Jump to solution
12-06-2018 01:54 PM
Sam_Roodbar
Emerging Contributor

Hello Everyone, 

I'm trying to write a script that will take a look at a list of county boundaries and then take each boundary and use it as a mask on a raster so that I can do Extract By Mask. Ideally I would like to have the county names on the output but I cannot figure out a way to do that, but any unique name at this point would do. Here is the script that I use:

>>> import os
>>> import arcpy
>>> from arcpy import env
>>> from arcpy.sa import *
>>> env.workspace = "C:/Users/s/Desktop/S/test"
>>> inRaster = "escrec"
>>> counter = 1
>>> ctyList = arcpy.ListFeatureClasses ("*.shp")
>>> for shp in ctyList:
... out = arcpy.sa.ExtractByMask(inRaster,shp)
... out.save("C:/Users/s/Desktop/test/out/masked"+ counter)
... counter = counter+2
...
>>>

I tried using this counter thing but it doesn't work due to concatenate issue (str and numbers). Anyone has any suggestions or tricks that they have done? I tried setting the counter as my "ctyList" but since feature have .shp it doesn't allow that to save.

extract by mask‌ acrpy# 

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
Sam_Roodbar
Emerging Contributor

I worked out another way to do the same, much easier:

>>> import os
>>> import arcpy
>>> from arcpy import env
>>> env.workspace = "C:\XXX"
>>> ctyList = arcpy.ListFeatureClasses ("*.shp")
>>> inRaster = "nlcdrecmosaic"
>>> for shp in ctyList:
... CtyName = shp.split('.')[0]
... out= arcpy.sa.ExtractByMask(inRaster,shp)
... out.save("C:\XXX\out\mskd"+ CtyName)
...

View solution in original post

0 Kudos
4 Replies
SteveLynch
Esri Regular Contributor

out.save(r"C:\Users\s\Desktop\test\out\masked"+ str(counter) + ".tif")

Sam_Roodbar
Emerging Contributor

Thank you, I tried it and it works but the names of outputs were strange. 

I specified counter as 01001 (for the first county) but the outputs started 513!!

is there a way to have the name of the shp s for output?

0 Kudos
DanPatterson_Retired
MVP Emeritus
perhaps....

counter = 100

shp = "test.shp"

pth = r"C:\Users\s\Desktop\test\out\masked"

out = "{}\\{}{}.tif".format(pth, shp[:-4], counter)

out
'C:\\Users\\s\\Desktop\\test\\out\\masked\\test100.tif'
0 Kudos
Sam_Roodbar
Emerging Contributor

I worked out another way to do the same, much easier:

>>> import os
>>> import arcpy
>>> from arcpy import env
>>> env.workspace = "C:\XXX"
>>> ctyList = arcpy.ListFeatureClasses ("*.shp")
>>> inRaster = "nlcdrecmosaic"
>>> for shp in ctyList:
... CtyName = shp.split('.')[0]
... out= arcpy.sa.ExtractByMask(inRaster,shp)
... out.save("C:\XXX\out\mskd"+ CtyName)
...

0 Kudos