I am really new in Python programming. I try to make a loop for Extract by Attributes tool in ArcGIS 10.1. I have a huge dataset of raster (760pc) and I want to extract a specific value. This part of script runs fine. But ArcGIS in this function can only produce a GRID format with 13 characters so it changes my original names (.img file).

How can I keep the original names of rasters from my datasets?

Maybe I have to produce a list of results and convert them to .img file?

When I did it, it has kept the new generated GRID name (Extract_1), not my original name.

Anyone can help?

# Name: ExtractByAttributes_Ex_02.py
# Description: Extracts the cells of a raster based on a logical query. 
# Requirements: Spatial Analyst Extension

# Import system modules
import arcpy
from arcpy import env
from arcpy.sa import *

# Set environment settings
env.workspace = r"d:\UEL\Data\poskod_cez_VI_2\ch_msi_vci"

# Set local variables
inRaster = "ch_n_msi_00_02_10.img"
inSQLClause = "VALUE = 5"
outFolder = r"d:\UEL\Data\poskod_cez_VI_2\export_values"

#Allows overwriting
arcpy.env.overwriteOutput = True

# Check out the ArcGIS Spatial Analyst extension license

# Get a list of the rasters in the workspace
rasters = arcpy.ListRasters()

# Loop through the list of rasters
for inRaster in rasters:
    # Set the outputname for each output to be the same as the input
    outRaster = outFolder + "\\" + inRaster

    # Process: Extract by Mask
    attExtract = ExtractByAttributes(inRaster, inSQLClause) 
# Save the output 

  • 1
    Do you need a GRID as output? GRID is the default but not the only output format. Change attExtract.save(r"d:\UEL\Data\poskod_cez_VI_2\export_values") to attExtract.save(r"d:\UEL\Data\poskod_cez_VI_2\export_values.img") to output a .img. – user2856 Mar 21 '14 at 11:51
  • Hi Luke, thanks for your answer. No, I dont need a GRID as output, the .img will be the best. I have tried your solution, bud simply adding .img (attExtract.save(r"d:\UEL\Data\poskod_cez_VI_2\export_values.img) convert only this one raster to .img format, not all rasters. The path: attExtract.save(r"d:\UEL\Data\poskod_cez_VI_2\export_values") represent only a folder to store the new-generated rasters. Maybe you know the other solution? – maycca Mar 21 '14 at 12:39
  • Are you wanting to save all rasters, or only the last? Your code only saves the last. And you should use a path, filename, and extension to save your raster, like @luke mentioned. i.e.(r"d:\UEL\Data\poskod_cez_VI\export_values\raster_name.img") – Barbarossa Mar 21 '14 at 13:19
  • Hi Barbarossa, no, I would like to save all rasters (to the different directory), not only the last one but keep their original names. Maybe I have firstly to keep the result object and then convert it to .img but iin this case I will loose my original raster names – maycca Mar 21 '14 at 13:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.