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:
Solved! Go to Solution.
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
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.
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.
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.
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
/blogs/dan_patterson/2016/08/14/script-formatting
makes it easy to read the code and provide feedback with line numbers
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
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.
thank you!! it was really big help, it works fine
If someone's response answered your question, mark it correct to give credit and close out the question.