Exercise 1: Finding Distance with Python

Functionality:

  • assign numeric values to two variables, add them together, and print the result
  • define a method that accepts four values (two pairs of coordinates) and computes the distance between them [ verify the proper calculations by trying several cases
  • import the Shapely library and use its distance function to compute distances between points

{

<em># numeric value assigned to 1st variable 'x'</em>
>>> x = 2
>>> Spartacus = 14
>>> x
2
>>> Spartacus + x
16
<em># numeric value assigned to 2nd variable 'y'</em>
>>> y = 3.2
<em># addition of 2 variables computed</em>
>>> y + x
5.2

<em># 4 values defined to establish coordinate values</em>
>>> x1 = 0
>>> y1 = 0
>>> x2 = 3
>>> y2 = 4
>>> x2 - x1
3
<em># distance between pairs computed</em>
>>> (x2-x1)**2
9
>>> (x2-x1)**2+(y2-y1)**2
25
>>> from math import sqrt
>>> sqrt((x2-x1)**2+(y2-y1)**2)
5.0
<em># Distance between points defined by following function 'stanza'</em>
>>> def myDistance(x1,y1,x2,y2):
	a=(x2-x1)
	b=(y2-y1)
	dist=sqrt(a**2+b**2)
	return dist

>>> myDistance(0,0,3,4)
5.0

<em># computing Euclidean distance</em>
>>> def myDistance(x1,y1,x2,y2):
a=(x2-x1)
b=(y2-y1)
dist=sqrt(a**2+b**2)
return dist

}

Questions to answer on the turn-in:

  • What major geospatial factor must your consider when interpreting the distance computed by either your method or the Shapely distance method? [ Cartesian plane vs. geoid ]

Elevation change is the key factor that can distort the accuracy of the Straight Line Metric computed by the                 defined Euclidian or Pythagorean distance because it is not accounted for in the equation. Both of the distances mentioned exist on a flat, two-dimensional plane known as the Cartesian coordinate plane. If slopes intersect the straight line between two points, the offset distance from that straight line must be accounted for in order to establish the most precise distance between two locations on the dynamic, real-world geoid.

  • What are the units of the computed distance from Shapely?
  • Describe an example scenario in which you might use a distance computation in a GIS analysis

Perhaps Lane Transit District wishes to update its bus time table for a specific route that has become notoriously late for its users each day of the week. The agency could choose to employ a study where average velocity of the bus is measured between each scheduled stop. A series of data points delineating dimensions of location and time could then be recorded via a GPS system. These data, once uploaded into a GIS, could then be analyzed with a series of line segments that contain the attribute of total time between the two points and, ultimately, an attribute for each point or bus stop with an averaged hourly time of arrival. A more precisely quantitative bus schedule can then result from the analysis described.

  • What does this code do?
    Describe the process in your own words (proper English sentences)
    what is the result?
    [ x=5
    y=3

print(x+2)

x = x+2
x += y # += is a special arithmetic operation which literally means ‘add right variable to left variable’

print(x) ]

The above code begins with a definition of the two variables being used to perform 2 basic arithmetic operations that follow. ‘print’ acts as a command for the script to perform the operation which resides inside the parentheses (). Following this line of code, a modification has been made to the definition of our first variable ‘x’ which currently equals ‘5’. ‘x’ is redefined as x(5) + 2 which then yields a new value for ‘x’ (7). A third and final operation is in fact a third and final definition of ‘x’ utilizing the definition of ‘y’ in the process. ‘+=’ states that ‘x’ now equates to ‘x’ + ‘y’. The final line ‘print(x)’ commands ‘x”s newly refined definition to be displayed. ‘x'(7) is added to ‘y'(3) yielding an equation with the value of (10).

Leave a Reply

Your email address will not be published. Required fields are marked *

Skip to toolbar