1

I have 2 gdb's; one that contains a polygon feature class of census blocks(cb), and one that contains centroids that may lie within those census blocks. They are organized by state. Ex: FL_cb = FL_centroid. I want to iterate through both of those cdb's at the same time using arcp.ListFeatureClasses() and find the matching feature class for each state. The problem seems to be that to do this, I have to switch workspaces.

res_centroids = r"<path>\Processed\ResidentialParcels_Processed_ByState.gdb"
cbs = r"<path>\CensusBlocksStates.gdb"
res_cb_pt_intersects = r"<path>\Processed\ResCentroid_CB_Intersects_ByState.gdb"

arcpy.env.workspace = res_centroids
cent_featureclasses = arcpy.ListFeatureClasses()
for k, v in stateabb.items():
    for fc in cent_featureclasses:
        if k in fc:
         print (k)
         print (fc)
         arcpy.env.workspace = cbs
         cb_features = arcpy.ListFeatureClasses()
         for cb in cb_features:
            if v in cb:
                print (v)
                outputInt = "{}_CB_Cent_Interest".format(v)
                outputFC = os.path.join(res_cb_pt_intersects, outputInt)
                arcpy.Intersect_analysis(in_features=[cb, fc], out_feature_class=outputFC , join_attributes="ALL", cluster_tolerance="-1 Unknown", output_type="POINT").

The end results is that my fc no longer exists and throws a Failed to execute. Parameters are not valid error when I do the intersect.

Is there a way to store it in memory or can I create a temporary feature somehow to perform the intersect?

2

Yes. So if you were to print cent_featureclasses, you'd see that it only has the feature class name, not the full path. So if you change the workspace, it is looking for those names in the current workspace. So basically all you have to do is add full path list, like so:

cent_featureclasses = arcpy.ListFeatureClasses()
cent_featureclassesFP = []
for k, v in stateabb.items():
    for fc in cent_featureclasses:
        cent_featureclassesFP.append(res_centroids+'\\'+fc)

But honestly, you are over-killing it with the loops. As long as the naming conventions are consistent (I assume k is the state name and v is the abbreviation in your dictionary), all you really need is this:

res_centroids = r"<path>\Processed\ResidentialParcels_Processed_ByState.gdb"
cbs = r"<path>\CensusBlocksStates.gdb"
res_cb_pt_intersects = r"<path>\Processed\ResCentroid_CB_Intersects_ByState.gdb"
arcpy.env.workspace = res_cb_pt_intersects

for k, v in stateabb.items():
    print (k)
    in_features = [res_centroids+"\\"+str(v)+"_centroid", cbs+"\\"+str(v)+"_cb"]
    outputInt = "{}_CB_Cent_Interest".format(v)
    arcpy.Intersect_analysis(in_features, outputInt, join_attributes="ALL", cluster_tolerance="-1 Unknown", output_type="POINT").

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.