ronny115

Looping with clip tool

Discussion created by ronny115 on Feb 11, 2013
Latest reply on Feb 12, 2013 by Caleb1987
Hello, I have a script that loops in sequence through two numbers that corresponds  for identification of single polygons in a layer feature, for each polygon, I perform a clip and then a Isocluster Unsupervised for the clipped raster, followed by field calculations...

My problem here is the time to execute all the loops, if the range is small no problem the operations are made quite fast, but when I need to make 1000 or even 10000 operations, starts fast but slows down when has done a bunch on loops...

I though that maybe is question of memory but I'm running on windows 7 64bit 6gb of ram and Core i7 (4 cores @2.8GHz) and all used memory is 2GB and the python process don't even reach 400 MB of used memory

So what i can do to avoid the slow dows or, is there another approach/tool to do what im doing?.

Here is the code

def nombre(Var1,Var2,rasterTemp,layer,x):
 for fid in xrange(int(Var1), int(Var2)+1):
  arcpy.SelectLayerByAttribute_management(layer, "NEW_SELECTION", "FID ={0}".format(fid))
  arcpy.Clip_management(rasterTemp,"#","eR",layer,"0","ClippingGeometry")
  
  try:
   outUnsupervised = IsoClusterUnsupervisedClassification("eR", "2", "3")
   outUnsupervised.save("iC")
  except:
   print "Error en parcela {0}".format(fid)
   continue
  arcpy.AddField_management("iC", "Area_Sombreada", "DOUBLE", "15", "2", "","", "NULLABLE")
  arcpy.AddField_management("iC", "Identificador", "DOUBLE", "15", "2", "","", "NULLABLE")
  arcpy.AddField_management("iC", "PercentArea", "DOUBLE", "15", "2", "","", "NULLABLE")

  arcpy.CalculateField_management("iC", "Identificador", "{0}".format(fid), "PYTHON")

  expression1 = "float(!Count!) * 0.25" 
  with arcpy.da.SearchCursor("iC","Count") as rows:
   for row in rows:
    x += row[0]
   del row,rows
  print x
  expression2 = "float(!Count!) / {0}".format(x)
  arcpy.CalculateField_management("iC", "Area_Sombreada", expression1, "PYTHON")
  arcpy.CalculateField_management("iC", "PercentArea", expression2, "PYTHON")
  x = 0
  with arcpy.da.SearchCursor("iC","*","Value = 1") as rows:
   for row in rows:
    areaSombreada = row[3]
    identificador = row[4]
    percentArea = row[5]
   print row
   del row,rows
####Adds the results to a New table


Thanks and sorry for my english

Outcomes