What is SharpMap
SharpMap is an opensource freely available mapping engine for the Microsoft.NET 2.0 Framework. It supports numerous datasources such as PostGIS, ESRI Shapefile, MSSQL Spatial, ECW and Oracle spatial. Future support is in the works for MapInfo Tab files and other datasources.
Unfortunately as of yet it has not been thoroughly tested in Mono.NET and the SharpMap.UI (desktop portion) is noted to not work in Mono.Net
- see notes for further details http://www.codeplex.com/Wiki/View.aspx?ProjectName=SharpMap&title=Can%20I%20use%20SharpMap%20on%20the%20Mono%20framework%20instead. What is especially interesting about SharpMap is that it is a relatively pure .NET implementation and can be used as both a Web mapping as well as a desktop toolkit.
If you are a .NET enthusiast as we are or just want to program in something like VB.NET, this is definitely something to take a look at.
In this exercise we will do what I call synchronized maps using PostGIS spatial database engine, SharpMap.Net mapping engine, and data from Boston Private Abandoned Property surveys. Synchronized Maps are maps laid out side by side that show the same location and are always in synch with each other. A common example of this is a keymap verses a full blown map where clicking in the key map zooms the main map and clicking on the main map zooms the key map.
In our example we will be using maps that are all the same size, but represent different periods in time.
I am going to use Visual Basic .NET for this example for a couple of reasons.
- People rarely show VB.NET examples in .NET and focus on C#. This leaves VB.NET and general VBA developers like me feeling "What about me?"
- C# doesn't have a With clause to my knowledge. How can people live without the convenience of a With clause :).
- VB.NET reads more like a spoken language so I think easier to follow and debug.
- VB.NET does a lot of transparent type-casting for you which C# doesn't. For example if you tried to glue together an int and a string, c# will yell (can't convert .. to ..) forcing you to write a mess of type-casting code
where as VB.NET will observe that the integer can be converted to a string and do the casting automagically.
- Webserver or workstation with ASP.NET 2.0 installed
- Microsoft.Net 2.0 Framework installed on workstation you will be using to compile
- PostGreSQL 8.0 or above server with PostGIS 1.0 or above installed
Compiling the SharpMap Source
The default binaries of SharpMap available do not come precompiled with PostGIS support, so in this section, we'll go over how to compile your own
Before you can start, you'll need to gather the following items
- SharpMap.NET source - download the latest version from here, give it a .zip extension if it doesn't default to that when downloading, and unzip it unto your C drive. It should create a SharpMap folder.
- Copy the PostGIS.cs file from the extracted folder SharpMap/SharpMap.Extensions/Data/Providers to the folder SharpMap/SharpMap/Data/Providers.
- Download the latest PostgreSQL.NET driver (npgsql) binary for MS 2.0 from http://pgfoundry.org/projects/npgsql. For this I used Npgsql 1.0-bin-ms2.0.zip.
At this point, you can either use a development tool such as Visual Studio 2005,
the freely available SharpDevelop ( http://www.sharpdevelop.com/OpenSource/SD/Default.aspx ) or Visual C# Express ( http://msdn.microsoft.com/vstudio/express/visualcsharp/ ) (a free download) and the included solution files to compile the source,
or you can just compile with a command line using the .NET framework that you have already installed.
I'm just going to describe how to do it with a commandline command since that doesn't assume any additional prerequisites and is simpler to explain.
- Create a bin folder in your extracted SharpMap/SharpMap folder and copy the Npgsql.* files and Mono.Security.dll from npgsql zip files into this new folder
- Creat a batch file in the bin folder with the following lines in it
"%SystemRoot%\microsoft.NET\Framework\v2.0.50727\csc.exe" /t:library /debug:full /out:%croot%bin\SharpMap.dll /recurse:%croot%*.cs /r:System.web.dll /r:System.data.dll /r:System.Xml.dll /r:system.dll /r:%croot%bin\Npgsql.dll
Then run the batch script.
Running the batch should create 2 files a .dll and a .pdb file. The pdb is used for debugging so it can highlight lines that break in the internal library.
Now create an application folder on your webserver complete with a bin folder. Drop the SharpMap.dll, SharpMap.dbg, npgsql.dll, Mono.Security.dll files into the bin folder.
Loading the Test Data
I have packaged in the download file for this exercise, 2 sql scripts one to load neighborhoods, one for abandoned survey data. Below is the batch script to load all the data. For details on how to create your own load files, read Part 1: Getting Started With PostGIS: An almost Idiot's Guide or OGR2OGR Cheatsheet
c:\pgutils\psql -d gisdb -h localhost -U postgres -f neighborhoods.sql
c:\pgutils\psql -d gisdb -h localhost -U postgres -f abansurveys.sql
Post Comments About Part 1 - PostGIS and SharpMap in ASP.NET 2.0 using VB.NET: Compiling SharpMap with PostGIS