Skip to content

Instantly share code, notes, and snippets.

@feomike
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save feomike/98a5eda0b2d73d3dc007 to your computer and use it in GitHub Desktop.
Save feomike/98a5eda0b2d73d3dc007 to your computer and use it in GitHub Desktop.
trig example

trig example

this gist holds the trig example

<!DOCTYPE html>
<html>
<body>
<h2>Trig Example</h2>
<p>This page is the use case for the trigonometry example i recently had to write. i pasted it here, because a friend, who is a high school math teacher asked me for a real world example for using trig. here goes.</p>
<p>Let say for some reason you wanted to model address locations. lets say you needed to do this b/c you were using these locations for say where mortgages are (e.g. mortgages are at houses, houses are at address). In stock geography terms, addresses are point locations, that is to say they are single locations of an x,y (in geography terms perhaps a longitude and latitude). Addresses in this context are just single XY locations, rather than say the actual area that would describe the building footprint. In abstract terms, this single XY represents the building footprint; its just a simpler term for the house.</p>
<p>Lets say we need a large number of these points, say all of the mortgages for a single county or something like that. Address points for every location are hard to come up, or at least they are expensive. One could buy them from a vendor, but then one might have all of the contract terms and conditions associated with what the vendor sold. One could also go to a given county to acquire the data, but not all counties have published this data or publish it with similar derivative terms as vendors. I can't use google or something else like that. Suffice it to say, that i need to have a surrogate for generating these addresses.</p>
<p>So now lets imagine i can get digital <a href="ftp://ftp2.census.gov/geo/tiger/TIGER2014">geographic data from say the census bureau </a> for all road data. lets say these road files, actually are a) actually line data (think cartesian lines with full geometry for every point assigned as strings making up each linear feature, b) that each linear feature represents real world coordinates for all (ok most) roads and c) each feature also has attributes that tell us the name of the road, and the address range of the road.</p>
<p>So these features might look exactly like the image below. The red lines here represent roads. The gold highlighted line down the middle represents the road upon which that i actually want to generate address points.</p>
<p><a href ./image1.pngtarget="_blank">
<img border="0" src="./image001.png" alt="Image 1" width="600" height="400"></a></p>
<p>If you look at the next image below, then you will see all of the information the US Census publishes along with this road. I have highlighted in the red box some of the ones interesting to me; lfromhn and ltohn (2 and 36 respectively) are the address numbers on the left hand side of the street which start at 2 and end at 36. rfromh and rtohn (1 and 33) are the address numbers on the right hand side of the street and start at 1 and end at 33. The name of this street is Powel Ave.</p>
<p><a href ./image1.pngtarget="_blank">
<img border="0" src="./image002.png" alt="Image 2" width="600" height="400"></a></p>
<p>Now lets suppose i place a single house at every address from 1 to 33 on the right, and 2 to 36 on the left. The geometry means to do this, is to say place a point representing each address every 1/36th of the distance from the beginning of the line to the end of the line. Perhaps that interpolation looks something like this next image.</p>
<p><a href ./image1.pngtarget="_blank">
<img border="0" src="./image003.png" alt="Image 3" width="600" height="400"></a>. </p>
<p>The problem with that, is that these points fall along the line and are not really that helpful in representing house locations, which in reality are even on the left hand side and odd on the right hand side. See look at this next image to see where 36 Powel Ave is; it shouldn't be in the middle of the street, it needs to be offset to the north east.</p>
<p> <a href ./image1.pngtarget="_blank">
<img border="0" src="./image004.png" alt="Image 4" width="600" height="400"></a>.</p>
<p>What i really need is something like this image below. In this case, all of the addresses are offset on the correct side of the street and 36 Powel (highlighted in red in the image above) is placed in about the right location.</p>
<p><a href ./image1.pngtarget="_blank">
<img border="0" src="./image005.png" alt="Image 5" width="600" height="400"></a></p>
<p>So how do i move a single XY from its place along the line, to an offset of a certain distance, like in this case 10 meters? I do it with trigonometry. Trig allows me to calculate the DX and DY for a new location offset by a certain distance (10 meters) along a line that is perpendicular to the street. With the simple equation;</p>
<p>
<ul>
<li> newX = originalX + sin(Azimuth-90)*theOffset</li>
<li> newY = originalY + cos(Azimuth-90)*theOffset</li>
<li> where: Azimuth is the north based radian direction of the street line</li>
<li> where = theOffset is the distance i want the point moved/li>
</ul>
</p>
<p>There are a whole bunch of other issues, but generally this works. If you want to see the very short code engine to move the point look <a href="https://gist.github.com/feomike/8205b9628a9a70f0602e">small code snipet.</a> If you want to see a much longer more in-depth example of the whole thing, then see <a href="https://github.com/feomike/grass_in">this full code example.</a> Both of these are in python.</p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment