0

I need to make a multivalue field to be selected as:

Input_Features = arcpy.GetParameterAsText (0)
Num_Pop = arcpy.GetParameterAsText (1) #(It contains population data for the periods 2020, 2025, 2030 and 2035 to about 1800 polygons#

PerPopFic - It contains percentage of data to calculate the new population and overwrite the respective fields

And be calculated for each field chosen Num_Pop a multiplication with PerPopFic field that is in the same shapefile Input_Features.

I've tried some routines, the last I stopped was this:

fc = Input_Features
rows = arcpy.SearchCursor(fc)
arcpy.ListFields fields = (fc, Num_Pop)
for row in rows:
 for field in fields:
 Expres3 = "round ([{0}] * [PerPopFic], 0)". Format (field.name)
 arcpy.CalculateField_management (Saida_Ident_1, field.name, Expres3, "VB", "")

This Num_Pop variable contains on average about 4 fields.

I want to overwrite existing fields. I have 4 fields with population ( 2020, 2025, 2030 and 2035) and another (PerPopFic) with percentage to calculate the new population. All those fields are in the same shapefile.

But I need to do this calculation to another 5 shapefiles who the population field haven't the same name then this. That's why I need to choose these fields.

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked May 31, 2016 at 23:00
7
  • can you please edit your Question and include some example data to help explain what you're trying to do? Commented May 31, 2016 at 23:40
  • is Num_pop a list of fields? What is PerPopFic? Commented May 31, 2016 at 23:41
  • 2
    What is your question? Commented May 31, 2016 at 23:53
  • are you wanting to overwrite the existing value in each Num_Pop field with the new multiplied calculated value? Or do you want to write these to new fields or existing empty fields? Commented May 31, 2016 at 23:53
  • I think arcpy.ListFields fields = (fc, Num_Pop) from your code snippet will throw an error. Commented Jun 1, 2016 at 0:35

1 Answer 1

2

Rather than using a Search Cursor and Calculate Field I would recommend you make use of an Update Cursor (which really just combines the functionality of the two, but much more efficiently).

The following code will take parameters from your ArcToolbox script tool for Layer, Calculate Field (ie your 'PerPopFic' field), and your list of fields to update (your 'Num_Pop' fields). It will then use an update cursor to step through the records, and update each Num_Pop field based on your calculation round ([<field value>] * [PerPopFic], 0).

It will also accept any number of fields to update, so doesn't need to be a set number of fields specified.

import arcpy
# Feature class to update
fc = arcpy.GetParameter(0)
# Field to calculate on (e.g. 'PerPopFic')
calcField = [arcpy.GetParameterAsText(1)] 
# Population fields to update - can be any number of fields
# then split Num_Pop fields string into list
Num_Pop = arcpy.GetParameterAsText(2) 
fields = Num_Pop.split(';') 
# Remove the calcField from field List (just-in-case)
for field in fields: 
 if field in calcField:
 fields.remove(field)
# Get count of fields to update - this allows a loop
# through all fields, and then doesn't require a set
# number of fields to be updated
fldCount = len(fields) 
# add calcField to fields (calcField is now first field
# in list - row[0] - so is always found in the same place
# for use in calcs on other fields - row[1]-row[n]
updateFields = calcField + fields 
# Update cursor to step through all records and update fields
# that match fields list by multiplying on the calcField
with arcpy.da.UpdateCursor(fc, updateFields) as cursor:
 for row in cursor:
 for i in range(1, fldCount + 1):
 row[i] = round(row[0] * row[i], 0)
 cursor.updateRow(row)

The three parameters take values from the script tool.

  • Layer to Update is the layer/feature class/shapefile you are updating
  • Field to Calculate By is your PerPopFic field, or any other field if it has a different name in a different shapefile
  • Fields to Calculate are your population fields which you say can have different names in different shapefiles. You select the fields you want to have updated in this list. If you also select your PerPopFic field here, the script will remove it from the list when it's run (so it's not trying to update itself).

enter image description here

answered Jun 2, 2016 at 3:26
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.