Select by attribute use cursor save off as feature class

3930
17
02-18-2014 10:39 AM
MikePowell
New Contributor II
I feel this is a very basic problem but for the life of me I can't seem to figure out what is wrong, which is driving mad. I am assuming it is my logic. I have a feature class with hundreds of polygons. I figured I could make a layer file, use a search cursor to select each polygon and save each one off as it's own feature class. I seem to always get an error that the dataset xxxxx does not exist or is not supported when trying to do Copy Feature.  Here is what I have:

import sys, string, os, cx_Oracle, arcpy, datetime

arcpy.env.workspace = "c:\\work\\scripting\\test\\testdata.gdb"
arcpy.MakeFeatureLayer_management("rsids","rsid_view")
rows = arcpy.SearchCursor("rsid_view","","","NUM; EXTENSION","NUM A")
row = rows.next()
while row:
    RSIDNum = str(row.getValue("NUM"))
    Ext = str(row.getValue("EXTENSION"))
    if Ext == "None":
        Ext = ""
    rsid = RSIDNum+Ext
    RSID = "RSID"+RSIDNum+Ext
    arcpy.CopyFeatures_management(RSID, "c:\\work\\scripting\\test\\testdata.gdb\\"+RSID)
    row - rows.next()
del row
del rows
if arcpy.Exists("rsid_view"):
   arcpy.Delete_management("rsid_view")
   print "View deleted"


I have tried using Select by attribute first (before the search cursor) but that almost seems redundant, and I get the same problem anyway. Any help will be appreciated. Thanks in advance
Tags (2)
0 Kudos
17 Replies
MikePowell
New Contributor II

Joshua, I was wondering if you were still out there. I am having a problem with the above script again (I know 2 yrs later). The FeatureClassTo FeatureClass is not working. I get the dreaded ERROR 9999999. I can't figure out why. Some stuff has changed in the database that I would think make thing easier but I just can't seem to get it past that point. If you are available for a little bit I was hoping to ask for a little help, since you were the one that was so kind to helped me before. If you can help I will fill you in on what has changed in the database and what I thought would work. Thanks again.

0 Kudos
DanPatterson_Retired
MVP Emeritus

Mike... I sent Josh a message, hopefully you will hear from him.

MikePowell
New Contributor II

I really appreciate it. I hope it’s not much of a problem

0 Kudos
JoshuaChisholm
Occasional Contributor III

Hello Mike,

I'm still kicking around. Feel free to ask away!

I'll see if I can help (and it looks like we may get help from guru Dan Patterson​)

MikePowell
New Contributor II

Again, thanks in advance for any help, and time. I hope this isn’t too hard to read. Basically, I am trying to do the same thing as before. I have a feature class (RSID)with a few hundred features. I would like to select each on and make it its own feature class and do stuff to it. So, I have everything done that I need to do to each RSID but I can’t seem to get the FeatureClassToFeatureClass to work. Now before, when we worked on it, there was a column NUM and an EXTENSION column that I needed and that is how we did what we did before. Now there is a NAME column that has what I need (both name and extension and when I dissolve on NAME I figure I don’t need a lot of what we had in there before. I thought that I could just search cursor through the NAME column, add “RSID” to the front of the name as I know that a feature class can’t start with a number (the NAME column looks like this ex. 789M, 801M, 541L) and make the name feature class. I am getting the ERROR 999999. I have what I did down below, and I have commented out the old code that we were using. To explain the script, I am making sure that that I have copies of feature classes and dissolving on NAME. I am than setting up some directories and feature datasets to put everything and making sure everything is correct with the slashes, than I do the searchcursor. It all works until it gets to the FeatureClassToFeatureClass. Any thoughts? I guess I haven't figured out how to copy and paste the script. The first attempt was pretty bad. The second was better but I decided just to attach it. Let me know if it doesn't work

0 Kudos
JoshuaChisholm
Occasional Contributor III

Hello Mike, Thanks for the clear post.

Nice looking script, I have a few suggestions of the get-go.

  1. the line arcpy.FeatureClassToFeatureClass_conversion(nfc, NewoutLocation, newFCName) has no where clause (the optional 4th parameter). I think this means that even if the line weren't erroring out, it would create a full copy of every row in the "nfc" feature class X times (where X is the number of rows in the "nfc" feature class).
    I think you will want to add a where clause so it only exports the current row each time. Something like this might work:
    arcpy.FeatureClassToFeatureClass_conversion(nfc, NewoutLocation, newFCName, "NAME = '"+row.NAME+"'")
  2. In addition to the query, you might want to try exporting to the GDB, not the Dataset within the GDB. I've run into problems before when exporting to a Dataset. Sometimes inconsistent projections or precisions can cause problems. Try outputting to "outLocation" for now, and exporting to a Dataset once we have everything else working. Your new line (including item 1 above, should look something like this:
    arcpy.FeatureClassToFeatureClass_conversion(nfc, outLocation, newFCName, "NAME = '"+row.NAME+"'")

Let me know if those 2 changes do anything. Also,any chance you can post a small data set for testing?

Good luck!

0 Kudos
MikePowell
New Contributor II

Cool, that seemed to work. I was under the impression that the where clause was optional and that you didn’t have to have it as a parameter. This is working now. It is bombing at another point in the script where it is doing other things but is using FeatureClassToFeatureClass_conversion without a where clause. I will have to look into that a little more to determine the where clause that I am going to need. I just haven’t reached that point before now. Thanks again for the help. I am glad to know that my thinking was correct in that I didn’t need all that other code now, but it makes me wonder why I didn’t dissolve it in the first place. Oh well, live and learn. I will keep in mind the Dataset issue as well. I will let you know if something else comes up. Again, appreciate the help

MikePowell
New Contributor II
Again thanks. That worked great. I tried a couple of different ways to make it blank but I never thought to not have quotes around None. Thanks for all the insight. The rest of the script is coming together great. Just cleaning up little things to make it all tie together. There will be one more section that I will have to write that will probably rack my brains a little but hopefully, I will be able to figure it out. If not, I will back on here. Thanks again
0 Kudos