SharpMap v2.0 and SQL 2008 samples?

Topics: Data Access, SharpMap Project, SharpMap v2.0
Sep 9, 2009 at 6:47 PM

Hi All - 

 

I really like what I see so far with the sharpmap library as far as functionality but Im at a complete loss as to how to implement this with SQL Server 2008.  I know, from reading posts here, that it is possible and supported in v.2.0 and most posts relate to the asp.net demo included with the source but, again, Im having issues running this as well - can anyone suggest other samples or provide snippets on how to get this running?  

 

 

thanks!

 

Kevin

Coordinator
Sep 9, 2009 at 7:44 PM

hi Kevin, Have a look at the MapViewer demo in the src tree it supports a few different datasources including sql 2008 hth jd

Sep 9, 2009 at 7:57 PM

Hi John - thanks for the response!  

 

When you say MapViewer are you refering to the Demo Projects -- Web -- SharpMap.Demo.AspNet solution?  I downloaded the source from googlecode and saw that demo but the provider it uses is the MSSpatial one - not the SQL 2008... or am I missing it?  Im thinking the MapViewer demo is something maybe?

 

thanks!

 

Kevin

Coordinator
Sep 9, 2009 at 8:25 PM

Hi Kevin, It is called MapView in the solution but is in a directory called SharpMapWinFormsDemo  http://code.google.com/p/sharpmapv2/source/browse/#svn/trunk/Demos/SharpMapWinFormsDemo hth jd

Sep 10, 2009 at 5:12 PM

 

Hi John - 

First. I wanted to say that I think this Lib is great and I appreciate the work you do to continue it forward... hopefully at some point I can become familiar enough with it to provide support is this forum too!

Unfortunitely, that day is not now and I'm pretty stuck - hopefully you might be able to help me out a little more here.  I have looked at the samples and there is a ton of functionailty there (over 50 cs projects) and Im getting a bit overwhelmed...  I have a sample written by Morten a couple of years back using version 0.9 to generate maps from shapefiles - all Im trying to do is do the very same thing but connect to sql 2008 intstead.  I dont need to do any transformation as the coordinates stored are also that which is displayed...  Again, just need to connect to sql 2008 - grab a layer and generate an image... code is pretty straight forward in 0.9 so I have been using that as a background and trying to upconvert it to version 2.0 which has the sql providers... However, things like not having the GetMap() function in v.2 and stuff are really, really tripping me up.  Are there docs that accompany this project - I looked but only saw the source code viewer?  Something like this:

 

			//Generate map
			System.Drawing.Bitmap img = (System.Drawing.Bitmap)myMap.GetMap();
			//Stream the image to the client
			context.Request.ContentType = "image/png";
			System.IO.MemoryStream MS = new System.IO.MemoryStream();
			img.Save(MS, System.Drawing.Imaging.ImageFormat.Png);
			// tidy up  
			img.Dispose();
			byte[] buffer = MS.ToArray();
			context.Response.OutputStream.Write(buffer, 0, buffer.Length);

which works in 0.9 doesn't in version 2.0 since sharpMap.GetMap() is no longer there and I have no idea where to look for it...   do you know where I can ref to crosswalk these diffferences?  Or - besides the MapServer Sample do you have any short snippets that would show/example out this functionality?

thanks again John,

Kevin

 

 

 

//Generate map
System.Drawing.Bitmap img = (System.Drawing.Bitmap)myMap.
//Stream the image to the client
context.Request.ContentType = "image/png";
System.IO.MemoryStream MS = new System.IO.MemoryStream();
img.Save(MS, System.Drawing.Imaging.ImageFormat.Png);
// tidy up  
img.Dispose();
byte[] buffer = MS.ToArray();
context.Response.OutputStream.Write(buffer, 0, buffer.Length);

 

 

Coordinator
Sep 11, 2009 at 8:50 AM

Hello Kevin,

