AnsweredAssumed Answered

Getting wrong results in Search Cursor?

Question asked by waqarah on May 31, 2015
Latest reply on Jun 1, 2015 by waqarah

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()

Outcomes