michelstos

Problems if "Extract by Mask script loop"

Discussion created by michelstos on Jan 10, 2011
Latest reply on Jan 11, 2012 by sypou
I'm a beginner using Python and I have some doubts.
My first script is write for extract some areas of interest (vector) of many topographic charts (raster) using "Extract By Mask"....and put a output name similar to the input name of the charts...

First I set the input folder "E:/GIS_Mestrado/Corumbatai/3_Cartas_um_metro"...there is about 80 charts...
The areas of interest were set in "E:/GIS_Mestrado/Corumbatai/Geodados/Grid_Cartas_sem_Grade.shp"...

And a have to loop the extract by mask in all the rasters in input folder...
Anyone can help me ??

Thanks...
PS: I'm using ArcGIS 9.2 yet...


Below is the script...
---------------------------
# Extracts the cells of a raster that correspond with the areas
# defined by a mask.
# Author: Michel
# Date: 10 de Janeiro de 2011
# -----------------------------

# Import system modules
import sys, string, os, arcgisscripting

# Create the Geoprocessor object
gp = arcgisscripting.create()

# Check out any necessary licenses
gp.CheckOutExtension("spatial")

# Load required toolboxes...
gp.AddToolbox("C:/Arquivos de programas/ArcGIS/ArcToolbox/Toolboxes/Spatial Analyst Tools.tbx")

GP.workspace = "E:/GIS_Mestrado/Corumbatai/3_Cartas_um_metro"
out_workspace = "E:/GIS_Mestrado/Corumbatai/4_Cartas_Con/"
mascara = "E:/GIS_Mestrado/Corumbatai/Geodados/Grid_Cartas_sem_Grade.shp"

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

# Loop through the list of rasters
rasters.reset()
raster = rasters.next()

while raster:
    # Set the outputname for each output to be the same as the input
    output = out_workspace + raster
  
    # Process: Extract by Mask...
    gp.ExtractByMask_sa(raster, mascara, output)

    # Loop da função...
    raster = rasters.next()
  
except:
    # If an error occurred while running a tool, then print the messages.
    print gp.GetMessages()

Outcomes