Masthead

Defining Functions

Not used

1. Introduction

We've allready used a number of functions in Python. You can also create your own functions. This is the first step in organizating large programs.

Functions can "encapsolate" code and shoould be of a specific purpose. Functions can then call other functions evenutally providing "high-level" functions that execute very complex processing. The great part about functions is that you just need to make one call to them to access all that complexity.

2. Defining Functions

Functions have a name that identifies them, a set of parameters, and an optional return value. Below is the definition for a function that will convert a distance and direction to an easting value.

import math
def GetEastingFromAzimuth(Easting,Azimuth,Distance):
 Azimuth=math.radians(Azimuth)
 DistanceEast = math.sin(Azimuth) * Distance
 return(Easting + DistanceEast)


NewEasting=GetEastingFromAzimuth(446142,1,13.9)
print(NewEasting)

Below is the companion function for obtaining a northing from an azimuth and distance.

import math
def GetNorthingFromAzimuth(Northing,Azimuth,Distance):
 Azimuth=math.radians(Azimuth)
 DistanceNorth = math.cos(Azimuth) * Distance
 return(Northing + DistanceNorth)

Northing=GetNorthingFromAzimuth(100000,90,1000)
print(Northing)

3. Optional Parameters

- need example

Exercise?

Use the table below to create a text file in Excel and then read the file into a Python script, convert the distance and azimuths to new values and write out the table with the new coordinates.

 The final output should appear similar to the table below.

Tree

Species

DBH

Distance

Azimuth

Easting

Northing

1

108

23.1

13.9

1

446142.7

4475372

10

108

27.1

16.2

6

446126.5

4475380

20

113

4.2

13.4

33

446144.4

4475364

30

108

23

6

46

446136.4

4475361

40

108

12.1

17.4

57

446138.6

4475380

50

113

15.9

7.9

76

446135.5

4475371

© Copyright 2018 HSU - All rights reserved.