1

I have 150 feature classes in a file geodatabase. Each consist of a few polygons with a different label class. I want to make a selection on Label "1E-06", but some feature classes do not have this label, only for instance label 1E-01 to 1E-05. Is it possible to make some if statement, so if label 1E-06 does not exist in feature class, add row with label = "1E-06". It doesnt have to have geometry. I want to merge all 1e-06 labels later. For statistics purposes I need the empty ones too.

EDIT: I tried it myself (with help of answer below), but still get stuck. Right now, it can find all the shapefiles and put it in a database. It can search for rows with my expression, but I want to add a row for all features without the expression. I tried this with for row not in scursor: but this gives an error. How can I add a row in the features who do not have a row with the stated expression?

import arcpy
import os
count = 0
ws = "N:\Documents\Project\data"
dst654 = "N:\Documents\Project\Contour654.gdb"
dst800 = "N:\Documents\Project\Contour800.gdb"
expression654 = "Label = '1E-06 654'"
expression800 = "Label = '1E-06 800'"
for dirpath, dirnames, filenames in arcpy.da.Walk(ws,datatype="FeatureClass"):
    count += 1
    for file in filenames:
     filepath = os.path.join(dirpath,file)
     file_nice = file.replace('-', '_')
     file_nice = file_nice.replace(' ', '_')
     file_nice= file_nice.replace('.', '_')
     if '654' in file_nice:
         outpath1 = os.path.join(dst654,file_nice[:-4])
         arcpy.CopyFeatures_management(filepath,outpath1)
         arcpy.AddField_management(outpath1, "gid", "DOUBLE", "", "", "", "", "NULLABLE", "", "")
         arcpy.CalculateField_management(outpath1, "gid", count, "PYTHON", "")
         with arcpy.da.SearchCursor(outpath1, ['Shape', 'Label'],
                            where_clause=expression654) as scursor:
                                for row in scursor:
                                    print "yes"
                                    print outpath1
     else: 
         outpath2 = os.path.join(dst800,file_nice[:-4])
         arcpy.CopyFeatures_management(filepath,outpath2)
         arcpy.AddField_management(outpath2, "gid", "DOUBLE", "", "", "", "", "NULLABLE", "", "")
         arcpy.CalculateField_management(outpath2, "gid", count, "PYTHON", "")
         with arcpy.da.SearchCursor(outpath2, ['Shape', 'Label'],
                            where_clause=expression800) as scursor:
                                for row in scursor:
                                    print "yes"
                                    print outpath2
1
  • 1
    With the answer provided you should have a great start to the ArcPy code you need to write. If you get stuck then just ask a separate question about the step you are stuck on and include your code so far to try and do it.
    – PolyGeo
    Commented Aug 16, 2017 at 10:46

1 Answer 1

2

What you are asking could be done in ModelBuilder but is probably better done as a Python script because of the if-then-else logic.

Your code will need to:

  1. Create a featurelayer from the featureclass
  2. Attempt to select a row in the featurelayer where label equals "1E-06"
  3. If a selection (get count) returns a zero then insert a new row with your data
  4. Move on to your new featureclass and start from step 1.

So look at the help file for insert cursors, and select by attribute. If you actually bother to scroll down the page all tools have code samples, it is a great way to learn arcpy using Python for free.

1
  • I edited my question. I am still stuck trying to figure it out, and I bet it must be some simple extra code. Please help
    – user30058
    Commented Aug 18, 2017 at 8:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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