MapServer Tutorial for C# mapscript (ASP .NET)

Posted by on September 20, 2006

This tutorial will try to guide you step by step in implementing a GIS (Geographic Information Systems) web solution, based on Open Source software (MapServer), within the .NET Framework.

The tutorial is designed to work with Visual Studio 2003 and .NET 1.1, but you will easily be able to perform all the necessary steps to complete it using another version of Visual Studio (ie: Visual Studio 2005) or other IDEs (like Visual Web Developer 2005 Express Edition or even the notepad). It will work also using .NET 2 as c# mapscript is .NET 2 compatible.

The tutorial is focused to people with knowledge of C# (but can be easily implemented with .NET Visual Basic) and the .NET framework, and with some background in GIS. If you are new to GIS, a good place to start is here.

This tutorial could be easily adapted for other languages and frameworks rather than C# (or VB) and Microsoft .NET Framewok. In fact the Open Source component we are going to use (MapServer) is also available for PHP, Java and Python, and other languages.

The whole tutorial is downloadable at this address, where you can find the data (shapefiles) necessary to complete the tutorial (but you can easily adapt your data), and a complete working Visual Studio 2003 solution with this tutorial’s code.

You can take a look at a working online demo of this tutorial here.

As we will implement a C# ASP .NET Application, take care about this issue (that should be solved in the future): MapServer thread safety and about plans to solve this issue.

Index of Tutorial
Introduction
1. Introduction to MapServer Web GIS development environment
2. Installing MapServer
3. Creating the MapFile and data configuration
4. Designing the tutorial user interface
5. Implementing the C# mapscript code
6. Migrating shapefiles to PostGIS
7. Connection MapFile layer to PostGIS
8. Adapting C# code to work indifferently with shapefile or PostGIS layers

And if the tutorial is not working for you (you get compilation errors, maps are not displayed, editing is not working…), please take a look here

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • Furl
  • LinkedIn
  • Reddit
  • StumbleUpon
71 Comments on MapServer Tutorial for C# mapscript (ASP .NET)

