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. subami chan says:

    Hi dear,

    What is the c# code to add pan tool?
    If someone knows Pl. let me know

    Chan

  2. Scott says:

    Did anyone every find out anything about the error: “The type initializer for ‘mapscriptPINVOKE’ threw an exception”? I am also using Visual Studio 2005 with Framework 2.0

  3. Adrian says:

    Hi all

    I have a problem to connect from C# to oracle spatial. When I try to draw the map I get trhis error:msDrawMap(): Image handling error. Failed to draw layer named ‘Hidro’.;msOracleSpatialLayerOpen(): OracleSpatial error. OracleSpatial is not supported.
    The section from map file for layer Hidro is:
    LAYER
    NAME Hidro
    STATUS ON
    TYPE POLYGON
    CONNECTIONTYPE oraclespatial
    CONNECTION “mapgdb/pass@orcl”
    DATA “GEOM FROM Hidro_1″
    CLASS
    STYLE
    OUTLINECOLOR 0 0 0
    COLOR 0 128 128
    END
    END
    END
    I am also using Visual Studio 2005 with Framework 2.0.
    I read from tutorial theat mapserver work nativly with orale spatial but in my case I aderstend from error theat oracle spatial is not suported, can you help me with this problem ? please

  4. Adrian says:

    My email :adrianfrumu@yahoo.com

  5. Paolo Corti says:

    Adrian
    AFAIK MapServer reads Oracle Spatial.
    You may try to post your issue at the MapServer group:
    http://www.nabble.com/MapServer-f1214.html

  6. Adrian says:

    Hy Paolo,

    Thenks for the link.

    When I try to use your example for conecting to oracle spatial I get theat error.

    I see theat there in the bin directory is a dll named ogr_oci.dll can you tell me how I can used ?

    Best regardes

  7. Nik Adib says:

    I allready set all the dlls into bin folder. Even web.config. But i have error “The type or namespace name ‘mapObj’ could be found.

    I have put all dll into bin folder. But why this could happend? Please help me..

  8. Venkat says:

    Dear All,

    Can Any one Help me about Legend using HTML template.

    please help me…

    thanks

  9. Nuno Sénica says:

    Dear all,

    I have a problem with “zoomRectangle” mapscript function! :S

    The error I get is: “mapscript::mapObj::zoomRectangle(): General error message. Georeferenced coordinates miny >= maxy”

    Doe anyone has a working example of selecting a polygon, highlight it and zoom to it ???

    This is really urgent! Help please! :S

    Best regards,
    Nuno Sénica.

  10. Paolo Corti says:

    Hi Nuno
    if i remember properly, at MapServer < 4.10 mapscript swig, miny and maxy in the zoomRectangel method where exchanded. Try to change your code, and you will see it will work. Let me know

  11. Nuno Sénica says:

    Hi Great Paolo,

    I decided to change my code like this:

    map.querymap.status = mapscript.MS_ON;
    map.querymap.color.setRGB(255, 255, 255);
    map.querymap.style = (int)MS_QUERYMAP_STYLES.MS_HILITE;
    map.setExtent(shape.bounds.minx, shape.bounds.miny, shape.bounds.maxx, shape.bounds.maxy);

    and the selection and zoom works fine.

    I just doesn’t get hiligted! Do you know why??

    Another thing, I want to add a reference map, so I have prepared my mapfile with it.
    But how do I make it visible in my Mapscript App???

    Best Regards Paolo ; )

  12. yatendra says:

    I am getting this message
    pls help me out.

    msDrawMap(): Image handling error. Failed to draw layer named ‘TILE’.;loadCustomLayerDLL: General error message. Failed to load dynamic Layer LIB:
    any suggetions urgent.
    Thanks regards

  13. yatendra says:

    HI to all

  14. yatendra says:

    Hi to all,
    I am trying to connect mapserver with SDE.
    I have successfully connected in html(Map file working fine).
    Now i am trying this asp.net connetion with same map file.
    i followed all instruction and source codes.
    application is giving error while running. and error is as follows in MapStream.aspx.cs
    msDrawMap(): Image handling error. Failed to draw layer named ‘TILE’.;loadCustomLayerDLL: General error message. Failed to load dynamic Layer LIB:

    PLease help me out to solve this problem.
    Why i am getting this problem.
    Can anyone explain please…
    Thanks regards

  15. yatendra says:

    sorry guys the full message is
    msDrawMap(): Image handling error. Failed to draw layer named ‘TILE’.;loadCustomLayerDLL: General error message. Failed to load dynamic Layer LIB: C:\ms4w\Apache\specialplugins\msplugin_sde_92.dll;msGetSymbol(): General error message. Can’t load requested dynamic library: C:\ms4w\Apache\specialplugins\msplugin_sde_92.dll

    please help me out…..
    i am waiting for reply

  16. Paolo Corti says:

    yatendra, I don’t know what could be the problem, but try to post that on the MapServer group at: http://www.nabble.com/MapServer-f1214.html
    this comments should be related to issues with the tutorial
    ciao

  17. yatendra says:

    Dear Paolo,
    Thank you for information.
    I wanted to know, is it possible to connect SDE and mapserver with asp.net?
    Did you tried that one?
    Is there any tutorial for asp.net with sde?
    Thanks Regards
    YAtendra

  18. Paolo Corti says:

    Yatendra, I am sure this tutorial should work also with ArcSde, but in your shoes I would keep things simple and I would proceed step by step.
    First create a map file connecting to your ArcSde layers and try to see with an html viewer (for example use the one that you can download from the mapserver web site included in the mapserver demo) if your layers are displayed properly.
    After that try to create a command line .Net solution and start playing with some mapscript methods and see if they are working properly.
    Finally start implementing the Asp .Net project.

    Let me know ;-)

  19. yatendra says:

    Dear Paolo,
    I will work on what you suggested me.
    My map file is working properly because in html viewer i can see and browse map.
    I think this means sde connection is successful.
    I will try as you mentioned.

    Now first i am trying to do with PostgreSQL and POSTGIS (as per your tutorial).
    I am getting error in MapStream.aspx.cs as follows
    “Error 3 The type or namespace name ‘mapObj’ could not be found (are you missing a using directive or an assembly reference?)”
    How to remove this error please let me know?
    Thanks Regards

  20. doremon says:

    Dear Paolo
    I have problem with change color (line, point, polygon) with mapserver
    i am using mapserver 4.10 and asp.net
    I am developing function identify. And now i want change color my result with object Line or polygon.
    How can i do that?

  21. jani nilay says:

    for editing map file from client side required some FTP control pls mention that control
    and inform that some control which use for editing ,retriving and maniplating the map server file
    pls send reply
    thnx