1

I am trying to find the feature classes from one feature data into another feature classes. But I'm getting nothing at the end as a result of the matc statement. The data in the arc environment is XYZ.LandCover while in the other environment data naming format is pqr_landcover

One environment is SDE and other is postgis Below is a script I'm using

import arcpy
import os, subprocess, fnmatch, string, re

arc = ("Connections\\arc.sde")

post = ("Connections\\Post.sde")

arcpy.env.workspace = arc


listfeatures = arcpy.ListDatasets()
for lf in listfeatures:
    print lf

    fclist = arcpy.ListFeatureClasses("","",lf)
    for fc in fclist:
         print fc
    arcpy.env.workspace = post
    postgisdata = arcpy.ListDatasets()
    for postg in postgisdata:
      print(postg)
    postlist = arcpy.ListFeatureClasses("", "",postg)
    for postfc in postlist:
        print postfc


        if postfc in fc:
                matches = matches+1
        if matches > 0:

            print str(matches)+" match found for "+postfc

        else:
            print "No matches found for "+postfc

1 Answer 1

1

There are two issues here:

  1. You need to declare your matches variable before you use it, if you are going to use in the manner that you are.
  2. You have to get the unqualified names in order to compare them. Otherwise the name of the user or database etc. will make your names not match.

I'm also not sure if you have your workspaces written correctly. I solved the problem by putting the script into a script tool. To put it back to a direct call, replace "arcpy.AddMessage(" with "print " and remove the ending parenthesis.

I left lots of messages so you can see how it works. Sorry if this causes any confusion!

This is how I got your script to work.

import arcpy, traceback, sys
import os, subprocess, fnmatch, string, re

arc = arcpy.GetParameterAsText(0) # WORKSPACE
post = arcpy.GetParameterAsText(1) # WORKSPACE

try:

    arcpy.env.workspace = arc

    listfeatures = arcpy.ListDatasets()

    for lf in listfeatures:
        arcpy.AddMessage("lf: " + lf)

        fclist = arcpy.ListFeatureClasses("","",lf)
        fclist_names = [] # NEED LIST OF UNQUALIFIED TABLE NAMES
        for fc in fclist:
             arcpy.AddMessage("fc: " + fc)
             n = fc.split(".")[len(fc.split("."))-1] # GET UNQUALIFIED TABLE NAME
             arcpy.AddMessage("n: " + n)
             fclist_names.append(str(n)) # APPEND UNQUALIFIED TABLE NAME TO LIST

        arcpy.env.workspace = post

        postgisdata = arcpy.ListDatasets()
        for postg in postgisdata:
            arcpy.AddMessage("postg: " + (postg))
            # NEED TO MAINTAIN INDENT HERE TO CONTINUE DATASET LOOP
            postlist = arcpy.ListFeatureClasses("", "",postg)
            for postfc in postlist:
                arcpy.AddMessage("postfc: " + postfc)
                matches = 0 # NEED TO DECLARE THE MATCHES VARIABLE
                n2 = postfc.split(".")[len(postfc.split("."))-1] # GET UNQUALIFIED TABLE NAME
                arcpy.AddMessage("n2: " + n2)
                if n2 in fclist_names: # NEED TO CHECK NAME AGAINST NAME
                    matches = matches+1
                if matches > 0:
                    arcpy.AddMessage(str(matches)+" match found for "+postfc)
                else:
                    arcpy.AddMessage("No matches found for "+postfc)

except:
    # PRINT ERROR MESSAGES
    tb = sys.exc_info()[2]
    tbinfo = traceback.format_tb(tb)[0]
    pymsg = tbinfo + "\n" + str(sys.exc_type)+ ": " + str(sys.exc_value)
    arcpy.AddError("Python Messages: " + pymsg + " GP Messages: " + arcpy.GetMessages(2))

finally:
    del arc, post, listfeatures
3
  • Thank you very much for your help. The issues I'm getting here is about looping. In the second work environment (post), all the feature datasets in the variable "postg" are printed but the feature classes (postfc) in those feature data don't get printed. Feature Classes those get printed are from only one Feature Data for multiple times. What could be the reason not all feature classes are not getting printed but from only one Feature Data sets?
    – RoS
    Commented Feb 1, 2016 at 15:20
  • You're welcome! I think your looping problem is caused by the indent not being maintained during the dataset loop for post. My apologies because I had fixed that in my working script but not in the posted script. I put a note where the indent happens in my code sample so you can see what I mean. Another suggestion I would offer would be to use variable names that accurately describe what they are. It would help you QC your script more easily. For example, post_dataset_list instead of postgisdata, and post_fc_list instead of postlist.
    – RHB
    Commented Feb 3, 2016 at 18:00
  • Thank you for all your help. I have adapted the script to make it work according to the way feature data and classes are labelled. I am a beginner in Python, your help was really a milestone to make it work.
    – RoS
    Commented Feb 12, 2016 at 12:19

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.