lockTest = gp.TestSchemaLock (selectionDissInt) if lockTest: ## Dissolve intersected features based on ID and land cover type, allowing multipart gp.Dissolve_management (selectionDissInt, tableName, idField + "; SCO_Class", "", "MULTI_PART", "") # <---- Fail print "dissolved intersected features based on ID and land cover type, allowing multipart" else: print "unable to get schema lock on selectionDissInt"
Could you please let me know if I have implemented your suggestions correctly, and if I have, do you have any others?? 😛Thank-you!
del row del rows
# Define any functions necessary to avoid schema locks ## Create a feature layer from catchments def MakeFL (catch): "Makes a feature layer from the input... hopefully a schema lock will be available for this layer" gp.MakeFeatureLayer_management (catch, "CatchLayer")
# Make a feature layer from the catchments #gp.MakeFeatureLayer_management (catchments, "CatchLayer") MakeFL (catchments) exists = gp.exists ("CatchLayer") print "Was 'CatchLayer' created? " + str(exists) lockTest = gp.TestSchemaLock ("CatchLayer") print "A schema lock is available for 'CatchLayer' immediately afer it is created: " + str(lockTest)
gp.Exists(tableName)
def AddField (fc): "Adds a field to a feature class, hopefully removes any schema locking errors" gp.AddField_management (fc, "Area", "DOUBLE", "#", "#", "#", "", "#", "#", "") .... ## Add field (Area) #gp.AddField_management (tableName, "Area", "DOUBLE", "#", "#", "#", "", "#", "#", "") AddField(tableName)
don't have 9.3 but could you make Management in that line lower case, I can't remember if 9.3 is case-sensitive or not
I've found using functions solves a lot of ghost schema locking issues I've had.
you could try to throw a Delete_management (see help for syntax) into the mix.
# Import modules import arcgisscripting import datetime # Create geoprocessor object gp = arcgisscripting.create(9.3) # Get Parameters catchments = gp.GetParameterAsText(0) # Feature Class idField = gp.GetParameterAsText(1) # Field streams = gp.GetParameterAsText(2) # Feature Class bufferDistance = int(gp.GetParameterAsText(3)) # Long landCover = gp.GetParameterAsText(4) # Feature Class outFolder = gp.GetParameterAsText (5) # Workspace tempWorkspace = gp.GetParameterAsText (6) # Workspace gp.overwriteoutput = True gp.Workspace = tempWorkspace gp.ScratchWorkspace = tempWorkspace # Set-up environment variables desc = gp.describe (catchments) xmin = desc.extent.xmin ymin = desc.extent.ymin xmax = desc.extent.xmax ymax = desc.extent.ymax gp.XYDomain = str(xmin - (bufferDistance*2)) + " " + str(ymin - (bufferDistance*2)) + " " + str(xmax + (bufferDistance*2)) + " " + str(ymax + (bufferDistance*2)) # Create names for geoprocessing interim files based on the type of workspace selected wsDesc = gp.Describe(tempWorkspace) wsType = wsDesc.WorkspaceType # If the workspace is a folder if wsType == "FileSystem": ## Interim files must have the ".shp" extension streamsd = "streamsd.shp" catchStrm = "CatchStrm.shp" catchStrmBuff = "CatchStrmBuff.shp" catchStrmBuffDiss = "CatchStrmBuffDiss.shp" catchStrmBuffInt = "CatchStrmBuffInt.shp" selection = "Selection.shp" selectionDiss = "SelectionDiss.shp" selectionDissInt = "SelectionDissInt.shp" tableName = "Rip" + str(bufferDistance) + ".shp" appendTable = "AppendMe.dbf" # If the workspace is a geodatabase elif wsType == "LocalDatabase": ## Interim files do not require an extension streamsd = "streamsd" catchStrm = "CatchStrm" catchStrmBuff = "CatchStrmBuff" catchStrmBuffDiss = "CatchStrmBuffDiss" catchStrmBuffInt = "CatchStrmBuffInt" selection = "Selection" selectionDiss = "SelectionDiss" selectionDissInt = "SelectionDissInt" tableName = "Rip" + str(bufferDistance) appendTable = "AppendMe" # Dissolve streams not allowing multipart to reduce the number of features to be buffered gp.Dissolve_management (streams, streamsd, "", "", "SINGLE_PART", "") # Count the total number of catchments resultCatchments = gp.GetCount_management(catchments) countCatchments = int(resultCatchments.GetOutput(0)) # Make a feature layer from the catchments gp.MakeFeatureLayer_management (catchments, "CatchLayer") # Create a search cursor to loop through the catchments 300 at a time rows = gp.SearchCursor (catchments) row = rows.next() x = 0 list = [] while x < countCatchments: ## If there are fewer than 300 features remaining if countCatchments - x <300: remaining = countCatchments - x for i in range (0,remaining,1): objectID = row.getvalue ("ObjectID") row = rows.next() x = x+1 ## Otherwise else: for i in range (0,300,1): objectID = row.getvalue("ObjectID") row = rows.next() x = x+1 ## Add the 300th, or last, ObjectID to the List list.append (objectID) # Get the ObjectIDs from the list and perform selections and geoprocessing # How many items are in the list? items = len(list) for i in range (0, items, 1): ## If this is the first item on the list if i == 0: query = '"OBJECTID" <= ' + str(list) lastitem = list ## Otherwise else: query = '"OBJECTID" > ' + str(lastitem) + ' AND "OBJECTID" <= ' + str(list) lastitem = list gp.SelectLayerbyAttribute_management("CatchLayer", "NEW_SELECTION", query) ## Get the number of catchments selected resultSelCatch = gp.GetCount_management("CatchLayer") countSelCatch = int(resultSelCatch.GetOutput(0)) ## Intersect catchments and streams gp.Intersect_analysis ("CatchLayer;" + streamsd, catchStrm, "ALL", "", "INPUT") resultIntersect = gp.GetCount_management(catchStrm) countIntersect = int(resultIntersect.GetOutput(0)) ## Buffer intersected catchments and streams gp.RepairGeometry_management (catchStrm) gp.Buffer_analysis (catchStrm, catchStrmBuff, bufferDistance, "FULL", "ROUND", "NONE") ## Dissolve the buffered streams... process may fail if this is done as part of the buffer operation gp.Dissolve_management (catchStrmBuff, catchStrmBuffDiss, idField, "", "MULTI_PART", "") ## Intersect catchments and buffer gp.Intersect_analysis (catchments + ";" + catchStrmBuffDiss, catchStrmBuffInt, "ALL", "", "INPUT") ## Select features where buffer ID and catchment ID are the same gp.Select_analysis (catchStrmBuffInt, selection, '"'+ idField + '"="' + idField + '_1"') ## Dissolve selected features based on ID, allowing multipart gp.Dissolve_management (selection, selectionDiss, idField, "", "MULTI_PART", "") ## Intersect dissolved features and land cover gp.Intersect_analysis (selectionDiss + ";" + landCover, selectionDissInt, "ALL", "", "INPUT") ## Dissolve intersected features based on ID and land cover type, allowing multipart gp.Dissolve_management (selectionDissInt, tableName, idField + "; SCO_Class", "", "MULTI_PART", "") ## Add field (Area) gp.AddField_Management (tableName, "Area", "DOUBLE", "#", "#", "#", "", "#", "#", "") ## Calculate area field gp.CalculateField_management (tableName, "Area", "!" + gp.describe(tableName).shapefieldname + ".AREA@SQUAREMETERS!", "PYTHON_9.3", "") ## If this is the first set if i == 0: ### Export attribute table gp.TableToDBASE_conversion (tableName, outFolder) ## Otherwise else: ### Export the attribute table and append it onto the existing one gp.MakeTableView_management (tableName, "RipView") gp.CopyRows_management ("RipView", appendTable) gp.Append_management (appendTable, outFolder + "\Rip" + str(bufferDistance) + ".dbf", "NO_TEST") # Delete the Temporary Files gp.Delete_management (catchStrm) gp.Delete_management (catchStrmBuff) gp.Delete_management (catchStrmBuffInt) gp.Delete_management (tableName) gp.Delete_management (selection) gp.Delete_management (selectionDiss) #gp.Delete_management (selectionDissInt) gp.Delete_management (streamsd) gp.Delete_management (catchStrmBuffDiss) gp.Delete_management (appendTable)
Aangemelde leden kunnen berichten plaatsen, updates volgen en meer. Nieuw hier? Registreer een gratis account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.