<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Spatial Horizons &#187; C#</title>
	<atom:link href="http://spatialhorizons.com/category/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://spatialhorizons.com</link>
	<description>Exploring Geographic Technologies</description>
	<lastBuildDate>Tue, 16 Aug 2011 23:01:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>World Mapper (1)</title>
		<link>http://spatialhorizons.com/2007/02/15/world-mapper-1/</link>
		<comments>http://spatialhorizons.com/2007/02/15/world-mapper-1/#comments</comments>
		<pubDate>Thu, 15 Feb 2007 00:42:48 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://spatialhorizons.com/2007/02/15/world-mapper-1/</guid>
		<description><![CDATA[One of the objectives of this website is to create our own mapping applications. This requires a strong knowledge base of geography, mathematics, and computer programming. So before we can jump into the more advanced projects, we have to start with the basics. We&#8217;ve already been investigating GPS in our previous posts but today we [...]]]></description>
			<content:encoded><![CDATA[<p>One of the objectives of this website is to create our own mapping applications.  This requires a strong knowledge base of geography, mathematics, and computer programming.  So before we can jump into the more advanced projects, we have to start with the basics.  We&#8217;ve already been investigating GPS in our previous posts but today we will create a very basic mapping application.</p>
<p>Our &#8220;World Mapper&#8221; program begins by acquiring a map of the earth.  I downloaded this one from <a href="http://commons.wikimedia.org/wiki/World_map" target="_blank">Wikipedia Commons</a> and shrunk it down to 400 pixels by 200 pixels.</p>
<p><img id="image17" style="width: 400px; height: 200px;" src="http://spatialhorizons.com/wp-content/uploads/2007/02/worldmap.jpg" alt="WorldMap" /><br />
<span id="more-20"></span></p>
<p>The simplest way to map the Earth is to treat longitude and latitude as X/Y points.  This approach is not a true geographic projection but it is the easiest to comprehend. Thus, the valid X ranges are -180 to 180 and the Y ranges are -90 to 90.</p>
<p><img id="image18" style="width: 400px; height: 200px;" src="http://spatialhorizons.com/wp-content/uploads/2007/02/latlon_map.jpg" alt="LatLongMap" /></p>
<p>Our program will plot the locations of points on the world map as entered by the user.  Nothing fancy here &#8211; this is just setting the stage for some upcoming projects.</p>
<p>Create a new C# Windows Application in <a href="http://msdn.microsoft.com/vstudio/express/visualcsharp/" target="_blank">Visual C# Express</a>. Add a PictureBox from the toolbox and set it&#8217;s image location to the world map. Then add 2 labels, 2 textboxes, and 2 buttons to create some thing like this:<br />
<img id="image19" style="width: 420px; height: 298px;" src="http://spatialhorizons.com/wp-content/uploads/2007/02/worldmapper1.jpg" alt="WorldMapper1" /></p>
<p>There are only 4 functions we need to add to make the program work so the first to write is the &#8220;Map It&#8221; button&#8217;s click event.  Double-click on the button to enter code view.  Here is the approach I used:</p>
<p>&nbsp;</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> btnMapLocation_Click<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, EventArgs e<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #6666cc; font-weight: bold;">double</span> longX <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
  <span style="color: #6666cc; font-weight: bold;">double</span> latY <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
  <span style="color: #0600FF; font-weight: bold;">try</span>
  <span style="color: #008000;">&#123;</span>
    longX <span style="color: #008000;">=</span> Convert<span style="color: #008000;">.</span><span style="color: #0000FF;">ToDouble</span><span style="color: #008000;">&#40;</span>txtLongitude<span style="color: #008000;">.</span><span style="color: #0000FF;">Text</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    latY <span style="color: #008000;">=</span> Convert<span style="color: #008000;">.</span><span style="color: #0000FF;">ToDouble</span><span style="color: #008000;">&#40;</span>txtLatitude<span style="color: #008000;">.</span><span style="color: #0000FF;">Text</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
  <span style="color: #0600FF; font-weight: bold;">catch</span>
  <span style="color: #008000;">&#123;</span>
    MessageBox<span style="color: #008000;">.</span><span style="color: #0000FF;">Show</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Invalid numbers!&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
  <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>longX <span style="color: #008000;">&amp;</span>gt<span style="color: #008000;">;</span> <span style="color: #FF0000;">180</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">||</span> <span style="color: #008000;">&#40;</span>longX <span style="color: #008000;">&amp;</span>lt<span style="color: #008000;">;</span> <span style="color: #008000;">-</span><span style="color: #FF0000;">180</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
    MessageBox<span style="color: #008000;">.</span><span style="color: #0000FF;">Show</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Longitude out of range!&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>latY <span style="color: #008000;">&amp;</span>gt<span style="color: #008000;">;</span> <span style="color: #FF0000;">90</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">||</span> <span style="color: #008000;">&#40;</span>latY <span style="color: #008000;">&amp;</span>lt<span style="color: #008000;">;</span> <span style="color: #008000;">-</span><span style="color: #FF0000;">90</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
    MessageBox<span style="color: #008000;">.</span><span style="color: #0000FF;">Show</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Latitude out of range!&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #0600FF; font-weight: bold;">else</span>
  <span style="color: #008000;">&#123;</span>
    Point mapPoint <span style="color: #008000;">=</span> ConvertLatLon2ImagePosition<span style="color: #008000;">&#40;</span>longX, latY<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    PlotPosition<span style="color: #008000;">&#40;</span>mapPoint<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>&nbsp;</p>
<p>We first validate the user&#8217;s input by checking for numbers and then for out of range errors.  Next we do 2 things: 1) convert the lat/long location to actual on-screen pixel coordinates and 2) use a small bitmap to display the location of the form.</p>
<p>When I created the form, I named the Picture Box of the world &#8220;picWorldMap&#8221; soâ€¦</p>
<p>&nbsp;</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> Point ConvertLatLon2ImagePosition<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">double</span> longX, <span style="color: #6666cc; font-weight: bold;">double</span> latY<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  Point imgPos <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Point<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  imgPos<span style="color: #008000;">.</span><span style="color: #0000FF;">X</span> <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>longX <span style="color: #008000;">+</span> <span style="color: #FF0000;">180</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">/</span> <span style="color: #FF0000;">360</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">*</span> picWorldMap<span style="color: #008000;">.</span><span style="color: #0000FF;">Width</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  imgPos<span style="color: #008000;">.</span><span style="color: #0000FF;">Y</span> <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">90</span> <span style="color: #008000;">-</span> latY<span style="color: #008000;">&#41;</span> <span style="color: #008000;">/</span> <span style="color: #FF0000;">180</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">*</span> picWorldMap<span style="color: #008000;">.</span><span style="color: #0000FF;">Height</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  imgPos<span style="color: #008000;">.</span><span style="color: #0000FF;">X</span> <span style="color: #008000;">+=</span> picWorldMap<span style="color: #008000;">.</span><span style="color: #0000FF;">Location</span><span style="color: #008000;">.</span><span style="color: #0000FF;">X</span><span style="color: #008000;">;</span>
  imgPos<span style="color: #008000;">.</span><span style="color: #0000FF;">Y</span> <span style="color: #008000;">+=</span> picWorldMap<span style="color: #008000;">.</span><span style="color: #0000FF;">Location</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Y</span><span style="color: #008000;">;</span>
  <span style="color: #0600FF; font-weight: bold;">return</span> imgPos<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>&nbsp;</p>
<p>Take a minute to review the math here.  It&#8217;s not to complex but is somewhat tricky.  We need to first find where within the map the point should go.  Then we adjust the location by adding the picture box&#8217;s position within the form itself.  This results an a X/Y location (measured from the top-left corner) that is based on the entire form and will plot correctly within the map itself.</p>
<p>To show each point on the map, I create a 4&#215;4 bitmap(<img id="image21" src="http://spatialhorizons.com/wp-content/uploads/2007/02/point.bmp" alt="WorldMapperPoint" width="4" height="4" /> )to serve as the point&#8217;s symbol.  I then needed a function to show the point on the map.  To be spatially accurate, you must modify the point&#8217;s position based on the bitmap size. Since the image was 4&#215;4, I subtracted 2 from the position to ensure the point is centered over the location.</p>
<p>&nbsp;</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> PlotPosition<span style="color: #008000;">&#40;</span>Point mapPoint<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  PictureBox p <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> PictureBox<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  p<span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;mappoint&quot;</span><span style="color: #008000;">;</span>
  p<span style="color: #008000;">.</span><span style="color: #0000FF;">ImageLocation</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;point.bmp&quot;</span><span style="color: #008000;">;</span>
  p<span style="color: #008000;">.</span><span style="color: #0000FF;">Location</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Point<span style="color: #008000;">&#40;</span>mapPoint<span style="color: #008000;">.</span><span style="color: #0000FF;">X</span> <span style="color: #008000;">-</span> <span style="color: #FF0000;">2</span>, mapPoint<span style="color: #008000;">.</span><span style="color: #0000FF;">Y</span> <span style="color: #008000;">-</span> <span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  p<span style="color: #008000;">.</span><span style="color: #0000FF;">Size</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Size<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">4</span>,<span style="color: #FF0000;">4</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  frmMain<span style="color: #008000;">.</span><span style="color: #0000FF;">ActiveForm</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Controls</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>p<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  p<span style="color: #008000;">.</span><span style="color: #0000FF;">BringToFront</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>&nbsp;</p>
<p>I also added a button to clear the points from the map.</p>
<p>&nbsp;</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> btnClearMap_Click<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, EventArgs e<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  Form currentForm <span style="color: #008000;">=</span> frmMain<span style="color: #008000;">.</span><span style="color: #0000FF;">ActiveForm</span><span style="color: #008000;">;</span>
  Control<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> mappoints <span style="color: #008000;">=</span> currentForm<span style="color: #008000;">.</span><span style="color: #0000FF;">Controls</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Find</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;mappoint&quot;</span>, <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&amp;</span>lt<span style="color: #008000;">;=</span> mappoints<span style="color: #008000;">.</span><span style="color: #0000FF;">GetUpperBound</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    currentForm<span style="color: #008000;">.</span><span style="color: #0000FF;">Controls</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Remove</span><span style="color: #008000;">&#40;</span>mappoints<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>&nbsp;</p>
<p>The application should compile and run successfully.  There is a lot of functionality that could be added but for now we&#8217;ve accomplished our objective.</p>
<p><a title="World Mapper (2)" href="http://spatialhorizons.com/2007/04/02/world-mapper-2/">Continue to part 2</a></p>
]]></content:encoded>
			<wfw:commentRss>http://spatialhorizons.com/2007/02/15/world-mapper-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

