arcpy.CalculateField_management(outputfc, percentfield, '"('+str(popfield)+'/SUM_'+str(popfield)+')*100"', "PYTHON")
popfield = arcpy.GetParameterAsText(2) #population field arcpy.CalculateField_management(outputfc, percentfield, '(!'+popfield+'!/!SUM_'+popfield+'!)*100', "PYTHON")
popfield = arcpy.GetParameterAsText(2) #population field popfield_new = '!'+popfield+'!' sum_popfield_new = '!SUM_'+popfield+'!' arcpy.CalculateField_management(outputfc, percentfield, '('+popfield_new+'/'+sum_popfield_new+')*100', "PYTHON")
import arcpy, os gp = arcpy arcpy.env.overwriteOutput = True # Parameters inputfc = arcpy.GetParameterAsText(0) #Input fc location and name statstable = r'd:\My Documents\ArcGIS\Default.gdb\STATS' #Stats Table (temporary) output name (don't attempt to put into a feature dataset) popfield = arcpy.GetParameterAsText(1) #population field tablename = os.path.basename(statstable) #temporary table layername = "templayer" casefield = arcpy.GetParameterAsText(2) #CWM, or BASINNAME, or COUNTYNAME, WUCANAME_n, etc., percentfield = arcpy.GetParameterAsText(3) #Percent of Basin, or Percent of CWM, Percent of County, PER_POP_WUCA1_15, etc. outputfc = arcpy.GetParameterAsText(4) #Output fc location and name ######################################################################################################################### arcpy.MakeFeatureLayer_management(inputfc, layername) arcpy.Statistics_analysis(layername, statstable, [[popfield, "SUM"]], casefield) arcpy.AddJoin_management(layername, casefield, statstable, casefield) arcpy.CopyFeatures_management(layername, outputfc) arcpy.RemoveJoin_management(layername, tablename) arcpy.AddField_management(outputfc, percentfield, "DOUBLE","","","","","") arcpy.CalculateField_management(outputfc, percentfield, '(!'+popfield+'!/!SUM_'+popfield+'!)*100', "PYTHON") arcpy.DeleteField_management(outputfc,['SUM_'+popfield,"FREQUENCY","OBJECTID_1",str(casefield)+"_1"])
import things get parameters from Arc statstable: sum layer popfield for each casefield layer: join to statstable layer: copy to output layer: remove join output: add field output: calculate field - percent of sum within each case output: delete sum field
import things get parameters from Arc copy input features to output output: add field output: calculate field - percent of sum within each case
import things get parameters from Arc copy input features to output use Search Cursor to iterate over input rows Python dictionary: accumulate popfield value, using case field as key # i.e try: popDict[inputRow.GetValue(casefield)] += inputRow.GetValue(popfield) # except KeyError: popDict[inputRow.GetValue(casefield)] = inputRow.GetValue(popfield) # first time it comes across each case field, it doesn't exist as a key, so just add it output: add field use update curosr to iterate over output rows get the pop_casefield_sum sum value from the dictionary get the popfield value from the output row update the field value for the row to (popfield/pop_casefield_sum)*100
# Author: Edward Briggler # Date: 1/4/2012 # Purpose: To add the 7 phase fields to a feature class and calculate based on selecting by subtype. # Bring in Arc and Sys. import arcgisscripting, sys # Create the Geoprocessor... gp = arcgisscripting.create() gp.Workspace = gp.GetParameterAsText(0) fc = gp.GetParameterAsText(1) f = gp.GetParameterAsText(2) # concatenate the bangs to let the equations know it is a field new_f = '!'+f+'!' # only held in memory tv = "table_view_memory" # expressions to evaluate the length of Primary Line (in the length of specified field) ## Involves concatenating the *2 and *3 to get the expression right. HAS TO BE A STRING!!! exp = new_f exp2 = new_f+'*2' exp3 = new_f+'*3' # Turn Feature Class into a layer...can only do the following geoprocesses while a layer or table view. gp.MakeFeatureLayer_management(fc, tv) # Add Fields to Layer gp.addfield_management(tv, "A_PHASE", "FLOAT") gp.addfield_management(tv, "B_PHASE", "FLOAT") gp.addfield_management(tv, "C_PHASE", "FLOAT") gp.addfield_management(tv, "AB_PHASE", "FLOAT") gp.addfield_management(tv, "AC_PHASE", "FLOAT") gp.addfield_management(tv, "BC_PHASE", "FLOAT") gp.addfield_management(tv, "ABC_PHASE", "FLOAT") # Select by SUBTYPECD and run each expression accordingly ##gp.MakeTableView_management(fc, tv) gp.SelectLayerByAttribute_management(tv,"NEW_SELECTION", "SUBTYPECD = 1") gp.CalculateField_management(tv, "A_PHASE",exp , "PYTHON" ) gp.SelectLayerByAttribute_management(tv,"NEW_SELECTION", "SUBTYPECD = 2") gp.CalculateField_management(tv, "B_PHASE",exp , "PYTHON" ) gp.SelectLayerByAttribute_management(tv,"NEW_SELECTION", "SUBTYPECD = 3") gp.CalculateField_management(tv, "C_PHASE",exp , "PYTHON" ) gp.SelectLayerByAttribute_management(tv, "NEW_SELECTION", "SUBTYPECD = 4") gp.CalculateField_management(tv, "AB_PHASE",exp2 , "PYTHON" ) gp.SelectLayerByAttribute_management(tv,"NEW_SELECTION", "SUBTYPECD = 5") gp.CalculateField_management(tv, "AC_PHASE",exp2 , "PYTHON" ) gp.SelectLayerByAttribute_management(tv,"NEW_SELECTION", "SUBTYPECD = 6") gp.CalculateField_management(tv, "BC_PHASE",exp2 , "PYTHON" ) gp.SelectLayerByAttribute_management(tv, "NEW_SELECTION", "SUBTYPECD = 7") gp.CalculateField_management(tv, "ABC_PHASE",exp3 , "PYTHON" )
#Alison Montgomery - Spatial Point Allocation Script #Created: 06.13.2013 #For use with ArcGIS 10.x, Python Version 2.7.2 import arcpy, sys, os, subprocess from arcpy import env #Supply the following arguments prior to running: #Workspace (full path using backslashes) #Catchment Polygons (full path, backslashes) #Point Data (full path using backslahes) #Count Field for the output: <15 characters to denote the point dataset. #An output txt file (full path, must be backslashes -> eg. C:\Users\Alison\Desktop\filename.txt) #Density Field for output: <8 characters #Area Field from Shapefile ######## #Each argument must be in double quotes, and they must be separated by a space. #Input fields that will be written into text file for GME processing. inputPoly = arcpy.GetParameterAsText(0) inputPoint = arcpy.GetParameterAsText(1) Field = arcpy.GetParameterAsText(2) code = 'countpntsinpolys(poly="' + inputPoly + '", pnt="' + inputPoint + '", field="' + Field + '");' #Input a text file name, write above code to text file. newFile = arcpy.GetParameterAsText(3) newFileObj = open(newFile, 'w') newFileObj.write(code) newFileObj.close() #Write textfile name into code line readable by GME call. lineCall = "run(in=\\\""'' + newFile + ''"\\\")" #Call GME, execute text file code, "-c" closes GME after processing. os.system(r'C:\Program Files (x86)\SpatialEcology\GME\SEGME.exe') subprocess.call(r'C:\Program Files (x86)\SpatialEcology\GME\SEGME.exe -c ' + lineCall + ';'); #Input name for Density field, and input the shapefile's area field. DensityField = arcpy.GetParameterAsText(4) CountField = Field area = arcpy.GetParameterAsText(5) #Add the new density field to shapefile. arcpy.AddField_management(inputPoly, DensityField, "FLOAT", "10", "4", "", "", "NULLABLE", "NON_REQUIRED", "") #Calculate density field using count of points, divided by the area field. arcpy.CalculateField_management(inputPoly, DensityField, '(!'+CountField+'!/!'+area+'!)', "PYTHON_9.3", "")