3

I am new to GIS.

I have several rasters in one file location

I am using clip tool to loop over several rasters in arcgis. When I start I am choosing workspace, which is the folder where my input files are. However after clip I want to save output into different file. I could not figure it out how to do.

Can you please show me?

Here is my code.


# Import arcpy module
import arcpy
from arcpy import env
from arcpy.sa import *
# Setting working directory
arcpy.env.workspace = r"D:\Research\arcgis"
# Check out any necessary licenses
arcpy.CheckOutExtension("spatial")
# Local variables:
Tr_pro = "D:\\Tr2009\\admin\\Trpro.shp"
Tr_lyr = "D:\\Tr2009\\admin\\Trlayer"
Tr_dist = "D:\\Tr2009\\admin\\Trdist.shp"
arcpy.MakeFeatureLayer_management(Tr_dist, Tr_lyr)
arcpy.SelectLayerByAttribute_management(Tr_lyr, "NEW_SELECTION", "\"Name\" = 'Adi'")
rasterList = arcpy.ListRasters("F*")
for raster in rasterList:
 arcpy.Clip_management(raster, "25.665181 35.812875 44.826408 42.104629", "M"+str(raster[1:]), Tr_lyr, "255", "ClippingGeometry", "NO_MAINTAIN_EXTENT")

If i use this code, it saves all output into the workspace that I defined in the beginning. However I want to save it to another location such as "D:\Tr2009\admin\output

Midavalo
30k11 gold badges53 silver badges108 bronze badges
asked Apr 23, 2016 at 22:10

1 Answer 1

1

Add an output path parameter (outputPath = "D:\\Tr2009\\admin\\output\\") and use that for your output raster in your clip e.g.

arcpy.Clip_management(raster, "25.665181 35.812875 44.826408 42.104629", outputPath + "M"+str(raster[1:]), Tr_lyr, "255", "ClippingGeometry", "NO_MAINTAIN_EXTENT")

Here is your code again with the above changes:

# Import arcpy module
import arcpy
from arcpy import env
from arcpy.sa import *
# Setting working directory
arcpy.env.workspace = r"D:\Research\arcgis"
# Check out any necessary licenses
arcpy.CheckOutExtension("spatial")
# Local variables:
Tr_pro = "D:\\Tr2009\\admin\\Trpro.shp"
Tr_lyr = "D:\\Tr2009\\admin\\Trlayer"
Tr_dist = "D:\\Tr2009\\admin\\Trdist.shp"
outputPath = "D:\\Tr2009\\admin\\output\\"
arcpy.MakeFeatureLayer_management(Tr_dist, Tr_lyr)
arcpy.SelectLayerByAttribute_management(Tr_lyr, "NEW_SELECTION", "\"Name\" = 'Adi'")
rasterList = arcpy.ListRasters("F*")
for raster in rasterList:
 arcpy.Clip_management(raster, "25.665181 35.812875 44.826408 42.104629", outputPath + "M"+str(raster[1:]), Tr_lyr, "255", "ClippingGeometry", "NO_MAINTAIN_EXTENT")
answered Apr 23, 2016 at 22:47
1
  • Thank you so much. I knew it was a minor thing but could not figure it out from other questions. Commented Apr 25, 2016 at 17:45

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.