Click here to Skip to main content
15,867,453 members
Articles / Desktop Programming / Windows Forms

Build a Desktop GIS Application Using MapWinGIS and C#

Rate me:
Please Sign up or sign in to vote.
4.89/5 (70 votes)
13 Jul 2009CPOL6 min read 318.9K   22.2K   171   81
The royal gate to building Desktop GIS applications using the Open Source MapWinGIS and C#.

Introduction

A Geographic Information System is a type of application which has a set of facilities to capture, store, retrieve, maintain, and display geographic data and information. GIS applications are developed to meet many purposes, from simple purposes like GIS data exploration to sophisticated purposes like watershed mapping. Different commercial libraries are available for developers to use and implement in their applications, like ArcObject from ESIR and Map Suite from ThinkGeo. Unfortunately, the cost of license for these libraries is usually high, which makes the GIS application development process not viable for free lance programmers and small firms. In this series of tutorials, I am going to describe in detail how to build a desktop GIS application using the Open Source MapWinGIS library and C#. MapWinGIS is an ActiveX control for GIS development, with built-in support for many Raster and Vector formats. MapWinGIS.ocx is the core of MapWindow GIS, the well known Open Source, multi-purpose, GIS desktop application. The first step in reading this tutorial is acquiring MapWinGIS.ocx from the MapWindow GIS Open Source project website, then installing it to your computer using the installation procedures provided by the project site.

Create Your Map

Preparation

In this tutorial, I am using .NET Framework 3.5 and Microsoft Visual Studio 2008 Professional Edition. Actually, you can use Visual Studio Express Edition without any problem. I am also using a set of GIS data in ESRI shapefile format, which I have provided above as a zipped file. I recommend you to download this data compressed file, decompress it, and place the file contents in an independent folder. Now, you are ready to go to the next step.

Create a Project and Add a Reference to MapWinGIS Components

Now, fire up your Visual Studio and create a new Windows Forms application. Point to the Project menu, then click Add Reference. In the Add Reference dialog box, point to COM tab, then select MapWinGIS Components from the Component Name list and click OK.

01.JPG

Now, you have a new reference in your project and you can show its name under References in the Solution Explorer.

02.JPG

Now, we are going to add the Map control to our toolbox. The Map control is a control used as a container and interactive display area for geographic data. To add this control to the toolbox, point to the General tab in the toolbox and right click. Select Choose Item from the menu. The Choose Toolbox Item dialog box will appear. Select the COM Component tab and then select Map Control. To add it to the toolbox, click OK.

03.JPG

Now, you can see the Map Control icon in the toolbox contained by the General tab.

04.JPG

Time to Create Your GUI

Now, it's time to build your interface. Drag a ToolStrip control from the Menu & Toolbars tab in the toolbox and drop it to the form. Add five buttons to the ToolStrip, and name these buttons toolCursor, toolZoomExtent, toolZoomIn, toolZoomOut, and toolPan. You can also assign meaningful labels and images to the buttons (you can use the images that I have in my project resources). Drag the Map control from the toolbox and drop it to the form. In the Properties window, set the Dock property of the Map control to Fill and set the CursorMode property to cmNone.

Load your GIS data to the Application Pragmatically

Now, we will add two ESRI Shapefiles for our map. The ESRI Shapefile or simply a Shapefile is a popular geospatial vector data format for geographic information systems software. In the Form1_Load event, add the following code:

C#
//Add first layer to your map
int intHandler1; //integer index to handle the layer
//create a new instance for MapWinGIS.Shapefile
//MapWinGIS.Shapefile  is a data provider for ESRI Shapefile
MapWinGIS.Shapefile shapefile1 = new MapWinGIS.Shapefile();
//Define the data source for MapWinGIS.Shapefile instance
shapefile1.Open(@"D:\GISSampleData\base.shp", null );
//display the layer on the map
intHandler1 = axMap1.AddLayer(shapefile1, true);

//Add second layer
int intHandler2;
MapWinGIS.Shapefile shapefile2 = new MapWinGIS.Shapefile();
shapefile2.Open(@"D:\GISSampleData\nile.shp", null);
intHandler2 = axMap1.AddLayer(shapefile2, true);

In the first line, we have created an integer variable (intHandler1). In the second line, we have created an instance (shapefile1) for the MapWinGIS.Shapefile class. MapWinGIS.Shapefile is a container for geometric entities that represent real world geographic features. In the third line, the data source for the new object (shapefile1) is defined. This data source is an ESRI Shapefile. This data source shows Egypt as a polygon. In the fourth line, we have added this new object to our map control (axMap1) and made it visible. The function AddLayer is used to add a geographic layer to the map control and return an integer indicating the layer handler. Lines 4 to 8 add the second Shapefile to the Map control and make it visible. It is now time to run your first GIS application; strike F5 in your key board to show Egypt's map in the map control.

05.JPG

Change Your Feature Symbol

Customizing different symbology for different geographic features included in the map is one of the most important tasks for a GIS developer. In this lesson, I am going to present the easiest way to customize features symbology. Actually, MapWinGIS provides many advanced methods to symbolize GIS data, which I will discuss in an advanced lesson. Now, add the following code after the previous code in Form1_Load event:

C#
//Set Filling color of the ploygon shapefile
axMap1.set_ShapeLayerFillColor(intHandler1,
    (UInt32)(System.Drawing.ColorTranslator.ToOle 
            (System.Drawing.Color.SaddleBrown)));
//Set the line color
axMap1 .set_ShapeLayerLineColor (intHandler2 ,
    (UInt32)(System.Drawing.ColorTranslator.ToOle
            (System.Drawing.Color.Yellow)));
