Using SharpMap (1)

October 14th, 2007

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 their own words,

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. [quote source]

So let’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.

Data Setup

Before beginning, we need to download a GIS dataset to display in the program. Our recent posts on QGIS discussed several options for download and viewing shapefiles. In part 3 of that tutorial, we downloaded a shapefile showing countries of the world so that shapefile will be used again in this project.

SharpMap Download

We first need to download the latest release of SharpMap here. 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.

Create a New C# Application

Using the free Visual C# Express program, create a new C# windows application. Here are several links (link 1, link 2) if you need some background on using Visual C# Express.

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.

Reference to SharpMap.dll

After switching to the form’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:

SharpMap Form

Press F5 to run the program although nothing special should happen yet.

Next go ahead and copy the shapefile to a folder in the project’s directory. For example, copy the shapefile (remember it is actually 3 separate files) into the program’s bin\debug folder.

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’s library to be defined just like any other C# class.

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";

Then in the form’s constructor, we first initialize the Map class with a size that matches the size of our Picture Box control.

public frmMap()
    //--> Initialize the map
    _sharpMap = new SharpMap.Map(new Size(600, 300));
    _sharpMap.BackColor = Color.White;

Now we can add our shapefile to the map via a 3-step process: (1) define a VectorLayer object, (2) set it’s data source, and (3) add it to the Map.

//--> 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);

Next we need to set the map’s view to the entire extent of our layer using the ZoomToExtents() function.

//--> Zoom the map to the entire extent

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.

private void RefreshMap()
    //--> Use SharpMap to generate the map image
    picMap.Image = _sharpMap.GetMap();

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:

SharpMap Program

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.