if you want the Bitmap of what was rendered to the MapViewControl, two ways come to my mind

- Add something like this to MapViewControl:

        public Bitmap MapImage
        {
            get { return _bufferedMapImage; }
        }

- Use Control.DrawToBitmap(...) function.

There is work in progress on a renderer/presenter using a .net port of AGG which might produce Bitmaps as output.

If your problem is that you don't see anything, you'll probably have to call ZoomToExtents() or ZoomToViewBounds(..), after you have added at least one layer to the map.

Hth

FObermaier

 

Coordinator
Sep 11, 2009 at 1:32 PM

Hi Kevin, there isn't really an equivalent to GetMap in the current project - though it seems like a big omission.

As a quick fix you could do something along the lines of what FObermaier suggested - though you may need to lock the bitmap while reading and writing and also return a clone rather than the original bitmap.
The DrawToBitmap method should be fine.

We are trying to refactor the v2 tree to be more useful in a whole range of rendering scenarios both interactive and not, so we should be able to get equivalent functionality to GetMap in the long run.

As a rule all the providers work in the same way - some expose the Oid type as a generic type param and the MsSqlServer2008 provider is one such;
so if your database table has an oid(primary key) of type bigint you would use MsSqlServer2008Provider<Int64>. The constructor arguments are fairly self explanatory and you can get an instance of an IGeometryFactory from the GeometryServices utility.
The DefaultProviderProperties property is a hack to enable you to force the usage of particular db indexes as the query planner can get things terribly wrong at times

The provider will work much quicker if the database has a few things such as a Geometry_Columns table, envelope columns and spatial indexes on the spatial table.
There are static utility methods on the MsSqlServer2008Provider class to aid in the creation of these but far easier is to use the FormatConverter project to load the data in the first place.
(make sure the project has been built a couple of times as various dlls need to be copied around as part of the build process)

The 0.9 code you posted above looks like it came from a web type project; the equivalent in v2 is to subclass SharpMap.Presentation.AspNet.Handlers.[Async]MapHandlerBase or SharpMap.Presentation.AspNet.WmsServer.Async.WmsHandlerBase
see SharpMap.Presentation.AspNet.Demo.NoCache.DemoMapHandler as an example of what needs to be overriden for a non-wms handler. A combination of IMapRequestConfig and the associated IMapRequestConfigFactory are used to set things
like the output image size and map extents from parameters in the querystring/ database or wherever.

 

Unfortunately with all things web, you run the risk of being severely bitten by the GDI renderer which breaks under any sort of load, hence the intended change to agg. There are also a couple of fixes in the desktop which haven't made it into the web yet (todo with view matrix transformations).

Got to go and get back to a 4 day old baby ;) but I will try and help where I can cheers jd

Sep 11, 2009 at 3:13 PM

4 day old?!?!?!  your wife must be pretty understanding with you answering blog posts!  <but tell her I REALLY APPRECIATE IT>  :)

 

I think Im in over my head here with this lib as it may be too much (and not enough) for what I need in a couple of areas - what I really need  (I suppose when it comes down to it) is to be able to use the 0.9 library with sql 2008.  If I could simply swap out the shapefile provider and substitute in the sql2008 provider this would be what i need since the simple rendering is all I need (this is for a tile overlay on Bing maps.  Do you have any suggestions on how to use the 0.9 drop with sql? 

 

thanks and CONGRATS!  (I have a 3 & 1 year old myself - kids are great!)

 

thanks,

 

Kevin

Coordinator
Sep 11, 2009 at 4:28 PM

Bill Dollins provided a provider for SQL Server 2008 back when it was a CTE. It might work for you anyway:

http://geobabble.wordpress.com/2007/11/28/sql-server-2008-sharpmap-data-provider-code/

hth

FObermaier

Coordinator
Sep 13, 2009 at 12:43 PM

This provider is now part of the trunk source - It has been renamed to SqlServer2008 cheers jd