I have two functions
1. First one will crated the feature layer
2. second one will access the same created layer and prints the result.
but when i run my script first time it will print the wrong results. when i run it second time it will shows correct result...
Correct Result:
1. layer which is creating contains only 5 Rows so my script is supposed to print only 5 messages
getting access to map layer 1
getting access to map layer 2
getting access to map layer 3
getting access to map layer 4
getting access to map layer 5
Wrong Result Script running 1st Time:
getting access to map layer 2
getting access to map layer 2
getting access to map layer 2
getting access to map layer 2
getting access to map layer 2
getting access to map layer 2
getting access to map layer 2
getting access to map layer 2
getting access to map layer 2
getting access to map layer 2
getting access to map layer 2
getting access to map layer 2
getting access to map layer 2
getting access to map layer 3
getting access to map layer 3
getting access to map layer 3
getting access to map layer 3
getting access to map layer 3
getting access to map layer 3
getting access to map layer 3
getting access to map layer 3
getting access to map layer 3
getting access to map layer 3
getting access to map layer 3
getting access to map layer 3
getting access to map layer 3
getting access to map layer 3
getting access to map layer 3
getting access to map layer 3
getting access to map layer 3
getting access to map layer 3
getting access to map layer 3
getting access to map layer 3
getting access to map layer 3
getting access to map layer 3
getting access to map layer 3
getting access to map layer 3
getting access to map layer 3
getting access to map layer 3
getting access to map layer 3
getting access to map layer 3
getting access to map layer 3
getting access to map layer 3
getting access to map layer 3
getting access to map layer 3
getting access to map layer 3
getting access to map layer 3
getting access to map layer 3
getting access to map layer 3
getting access to map layer 3
getting access to map layer 3
getting access to map layer 3
getting access to map layer 3
getting access to map layer 3
getting access to map layer 3
getting access to map layer 3
getting access to map layer 3
getting access to map layer 3
getting access to map layer 3
getting access to map layer 3
getting access to map layer 3
getting access to map layer 3
getting access to map layer 3
getting access to map layer 3
getting access to map layer 3
getting access to map layer 3
getting access to map layer 3
getting access to map layer 3
getting access to map layer 3
getting access to map layer 3
getting access to map layer 3
.
.
.
.
.
.
# import system modules import arcpy import math import sys import traceback from arcpy import env # Class to Build Boundry of Districts class Build_Boundry: def __init__(self, workSpace,mapLayerFinal,censusLayer,totalDistricts,mxdLocation): self.workSpace = workSpace self.censusLayer = censusLayer self.mapLayerFinal = mapLayerFinal self.totalDistricts = totalDistricts self.mxdLocation = mxdLocation # Function to Calculate Average of Total Voters in each District def boundry(self): arcpy.AddMessage("Building Boundry of Districts ") if arcpy.Exists(self.mapLayerFinal): arcpy.AddMessage("Layer Already Exist.....") print(self.mapLayerFinal) else: arcpy.MakeFeatureLayer_management(self.censusLayer, self.mapLayerFinal,"DISTRICT_ID BETWEEN 1 AND "+str(self.totalDistricts),self.workSpace) # Execute Dissolve using LANDUSE and TAXCODE as Dissolve Fields arcpy.Dissolve_management(self.mapLayerFinal, str(self.workSpace)+"/"+str(self.mapLayerFinal), "DISTRICT_ID", "","MULTI_PART", "DISSOLVE_LINES") print(self.mapLayerFinal) arcpy.AddMessage("Boundry of Districts Calculated") mxd = arcpy.mapping.MapDocument(self.mxdLocation) df = arcpy.mapping.ListDataFrames(mxd, "")[0] addLayer = arcpy.mapping.Layer(self.mapLayerFinal) arcpy.mapping.AddLayer(df, addLayer,"BOTTOM") mxd.save() arcpy.RefreshTOC() del mxd, addLayer return; # Class to Calculate Compactness class Calculate_Compactness: def __init__(self, workSpace,mapLayerFinal): self.workSpace = workSpace self.mapLayerFinal = mapLayerFinal # Function to Calculate Compactness def compactness_quotient(self): arcpy.AddMessage("Calculating Compactness Quotient") with arcpy.da.SearchCursor(self.mapLayerFinal,["Shape_Area","DISTRICT_ID","Shape_Length"]) as rows: print "layer "+str(self.mapLayerFinal) for row in rows: area = row[0] district_id = row[1] perimeter = row[2] print "getting access to map layer "+str(district_id) print "Calculating Compactness Quotient Process Complete" return; folder = "C:/Users/Abrar ahmad/Desktop/CASES/Rural/Case_1_Boundary_Input" #raw_input("Please select Folder Containing data: ") mxdLocation = r"C:/Users/Abrar ahmad/Desktop/CASES/Rural/Case_1_Boundary_Input/New_Rwp_Cencus.mxd" workSpace = "C:/Users/Abrar ahmad/Desktop/CASES/Rural/Case_1_Boundary_Input/Selected_BU2.gdb" #raw_input("Please enter your Work Space: ") env.workspace = workSpace censusLayer = "Rural_Area_Rwp" districtsLayer = "Existing_District_Boundary" totalDistricts = int(5) mapLayerFinal = str(districtsLayer)+"_map" # Function to Build Boundry boundry = Build_Boundry(workSpace,mapLayerFinal,censusLayer,totalDistricts,mxdLocation) boundry.boundry() cc = Calculate_Compactness(workSpace,mapLayerFinal) cc.compactness_quotient()
Solved! Go to Solution.
You may have a variable collision where the script is picking up the feature layer object before the dissolve is happening. In these two lines self.mapLayerFinal is referring to both the feature layer and the result of the dissolve:
arcpy.MakeFeatureLayer_management(self.censusLayer, self.mapLayerFinal,"DISTRICT_ID BETWEEN 1 AND "+str(self.totalDistricts),self.workSpace) # Execute Dissolve using LANDUSE and TAXCODE as Dissolve Fields arcpy.Dissolve_management(self.mapLayerFinal, str(self.workSpace)+"/"+str(self.mapLayerFinal), "DISTRICT_ID", "","MULTI_PART", "DISSOLVE_LINES")
Try changing the script so that two different names are used:
arcpy.MakeFeatureLayer_management(self.censusLayer, "tmp_Layer","DISTRICT_ID BETWEEN 1 AND "+str(self.totalDistricts),self.workSpace) # Execute Dissolve using LANDUSE and TAXCODE as Dissolve Fields arcpy.Dissolve_management("tmp_Layer", str(self.workSpace)+"/"+str(self.mapLayerFinal), "DISTRICT_ID", "","MULTI_PART", "DISSOLVE_LINES")
You may have a variable collision where the script is picking up the feature layer object before the dissolve is happening. In these two lines self.mapLayerFinal is referring to both the feature layer and the result of the dissolve:
arcpy.MakeFeatureLayer_management(self.censusLayer, self.mapLayerFinal,"DISTRICT_ID BETWEEN 1 AND "+str(self.totalDistricts),self.workSpace) # Execute Dissolve using LANDUSE and TAXCODE as Dissolve Fields arcpy.Dissolve_management(self.mapLayerFinal, str(self.workSpace)+"/"+str(self.mapLayerFinal), "DISTRICT_ID", "","MULTI_PART", "DISSOLVE_LINES")
Try changing the script so that two different names are used:
arcpy.MakeFeatureLayer_management(self.censusLayer, "tmp_Layer","DISTRICT_ID BETWEEN 1 AND "+str(self.totalDistricts),self.workSpace) # Execute Dissolve using LANDUSE and TAXCODE as Dissolve Fields arcpy.Dissolve_management("tmp_Layer", str(self.workSpace)+"/"+str(self.mapLayerFinal), "DISTRICT_ID", "","MULTI_PART", "DISSOLVE_LINES")
Thanks , variable collision was the problem...
Good to hear it is resolved. This can be a tricky issue to pick up so it is good practice to be aware of the issue and use a strategy to avoid this problem. I generally use a prefix for temporary feature layers such as tmp_ or FL_ to avoid confusion with geodatabase feature class names.
Will keep these points in mind , Thanks