# creating class

566
7
03-20-2020 03:12 PM
by
New Contributor II

hi guys, is anybody can help me with this?

Consider a text file called trees.txt with different fields called Block, Plot, Species and DBH, separated by spaces. Every record represents one tree. DBH is the trunk diameter at 4.5 feet from the ground. Create a script with a tree class with the following properties: Block, Plot, Species and DBH. The class should also have one method: diameter inside bark, calculated as DIB = DBH * (1/1.09). Once the class has been created, use the class to create tree objects by reading the data from the text file. The script should print the following: Report Tree 1

Block:

Plot:

Species:

DBH:

DIB:

Report Tree 2

Block:

etc

Tags (4)
1 Solution

Accepted Solutions
MVP Regular Contributor

Your tree class looks good, with one possible exception.  The next to last line appears to need indentation.  When sharing Python scripts, it is good to use the "syntax highlighter" when posting to GeoNet.  There are some good instructions on using the highlighter here: Code Formatting... the basics++ Here is what your code looks like using the highlighter with the indentation corrected:

class Tree:    def __init__(self, block, plot, species, dbh):        self.block = block        self.plot=plot        self.species=species        self.dbh=dbh        def dib(self):        dib = self.dbh * (1/1.09) # 1/1.09 = 0.917431193        return dib‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Also in line 9, I added some parenthesis around the 1/1.09.  I don't think it matters in this case, but it insures the division happens before the multiplication.  You could also use the 0.91743 equivalent.

For the next step, you will need some code to read your text file.  Here's a basic approach.  It assumes the text file does not have a header.

filename = r"C:\Path\to\trees.txt" # enter full path and filenametf = open(filename, 'r')treeData = tf.readlines()for row in treeData:    td = row.strip().split() # strip any line feeds, then split on spaces    print tdtf.close()‍‍‍‍‍‍‍‍‍‍

I created a simple tree data file, and the script prints out:

['1', '1', 'cedar', '4.5']['1', '2', 'spruce', '3.75']['2', '1', 'hemlock', '2.5']‍‍‍

Note that a list is produced and all the elements are text (in quotes).  It is possible that the Block and Plot will be text, but they could be numbers.  The DBH data is probably a floating point number, so you will need to change it from type text to float.  The next steps are to include the tree class and print the results.  This should be similar to your parcels  example.

You may need to deal with a header row when reading your text file.  If you were wanting to number each tree by row in the file, you will need a counter.

Let me know how this part goes.

7 Replies
Occasional Contributor

Hi CRNF,

Could you clarify what you want the output to be and what your input is?  Are you starting with a text file or you are looking for a script to produce a text file from a feature class/shapefile/etc?

by
New Contributor II

start with the text file trees.txt withw different fields called Block, Plot, Species and DBH, separated by spaces.

Esteemed Contributor

Do you have any spatial information in the txt file such as latitude and longitude?  If not, are you joining to join this data to a feature class that does have spatial information?

MVP Regular Contributor

I assume this is related to your question about Exercise 12.  The parcel class script used in that exercise can be used as a pattern to create a tree class.  Once the class is created, then you would come up with a way to read the lines in the text file.  Here is the parcel class example:

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‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
1. In line 1 change Parcel to Tree.
2. In lines 2, 3 and 4, replace landuse and value with block, plot, species and dbh. (You'll be adding a couple of new lines in this section.)
3. In lines 6, 13 and 14, replace assessment with dib.
4. In line 13, replace value with dbh, and replace rate  with your numeric value.
5. Delete lines 7 thru 12 in above script (landuse and rate section).

Share the script you come up with.  The next steps would be to read the text file, split each line where there are spaces, use the data to create a tree class, and print the results.

by
New Contributor II

class Tree:

def __init__(self, block, plot, species, dbh):

self.block = block

self.plot=plot

self.species=species

self.dbh=dbh

def dib(self):

dib = self.dbh * 1/1.09

return dib

MVP Regular Contributor

Your tree class looks good, with one possible exception.  The next to last line appears to need indentation.  When sharing Python scripts, it is good to use the "syntax highlighter" when posting to GeoNet.  There are some good instructions on using the highlighter here: Code Formatting... the basics++ Here is what your code looks like using the highlighter with the indentation corrected:

class Tree:    def __init__(self, block, plot, species, dbh):        self.block = block        self.plot=plot        self.species=species        self.dbh=dbh        def dib(self):        dib = self.dbh * (1/1.09) # 1/1.09 = 0.917431193        return dib‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Also in line 9, I added some parenthesis around the 1/1.09.  I don't think it matters in this case, but it insures the division happens before the multiplication.  You could also use the 0.91743 equivalent.

For the next step, you will need some code to read your text file.  Here's a basic approach.  It assumes the text file does not have a header.

filename = r"C:\Path\to\trees.txt" # enter full path and filenametf = open(filename, 'r')treeData = tf.readlines()for row in treeData:    td = row.strip().split() # strip any line feeds, then split on spaces    print tdtf.close()‍‍‍‍‍‍‍‍‍‍

I created a simple tree data file, and the script prints out:

['1', '1', 'cedar', '4.5']['1', '2', 'spruce', '3.75']['2', '1', 'hemlock', '2.5']‍‍‍

Note that a list is produced and all the elements are text (in quotes).  It is possible that the Block and Plot will be text, but they could be numbers.  The DBH data is probably a floating point number, so you will need to change it from type text to float.  The next steps are to include the tree class and print the results.  This should be similar to your parcels  example.

You may need to deal with a header row when reading your text file.  If you were wanting to number each tree by row in the file, you will need a counter.

Let me know how this part goes.

by
New Contributor II

thank you i think from this i can handle it. i appreciate it.