Closed

  1. Charles Rabello de A says:

    This article was very usefull to me.
    I will keep this site in my favorites.
    Tanks.

    Charles Rabello de Almeida

  2. Anesh says:

    Hi

    I can’t get a map a render even though app compiles and runs.

    thanks
    Anesh

  3. Paolo Corti says:

    Hello Anesh
    it is very possible that you have some troubles in configuring your mapfile and data, so check them
    Otherwise should work, as I had until now only positive feedback from people using this tutorial

  4. Anesh says:

    Hi Paolo

    It works brilliantly. Thanks.

    -A

  5. Le Duc Thanh says:

    Hi
    I haved download your TotorialMapserver and do all steps you guide in http://www.paolocorti.net/public/dokuwiki/doku.php?id=mapserver:tutorial.

    But when i run by Visual Studio 2003 and .NET 1.1 it return Err “Unable to load DLL (mapscript)”
    Could you help me pls ?

  6. Paolo Corti says:

    Hello Thanh
    Unable to load DLL is an error that you could get if mapserver dll references cannot be found. As is written here:
    http://www.paolocorti.net/public/dokuwiki/doku.php?id=mapserver:tutorial:installing_map_server

    the very important thing to do is one of the followings:
    1) add all the mapserver dlls (found in C:\ms4w\Apache\cgi-bin\mapscript\csharp and C:\ms4w\Apache\cgi-bin) inside the bin folder of your asp .net project
    2) put the path to all these dll under PATH Windows environment variable (PATH=C:\ms4w\Apache\cgi-bin\mapscript\csharp;C:\ms4w\Apache\cgi-bin)

    Obviously you have to change the path according to your mapserver installation

    good luck!
    Paolo

  7. Le Duc Thanh says:

    When i add references mapserver dlls in (C:\ms4w\Apache\cgi-bin\mapscript\csharp and C:\ms4w\Apache\cgi-bin)

    It warning: like this
    “A reference to to ‘C:\ms4w\Apache\cgi-bin\mapscript\csharp\mapscript.dll’ cold not be add. This is not a valid assembly or Com component. ”

    if i ignore and close warning and run TotorialMapserver
    It show Err:
    “Unable to find an entry point named SWIGRegisterByteArrayCallback_mapscript in DLL mapscript”

    Could you show me what mistakes I made and what steps I missed?

  8. Paolo Corti says:

    Thanh
    download the vs2003 project and look at it.
    You need to add references only for mapscript_csharp.dll and Npgsql.dll.
    For all the other dlls (in (C:\ms4w\Apache\cgi-bin\mapscript\csharp and C:\ms4w\Apache\cgi-bin) you may set the PATH in environment variables or just copy all of them under the bin folder created from vs when you compile.

  9. Le Duc Thanh says:

    Hi Paolo

    It works very well

    thanks you very much!

  10. Le Viet Thanh says:

    @ Le Duc Thanh:Hello bro, i know u r Vietnamese.I’m newbie at MapServer and C#. I want to researching about it more.Could u help me.
    Thanks bro:)

  11. Le Duc Thanh says:

    Yes, We are VNmese
    What would can i help you?

  12. MeoMeo says:

    hi Le Duc Thanh your example very good.
    can u show me how do i want add polyline, point, polygon to personal geodatabase . this is my geodatabase
    http://www.esnips.com/nsdoc/b185576c-f9ef-49ba-8fb0-3246ffbba2db
    can u send me Example
    my email: tin.buianh@gmail.com
    thanks

  13. David says:

    I’m having the same problem as Anesh. Everyting compiles but the webpage does not show a map. It seems to fail in the RefreshMap sub when calling imageObj image1 = map.draw(). Any help would be great!

  14. Le Duc Thanh says:

    You shoul be check your mapfile.
    Check the path of mapfile in webconfig.
    Check font name (if you use sample of Paolo)

  15. Le Duc Thanh says:

    You should check your path of mapfile.
    You check the font name in your computer (if you use the sample in this blog)

  16. Tommy says:

    Hi Paolo , your example works well. I will be very thankful if you show how I can draw a reference map and to do some extra things like panning with C# Mapscript. All help will be gratefully accepted.

  17. Manasi says:

    I want to implement search functionlity using mapserver where user enters name of some place and it is located on map.. could someone guide me about the right function to use for this?

  18. subami chan says:

    Dear paolo
    I haved download your TotorialMapserver and did all steps as your guide in , But I have two problems
    1.Default.aspx page is loaded withot imag (map)
    2.When I click Refresh map Button, Following err is appearing.
    Object reference not set to an instance of an object

    Line 249: for(int i=0; i

  19. amay says:

    hi paolo,
    our project is working well, now we want to put google map images as a background for our map. is it possible in mapserver? if yes, please show me how i can do that.

  20. Hi, Your tutorial was extremely helpful, but for some odd reason my point layer does not show up at specific zoom levels. I have tried to change the scale but no use, any ideas?

    Thanks in advance.

    Regards,
    Jandost

  21. Adrian says:

    I did exactly as you said abouve but I am still get the following exception. Please help

    Source Error:

    Line 48: // string mapFilePath = System.Configuration.ConfigurationSettings.AppSettings["mapFilePath"].ToString();
    Line 49: // Page.Response.Write (”filePath” + mapFilePath);
    Line 50: mapObj map = new mapObj(System.Configuration.ConfigurationSettings.AppSettings["mapFilePath"].ToString());
    Line 51: //iterate the map layer to populate ddlLayer and cblLayer
    Line 52: for(int i=0;i

  22. subami chan says:

    To Adrian,
    I think ,this problems come from your map file.
    Map file is the heart of the map server.
    You shoul be check your mapfile and do following
    Check the path of mapfile in webconfig.
    check the shapepath in the map file
    Check font name

    Su

  23. Paolo Corti says:

    It is exactly as chan is saying.
    99.99% of people having problems to make the application working is because they have problems with mapfile.
    Jandost, I can’t figure it out what could be the issue with your layer, you may try to post a message at the MapServer group.
    Amany, AFAIK you cannot mashup google maps with mapserver data, but again, try to post a message at the MapServer group just to make it for sure.

  24. Minh says:

    I checked Map file and web.config, all is working properly.
    but
    Source Error:
    mapObj map = new mapObj(System.Configuration.ConfigurationSettings.AppSettings["mapFilePath"].ToString());

    Please help

  25. subami chan says:

    TO Minh,
    You can not say exactly that your map file is working properly.Here I think you cause problems with your Map file.Pl, Patietly cheak your map file again, I suppose,Somewhere of the map file there is a some mistake,likely syntax,

    Su

  26. Minh says:

    I run by Visual Studio 2005 and IIS 5.1
    I add references only for mapscript_csharp.dll and Npgsql.dll and i copy all the other dlls under the bin folder created from vs

    When i run by IIS 5.1 it return Err page:
    //////////////////////////////////////////////////////////////////////////////”Server Error in ‘/Why’ Application.
    —————————————————————————–
    Unable to load DLL ‘mapscript’: The specified module could not be found. (Exception from HRESULT: 0×8007007E)
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
    Exception Details: System.DllNotFoundException: Unable to load DLL ‘mapscript’: The specified module could not be found. (Exception from HRESULT: 0×8007007E)
    Source Error:
    Line 36: ibMap.ImageUrl = “MapStream.aspx?ACTION=INITMAP”;
    Line 37: //initialize controls
    Line 38: mapObj map = new mapObj(System.Configuration.ConfigurationSettings.AppSettings["mapFilePath"].ToString());
    Line 39: //iterate the map layer to populate ddlLayer and cblLayer
    Line 40: for(int i=0;i

  27. Minh says:

    Hi
    I think my map file is working properly,
    because
    I run by Visual Studio 2005 and IIS 5.1
    I add references only for mapscript_csharp.dll and Npgsql.dll and i copy all the other dlls under the bin folder created from vs
    When i run by IIS 5.1 it return Err :
    “Unable to load DLL ‘mapscript’”
    and When i run by Visual Studio 2005 it return Err :
    mapObj map = new mapObj(System.Configuration.ConfigurationSettings.AppSettings

    why?
    Could you show me what mistakes I made and what steps I missed?

    Minh

  28. subami chan says:

    To Minh,
    Did you copy mapsricript.dll (Available at C:\ms4w\Apache\cgi-bin\mapscript\csharp folder) too to the Bin created by VS ?

    Su

  29. Minh says:

    Hi
    I tried to check missing dependencies, I tried to set the PATH env, I tried to copy dlls under system32, and I tried to copy mapscript.dll (Available at C:\ms4w\Apache\cgi-bin\mapscript\csharp folder), all without success.

    I try recompile by mapserver-4.8.3-2-msvc71-buildk-kit.zip from http://hobu.stat.iastate.edu/mapserver/build_output/, but I don’t look for.

    can you help me?

    Minh

  30. subami chan says:

    To Minh,
    Please check your web.config file. I think path might be wrong for map file, you have given in the web.config.

    Check path name very carefully.

    Cheers,
    Su

  31. Minh says:

    Hi subami chan

    I checked path name very carefully.
    I am trying to recompile Mapserver-4.8.2, but I don’t have file gd.h in Mapserver-4.8.2. Do you have file gd.h? can you send to me?

    Minh

  32. Tommy says:

    Hi Minh, you should not to recompile the MapServer because the precomipled version works fine. Check all recommendations written by others .

  33. subami chan says:

    TO Minh,
    You Don`t need to recompile Map server unless you use binaries.I thik you are using alredy recomiled files contain in ms4w folder.
    All C# programme work realy well.

    Still You are struggling with the map file.If Can show your map file,I will try to detect the problems?
    Su

  34. bert says:

    Hi all,
    I use MapServer under IIS, which is behind a proxy. I want to call some maps from a wms; how is this possible? I edited the web.config, but the libcurl produces error 7 - Failed connect .
    Has anybody some ideas to solve this prob?
    Thanks in advance
    bert

  35. Minh says:

    Hi all
    I repaired error and it works very well. I am very happy and I can start.
    Thank you very much!!

  36. wenwen says:

    Dear subami chan,
    how did you solve prolem
    - Default.aspx dislay without map
    - click refresh button then appear error,
    thank for your help!

  37. subami chan says:

    To Wenwen,

    Your Map file has errs, Check it patiently.

  38. michal says:

    I managed to create website with the use of given tutorial. I did it in .Net 2.0.
    Now I am trying to build simple windows C# application to generete image based on MapFile.
    I am using exactly same procedure, data and mapfile itselt as in tutorial, however I encounter some problems.
    When I want to create mapObj from MapFile I receive the following error “The type initializer for ‘mapscriptPINVOKE’ threw an exception”.
    Does anyone nows what might be the possible error?
    Greets,
    Michal

  39. lr2651 says:

    msGetLabelSize(): TrueType Font error. Could not find/open font

    I’m sure the font file right,but still get this msg, help me please!

  40. asoujon says:

    Reply to Ir2651 May 4, 2007:
    I encountered the same problem. Check your fonts folder for the verdana.ttf font file (not verdanab.ttf etc.). Mine was missing. Copy it from the Windows\fonts folder (or change the entries in the mapfile(s) to another font, for example arial). Copy via cmd or TotalCommander etc., explorer will not let you copy fonts.
    Hope that helps,
    asoujon

  41. Pham says:

    Hi
    the source works very well.
    But I want to draw point, line, polygon by mapserver. And I want to use font ‘unicode’ in postgis.
    I tried and not success.
    Can you help me?
    Thank!
    Pham

  42. Pham says:

    Hi
    private void ibMap_Click(object sender, System.Web.UI.ImageClickEventArgs e)
    {
    switch(rblGisTools.SelectedItem.Text.ToUpper())
    {
    ..
    case “DRAW”:
    layerObj layer = map.getLayerByName(”POI”);
    imageObj img = map.draw();
    pointObj pt = pixel2point(new pointObj(e.X, e.Y, 0, 0));
    lineObj ln = new lineObj();
    ln.add(pt);
    shapeObj sh = new shapeObj((int)MS_SHAPE_TYPE.MS_SHAPE_POINT );
    sh.add(ln);
    if (sh.draw(map, layer, img) == (int)MS_RETURN_VALUE.MS_FAILURE )
    {
    Response.Write(”error”);
    }
    break;
    ..
    }
    }

    I want to draw point on the map where I clicked on ibMap(draw point!=Add point).I added this code on this tourist but the point doesn’t appearing on the map.
    What am I do wrong?
    Please help me.
    Pham

  43. sonj says:

    Hi

    I have followed the steps as per article but when I run the code it thorwn error before creating the mapObj that error type is ‘ An unhandled exception of tye ‘System.DllNotFoundExecptoin’ occurred in mapscript_csharp.dll ‘. And I have used map file which is provided in the sample.

    I’m using ms4w_2.2,IIS5.1,.Net1.1 Framework,VS2003

    Please suggest me where am I wrong ?

    sonj

  44. ucin says:

    Hi,
    I very good work..
    I have some question:
    1. how can we integrated with SQL Server..
    If we have longitude and latitude point in SQL Server..
    2. how to make pan control integrate with it..?
    3. how to integrate with ajax..?

    Thanks..

  45. Paolo Corti says:

    Ucin,
    use inline features to display the data from SQL Server, and still inline features if you want to add point to the map. When you add point to the map you have also to insert a new record in your SLQ table passing the point coordinates.

  46. ucin says:

    Hai Paolo Corti
    What about the pan tool ..?
    I thinking to make a module for integrate with DotNetNuke..
    So I have to work for integrate it ajax..
    Thanks
    Ucin Shihab

  47. ucin says:

    Hai again..
    I had integrate it with Ajax and also for the pan tool.
    But I still confuse with Longitude and Latitude from SQL Server to bind to the layer. And add a point with info with it..

    Can anyone help me please..
    I need it so much..
    Thanks.

  48. sonj says:

    hello all

    Still I have not received any help from forum….Although I played a trick to resolve ”System.DllNotFoundExecptoin’ I copied all dll’s into my Application directory then its start working on ASP.Net. Apart from this trick is there any other ways???

    thanks for all

  49. ucin says:

    Hi all..
    I had used C# MapServer and I made a lot of work.. Working with ajax.. connected to SQL Server to render spatial data… but when I tested the component is not stable .. it will crash and destroy your session after user zoom or pan over and over..
    Better use the CGI engine
    Any advise

  50. Le Van Thang says:

    Hi Le Duc Thanh! I’m student, Your example very good. I need your example, can you send me your example? Thanks you very much! My email : levanthang_hn@yahoo.com