//Set the line width
axMap1.set_ShapeLayerLineWidth(intHandler2,5);

In the first line, we use the set_ShapeLayerFillColor method of the Map control (axMap1). This method uses the layer handler (intHandler1) and a color in UInt32 format to set the fill color for the defined layer. There are many other useful methods provided by the Map control to customize the layer symbology, like set_ShapeLayerFillStipple which defines the filling type of the layer, and set_ShapeLayerFillTransparency which defines the transparency level of the layer. In the second line, another method is used. This method - set_ShapeLayerLineColor - defines the color of line for line-geometry layers using the layer handler and the UInt32 format for color. In the third line, I have used the set_ShapeLayerLineWidth method to define the width of the line-geometry layer using this layer handler, and a float number denotes the line width. Run your application to see how it displays your map now.

06.JPG

Add Some Actions to Your Map

Sure, you want to add some amazing actions like zoom in, zoom out, and pan for your map. Adding such actions is a very simple task with MapWinGIS. The CursorMode property for the Map control provides these tasks in an easy to use way; you have to assign the mode that you want to this property to use it directly in your map. In the following code, I am going to play with the CursorMode property using the Click event for each button in my toolbar.

C#
private void toolCursor_Click(object sender, EventArgs e)
{
    axMap1.CursorMode = MapWinGIS.tkCursorMode.cmNone;
}

private void toolZoomIn_Click(object sender, EventArgs e)
{
    axMap1.CursorMode = MapWinGIS.tkCursorMode.cmZoomIn;
}

private void toolZoomOut_Click(object sender, EventArgs e)
{
    axMap1.CursorMode = MapWinGIS.tkCursorMode.cmZoomOut;
}

private void toolPan_Click(object sender, EventArgs e)     
{
    axMap1.CursorMode = MapWinGIS.tkCursorMode.cmPan;
}

To set your map to the maximum map extent, you will use the ZoomToMaxExtents method of the Map control. Just call this method using the Click event as follows to display your map in the maximum extent:

C#
private void toolZoomExtent_Click(object sender, EventArgs e)
{
    axMap1.ZoomToMaxExtents();
}

Time to test these new features; run your application, and check every button.

Conclusion

MapWinGIS is the most easy way to create GIS desktop applications. The above features show the principle tasks required by a GIS programmer. Future lessons in this series will look at many useful and advanced features of MapWinGIS.

History

  • First version: July 12, 2009.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Dammam University, Kingdom of Saudi Arabia
Egypt Egypt
Wisam Mohammed was graduated with B.Sc in Math then got M.Sc and Ph.D in Geographic Information Systems. He is working now as Assistant Professor for GIScience in Dammam University, Kingdom of Saudi Arabia where he is living. He had worked for different international and governmental firms.
He is also a Technical Writer. He published many books in Arabic and many scientific papers in the scientific journals. He is the official editor for the Arabic Manual of MapWindow GIS. His book "Basics of Geographic information Systems" was best seller technical book for 2008 in many Arabian Countries. He was honored by many scientific and academic institutions in Arabic countries as a pioneer of the Open Source paradigm in the Arabian World.

Comments and Discussions

 
GeneralRe: ActiveX control performance Pin
bublik47813-Sep-09 14:29
bublik47813-Sep-09 14:29 
GeneralRe: ActiveX control performance Pin
Wisam E. Mohammed14-Sep-09 19:37
Wisam E. Mohammed14-Sep-09 19:37 
GeneralRe: ActiveX control performance Pin
bublik47814-Sep-09 21:56
bublik47814-Sep-09 21:56 
GeneralRe: ActiveX control performance Pin
Wisam E. Mohammed1-Oct-09 22:39
Wisam E. Mohammed1-Oct-09 22:39 
Generalthanks a lot Pin
assmed26-Aug-09 3:03
assmed26-Aug-09 3:03 
GeneralRe: thanks a lot Pin
Wisam E. Mohammed12-Sep-09 19:57
Wisam E. Mohammed12-Sep-09 19:57 
GeneralImport/Expport Shape files to/from SQL Pin
mhabib7-Aug-09 12:26
mhabib7-Aug-09 12:26 
GeneralRe: Import/Expport Shape files to/from SQL Pin
Wisam E. Mohammed10-Aug-09 7:04
Wisam E. Mohammed10-Aug-09 7:04 
Unfortunately, there is no code to such this function.
GeneralRe: Import/Expport Shape files to/from SQL Pin
Wisam E. Mohammed23-Jun-10 8:45
Wisam E. Mohammed23-Jun-10 8:45 
Questiondo the buttons work? Pin
chris izatt1-Aug-09 14:18
chris izatt1-Aug-09 14:18 
AnswerRe: do the buttons work? Pin
Wisam E. Mohammed10-Aug-09 7:03
Wisam E. Mohammed10-Aug-09 7:03 
GeneralMy vote of 2 Pin
Joe Sonderegger20-Jul-09 18:56
Joe Sonderegger20-Jul-09 18:56 
GeneralCOM Error Pin
Md. Marufuzzaman16-Jul-09 0:25
professionalMd. Marufuzzaman16-Jul-09 0:25 
GeneralRe: COM Error Pin
Wisam E. Mohammed16-Jul-09 6:54
Wisam E. Mohammed16-Jul-09 6:54 
GeneralGive the Information for setting the environment to Run the Application Pin
pcm_it12-Jul-09 19:12
pcm_it12-Jul-09 19:12 
GeneralRe: Give the Information for setting the environment to Run the Application Pin
Wisam E. Mohammed12-Jul-09 20:13
Wisam E. Mohammed12-Jul-09 20:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.