mask raster by many shapefile

515
1
06-22-2011 03:31 AM
havalhaji
New Contributor
Hi guys,
i am very new in python and i have problem with what i want to do.
i have many shapefiles (more than 35) and i have one image. i want to mask the image by these shapefiles. i have made a simple python code but it does not work!!.. i need some help to achieve that.

The code is

import sys, string, os, arcgisscripting, math , arcpy, arcgisscripting, string

from arcpy import env

from arcpy.sa import *

gp = arcgisscripting.create()

gp.OverWriteOutput = 1

gp.CheckOutExtension("spatial")

arcpy.CheckOutExtension("spatial")

gp.workspace = "D:/DdAaTtAa/Data/"


outshp = "D:/DdAaTtAa/Data/new/" # The output

fcstring = " "

fcs = gp.ListFeatureClasses()

Inraster = "D:/DdAaTtAa/Data/image/"

fc = fcs.next()
while fc:
    print fc
    fc = fcs.next()
fcs.reset()
fc = fcs.next()

print gp.GetMessages()

while fc:
    print fc
infc= fc
OutRaster = outshp + "/" + str(fc)
print gp.GetMessages()
gp.ExtractByMask_sa(Inraster, infc, OutRaster)
print gp.GetMessages()



Any help it would be highly appreciated.

cheers
Tags (2)
0 Kudos
1 Reply
StephenBarrow
New Contributor
So let's look at what you are doing
fcs = gp.ListFeatureClasses()

fc = fcs.next()
while fc:
    print fc
    fc = fcs.next()
fcs.reset()
fc = fcs.next()


With this code block (above) you are simply printing out the names of the fcs found and not processing anything, fcs.reset() does nothing as it is not implemented so don't use it. so the code below isn't going to do much work as fc will not hold a value.

print gp.GetMessages()
while fc:
    print fc
infc= fc
OutRaster = outshp + "/" + str(fc)
print gp.GetMessages()
gp.ExtractByMask_sa(Inraster, infc, OutRaster)
print gp.GetMessages()


If this did work infc would only be the last fc recorded so the mask will only be on that one and not all of them,

Why are you using gp and arcpy, just use arcpy.

try you code for just 1 fc first and build on that.

import arcpy
arcpy.CheckOutExtension("Spatial")
fcs = gp.ListFeatureClasses()
Inraster = "D:/DdAaTtAa/Data/image/"
for fc in fcs:
    print fc
infc= fc
OutRaster = outshp + "/" + str(fc)
gp.ExtractByMask_sa(Inraster, infc)
outExtractByMask.save(OutRaster)

which will give you a mask with the last fc

Once you have this working to work on all fc in the list you will need to combine (union/merge...) them into 1 and then apply the mask with this new fc.

Hope this helps to get you started if you haven't figured it out already.
0 Kudos