ArcMap script help
I'm trying to get a rather simple program to work, but I'm having difficulty with MakeTableView and GP.SearchCursor working correctly together. It may just be the way I'm trying to test the script (i.e. not making my "selection" correctly in ArcMap).
I have a simple script to calculate the sum of a field for selected features in a layer, sum the field for all values of that field for the layer, and then find the proportion. I'm making my selection using the "select by attributes" option on the table in ArcMap. However, when the program runs, it gives me the sum for the entire table for both the "selectedSum" and "totalSum". This leads me to believe I'm not selecting things the right way for the distinction between GP.SearchCursor and MakeTableView. I really don't understand how they work well, since I can't find good documentation to save my life (have been told that there's stuff in ESRI's online documentation, but none of it is clear to me at all). It's possible I'm missing something simple but obvious in my code, but I'm starting to think I'm not testing it correctly.
Any ideas? It's really quite simple, which is why I'm frustrated. I'm just very very new to Python scripting, and how it interacts with ArcMap. Thanks for any help/guidance.
EDIT: At this point, I'm 90% sure that the issue is with how I'm trying to test the script (how to make the selection), rather than my code. Got a classmate's code, and got the exact same result when running it (and it appears to effectively be the same code).
My code is
Crossposted to
I have a simple script to calculate the sum of a field for selected features in a layer, sum the field for all values of that field for the layer, and then find the proportion. I'm making my selection using the "select by attributes" option on the table in ArcMap. However, when the program runs, it gives me the sum for the entire table for both the "selectedSum" and "totalSum". This leads me to believe I'm not selecting things the right way for the distinction between GP.SearchCursor and MakeTableView. I really don't understand how they work well, since I can't find good documentation to save my life (have been told that there's stuff in ESRI's online documentation, but none of it is clear to me at all). It's possible I'm missing something simple but obvious in my code, but I'm starting to think I'm not testing it correctly.
Any ideas? It's really quite simple, which is why I'm frustrated. I'm just very very new to Python scripting, and how it interacts with ArcMap. Thanks for any help/guidance.
EDIT: At this point, I'm 90% sure that the issue is with how I'm trying to test the script (how to make the selection), rather than my code. Got a classmate's code, and got the exact same result when running it (and it appears to effectively be the same code).
My code is
#Find the sum of a value for a selected set of features. Calculate
#the proportion of the sum to the sum of all records in the layer.
#Import libraries.
import sys
from win32com.client import Dispatch
#Get filename and field from the user.
theLayerName = sys.argv[1]
theFieldName = sys.argv[2]
GP = Dispatch("esriGeoprocessing.GPDispatch.1")
#GP.Toolbox = "C:\Program Files\ArcGIS\ArcToolbox\Toolboxes\Data Management Tools.tbx"
GP.Toolbox = "Management"
#Check for field being numeric.
numericTypes = ['Integer', 'SmallInteger', 'Single', 'Double']
theFieldList = GP.ListFields(theLayerName, theFieldName)
theFieldItem = theFieldList.Next()
if (theFieldItem.Type in numericTypes):
#Get the selected rows of the file.
selectedSum = 0
theItemList = GP.SearchCursor(theLayerName)
thisItem = theItemList.Next()
#Get the sum of the selected field values.
while (thisItem):
selectedSum = selectedSum + thisItem.GetValue(theFieldName)
thisItem = theItemList.Next()
GP.AddMessage("The selected value sum is: " + str(selectedSum) + '\n')
#Get the whole table.
theTableView = "theWholeTable"
GP.maketableview (theLayerName,theTableView)
totalSum = 0
theTableList = GP.SearchCursor(theTableView)
thisTable = theTableList.Next()
#Get the sum of the whole table's field values.
while (thisTable):
totalSum = totalSum + thisTable.GetValue(theFieldName)
thisTable = theTableList.Next()
GP.AddMessage("The whole table sum is: " + str(totalSum) + '\n')
#Calculate the proportion of selectedSum / totalSum
theProportion = (selectedSum / totalSum)
GP.AddMessage ("The proportion of the selected features to all records is: " + str(theProportion) + '\n')
#If field is _not_ numeric, exit script.
else:
GP.AddMessage ('\n' + "Exiting script. The field value is not a number." + '\n')
Crossposted to
[Error: Irreparable invalid markup ('<lj-comm="geographile">') in entry. Owner must fix manually. Raw contents below.]
I'm trying to get a rather simple program to work, but I'm having difficulty with MakeTableView and GP.SearchCursor working correctly together. It may just be the way I'm trying to test the script (i.e. not making my "selection" correctly in ArcMap).
I have a simple script to calculate the sum of a field for selected features in a layer, sum the field for <i>all</i> values of that field for the layer, and then find the proportion. I'm making my selection using the "select by attributes" option on the table in ArcMap. However, when the program runs, it gives me the sum for the entire table for both the "selectedSum" and "totalSum". This leads me to believe I'm not selecting things the right way for the distinction between GP.SearchCursor and MakeTableView. I really don't understand how they work well, since I can't find good documentation to save my life (have been told that there's stuff in ESRI's online documentation, but none of it is clear to me at all). It's possible I'm missing something simple but obvious in my code, but I'm starting to think I'm not testing it correctly.
Any ideas? It's really quite simple, which is why I'm frustrated. I'm just very very new to Python scripting, and how it interacts with ArcMap. Thanks for any help/guidance.
EDIT: At this point, I'm 90% sure that the issue is with how I'm trying to test the script (how to make the selection), rather than my code. Got a classmate's code, and got the exact same result when running it (and it appears to effectively be the same code).
My code is <lj-cut text="here">
<html><pre><code><br>
#Find the sum of a value for a selected set of features. Calculate
#the proportion of the sum to the sum of all records in the layer.
#Import libraries.
import sys
from win32com.client import Dispatch
#Get filename and field from the user.
theLayerName = sys.argv[1]
theFieldName = sys.argv[2]
GP = Dispatch("esriGeoprocessing.GPDispatch.1")
#GP.Toolbox = "C:\Program Files\ArcGIS\ArcToolbox\Toolboxes\Data Management Tools.tbx"
GP.Toolbox = "Management"
#Check for field being numeric.
numericTypes = ['Integer', 'SmallInteger', 'Single', 'Double']
theFieldList = GP.ListFields(theLayerName, theFieldName)
theFieldItem = theFieldList.Next()
if (theFieldItem.Type in numericTypes):
#Get the selected rows of the file.
selectedSum = 0
theItemList = GP.SearchCursor(theLayerName)
thisItem = theItemList.Next()
#Get the sum of the selected field values.
while (thisItem):
selectedSum = selectedSum + thisItem.GetValue(theFieldName)
thisItem = theItemList.Next()
GP.AddMessage("The selected value sum is: " + str(selectedSum) + '\n')
#Get the whole table.
theTableView = "theWholeTable"
GP.maketableview (theLayerName,theTableView)
totalSum = 0
theTableList = GP.SearchCursor(theTableView)
thisTable = theTableList.Next()
#Get the sum of the whole table's field values.
while (thisTable):
totalSum = totalSum + thisTable.GetValue(theFieldName)
thisTable = theTableList.Next()
GP.AddMessage("The whole table sum is: " + str(totalSum) + '\n')
#Calculate the proportion of selectedSum / totalSum
theProportion = (selectedSum / totalSum)
GP.AddMessage ("The proportion of the selected features to all records is: " + str(theProportion) + '\n')
#If field is _not_ numeric, exit script.
else:
GP.AddMessage ('\n' + "Exiting script. The field value is not a number." + '\n')
<br><!-- Text to HTML Copyright(C) 2005-2006 by L. N. Reed--></code></pre></html>
Crossposted to <lj-comm="geographile">
I have a simple script to calculate the sum of a field for selected features in a layer, sum the field for <i>all</i> values of that field for the layer, and then find the proportion. I'm making my selection using the "select by attributes" option on the table in ArcMap. However, when the program runs, it gives me the sum for the entire table for both the "selectedSum" and "totalSum". This leads me to believe I'm not selecting things the right way for the distinction between GP.SearchCursor and MakeTableView. I really don't understand how they work well, since I can't find good documentation to save my life (have been told that there's stuff in ESRI's online documentation, but none of it is clear to me at all). It's possible I'm missing something simple but obvious in my code, but I'm starting to think I'm not testing it correctly.
Any ideas? It's really quite simple, which is why I'm frustrated. I'm just very very new to Python scripting, and how it interacts with ArcMap. Thanks for any help/guidance.
EDIT: At this point, I'm 90% sure that the issue is with how I'm trying to test the script (how to make the selection), rather than my code. Got a classmate's code, and got the exact same result when running it (and it appears to effectively be the same code).
My code is <lj-cut text="here">
<html><pre><code><br>
#Find the sum of a value for a selected set of features. Calculate
#the proportion of the sum to the sum of all records in the layer.
#Import libraries.
import sys
from win32com.client import Dispatch
#Get filename and field from the user.
theLayerName = sys.argv[1]
theFieldName = sys.argv[2]
GP = Dispatch("esriGeoprocessing.GPDispatch.1")
#GP.Toolbox = "C:\Program Files\ArcGIS\ArcToolbox\Toolboxes\Data Management Tools.tbx"
GP.Toolbox = "Management"
#Check for field being numeric.
numericTypes = ['Integer', 'SmallInteger', 'Single', 'Double']
theFieldList = GP.ListFields(theLayerName, theFieldName)
theFieldItem = theFieldList.Next()
if (theFieldItem.Type in numericTypes):
#Get the selected rows of the file.
selectedSum = 0
theItemList = GP.SearchCursor(theLayerName)
thisItem = theItemList.Next()
#Get the sum of the selected field values.
while (thisItem):
selectedSum = selectedSum + thisItem.GetValue(theFieldName)
thisItem = theItemList.Next()
GP.AddMessage("The selected value sum is: " + str(selectedSum) + '\n')
#Get the whole table.
theTableView = "theWholeTable"
GP.maketableview (theLayerName,theTableView)
totalSum = 0
theTableList = GP.SearchCursor(theTableView)
thisTable = theTableList.Next()
#Get the sum of the whole table's field values.
while (thisTable):
totalSum = totalSum + thisTable.GetValue(theFieldName)
thisTable = theTableList.Next()
GP.AddMessage("The whole table sum is: " + str(totalSum) + '\n')
#Calculate the proportion of selectedSum / totalSum
theProportion = (selectedSum / totalSum)
GP.AddMessage ("The proportion of the selected features to all records is: " + str(theProportion) + '\n')
#If field is _not_ numeric, exit script.
else:
GP.AddMessage ('\n' + "Exiting script. The field value is not a number." + '\n')
<br><!-- Text to HTML Copyright(C) 2005-2006 by L. N. Reed--></code></pre></html>
Crossposted to <lj-comm="geographile">
