adding and updating field using class

451
4
04-03-2022 05:36 PM
KristenTowsend
New Contributor

So, i am tryinrg to solve this problem!

"You are given a feature class called parcels.shp that contains the following fields: FID, Shape, Landuse, and
Value. You need to write a script that determines the property tax and writes these values to the same feature
class in a new field called Tax." 

i am stuck when it comes to adding and updating the Tax field. this is what i have so far. any hint would be very helpful. thanks

class parcel(object):

def __init__(self,landuse,value):
self.landuse = landuse
self.value = value

def assessment(self):
if self.landuse == "SFR":
rate = 0.05
elif self.landuse == "MFR":
rate = 0.04
else:
rate = 0.02

assessment = self.value*rate
return assessment

import arcpy
import parcelclass

arcpy.env.workspace = "C:/Lab8"

fc = "parcels.shp"

arcpy.AddField_management(fc, "Tax", "LONG", "", "", 10)

cursor = arcpy.da.UpdateCursor(fc, ["FID","Landuse","Value"])

for row in cursor:

myparcel = parcelclass.parcel(row[1],row[2],row[3])

mytax = myparcel.assessment()

cursor.updateRow(row)

 

 

0 Kudos
4 Replies
DonMorrison1
Occasional Contributor III

It would be best if you format your code so we can see the indentation and explain what you mean by being "stuck".  Without putting a lot of thought into it (since it seems the objective is for you to figure it out) I would try something along these lines, in terms of writing out the calculated tax value :

 

cursor = arcpy.da.UpdateCursor(fc, ["FID","Landuse","Value", "Tax"])
for row in cursor:
    myparcel = parcelclass.parcel(row[1],row[2],row[3])  # This doesn't look right
    mytax = myparcel.assessment()   # This call appears to only return the rate, not the assessment
    cursor.updateRow(row[1],row[2],row[3], mytax)

 

0 Kudos
KristenTowsend
New Contributor

Hi , so below is the formatted code pic 

KristenTowsend_0-1649034073468.png

when i run the code i get the following error. 

myparcel = parcelclass.Parcel(row[1],row[2],row[3])
TypeError: __init__() takes 3 positional arguments but 4 were given
>>>

0 Kudos
DonMorrison1
Occasional Contributor III

You should only pass 2 parameters to the init function - 'self'  is sort of implied, which I find confusing coming from a C++ background. Anyway - here is how I would do it which I think is a bit clearer. It didn't look like you were using the FID field so I got rid of it - and I now see how you calculate the assessment.  Using a class to calculate the assessment seems like overkill, but you must have a reason to use it .....

 

cursor = arcpy.da.UpdateCursor(fc, ["Landuse","Value", "Tax"])
for land_use, value, tax in cursor:
    myparcel = parcelclass.parcel(land_use, value)  
    mytax = myparcel.assessment()   
    cursor.updateRow([land_use, value, mytax])

 

0 Kudos
KristenTowsend
New Contributor

thank you! but for some reason, I think the code isn;t working for line 3. 

0 Kudos