<?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; SharpMap</title>
	<atom:link href="http://spatialhorizons.com/category/sharpmap/feed/" rel="self" type="application/rss+xml" />
	<link>http://spatialhorizons.com</link>
	<description>Exploring Geographic Technologies with GIS, GPS, C#, XNA, and Open Source Tools</description>
	<lastBuildDate>Thu, 18 Sep 2008 19:00:31 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Using SharpMap (4)</title>
		<link>http://spatialhorizons.com/2007/11/07/using-sharpmap-4/</link>
		<comments>http://spatialhorizons.com/2007/11/07/using-sharpmap-4/#comments</comments>
		<pubDate>Wed, 07 Nov 2007 23:55:19 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[SharpMap]]></category>

		<guid isPermaLink="false">http://spatialhorizons.com/2007/11/07/using-sharpmap-4/</guid>
		<description><![CDATA[This post concludes our tutorial introducing SharpMap and how to use it in a mapping application. We were planning to explore it in some more depth by demonstrating some additional functionality, but we are working on another project that will be discussed in the near future. In short, SharpMap has tremendous potential as a lightweight [...]]]></description>
			<content:encoded><![CDATA[<p>This post concludes our tutorial introducing <a href="http://www.codeplex.com/SharpMap">SharpMap</a> and how to use it in a mapping application. We were planning to explore it in some more depth by demonstrating some additional functionality, but we are working on another project that will be discussed in the near future. In short, SharpMap has tremendous potential as a lightweight component for adding mapping capabilities to desktop and web applications.</p>
<p>Our sample application&#8217;s source code is available to download below. It includes everything needed to compile and run the application using <a href="http://msdn2.microsoft.com/en-us/express/aa700756.aspx">Visual C# Express</a>. The SharpMap library, included in the download, is released under the GNU Lesser General Public License. For more information on that, see <a href="http://www.codeplex.com/SharpMap/Wiki/View.aspx?title=Is%20SharpMap%20free%20with%20no%20strings%20attached&#038;referringTitle=FAQ">here</a>.</p>
<p><img src="http://spatialhorizons.com/wp-content/uploads/2007/10/sharpmap_coloredmap.jpg" alt="SharpMap Demo Application" /></p>
<p><a href="http://spatialhorizons.com/downloads/SharpMapDemo.zip"><img border="0" alt="Download Source Code" src="http://spatialhorizons.com/downloads/code_download.jpg" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://spatialhorizons.com/2007/11/07/using-sharpmap-4/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using SharpMap (3)</title>
		<link>http://spatialhorizons.com/2007/10/28/using-sharpmap-3/</link>
		<comments>http://spatialhorizons.com/2007/10/28/using-sharpmap-3/#comments</comments>
		<pubDate>Sun, 28 Oct 2007 23:31:54 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[SharpMap]]></category>

		<guid isPermaLink="false">http://spatialhorizons.com/2007/10/28/using-sharpmap-3/</guid>
		<description><![CDATA[Our third post in this series (part 1, part 2) on SharpMap continues the development of a basic mapping application programmed in C#. We will now add some more functionality to the program.

Zooming By Percentages
The SharpMap.Map class has a Zoom property that stores the map&#8217;s current width in the units of the map.  To [...]]]></description>
			<content:encoded><![CDATA[<p>Our third post in this series (<a href="http://spatialhorizons.com/2007/10/14/using-sharpmap-1/">part 1</a>, <a href="http://spatialhorizons.com/2007/10/20/using-sharpmap-2/">part 2</a>) on <a href="http://www.codeplex.com/SharpMap">SharpMap</a> continues the development of a basic mapping application programmed in C#. We will now add some more functionality to the program.</p>
<p><img src="http://spatialhorizons.com/wp-content/uploads/2007/10/sharpmap_coloredmap.jpg" alt="SharpMap with Coloring" /></p>
<p><strong>Zooming By Percentages</strong></p>
<p>The SharpMap.Map class has a Zoom property that stores the map&#8217;s current width in the units of the map.  To understand this better, add this line of code to the RefreshMap function we&#8217;ve already created:</p>
<p>
<pre lang="csharp">MessageBox.Show(_sharpMap.Zoom.ToString());</pre>
</p>
<p><span id="more-127"></span><br />
Running the program now should immediately display a value of almost exactly 360. This makes sense because our data source is a <a href="http://spatialhorizons.com/faqs#shapefile">shapefile</a> of the world, our map units are decimal degrees, and the width of the earth is 360 degrees.</p>
<p>So &#8220;zooming&#8221; is really just a process of changing the the width of the map.  Zooming in will require us to subtract from the SharpMap.Map.Zoom while zooming out adds a value to Zoom. We could simply use a constant value like 20 (degrees) but that is only applicable when our source data is in decimal degrees. If we switched to data in feet for example, adding or subtracting 20 feet from the a map width would takes hundreds of clicks to zoom in and out. </p>
<p>Instead, a better approach for zooming is to use a percentage change. Remember we defined a class-level constant in our the form&#8217;s code:</p>
<p>
<pre lang="csharp">const float ZOOM_FACTOR = 0.3f;</pre>
</p>
<p>We can use that value to zoom in or out 30% each time our zoom buttons are pressed.</p>
<p><strong>Zooming Code</strong></p>
<p>Double-clicking on the Zoom In button in our form will create the click event for the button. We only need to add 2 lines of code to change the Zoom value (i.e., map width).</p>
<p>
<pre lang="csharp">private void btnZoomIn_Click(object sender, EventArgs e)
{
    _sharpMap.Zoom -= _sharpMap.Zoom * ZOOM_FACTOR;
    RefreshMap();
}</pre>
</p>
<p>And zooming out does just the opposite:</p>
<p>
<pre lang="csharp">private void btnZoomOut_Click(object sender, EventArgs e)
{
    _sharpMap.Zoom += _sharpMap.Zoom * ZOOM_FACTOR;
    RefreshMap();
}</pre>
</p>
<p>Additionally, our Zoom to Full button click event looks like this:</p>
<p>
<pre lang="csharp">private void btnZoomFull_Click(object sender, EventArgs e)
{
    _sharpMap.ZoomToExtents();
    RefreshMap();
}</pre>
</p>
<p><strong>Panning Code</strong></p>
<p>We also can easily implement panning in this application. A map pan (or recenter) will occur any time the user clicks on the map image. The mouse click location will then become the map center. This process requires two steps: (1) convert the &#8220;image&#8221; coordinates to real-world coordinates and (2) recenter the map.</p>
<p>Fortunately the SharpMap.Map class has a ImageToWorld method to convert the mouse click coordinates to the real-world location in the map. Then we just need the change the map&#8217;s center coordinates before refreshing the map. The code is added to Picture Box&#8217;s mouse click event and looks like this:</p>
<p>
<pre lang="csharp">private void picMap_MouseClick(object sender, MouseEventArgs e)
{
    //--> Convert mouse click point from image coordinates to world coordinates
    SharpMap.Geometries.Point p = _sharpMap.ImageToWorld(new PointF(e.X, e.Y));

    //--> Recenter map
    _sharpMap.Center.X = p.X;
    _sharpMap.Center.Y = p.Y;

    RefreshMap();
}</pre>
</p>
<p>Running the program now allows the user to zoom in and out and pan around the map.</p>
<p><img src="http://spatialhorizons.com/wp-content/uploads/2007/10/sharpmap_labels2.jpg" alt=SharpMap Zooming" /></p>
<p>Next time we will add some more functionality to the application before moving on to another topic.</p>
]]></content:encoded>
			<wfw:commentRss>http://spatialhorizons.com/2007/10/28/using-sharpmap-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using SharpMap (2)</title>
		<link>http://spatialhorizons.com/2007/10/20/using-sharpmap-2/</link>
		<comments>http://spatialhorizons.com/2007/10/20/using-sharpmap-2/#comments</comments>
		<pubDate>Sat, 20 Oct 2007 15:41:21 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[SharpMap]]></category>

		<guid isPermaLink="false">http://spatialhorizons.com/2007/10/20/using-sharpmap-2/</guid>
		<description><![CDATA[Our previous post introduced SharpMap and how to use it in a C# program. We built a form and used a PictureBox control to display maps generated by SharpMap. We left off with a rather bland looking map of the world:

Adding Layer Styles
Adding colors to our countries layer is straightforward. In our form&#8217;s constructor, we [...]]]></description>
			<content:encoded><![CDATA[<p>Our <a href="http://spatialhorizons.com/2007/10/14/using-sharpmap-1/">previous post</a> introduced <a href="http://www.codeplex.com/SharpMap">SharpMap</a> and how to use it in a C# program. We built a form and used a PictureBox control to display maps generated by SharpMap. We left off with a rather bland looking map of the world:</p>
<p><img src="http://spatialhorizons.com/wp-content/uploads/2007/10/sharpmap_program1.jpg" alt="SharpMap Program" /></p>
<p><strong>Adding Layer Styles</strong></p>
<p>Adding colors to our countries layer is straightforward. In our form&#8217;s constructor, we simply define colors using the Style class:</p>
<p>
<pre lang="csharp">countriesLayer.Style.Fill = Brushes.LightGreen;
countriesLayer.Style.EnableOutline = true;
countriesLayer.Style.Outline = Pens.DarkGreen;</pre>
</p>
<p>We also switched the map&#8217;s background color:</p>
<p>
<pre lang="csharp">_sharpMap.BackColor = Color.LightBlue;</pre>
</p>
<p><span id="more-129"></span></p>
<p>Running the program now produces a better looking map:</p>
<p><img src="http://spatialhorizons.com/wp-content/uploads/2007/10/sharpmap_coloredmap.jpg" alt="SharpMap with Coloring" /></p>
<p><strong>Adding Labels</strong></p>
<p>SharpMap also allows for creating label layers to add to the map that display text from another layer&#8217;s attributes. In our example, we can display the names of the countries on the map using the following code:</p>
<p>
<pre lang="csharp">SharpMap.Layers.LabelLayer labelLayer =
     new SharpMap.Layers.LabelLayer("Country Names");
labelLayer.DataSource = countriesLayer.DataSource;
labelLayer.LabelColumn = "NAME";
labelLayer.Style.CollisionDetection = true;
labelLayer.Style.CollisionBuffer = new SizeF(10, 10);
labelLayer.MultipartGeometryBehaviour =
     SharpMap.Layers.LabelLayer.MultipartGeometryBehaviourEnum.Largest;
labelLayer.Style.Font = new Font(FontFamily.GenericSansSerif, 8);
_sharpMap.Layers.Add(labelLayer);</pre>
</p>
<p>In the code, we create a LabelLayer, assign it to the same data source as our countries layer, choose the field that contains the country names (&#8221;NAME&#8221;), and then define some additional properties. Enabling collision detection avoids some overlapping labels while the multi-part geometry behavior instructs SharpMap to label the largest polygon if a country has multiple shapes.</p>
<p>The final result is not perfect because there are just too many country names to display at once. Setting the collision buffer to 10,10 reduces the number of labels drawn but which country names are displayed is completely arbitrary.</p>
<p><img src="http://spatialhorizons.com/wp-content/uploads/2007/10/sharpmap_labels.jpg" alt="SharpMap with Labels" /></p>
<p>When we add code to the zoom in button in the next post, overlapping labels will become less of an issue:</p>
<p><img src="http://spatialhorizons.com/wp-content/uploads/2007/10/sharpmap_labels2.jpg" alt="SharpMap with Labels" /></p>
]]></content:encoded>
			<wfw:commentRss>http://spatialhorizons.com/2007/10/20/using-sharpmap-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using SharpMap (1)</title>
		<link>http://spatialhorizons.com/2007/10/14/using-sharpmap-1/</link>
		<comments>http://spatialhorizons.com/2007/10/14/using-sharpmap-1/#comments</comments>
		<pubDate>Sun, 14 Oct 2007 18:31:17 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[SharpMap]]></category>

		<guid isPermaLink="false">http://spatialhorizons.com/2007/10/14/using-sharpmap-1/</guid>
		<description><![CDATA[In our ongoing effort to explore various methods for developing mapping applications, we need to consider incorporating existing libraries instead of writing code ourselves.  One such solution is SharpMap.  SharpMap is an emerging open source mapping engine that provides functionality to access GIS data, such as shapefiles, and generate map images.  In [...]]]></description>
			<content:encoded><![CDATA[<p>In our ongoing effort to explore various methods for developing mapping applications, we need to consider incorporating existing libraries instead of writing code ourselves.  One such solution is <a href="http://www.codeplex.com/SharpMap">SharpMap</a>.  SharpMap is an emerging open source mapping engine that provides functionality to access GIS data, such as shapefiles, and generate map images.  In their own words,</p>
<p class="shquote">SharpMap is an easy-to-use mapping library for use in web and desktop applications. It provides access to many types of GIS data, enables spatial querying of that data, and renders beautiful maps. The engine is written in C# and based on the .Net 2.0 framework. SharpMap is released under GNU Lesser General Public License. [<a href="http://www.codeplex.com/SharpMap">quote source</a>]</p>
<p>So let&#8217;s go ahead and program a simple mapping application in C# that uses SharpMap. As with our other tutorials, this example is rather primitive on purpose to highlight specifically how easy it use to use SharpMap. Readers are encouraged to use this tutorial as a basis for creating much richer applications.</p>
<p><span id="more-99"></span></p>
<p><strong>Data Setup</strong></p>
<p>Before beginning, we need to download a GIS dataset to display in the program. Our recent <a href="http://spatialhorizons.com/2007/09/09/using-qgis-1-introduction/">posts</a> on QGIS discussed several options for download and viewing <a href="http://spatialhorizons.com/faqs/#shapefile">shapefiles</a>.  In <a href="http://spatialhorizons.com/2007/09/23/using-qgis-3-more-layer-symbology/">part 3</a> of that tutorial, we downloaded a shapefile showing countries of the world so that shapefile will be used again in this project.</p>
<p><strong>SharpMap Download</strong></p>
<p>We first need to download the latest release of SharpMap <a href="http://www.codeplex.com/SharpMap/Release/ProjectReleases.aspx?ReleaseId=154">here</a>. Download the version 0.9 binary file and unzip it to your hard drive.  The zip file contains 4 files but the only file we will use is the SharpMap.dll.</p>
<p><strong>Create a New C# Application</strong></p>
<p>Using the free <a href="http://msdn2.microsoft.com/en-us/express/aa700756.aspx">Visual C# Express program</a>, create a new C# windows application. Here are several links (<a href="http://msdn2.microsoft.com/en-us/library/ms379606(vs.80).aspx">link 1</a>, <a href="http://csharp.net-tutorials.com/basics/visual-csharp-express/">link 2</a>) if you need some background on using Visual C# Express.</p>
<p>The first step is to reference the SharpMap.dll we downloaded. Go to the Project menu iten, select Add Reference, then chose browse, and navigate to the SharpMap.dll.  It should then appear in the solution explorer as a project reference.</p>
<p><img src="http://spatialhorizons.com/wp-content/uploads/2007/10/sharpmap_reference.jpg" alt="Reference to SharpMap.dll" /></p>
<p>After switching to the form&#8217;s design view, add a Picture Box control to serve as our map image area. We added 3 buttons and a label to produce something that looks like this:</p>
<p><img src="http://spatialhorizons.com/wp-content/uploads/2007/10/sharpmap_form.jpg" alt="SharpMap Form" /></p>
<p>Press F5 to run the program although nothing special should happen yet.</p>
<p>Next go ahead and copy the shapefile to a folder in the project&#8217;s directory. For example, copy the shapefile (remember it is actually 3 separate files) into the program&#8217;s bin\debug folder.</p>
<p>Now switch to the code view for the form and begin by adding some class-level variables and constants. The main class we will use throughout the program is the SharpMap.Map class. Because we have already referenced the SharpMap.dll, Visual C# Express should allow classes from SharpMap&#8217;s library to be defined just like any other C# class. </p>
<p>
<pre lang="csharp">public partial class frmMap : Form
{
    //--> Define the SharpMap object
    SharpMap.Map _sharpMap;  

    //--> Set the zoom factor percentage
    const float ZOOM_FACTOR = 0.3f;

    //--> Define the data name and source
    const string DATA_NAME = "World Countries";
    const string DATA_PATH = @"world_adm0\world_adm0.shp";
</pre>
</p>
<p>Then in the form&#8217;s constructor, we first initialize the Map class with a size that matches the size of our Picture Box control.</p>
<p>
<pre lang="csharp">public frmMap()
{
    //--> Initialize the map
    _sharpMap = new SharpMap.Map(new Size(600, 300));
    _sharpMap.BackColor = Color.White;</pre>
</p>
<p>Now we can add our shapefile to the map via a 3-step process: (1) define a VectorLayer object, (2) set it&#8217;s data source, and (3) add it to the Map.</p>
<p>
<pre lang="csharp">//--> Add the countries shapefile to the map
SharpMap.Layers.VectorLayer countriesLayer = new SharpMap.Layers.VectorLayer(DATA_NAME);
countriesLayer.DataSource = new SharpMap.Data.Providers.ShapeFile(DATA_PATH);
_sharpMap.Layers.Add(countriesLayer);</pre>
</p>
<p>Next we need to set the map&#8217;s view to the entire extent of our layer using the ZoomToExtents() function.</p>
<p>
<pre lang="csharp">//--> Zoom the map to the entire extent
_sharpMap.ZoomToExtents();
RefreshMap();</pre>
</p>
<p>We also needed to alert SharpMap to draw our map and return it as an image. Because we will need to call this function from several spots in our program, a separate function was created called RefreshMap that does just that. The returned map image is then displayed in our Picture Box.</p>
<p>
<pre lang="csharp">private void RefreshMap()
{
    //--> Use SharpMap to generate the map image
    picMap.Image = _sharpMap.GetMap();
}</pre>
</p>
<p>Even without adding code to the buttons, we can still run the program at this point. Press F5 and you should see something like this:</p>
<p><img src="http://spatialhorizons.com/wp-content/uploads/2007/10/sharpmap_program1.jpg" alt="SharpMap Program" /></p>
<p>The map image was indeed generated by SharpMap but it lacks a decent coloring scheme. Next time we will correct that and later we will add functionality to the zooming buttons.</p>
]]></content:encoded>
			<wfw:commentRss>http://spatialhorizons.com/2007/10/14/using-sharpmap-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
