0

I'm running a for loop with a cursor to disaggregate a large feature class into many smaller ones (of one polygon each). ArcMap crashes after about 1100 iterations, and I am trying to break the job down into more manageable snippets. I just can't get the code right. I have 4909 entries, and want to do it in five sets of 1000.

Below is a copy of my original code.

How do I change it so that it runs for a selection of rows at a time?

fc='C:\\Users\\xxxxx\\popcenters\\miyun_villages_poly.shp'
cursor=arcpy.SearchCursor(fc)
for row in cursor:
    FID = str(row.getValue("FID"))
    where = '"FID" =' + FID
    out_fl='C:\\Users\\xxxx\\miyun_allvillages.gdb\\allvillages_miyun\\fl_{0}'.format(FID)
    arcpy.MakeFeatureLayer_management(fc,out_fl,where)
    SaveToLayerFile_management(out_fl, allvillagesfl)
  • why not delete the feature layers after being used ? arcpy.Delete(out_fl) after saving to layer file. – radouxju Jun 11 '14 at 14:24
  • oh i never even thought of that. do you reckon that will stop it from crashing when it reaches 1100? – Fred Jun 11 '14 at 14:40
  • thanks for the input @radouxju, I tried it and it crashed again! – Fred Jun 11 '14 at 15:31
  • 1
    What version of ArcGIS and what level of licence do you have ? There are different ways to achieve your objective with simpler code, but it depends on what you have. Also saveTolayerfile does not create a new feature class, bt a new layer to still points to the full feature class. – radouxju Jun 11 '14 at 19:07
  • i'm using ArcMap 10.2.2 with an Advanced license type. i'm now running it straight through a costdistance tool (the next step in my analysis) rather than saving all these FIDs, so far it's working. Thanks for your help this morning @radouxju – Fred Jun 11 '14 at 19:26
1

FID values in shapefiles always start at 0 and are consecutive and ascending (while you are not editing) you can use that to your advantage.

Python doesn't do a for n = 1 to value loop but you can do FidList = range(start,stop,step) and then iterate for each FidList.

import os,sys,arcpy

InFeatures = sys.argv[1]

FtCnt = int(arcpy.GetCount_management(InFeatures).getOutput(0))
FIDlist = range(0,FtCnt) # start at 0 is defalut, added for clarity
# you can do lots of 1k here by doing range(0,1000) then range(1000,2000) ...
# range(0,1000) returns a list of 0 to 999
# range(1000,2000) returns a list of 1000 to 1999

for ThisFid in FIDlist:
    DefQ = "\"FID\" = %i" % ThisFid
    arcpy.MakeFeatureLayer_management(InFeatures,"Layer",DefQ)
    # save your layer here
    out_fl="C:\\Users\\xxxx\\miyun_allvillages.gdb\\allvillages_miyun\\fl_%i.lyr" % ThisFid
    arcpy.SaveToLayerFile_management("Layer",out_fl)

    # clean up
    arcpy.Delete_management("Layer") # clear the reference to the layer

This will not work for any other data source.

  • This code appears to spit out one feature at a time, doesn't the OP want batches of 1000? – Hornbydd Jun 11 '14 at 22:22
  • Yes, @Hornbydd, that's what the op is after. One feature per layer file... but due to crashing the op wants to do 1k, stop, the next 1k then stop... until all done. That can be done with the lists using the range() function. – Michael Stimson Jun 11 '14 at 22:24
  • thanks for the reply @MichaelMiles-Stimson! i'll try to wriggle this into my code. – Fred Jun 11 '14 at 23:05

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.