1

I have multiple shapefiles in a folder which I am trying to compare with same named features in geodatabase.

I want to first clip them within a polygon and then copy and overwrite the features in the geodatabase.

My script is currently skipping the if statement (no geoprocessing) and printing the else statement.

# Set workspace
arcpy.env.workspace = "C:\\Users\\mwisniewski\\My Documents\\DPIPWE"
arcpy.env.overwriteOutput = True
shpFile = arcpy.env.workspace
arcpy.AddMessage("\n")

# Location file geodatabase to update feature classes
geoPath = "U:\\NRM_Base_Data.gdb"

# Local variables
clipRegion = "U:\\Boundary\\NRM_Regions\\DPIPWE_NRMRegionBoundary_Tas_Unk.gdb\\Cradle_Coast_NRM_Region"
shpName = arcpy.Describe(shpFile).baseName
featureName = arcpy.Describe(geoPath).baseName
outFc = "" 

shpName = arcpy.Describe(shpFile).baseName
featureName = arcpy.Describe(geoPath).baseName
outFc = ""

# Count the number of shapefiles clipped
count1 = 0
# Count the number of feature classes being copied
count2 = 0

try:
 # Loop through shapefiles and clip to NRM region
 for shp in shpFile:
     if shpName == featureName:
         arcpy.AddMessage(shpFile)
         arcpy.Clip_analysis (shpFile, clipRegion, outFc, "", "", "")
         count1 += 1
         # Copy clipped shapefile into file geodatabase
         arcpy.FeatureClassToFeatureClass_conversion(outFc, geoPath + "\\" + shpName)
         count1 += 1
     else:
         arcpy.AddMessage("Error: Shapefile/s not clipped and copied to file geodatabase ")
  • Would you be able to edit your question to include the values of shpFile and geoPath, please? I would also recommend removing your try/except while you are trying to debug this code snippet. – PolyGeo Jun 25 '15 at 6:36
  • 1
    Could you print the shpName and featureName variables and see what they contain? – Alex Tereshenkov Jun 25 '15 at 7:19
  • 3
    It would appear that you are trying to implement the example in the answer to your other question. Take a closer look...as it is, you are likely iterating over a string (shpFile) which will produce individual characters which are never actually used for comparison. – Evil Genius Jun 25 '15 at 11:16
2

Probably the best way to do this is with a dictionary, this is an object that stores values like Key,Value (not an easy python concept so bear with me).. There are some tutorials that describe dictionaries and what they can do here and here.

First thing we need to do is go through the 'to' database finding all the feature classes and storing their full path; note that in a single database all names must be unique, this approach wouldn't necessarily work for a folder tree where names are not guaranteed to be unique.

WSdict = {} # new empty directory..

# put all the names into a dictonary with a key of the name
for (path, dir, datasets) in arcpy.da.Walk(InGDB,datatype="FeatureClass"):
    for thisDS in datasets:
        WSdict[thisDS.upper()] = os.path.join(path,thisDS) # key is upper case to avoid case problems later

This creates a dictonary like:

{ "FEATURECLASS1":"c:\\full\\path.gdb\\FeatureClass1",
"FEATURECLASS2":"c:\\full\\path.gdb\\FeatureClass2",
"FEATURECLASS3":"c:\\full\\path.gdb\\dataset\\FeatureClass3" }

which when you ask for "FeatureClass1" will return "c:\full\path.gdb\FeatureClass1"... nice! That way you can have your base name and also your full path, but wait, it gets cooler! you can now ask do you have "FEATURECLASS1"? by using WSdict.has_key("FEATURECLASS1") to find out if the shape file is matched in the geodatabase.

Now the whole thing together:

import os, sys, arcpy

InGDB = sys.argv[1]
InShpFolder = sys.argv[2]

WSdict = {} # empty dictionary

# put all the names into a dictonary with a key of the name
for (path, dir, datasets) in arcpy.da.Walk(InGDB,datatype="FeatureClass"):
    for thisDS in datasets:
        WSdict[thisDS.upper()] = os.path.join(path,thisDS) # key is upper case to avoid case problems later

arcpy.env.workspace = InShpFolder
for thisFC in arcpy.ListFeatureClasses():
    thisName,thisExt = os.path.splitext(thisFC) # get the shapefile name without extension (base name)

    if WSdict.has_key(thisName.upper()):
        # the shapefile is found in the database..
        shpFull = os.path.join(InShpFolder,thisFC)# full path of shape file
        gdbFull = WSdict[thisName.upper()]        # get the path from the dictionary
        print("Found shapefile")
        print(shpFull + " -> " + gdbFull)
  • Sorry, was away sick, have edited question to include the values of shpFile and geoPath in case someone has another answer. – Mark W Jun 28 '15 at 23:23
  • 1
    I had a sick day last week too. Replace sys.argv[n] with your hard paths.. the sys.argv is for putting the script into a toolbox. I tested this code by exporting a geodatabase to shapefile and then changing a few names (in both) and it worked just fine for me. All you need to do is incorporate your clip/copy into the loop and fix the paths and it's right to go. – Michael Stimson Jun 28 '15 at 23:28
  • Awesome Michael, I will incorporate the clip/copy. Thanks a lot for your help. – Mark W Jun 29 '15 at 2:34

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.