Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Add GIS and Mapping Functionality to your .NET Applications

0.00/5 (No votes)
28 Sep 2014 2  
Add GIS and Mapping Functionality to your .NET Applications

Sample Image - maximum width is 600 pixels Sample Image - maximum width is 600 pixels

Introduction

I assume you have a basic knowledge of ASP.NET and C#.NET, and so on.
Easy GIS .NET is a suite of .NET GIS mapping tools and controls to let developers easily incorporate GIS functionality into their own applications. The Desktop Edition includes a stand-alone viewing application which can be used to open and view ESRI shapefiles. This application is built using the same .NET controls that can be included in your own applications. Easy GIS .NET is a free software released under the Lesser General Public License. You can freely use it in personal and commercial software.

Background

Presently, I am working on a project where I need to generate some map based on various distributions on Bangladesh map. I am using QGIS (A Free and Open Source Geographic Information System) which is an excellent tool to generate complex map. Then I wanted to generate some map based on my database value so I searched in Google and Easy GIS grab my attention. After some initial struggle, I am able to generate something that I think will be required by many.

How to Configure

Step 1

Download the required .dll from here.

Step 2

Start a new project as ASP.NET Web application; give the name as WebExample1. It will give you one Default.aspx and .cs file.

Step 3

Now add these references:

  • EGIS.ShapeFileLib.dll
  • EGIS.Web.Controls.dll
  • geomutil_lib.dll
  • geomutil_libx64.dll

Step 4

Add the below mentioned code in Web.config file under "system.web" section:

        <httpHandlers>
   <add path="egismap.axd" verb="*" 
   type="EGIS.Web.Controls.SFMapImageProvider, EGIS.Web.Controls, 
   Version=4.3.0.0, Culture=neutral, PublicKeyToken=05b98c869b5ffe6a"
    validate="true" />
  </httpHandlers>

Step 5

Download shapefile for Bangladesh from here.

Step 6

Add the folder in your project.

Step 7

Download bangladesh.egp here.

Step 8

Add the file in your project

Step 9

Now your Solution Explorer will look like this:

Using the Code

Now pay some attention to the "bangladesh.egp" file:

<sfproject version="1.0">
  <MapBackColor>buttonface</MapBackColor>
  <layers>
    <shapefile>
      <name>BGD_adm3.shp</name>
      <path>bangladesh/BGD_adm3</path>
      <renderer>
        <MinZoomLevel>-1</MinZoomLevel>
        <MaxZoomLevel>-1</MaxZoomLevel>
        <MinRenderLabelZoom>-1</MinRenderLabelZoom>
        <FieldName>NAME_3</FieldName>
        <Font Size="10" Name="Microsoft Sans Serif" Style="Regular">
        </Font>
        <FontColor>Black</FontColor>
        <FillColor>#FCFCFC</FillColor>
        <OutlineColor>#969696</OutlineColor>
        <PenWidthScale>0.0001355183</PenWidthScale>
        <FillInterior>True</FillInterior>
        <Selectable>False</Selectable>
        <LineType>Outline</LineType>
        <ShadowText>True</ShadowText>
        <PointSize>5</PointSize>
        <UseToolTip>True</UseToolTip>
        <ToolTipFieldName>NAME_3</ToolTipFieldName>
      </renderer>
    </shapefile>
  </layers>
</sfproject>
  1. <name> field contains the shapefile name, so change it accordingly.
  2. <path> field contains the location of the shapefile, so change it accordingly.
  3. Change rest of the fields accordingly.

Now compile and run the project. You will see your assigned map.

Now, we want to display some distribution wise data at district level.
For this purpose, I have added a column name "popu_mis" which contains district wise population in BGD_adm3.shp file.

What is shapefile

The shapefile format is now a common format for storing GIS data. Shapefiles stored non-topological vector data along with related attribute data. Developed by Esri, shapefiles can be directly read by a number of GIS software programs such as ArcGIS and QGIS. A shapefile is actually a collection of at least three basic files: .shp, .shx and .dbf. All three files must be present in the same directory for them to be viewable. There may be additional files such as a .prj with the shapefile’s projection information. Commonly, shapefiles are compressed in a .zip file for transfer such as emailing as an attachment or via a web site download.

If you want to learn more about shapefile, then click here.
If you want to download shapefile viewer, then click here.

Render with Data

private void SetupPopulation()
{
    TooltipHeaderFieldNamePair[] tooltipPairs;
    tooltipPairs = new TooltipHeaderFieldNamePair[] { 
            new TooltipHeaderFieldNamePair("District: ", "NAME_3"),
            new TooltipHeaderFieldNamePair("Population: ", "popu_mis")};
    SetupCustomRenderSettings("popu_mis", 0, tooltipPairs);
}

"NAME_3" is the district field and "popu_mis" is the population field. It will create a tooltip text.

double[] samples = new double[numRecords];
//find the range of population values and obtain the quintile quantiles
for (int n = 0; n < numRecords; n++)
{
    if (String.IsNullOrEmpty(dbfReader.GetField(n, fieldIndex)) == false)
    {
        string tmp = dbfReader.GetField(n, fieldIndex);
        int value;
        if (int.TryParse(tmp, out value))
        {
            double d = double.Parse(dbfReader.GetField(n, fieldIndex), System.Globalization.CultureInfo.InvariantCulture);
            samples[n] = d;
        }
    }
}

This code creates an array of population distribution value from shapefile.

private static double[] GetQuintiles(double[] samples)
{
    Array.Sort(samples);
    double[] quintiles = new double[4];

    quintiles[0] = samples[10];
    quintiles[1] = samples[30];
    quintiles[2] = samples[50];
    quintiles[3] = samples[60];
    return quintiles;
}

This code creates an array of range value. eg. 10000-20000, 20000-40000, 40000-80000, etc.

Color[] cols = new Color[] { 
        Color.FromArgb(96,169, 108), 
        Color.FromArgb(201, 200, 123), 
        Color.FromArgb(157, 135, 151), 
        Color.FromArgb(112, 155, 235),
        Color.FromArgb(192,94,113)};
}

This code creates an array of colors. There will be 1 more color than the number of elements in quantiles.

After running the code, you will see the below MAP with tooltip population and district name value.

References

Conclusion

Easy GIS is fast and easy to implement for basic mapping functionality. Still I am unable to display the legend.

 

 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here