Hi David, thanks for you speedy response.
Thanks for the tip on calling Open() on my ShapeFileFeatureLayer. That fixed the problem with the excessive File IO.
I'm now working on the second part of my query, that is caching a shapefile in memory to avoid repeated disk access.
I'm having some difficulty in getting your example working with a MemoryStream as opposed to a FileStream.
To setup my layer:
public bool LoadWorldBackground()
{
string backgroundShape = ShapeFileDirectory + @"\Countries02.shp";
if (Directory.Exists( ShapeFileDirectory ))
{
AreaStyle areaStyle = AreaStyles.CreateSimpleAreaStyle( new GeoColor( 255, 0xe2, 0xdb, 0xa4 ));
ShapeFileFeatureLayer layer = new ShapeFileFeatureLayer( backgroundShape);
((ShapeFileFeatureSource)layer.FeatureSource).StreamLoading += new EventHandler<StreamLoadingEventArgs>(LoadAMapFromStreams_StreamLoading);
layer.Name = Constants.DefaultBackgroundLayer;
layer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = areaStyle;
layer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
layer.IsVisible = true;
layer.DrawingQuality = DrawingQuality.HighSpeed;
BackgroundLayers.Add( Constants.DefaultBackgroundLayer, layer );
return true;
}
else
{
Debug.Print( "Failed to find background shape file directory ({0}) or shapefile ({0}) - using default",
ShapeFileDirectory, backgroundShape );
BackgroundLayers.Add( Constants.DefaultBackgroundLayer,
new BackgroundLayer( new GeoSolidBrush( GeoColor.StandardColors.LightSteelBlue ) ) );
}
return false;
}
Callback method:
private void LoadAMapFromStreams_StreamLoading(object sender, StreamLoadingEventArgs e)
{
Console.WriteLine("In Callback");
if (m_WorldMapStream == null)
{
m_Buffer = File.ReadAllBytes(e.AlternateStreamName);
m_WorldMapStream = new MemoryStream(m_Buffer, false);
}
e.AlternateStream = m_WorldMapStream;
}
However when I run this I receive a divide by zero exception:
Unhandled Exception: System.DivideByZeroException: Attempted to divide by zero.
at ThinkGeo.MapSuite.WpfDesktopEdition.Tile.DrawException(GeoCanvas geoCanvas, Exception exception)
at ThinkGeo.MapSuite.WpfDesktopEdition.Tile.Draw(GeoCanvas geoCanvas)
at ThinkGeo.MapSuite.WpfDesktopEdition.LayerOverlay.DrawTileCore(Tile tile, RectangleShape targetExtent)
at ThinkGeo.MapSuite.WpfDesktopEdition.TileOverlay.DrawTile(Tile tile, RectangleShape targetExtent)
at ThinkGeo.MapSuite.WpfDesktopEdition.TileOverlay.vhQ=(RectangleShape vxQ=)
at ThinkGeo.MapSuite.WpfDesktopEdition.TileOverlay.DrawCore(RectangleShape targetExtent, OverlayRefreshType overlayRefreshType)
at ThinkGeo.MapSuite.WpfDesktopEdition.LayerOverlay.DrawCore(RectangleShape targetExtent, OverlayRefreshType refreshType)
at ThinkGeo.MapSuite.WpfDesktopEdition.Overlay.Draw(RectangleShape targetExtent, OverlayRefreshType refreshType)
However, if I replace the e.AlternateStream assignment line in the callback with:
e.AlternateStream = new FileStream(e.AlternateStreamName, e.FileMode, e.FileAccess);
However this obviously defeats the purpose of having a MemoryStream to cache the file. I did also try setting AlternateStream to a newly constructed MemoryStream (from my stored byte buffer cache) however that produced the same exception.
It is strange, both MemoryStream and FileStream are 'Stream's so I would expect the substitution to be invisible.
Any ideas?