Select to view content in your preferred language

use ListFeatureClasses to provide output names within for: loop

2483
10
Jump to solution
12-09-2021 11:26 AM
PhilCrossley1
Occasional Contributor

Thanks for previous help.  This question follows on an earlier one that was so helpfully answered.

I've generated a 'ctylist' variable with ListFeatureClasses

I now want to iterate through that list (of counties), and use each county boundary in a sequence of spatial queries and geoprocessing tasks.  

I think the final results would be best if they were within a unique feature dataset (one for each county).

I've set up the code snippet below)to create those feature datasets (a set of county feature classes in a gdb created earlier in the script by StripByAttribute) within the for: loop, but can't figure out how to assign the county name to the new feature dataset (the Pro help examples show how to provide a specific name.

I think It would help to create a 'name' variable..and I've seen others suggesting use of a .format. command, but I'm stuck ..

ctylist = arcpy.ListFeatureClasses(r"E:\advAppScripting\testing\tornhazard\Indiana\Indiana.gdb")
for cty in ctylist:
arcpy.CreateFeatureDataset_management(r"E:\advAppScripting\testing\tornhazard\Indiana\Indiana.gdb", cty)
arcpy.CopyFeatures_management(cty, r"E:\advAppScripting\testing\tornhazard\Indiana\Indiana.gdb" + cty)

WIll appreciate any suggestions.

0 Kudos
2 Solutions

Accepted Solutions
DanPatterson
MVP Esteemed Contributor
import os
gdb = r"E:\advAppScripting\testing\tornhazard\Indiana\Indiana.gdb"
ctylist = arcpy.ListFeatureClasses(gdb)
for cty in ctylist:
    out_fc = os.path.join(gdb, cty)
    arcpy.CreateFeatureDataset_management(gdb, cty)
    arcpy.CopyFeatures_management(cty, out_fc)

# or
gdb = r"E:\advAppScripting\testing\tornhazard\Indiana\Indiana.gdb"
ctylist = arcpy.ListFeatureClasses(gdb)
for cty in ctylist:
    arcpy.CreateFeatureDataset_management(gdb, cty)
    arcpy.CopyFeatures_management(cty, f"{gdb}\\{cty}")

... sort of retired...

View solution in original post

DanPatterson
MVP Esteemed Contributor

sometimes I forget too

desc = arcpy.Describe("your featureclass path and name")
if hasattr(desc, "dataType"):
    print("DataType:    " + desc.dataType)

Describe object properties—ArcGIS Pro | Documentation


... sort of retired...

View solution in original post

0 Kudos
10 Replies
JoeBorgione
MVP Emeritus

I think the final results would be best if they were within a unique feature dataset (one for each county).

Are you using feature datasets to organize your data or are you using them as they were designed to be a container of features that have special characteristics or relationships like topology, network datasets, etc?  If the former, my suggestion is to rethink that approach as it will eventually cause you headaches.

As far as your for loop is concerned, that's pretty straight forward and if you are using Pro you are using python 3.x so while you can use the format function, I prefer "f-strings":

countyList = ['Cache', 'Weber', 'Rich', 'Salt Lake']
for name in countyList:
     print(f'Some extra text {name}')

 Copy and paste this code into your python ide of choice....

 

That should just about do it....
0 Kudos
PhilCrossley1
Occasional Contributor

Thanks for the advice about using feature datasets improperly.  For now, it has worked ok for organizing the outputs better but will consider a separate fileGDB for each in the future.

0 Kudos
DanPatterson
MVP Esteemed Contributor
import os
gdb = r"E:\advAppScripting\testing\tornhazard\Indiana\Indiana.gdb"
ctylist = arcpy.ListFeatureClasses(gdb)
for cty in ctylist:
    out_fc = os.path.join(gdb, cty)
    arcpy.CreateFeatureDataset_management(gdb, cty)
    arcpy.CopyFeatures_management(cty, out_fc)

# or
gdb = r"E:\advAppScripting\testing\tornhazard\Indiana\Indiana.gdb"
ctylist = arcpy.ListFeatureClasses(gdb)
for cty in ctylist:
    arcpy.CreateFeatureDataset_management(gdb, cty)
    arcpy.CopyFeatures_management(cty, f"{gdb}\\{cty}")

... sort of retired...
PhilCrossley1
Occasional Contributor

Thanks Dan.  I was arriving at the os.path.join as the solution, but thanks for affirming that approach. 

I did have to alter it a bit as I wanted to copy the 'cty' into the new Feature Dataset, and that failed unless I used FeatureClass toFeature Class.   

I then tried to add a Delete_management line at the end (after doing some additional geoprocessing within the for: loop, but it deleted all the new data instead of the original 'cty' as I intended.  I now see that the Delete_management needs a Data Type specified when you anticipate a naming conflict within the workspace, but I don't find any Help on what the syntax of that Data type parameter should be (e.g. "FeatureClass", "FEATURE_CLASS", or "Feature_Class")...

0 Kudos
DanPatterson
MVP Esteemed Contributor

sometimes I forget too

desc = arcpy.Describe("your featureclass path and name")
if hasattr(desc, "dataType"):
    print("DataType:    " + desc.dataType)

Describe object properties—ArcGIS Pro | Documentation


... sort of retired...
0 Kudos
PhilCrossley1
Occasional Contributor

Thanks again!  BtW, "FeatureClass" is what is returned. Thanks!

0 Kudos
PhilCrossley1
Occasional Contributor

If you're willing to continue this discussion, and its not 'out of bounds' to morph the post into a related, but different problem....
If I run the Describe() on a 'ctylist' that was previously created by ListFeatureClasses(mygdb), and that gdb includes both feature datasets and feature classes with the same name (because the former was created from the latter in the previous steps of the script (as per my initial question, and as in the test example pasted below), the print(desc.datatype) only returns the FeatureDatasets (and then when I add a Delete_management(fc) line, only the FeatureDatasets get deleted.  I added an if desc.datatType != "FeatureDataset":     test, but that does not resolve the issue. The whole script attached below.

Curious, and confused, but not in crisis mode...

PhilCrossley1_0-1639491498126.png

 

 

0 Kudos
DanPatterson
MVP Esteemed Contributor

not sure I am following... never, have duplicate names, if that is the case, change something, too many "another" and "test"


... sort of retired...
JoeBorgione
MVP Emeritus

...never, have duplicate names...

^^^ This...

That should just about do it....
0 Kudos