python functions and classes

2250
7
Jump to solution
03-20-2020 03:15 PM
CNRFGN
by
New Contributor II

its the challenge 2 frompython scriptinh for arcgis book. is anybody could solve this? thank you

You are given a feature class called parcels.shp located in the Exercise12 folder that contains the following fields: FID, Shape, Landuse, and Value. Modify the parceltax.py script so that it determines the property tax for each parcel and stores these values in a list. You should use the class created in the parcelclass.py script—the class can remain unchanged. Print the values of the final list as follows: FID:

0 Kudos
1 Solution

Accepted Solutions
RandyBurton
MVP Alum

For Challenge 2 you have a parcel class module.  The code needs no modification; it just needs to be included in the final project:

class Parcel:
    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

The parcel tax script illustrates how to use the parcel class:

import parcelclass
myparcel = parcelclass.Parcel("SFR", 125000)
print "Land use: ", myparcel.landuse
mytax = myparcel.assessment()
print "Tax assessment: ", mytax

What is being asked is to loop through a feature and calculate the tax for each parcel based on the FID, Landuse and Value of the property.  To do this, you will use a search cursor.  The author has given an example of how to use a search cursor in exercise 7.  Review this section if needed.

import arcpy
from arcpy import env
env.workspace = r"C:/EsriPress/Python/Data/Exercise07"
fc = "airports.shp"
cursor = arcpy.da.SearchCursor(fc, ["NAME"])
for row in cursor:
    print "Airport name = {0}".format(row[0])‍‍‍‍‍‍‍

Since this last script is closest to the goal, modify it to read the desired data. Replace the path given in line 3 to the path for Exercise 12.  In line 3, correct the name of the shape file where the data is located.  Replace "NAME" in line 5 with the three fields needed.  Then change line 7 to print the data; this is for testing and will be replaced later.

# line 7 will become (note indentation)
    print "FID: {0}  Landuse: {1}  Value: {2}".format(row[0], row[1], row[2])

Once you have this script reading your shape file correctly, the parcel class and tax scripts will be added into the mix by

  1. Import the parcel class by placing line 1 of the tax script at the top of your working script.
  2. Copy lines 2 and 4 from your tax script and insert them between the "for row in cursor" and "print ..." rows of your working script.
  3. You will need to change "SFR", 125000 of the copied line 2 of the tax script to use row[1] and row[2].
  4. Modify the print statement to: print "{0}: {1}".format(row[0], mytax)

Then test your script and let us know how it is working.   If you have problems, post your script, and comments will be provided.

Hope this helps.

View solution in original post

7 Replies
JoeBorgione
MVP Emeritus

If you could add the  script to a syntax highlighter window, I'll bet you get some takers:  nothing personal but I don't open zip files.

That should just about do it....
0 Kudos
RandyBurton
MVP Alum

The solution the author intended was to print a list of FIDs and the corresponding tax assessment; and not to create a "Python list" containing this data.  The author's solution is near the end of the PDF exercise document available on the book's webpage.

The basic solution is to create a SearchCursor to read the data from a shape file, use the Parcel class to create a parcel object from each row of data, use the assessment method to calculate the tax, and then print the FID and tax amount.

CNRFGN
by
New Contributor II

parcelclass.py

class Parcel:
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

parceltax.py

import parcelclass
myparcel = parcelclass.Parcel("SFR", 125000)
print 'Land use: ", myparcel.landuse
mytax = myparcel.assessment()
print "Tax assessment: ", mytax

0 Kudos
DanPatterson_Retired
MVP Emeritus

/blogs/dan_patterson/2016/08/14/script-formatting 

makes it easy to read the code and provide feedback with line numbers

RandyBurton
MVP Alum

For Challenge 2 you have a parcel class module.  The code needs no modification; it just needs to be included in the final project:

class Parcel:
    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

The parcel tax script illustrates how to use the parcel class:

import parcelclass
myparcel = parcelclass.Parcel("SFR", 125000)
print "Land use: ", myparcel.landuse
mytax = myparcel.assessment()
print "Tax assessment: ", mytax

What is being asked is to loop through a feature and calculate the tax for each parcel based on the FID, Landuse and Value of the property.  To do this, you will use a search cursor.  The author has given an example of how to use a search cursor in exercise 7.  Review this section if needed.

import arcpy
from arcpy import env
env.workspace = r"C:/EsriPress/Python/Data/Exercise07"
fc = "airports.shp"
cursor = arcpy.da.SearchCursor(fc, ["NAME"])
for row in cursor:
    print "Airport name = {0}".format(row[0])‍‍‍‍‍‍‍

Since this last script is closest to the goal, modify it to read the desired data. Replace the path given in line 3 to the path for Exercise 12.  In line 3, correct the name of the shape file where the data is located.  Replace "NAME" in line 5 with the three fields needed.  Then change line 7 to print the data; this is for testing and will be replaced later.

# line 7 will become (note indentation)
    print "FID: {0}  Landuse: {1}  Value: {2}".format(row[0], row[1], row[2])

Once you have this script reading your shape file correctly, the parcel class and tax scripts will be added into the mix by

  1. Import the parcel class by placing line 1 of the tax script at the top of your working script.
  2. Copy lines 2 and 4 from your tax script and insert them between the "for row in cursor" and "print ..." rows of your working script.
  3. You will need to change "SFR", 125000 of the copied line 2 of the tax script to use row[1] and row[2].
  4. Modify the print statement to: print "{0}: {1}".format(row[0], mytax)

Then test your script and let us know how it is working.   If you have problems, post your script, and comments will be provided.

Hope this helps.

CNRFGN
by
New Contributor II

thank you!! it was really big help, it works fine

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

If someone's response answered your question, mark it correct to give credit and close out the question.