Sep
20
MapServer Tutorial for C# mapscript (ASP .NET)
Filed on September 20th, 2006 at 5:34 pm under .NET, devs, GIS, MapServer, PostGIS, Tutorials
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
Comments
71 Responses to “MapServer Tutorial for C# mapscript (ASP .NET)”









This article was very usefull to me.
I will keep this site in my favorites.
Tanks.
Charles Rabello de Almeida
Hi
I can't get a map a render even though app compiles and runs.
thanks
Anesh
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
Hi Paolo
It works brilliantly. Thanks.
-A
Hi
I haved download your TotorialMapserver and do all steps you guide in http://www.paolocorti.net/publ.....r: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 ?
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/publ.....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
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?
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.
Hi Paolo
It works very well
thanks you very much!
@ 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:)
Yes, We are VNmese
What would can i help you?
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/b1.....46ffbba2db
can u send me Example
my email: tin.buianh@gmail.com
thanks
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!
You shoul be check your mapfile.
Check the path of mapfile in webconfig.
Check font name (if you use sample of Paolo)
You should check your path of mapfile.
You check the font name in your computer (if you use the sample in this blog)
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.
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?
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
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.
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
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
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
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.
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
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
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
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
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
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/m.....ld_output/, but I don't look for.
can you help me?
Minh
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
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
Hi Minh, you should not to recompile the MapServer because the precomipled version works fine. Check all recommendations written by others .
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
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
Hi all
I repaired error and it works very well. I am very happy and I can start.
Thank you very much!!
Dear subami chan,
how did you solve prolem
- Default.aspx dislay without map
- click refresh button then appear error,
thank for your help!
To Wenwen,
Your Map file has errs, Check it patiently.
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
msGetLabelSize(): TrueType Font error. Could not find/open font
I'm sure the font file right,but still get this msg, help me please!
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
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
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
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
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..
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.
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
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.
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
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
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
Hi dear,
What is the c# code to add pan tool?
If someone knows Pl. let me know
Chan
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
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
My email :adrianfrumu@yahoo.com
Adrian
AFAIK MapServer reads Oracle Spatial.
You may try to post your issue at the MapServer group:
http://www.nabble.com/MapServer-f1214.html
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
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..
Dear All,
Can Any one Help me about Legend using HTML template.
please help me…
thanks
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.
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
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 ; )
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
HI to all
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
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
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
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
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
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
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?
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