0

Does anyone know of a way to create a standalone shapefile from every entry (row) in the attribute table of a large set of polygons (4909 in total)? preferably using python in a way that the results could be stored as a list of values which could be assigned to a variable in an equation later.

  • 1
    Is your goal to have the unique shapefiles/features permanently, or only to treat each individual polygon separately within a script? Either is feasible, but the answers would be different. – Erica Jun 10 '14 at 13:38
  • ultimately they only need to be treated separately within the script, would there be a big difference in the steps? – Fred Jun 10 '14 at 13:48
  • 1
    In the steps, yes. Also, you'd be making a large number of copies of data that you really don't need (at least in my opinion) – Erica Jun 10 '14 at 13:50
  • ah, well that doesn't matter too much so long as i delete them after. I just need to run each line through a cost distance and then possibly cost path analysis (spatial analyst) – Fred Jun 10 '14 at 13:54
0

pseudo code:

  1. make empty list, like final_list = []
  2. create a search cursor, and do the following steps for each row as you iterate through it.
  3. get the FID value (or any field whose values are unique across the entire dataset) for the row
  4. create a feature layer using that value in the where clause
  5. create a print statement or something to confirm that only 1 feature exists in the feature layer
  6. use copy features to create a new shapefile or feature class from that one-feature feature layer.
  7. append information to final_list (not sure what you want, a list of paths to the new shapefiles, or what)

You could define a function that returns final_list, or you could just add more code to the above in order to use the final_list variable.

EDIT: Here is an real code example of how to make a feature layer for each row. I work mostly with feature classes, so you may have to make slight syntax alterations for shapefiles, but this is basically it. Also, like @Paul said, check out the data access (da) module for the new and improved cursor objects. Below I'm using the older cursor objects (they work fine too).

shp = r"shapefile\path"
for row in arcpy.SearchCursor(shp):
    fid = str(row.getValue("FID"))
    where = '"FID" = ' + fid
    out_fl = r"in_memory\fl_{0}".format(fid)
    arcpy.MakeFeatureLayer(shp,out_fl,where)
    #this should be a one-feature feature layer.
    #do cost analysis stuff with out_fl as input
    #you can name the output raster using the fid

Like I said, I don't have experience with this tool, so I'm not sure of the best way to collect all of the information from the resulting rasters. Perhaps you could do it all in the search cursor, e.g. make a temporary raster layer with the cost path tool, compare against your baseline, and print those results to a log file, etc.....

  • thanks! what does 'use copy rows' mean? i did something similar with assigning the lines to lists, but then needed to run the information from each line (so each entry in the list) through the CostDistance tool and couldn't find a way of automatically changing the variable for each iteration. any idea on if that would be possible with this method? cheers – Fred Jun 10 '14 at 13:52
  • Hi Fred, I made a mistake, should have said "copy features". I'll edit my answer. However, it sounds like you just want to use each row for the cost tool, so instead of making a list you can just use that cost tool instead of steps 6 and 7 above, and use the one-feature feature layer as the input features. That way, like erica says, you won't have to worry about making and deleting shapefiles. – mr.adam Jun 10 '14 at 14:12
  • oh ok, thanks! any idea of the proper synthax to run each one-feature feature layer as an automated task through the cost tool? because with 4909 features it's too much to do manually.. – Fred Jun 10 '14 at 14:14
  • yeah, you can do it within the search cursor, so it runs for each row. – mr.adam Jun 10 '14 at 14:17
  • 1
    Don't forget to remove the datasets and layers stored in memory. – Paul Jun 10 '14 at 15:46
1

For running operations on one feature at a time within a script, I prefer to use cursors. You still get to analyze one feature at a time, but don't have to deal with creating/deleting features.

fc = 'yourshapefile.shp'
cursor = arcpy.SearchCursor(fc)

for row in cursor:
    costDist = CostDistance(row, 'raster')
  • thanks so much! i've been trying to get something to do what that second bit of code does for days, and it's way more complicated and doesn't work! so i'll give yours a try right now. any idea which way would be best to store the results? i have a threshold cost i want to compare my results to and am still defining wether I should calculate cost Path as well – Fred Jun 10 '14 at 14:28
  • but it looks like the input for cost path (or cost distance) must be a feature layer/raster layer, not a row object, which is why I recommended making a feature layer using a where clause for each row, and using that feature layer as the input data for the tool. That said, I haven't used that tool so I can't say for sure... – mr.adam Jun 10 '14 at 14:30
  • If you're using 10.2, you're better off with the cursors in the data access module. – Paul Jun 10 '14 at 14:33
  • Yes, if the Cost tool requires feature layers, @mr.adam is right you'll need his approach -- it is still working with the search cursor, but add extra steps [copy features etc] within that loop. To store the results, you'll need some way to change the output name -- e.g. pull the FID from the row and append it to the raster output name. I may be able to expand the code snippet here later, but not for a few hours. – Erica Jun 10 '14 at 14:47
  • thanks for the help everyone. Erica, I tried your method and keep getting runtime errors. i'm going to try making feature layers, @mr.adam , could you possibly give an example of code for the 'where clause for each row'? i'm relatively new to python and am having trouble conceptualising this – Fred Jun 10 '14 at 15:08

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.