how to cache a map in a desktop application?

Topics: Data Access
Jul 19, 2010 at 1:55 PM
Edited Jul 19, 2010 at 1:55 PM



I'm currently writing a desktop application, which should display several layers of shapefile, colored by an attribute given from a .dbf.

So far, so good. I'm able to color the lines, I can display several layers at once, but the performance is pretty bad.

So I wanted to cache the map, and because this is my first project with SharpMap I have no clue how to solve this.

I'm searching now for several days for an explanation, but either I'm unable to find something or I found something but didn't realise that this might be my solution (I guess, this one).

Now I'm asking you, if you could show me sample code of how to cache a map in a desktop application, please. This would be really helpful!



Jul 19, 2010 at 4:18 PM
Edited Jul 19, 2010 at 4:19 PM

it's me again.


Now I tried


System.Drawing.Image mapImg = _Map.GetMap();



imgID = SharpMap.Web.Caching.InsertIntoCache(10, mapImg);
 pbMap.ImageLocation = "getmap.aspx?ID=" + HttpUtility.UrlEncode(imgID);

but I receive a NullReferenceException at SharpMap.Web.Caching....

Then I tried
Cache cache = new Cache();
Cache.Insert as listed in all the examples in the internet doesn't work, so I tried it this way, but the result is another
NullReferenceException at the Insert line.

Is the code wrong? (I guess so) Do you know, why I am getting such an exception? I already tested it mapImg and
imgID are initialised before that so it has to be something with the cache...

Thank you!




Jul 19, 2010 at 6:56 PM

Hello apr_shlf,

are you doing a desktop application or a web application? If you are doing the first, Web.Caching is not the way to go.

You say it's slow: Could you give some statistics about your data?

Maybe using a database driven provider (SqlServer2008, PostGis, SpatiaLite) would suit your needs better, since you could limit your dataset with a definition query string.

Hth FObermaier


Jul 19, 2010 at 8:30 PM

Hello FObermaier,


thank you for your quick reply.


I'm doing a desktop application, but I was desperate, so I thought, maybe Web.Caching works...


At the moment I'm trying to represent a shapefile  with about 60,000 features (?), it's a street map. It takes about 30 seconds to load (I have to compare the data from the shapefile with a .dbf (in this case ~70,000 lines)). There's also the possibility to show several layers at once. The only time, the information changes is, when you add or remove a layer.

I'm using shapefiles, and I can't use any other provider.


I hope this could help you a bit.



Jul 19, 2010 at 9:36 PM

If you are stuck with shapefiles, try to disaggregate the shapefiles in parts like e.g.

  • MotorWays,
  • ...,
  • rural roads,
  • ...,
  • footpaths.

That way you might not have to use a custom theme, which slows things down quite a bit, and you can turn layers visibilty on and off with MinZoom and MaxZoom property.

Hth FObermaier


Jul 20, 2010 at 6:09 AM
Edited Jul 20, 2010 at 6:11 AM

I understand what you mean, but how can I implement it?


and, if I make it like you said, I don't have to do anything with cache?



Jul 20, 2010 at 7:35 AM

Hello apr_shfl,

You can disaggregate your shapefiles using QGIS along with ftools plugin.

As far as I know there is no caching for SharpMap v0.9 Desktop. The image of the map is doublebuffered in the control. The Web.Caching operates similar, it stores images along with the viewport in order not to redraw the map if it gets requeried.

You might get -I have not tested that- better performance if you load the contents of your shapefiles in FeatureDataTables and use GeometryFeatureProvider as a datasource.

Hth FObermaier



Jul 20, 2010 at 8:30 AM

Ok, thank you.


I'll try this now.




Jul 21, 2010 at 9:16 AM

Another thing you might want to consider is the number of columns in the dbf file accompanied with your shapefile. How many are there and are they all necessary.



Jul 22, 2010 at 9:16 AM

You said that there is no caching for SharpMap v0.9 Desktop. Is there caching for another version? And what do I have to consider if I want to switch to this version?