POST
|
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()
... View more
05-31-2015
01:27 PM
|
0
|
4
|
3837
|
POST
|
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()
... View more
05-31-2015
01:27 PM
|
0
|
4
|
4042
|
POST
|
Sir I am making Feature layer first. arcpy.MakeFeatureLayer_management creates datasource of the layer in .gdb how to add that datasouce in MXD?
... View more
05-29-2015
01:42 PM
|
0
|
3
|
2089
|
POST
|
ok I got how to save MXD , and add Layer in MXD... But that layer is exist on disk with .lyr extension .. My layer data is in .gdb datasource.... how to add layer from .gdb source to MXD? Am i Getting it right?
... View more
05-29-2015
01:08 PM
|
0
|
5
|
2089
|
POST
|
I have a layer Test generated by arcpy.MakeFeatureLayer_management and Dissolved by arcpy.Dissolve_management .. I know that this layer is in .gdb data source.. How can i add this layer to my .MXD and save that .MXD using PYTHON?
... View more
05-29-2015
12:59 PM
|
0
|
8
|
6038
|
POST
|
Corrected NOTE. Working correctly if i run each function as a separate script.... adding function2 script
... View more
05-27-2015
12:41 PM
|
0
|
0
|
656
|
POST
|
i am writing script which uses different Tools of arcgis and implementing different functionalities .. My script consists in different Functions running in an order. Each function is connected to each other , and works on layers and performs functions like(update cursor, search cursor ) When function1 finishes execution funciton2 starts and works on the same layer on which first one was working. The problem is here function2 generates wrong results or works wrong. works wrong means if a loop is defined to 5 iterations it will runs more then 5 times. like 20, 30 times and generates different results of one value. When i run each function separately it will works fine. but when i run function in one script it will create problems. --------------------------- My findings of this are LOCKS which are creating the problem. Sample Structure of my script: import arcpy
import math
import sys
import traceback
from arcpy import env
# Class to Generate Report
class test:
def __init__(self, workSpace):
self.workSpace = workSpace
# Add Fields
def function1(self):
# Script
return;
# Generate Report
def function2(self):
# Script
rows = arcpy.SearchCursor(self.mapLayerFinal,"","","Shape_Area")
row = rows.next()
while row:
area = row.Shape_Area
print "function2"
destination_cursor = arcpy.UpdateCursor(self.censusLayer, '"DISTRICT_ID" = '+ str(district_id))
row1 = destination_cursor.next()
while row1:
row1.setValue("District_Area",area)
destination_cursor.updateRow(row1)
row1 = destination_cursor.next()
row = rows.next()
return;
t = test(workSpace)
t.function1()
t2 = test(workSpace)
t2.function2() NOTE: code is tested and working correct if each function is executed separately
... View more
05-27-2015
11:57 AM
|
0
|
5
|
3957
|
POST
|
i have only polygons which touching EACH OTHER'S boundry
... View more
03-12-2015
02:24 PM
|
0
|
0
|
875
|
POST
|
I want to make some clusters from a bunch of small polygons with the constraint that each basic unit (polygon) in the resultant cluster must share a portion of its boundary with at least one other basic unit (polygon) in the same cluster. This is becuase of the reason that i want the resultant clusters contagious.
... View more
03-11-2015
12:06 PM
|
0
|
5
|
4530
|
POST
|
I want a result that , if two or more polygons are connecting to each other, then they would be one feature class
... View more
03-09-2015
02:12 PM
|
0
|
2
|
1014
|
Online Status |
Offline
|
Date Last Visited |
11-11-2020
02:23 AM
|