Count features in geodatabase

8131
18
02-22-2017 08:44 AM
MihaKlemencic
New Contributor III

I am new to python and I would to like to count the number of features in each feature class that meet certain criteria.

In geodatabase I have many feature classes. One of the attribute field has values "number/number" --> 50/2, 99/6

I would to like to loop each feature class, select layer by attribute and count selected features, which contain sign "/" and print the result. Below is python code:

import arcpy

#Set the input workspace
arcpy.env.workspace = "d:/TMP/miha_k/zirovnica/novo/test.gdb"

fcs = arcpy.ListFeatureClasses()
cursor = arcpy.SearchCursor(fcs)

for row in fcs:
      arcpy.SelectLayerByAttribute_management(fcs, "NEW_SELECTION", "PARCELA LIKE '%/%'")
      arcpy.GetCount_management(fcs)
      print row‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

I would appreciate your help 

Tags (2)
18 Replies
DanPatterson_Retired
MVP Emeritus

taking a wild guess that you got an error... what was it? what field are you querying in each table? is there any similarity between the tables? Have you done this manually? can you put your workflow into a model? 

Just some ideas to get you started

0 Kudos
MihaKlemencic
New Contributor III

Field "PARCELA" exits in all tables.

The workflow is:

  1. select by attributes
  2. write query "PARCELA LIKE '%/%'"
  3. Count selected features

But doing this workflow manually on more than 100 feature classes is time consuming.

0 Kudos
DarrenWiens2
MVP Honored Contributor

A few things:

- Cursors cycle through individual features within a feature class/table. You don't seem to need that here.

- When you do 'for row in fcs:', the individual feature class is called row. The list of feature classes is fcs. So, you should select from row, not fcs.

- Refer to the help for GetCount, especially the example. Note that it returns a result, not a number.

0 Kudos
RandyBurton
MVP Alum

As Darren suggested, I don't think you would want to cycle through all your features; they might not have the fields you want to query anyway.

Here's some code based on this link:

arcpy.env.workspace = r"d:/TMP/miha_k/zirovnica/novo/test.gdb"
in_table = "featureToSearch"

# MakeTableView_management (in_table, out_view, {where_clause}, {workspace}, {field_info})
arcpy.MakeTableView_management(in_table, "myTableView", "PARCELA LIKE '%/%'")
count = int(arcpy.GetCount_management("myTableView").getOutput(0))‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

But, if your features all contain the same field in your where query, you could do a "for row in fcs" loop.  You could also add an if "feature in list" line to limit the features used.

0 Kudos
RandyBurton
MVP Alum

To loop through all the features, you might try the following code.  However, if the where clause fails, you may get a count of all records.

import arcpy

arcpy.env.workspace = r"d:/TMP/miha_k/zirovnica/novo/test.gdb"

fcs = arcpy.ListFeatureClasses()

for row in fcs:
    arcpy.MakeTableView_management(row, "myTableView", "PARCELA LIKE '%/%'")
    print row, int(arcpy.GetCount_management("myTableView").getOutput(0))
    arcpy.Delete_management("myTableView")
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
DarrenWiens2
MVP Honored Contributor

Just to add that you can test whether or not there is a selection by inspecting the describe FIDSet property:

... if arcpy.Describe(fc).FIDSet == '':
...     print 'There is no selection'
... else:
...     print 'There is a selection'
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

FIDSet is a Layer property, calling it will generate an attribute error if Describe is passed a feature class directly.  If a table view or feature layer is passed to Describe, then FIDSet should return a value, but I think rvburton‌ approach obviates the need for checking FIDSet.

0 Kudos
DarrenWiens2
MVP Honored Contributor

Ah, yes, that's right. I was back on the original idea of using SelectLayerByAttributes, which now that I look at it, needs to be preceded by MakeFeatureLayer (or MakeTableView).

0 Kudos
MihaKlemencic
New Contributor III

Thanks for help 

Really need to learn python for ArcGIS. Can you suggest any of books?

0 Kudos