Problem in looping all the features of a cursor

المشرف العام

Administrator
طاقم الإدارة
It might be me but I've been experiencing something weird. (I actually tried what suggested in this thread Arcpy MakeFeatureLayer won't loop with no luck)

What I'm trying to accomplish is to use a MakeFeatureLayer and an AggregatePolygons functions in a loop. Everything works well when the input file is a Feature Class in the Default database but when I use the same code for a Future Class in a SQL Server database, the code does not loop through all the elements of the previously defined cursor.

Here is the working code in the Default.gdb:

import os import sys import arcpy from arcpy import env env.overwriteOutput = True env.workspace = r"path_to\Default.gdb" try: #Feature Class to be used as input fc = "input" #whereclause for a cursor that contain all the features but the last whereCur = 'OBJECTID < (SELECT MAX( OBJECTID ) FROM {0})'.format(fc) #create the cursor that iterates the features rows = arcpy.SearchCursor(fc,whereCur) for row in rows: #whereclause to recursively select OBJECTID and OBJECTID+1 whereclause = 'OBJECTID = {0} OR OBJECTID = {1}'.format(row.OBJECTID, row.OBJECTID+1) #just checking print whereclause #just checking print row.OBJECTID # make selection of consecutive IDs - (ID and ID+1) tempSel = arcpy.MakeFeatureLayer_management(fc, "NEW_SELECTION" +"_"+str(row.OBJECTID), whereclause) # aggregate the the selected features arcpy.AggregatePolygons_cartography(tempSel, "aggregate" +"_"+str(row.OBJECTID), 50) # deleting cursor del rows, row except Exception as e: print (e) The code used for the SQL Server feature class:

import os import sys import arcpy from arcpy import env env.overwriteOutput = True #connection to SQL Server env.workspace = r"Database Connections\SQLServer" try: #input table fc = "input" #whereclause for a cursor that contain all the features but the last whereCur = 'OBJECTID < (SELECT MAX( OBJECTID ) FROM {0})'.format(fc) #create the cursor that iterates the features rows = arcpy.SearchCursor(fc,whereCur) for row in rows: whereclause = 'OBJECTID = {0} OR OBJECTID = {1}'.format(row.OBJECTID, row.OBJECTID+1) #just checking print row.OBJECTID #just checking print whereclause # make selection of consecutive IDs - (ID and ID+1) # adding this line will end the loop only after the first selection (OBJECTID = 1 OR OBJECTID = 2) tempSel = arcpy.MakeFeatureLayer_management(fc, "NEW_SELECTION" +"_"+str(row.OBJECTID), whereclause) # aggregate the the selected features # only two features will be aggregated arcpy.AggregatePolygons_cartography(tempSel, "aggregate" +"_"+str(row.OBJECTID), 50) # deleting cursor del rows, row except Exception as e: print (e) Oddily enough if I comment out the MakeFeatureLayer function (and consequntly the AggregatePolygons one) the loop complete its cycle because the print row.OBJECTID and print whereclause give the expected result!

What am I doing wrong in using these functions? Is this somehow related with the use of SQL Server rather than the Default.gdb?

Thanks in advance, m.



أكثر...
 
أعلى