# Caching shapefiles into memory

**URL:** <https://community.thinkgeo.com/t/caching-shapefiles-into-memory/3116>\
**Category:** WPF\
**Created:** [December 6, 2011, 2:59am UTC](https://community.thinkgeo.com/t/caching-shapefiles-into-memory/3116 "2011-12-06T02:59:44Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rodney](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/r/cab0a1/32.png) [@Rodney](https://community.thinkgeo.com/u/Rodney)\
**Post date:** [December 6, 2011, 2:59am UTC](https://community.thinkgeo.com/t/caching-shapefiles-into-memory/3116/1 "2011-12-06T02:59:44Z")

</div>

Hi&nbsp;there,

I've&nbsp;been&nbsp;prototyping&nbsp;with&nbsp;MapSuite&nbsp;5.5&nbsp;WPF&nbsp;as&nbsp;a&nbsp;possible&nbsp;replacement&nbsp;for&nbsp;our&nbsp;in-house&nbsp;developed&nbsp;PPI.

I&nbsp;have&nbsp;ported&nbsp;an&nbsp;example&nbsp;of&nbsp;our&nbsp;code&nbsp;to&nbsp;use&nbsp;the&nbsp;MapSuite&nbsp;map.&nbsp;My&nbsp;structure&nbsp;is&nbsp;as&nbsp;follows:

The&nbsp;WpfMap&nbsp;has&nbsp;3&nbsp;Overlays:  
  
-&nbsp;One&nbsp;for&nbsp;the&nbsp;background&nbsp;world&nbsp;map&nbsp;using&nbsp;the&nbsp;countries02.shp&nbsp;file&nbsp;supplied&nbsp;in&nbsp;your&nbsp;examples&nbsp;(for&nbsp;now,&nbsp;ideally&nbsp;it&nbsp;will&nbsp;use&nbsp;ENC&nbsp;charts)&nbsp;  
  
-&nbsp;A&nbsp;second&nbsp;(SimpleMarkerOverlay)&nbsp;for&nbsp;vessel/feature&nbsp;markers  
  
-&nbsp;Final&nbsp;layer&nbsp;for&nbsp;line&nbsp;shapes&nbsp;such&nbsp;as&nbsp;vessel&nbsp;trails&nbsp;(InMemoryFeatureLayer)

Whilst&nbsp;it&nbsp;is&nbsp;performing&nbsp;fairly&nbsp;well,&nbsp;I&nbsp;notice&nbsp;that&nbsp;when&nbsp;zooming&nbsp;/&nbsp;panning&nbsp;around&nbsp;the&nbsp;CPU&nbsp;usage&nbsp;will&nbsp;jump&nbsp;to&nbsp;~40-50%&nbsp;(2&nbsp;of&nbsp;my&nbsp;4&nbsp;cores)&nbsp;and&nbsp;the&nbsp;File&nbsp;IO&nbsp;climbs&nbsp;up&nbsp;to&nbsp;20&nbsp;MB/s.&nbsp;Even&nbsp;whan&nbsp;I&nbsp;am&nbsp;not&nbsp;zooming&nbsp;and&nbsp;simply&nbsp;refreshing&nbsp;layers&nbsp;(on&nbsp;a&nbsp;2s&nbsp;timer&nbsp;if&nbsp;required)&nbsp;it&nbsp;continously&nbsp;spikes&nbsp;at&nbsp;5MB/s&nbsp;with&nbsp;an&nbsp;associated&nbsp;CPU&nbsp;load&nbsp;spike).  
  
  
  
Using&nbsp;Process&nbsp;Monitor&nbsp;(procmon.exe)&nbsp;I&nbsp;can&nbsp;see&nbsp;that&nbsp;is&nbsp;is&nbsp;the&nbsp;application&nbsp;making&nbsp;repeated&nbsp;open/read/close&nbsp;calls&nbsp;to&nbsp;the&nbsp;cuontries02.shp&nbsp;file.&nbsp;As&nbsp;in&nbsp;over&nbsp;1000&nbsp;per&nbsp;second.

Now&nbsp;this&nbsp;feels&nbsp;unneccesary.&nbsp;I&nbsp;searched&nbsp;for&nbsp;a&nbsp;way&nbsp;to&nbsp;cache&nbsp;shape&nbsp;files&nbsp;in&nbsp;memory&nbsp;(seeing&nbsp;as&nbsp;this&nbsp;file&nbsp;is&nbsp;less&nbsp;than&nbsp;4MB&nbsp;in&nbsp;size)&nbsp;and&nbsp;came&nbsp;across&nbsp;this:&nbsp;gis.thinkgeo.com/Support/DiscussionForums/tabid/143/aff/22/aft/4844/afv/topic/Default.aspx#techtip

However,&nbsp;It&nbsp;is&nbsp;from&nbsp;2007&nbsp;and&nbsp;I&nbsp;can't&nbsp;seem&nbsp;to&nbsp;find&nbsp;anyway&nbsp;of&nbsp;implementing&nbsp;something&nbsp;similar&nbsp;in&nbsp;the&nbsp;current&nbsp;(5.5)&nbsp;WPF&nbsp;release.  
  
  
  
Any&nbsp;tips?

Cheers  
  
Rodney

---

<div class="post-metadata">

**Author:** ![David](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@David](https://community.thinkgeo.com/u/David)\
**Post date:** [December 6, 2011, 4:20am UTC](https://community.thinkgeo.com/t/caching-shapefiles-into-memory/3116/2 "2011-12-06T04:20:29Z")

</div>

Rodney,  
  
  
  
&nbsp;&nbsp;Let&nbsp;me&nbsp;help&nbsp;you&nbsp;with&nbsp;the&nbsp;two&nbsp;main&nbsp;questions&nbsp;in&nbsp;this&nbsp;post.&nbsp;&nbsp;First&nbsp;the&nbsp;open&nbsp;/&nbsp;close&nbsp;calls&nbsp;were&nbsp;a&nbsp;bug&nbsp;in&nbsp;Map&nbsp;Suite.&nbsp;&nbsp;There&nbsp;was&nbsp;a&nbsp;ticket&nbsp;a&nbsp;few&nbsp;months&nbsp;back&nbsp;where&nbsp;there&nbsp;was&nbsp;a&nbsp;scenario&nbsp;where&nbsp;the&nbsp;user&nbsp;wanted&nbsp;us&nbsp;to&nbsp;maintain&nbsp;the&nbsp;Layer's&nbsp;open&nbsp;status,&nbsp;i.e.&nbsp;if&nbsp;the&nbsp;layer&nbsp;was&nbsp;closed&nbsp;when&nbsp;drawing&nbsp;started&nbsp;then&nbsp;we&nbsp;should&nbsp;close&nbsp;it&nbsp;when&nbsp;we&nbsp;are&nbsp;done&nbsp;drawing.&nbsp;&nbsp;Somehow&nbsp;that&nbsp;made&nbsp;it&nbsp;into&nbsp;the&nbsp;main&nbsp;codebase&nbsp;and&nbsp;it&nbsp;was&nbsp;a&nbsp;big&nbsp;mistake.&nbsp;&nbsp;It&nbsp;causes&nbsp;exactly&nbsp;what&nbsp;you&nbsp;are&nbsp;seeing&nbsp;and&nbsp;is&nbsp;a&nbsp;big&nbsp;performance&nbsp;issue.&nbsp;&nbsp;Our&nbsp;solution&nbsp;is&nbsp;to&nbsp;revert&nbsp;the&nbsp;functionality&nbsp;back&nbsp;to&nbsp;opening&nbsp;layers&nbsp;and&nbsp;leaving&nbsp;them&nbsp;open&nbsp;and&nbsp;then&nbsp;adding&nbsp;a&nbsp;new&nbsp;property&nbsp;to&nbsp;allow&nbsp;the&nbsp;user&nbsp;to&nbsp;override&nbsp;this,&nbsp;but&nbsp;the&nbsp;default&nbsp;is&nbsp;to&nbsp;open&nbsp;and&nbsp;leave&nbsp;it&nbsp;open.&nbsp;&nbsp;If&nbsp;you&nbsp;grab&nbsp;the&nbsp;latest&nbsp;daily&nbsp;build&nbsp;version&nbsp;from&nbsp;our&nbsp;website&nbsp;this&nbsp;issue&nbsp;should&nbsp;be&nbsp;resolved.&nbsp;&nbsp;  
  
  
  
The&nbsp;second&nbsp;item&nbsp;is&nbsp;related&nbsp;to&nbsp;loading&nbsp;things&nbsp;from&nbsp;steam&nbsp;and&nbsp;allowing&nbsp;you&nbsp;to&nbsp;pin&nbsp;stuff&nbsp;in&nbsp;memory.&nbsp;&nbsp;We&nbsp;have&nbsp;a&nbsp;new&nbsp;method&nbsp;for&nbsp;doing&nbsp;this&nbsp;and&nbsp;a&nbsp;sample&nbsp;of&nbsp;this&nbsp;ships&nbsp;with&nbsp;the&nbsp;How&nbsp;Do&nbsp;I&nbsp;Samples.&nbsp;&nbsp;If&nbsp;you&nbsp;look&nbsp;in&nbsp;the&nbsp;Data&nbsp;Providers&nbsp;tree&nbsp;node&nbsp;the&nbsp;item&nbsp;is&nbsp;"Load&nbsp;a&nbsp;map&nbsp;from&nbsp;streams".&nbsp;&nbsp;This&nbsp;technique&nbsp;allows&nbsp;you&nbsp;to&nbsp;pass&nbsp;in&nbsp;a&nbsp;fake&nbsp;shapefile&nbsp;name&nbsp;and&nbsp;when&nbsp;we&nbsp;internally&nbsp;need&nbsp;to&nbsp;access&nbsp;the&nbsp;stream&nbsp;we&nbsp;raise&nbsp;an&nbsp;event&nbsp;and&nbsp;you&nbsp;can&nbsp;pass&nbsp;in&nbsp;whatever&nbsp;you&nbsp;want.&nbsp;&nbsp;In&nbsp;this&nbsp;way&nbsp;you&nbsp;can&nbsp;load&nbsp;the&nbsp;entire&nbsp;file&nbsp;into&nbsp;a&nbsp;memory&nbsp;stream&nbsp;and&nbsp;pass&nbsp;that&nbsp;into&nbsp;us.&nbsp;&nbsp;It&nbsp;is&nbsp;also&nbsp;handy&nbsp;in&nbsp;environment&nbsp;where&nbsp;you&nbsp;have&nbsp;access&nbsp;to&nbsp;only&nbsp;isolated&nbsp;storage&nbsp;or&nbsp;read&nbsp;files&nbsp;from&nbsp;SQL&nbsp;server&nbsp;etc.&nbsp;&nbsp;I&nbsp;went&nbsp;ahead&nbsp;and&nbsp;included&nbsp;the&nbsp;code&nbsp;from&nbsp;the&nbsp;sample&nbsp;as&nbsp;it's&nbsp;short&nbsp;and&nbsp;easy&nbsp;to&nbsp;read.&nbsp;&nbsp;It&nbsp;might&nbsp;save&nbsp;you&nbsp;from&nbsp;digging&nbsp;it&nbsp;up.  
  
  
  
`using System;   using System.IO;   using System.Windows.Forms;   using ThinkGeo.MapSuite.Core;   using ThinkGeo.MapSuite.DesktopEdition;       namespace CSharpWinformsSamples   {       public class LoadAMapFromStreams : UserControl       {           public LoadAMapFromStreams()           {               InitializeComponent();           }             private void LoadAMapFromStreams_Load(object sender, EventArgs e)           {               winformsMap1.MapUnit = GeographyUnit.DecimalDegree;               winformsMap1.BackgroundOverlay.BackgroundBrush = new GeoSolidBrush(GeoColor.GeographicColors.ShallowOcean);                 WorldMapKitWmsDesktopOverlay worldMapKitDesktopOverlay = new WorldMapKitWmsDesktopOverlay();               winformsMap1.Overlays.Add(worldMapKitDesktopOverlay);                 ShapeFileFeatureLayer shapeFileLayer = new ShapeFileFeatureLayer(@"C:\DoesNotExistDirectory\Countries02.shp");               ((ShapeFileFeatureSource)shapeFileLayer.FeatureSource).StreamLoading += new EventHandler<STREAMLOADINGEVENTARGS>(LoadAMapFromStreams_StreamLoading);               shapeFileLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.CreateSimpleAreaStyle(GeoColor.SimpleColors.Transparent, GeoColor.FromArgb(100, GeoColor.SimpleColors.Green));               shapeFileLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;                 LayerOverlay staticOverlay = new LayerOverlay();               staticOverlay.Layers.Add("WorldLayer", shapeFileLayer);               winformsMap1.Overlays.Add(staticOverlay);                 winformsMap1.CurrentExtent = new RectangleShape(-139.2, 92.4, 120.9, -93.2);               winformsMap1.Refresh();           }             private void LoadAMapFromStreams_StreamLoading(object sender, StreamLoadingEventArgs e)           {               string fileName = Path.GetFileName(e.AlternateStreamName);               e.AlternateStream = new FileStream(@"..\..\SampleData\Data\" + fileName, e.FileMode, e.FileAccess);           }             private WinformsMap winformsMap1;       }   }  `&nbsp;  
  
  
  
  
David  
  
&nbsp;

---

<div class="post-metadata">

**Author:** ![David](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@David](https://community.thinkgeo.com/u/David)\
**Post date:** [December 6, 2011, 4:21am UTC](https://community.thinkgeo.com/t/caching-shapefiles-into-memory/3116/3 "2011-12-06T04:21:48Z")

</div>

Rodney,&nbsp;  
&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;One&nbsp;thing&nbsp;I&nbsp;forgot&nbsp;to&nbsp;mention&nbsp;is&nbsp;that&nbsp;if&nbsp;you&nbsp;don’t&nbsp;want&nbsp;to&nbsp;grab&nbsp;the&nbsp;latest&nbsp;developer&nbsp;build&nbsp;another&nbsp;way&nbsp;to&nbsp;‘fix’&nbsp;this&nbsp;issue&nbsp;is&nbsp;to&nbsp;call&nbsp;the&nbsp;Layer.Open&nbsp;method&nbsp;on&nbsp;all&nbsp;of&nbsp;your&nbsp;layers.&nbsp;&nbsp;If&nbsp;you&nbsp;open&nbsp;them&nbsp;before&nbsp;hand&nbsp;then&nbsp;our&nbsp;default&nbsp;logic&nbsp;should&nbsp;be&nbsp;to&nbsp;keep&nbsp;them&nbsp;open.&nbsp;&nbsp;Let&nbsp;me&nbsp;know&nbsp;the&nbsp;results.&nbsp;  
&nbsp;&nbsp;  
&nbsp;David

---

<div class="post-metadata">

**Author:** ![Rodney](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/r/cab0a1/32.png) [@Rodney](https://community.thinkgeo.com/u/Rodney)\
**Post date:** [December 7, 2011, 2:30am UTC](https://community.thinkgeo.com/t/caching-shapefiles-into-memory/3116/4 "2011-12-07T02:30:24Z")

</div>

Hi&nbsp;David,&nbsp;thanks&nbsp;for&nbsp;you&nbsp;speedy&nbsp;response.  
  
  
  
Thanks&nbsp;for&nbsp;the&nbsp;tip&nbsp;on&nbsp;calling&nbsp;Open()&nbsp;on&nbsp;my&nbsp;ShapeFileFeatureLayer.&nbsp;That&nbsp;fixed&nbsp;the&nbsp;problem&nbsp;with&nbsp;the&nbsp;excessive&nbsp;File&nbsp;IO.

I'm&nbsp;now&nbsp;working&nbsp;on&nbsp;the&nbsp;second&nbsp;part&nbsp;of&nbsp;my&nbsp;query,&nbsp;that&nbsp;is&nbsp;caching&nbsp;a&nbsp;shapefile&nbsp;in&nbsp;memory&nbsp;to&nbsp;avoid&nbsp;repeated&nbsp;disk&nbsp;access.

I'm&nbsp;having&nbsp;some&nbsp;difficulty&nbsp;in&nbsp;getting&nbsp;your&nbsp;example&nbsp;working&nbsp;with&nbsp;a&nbsp;MemoryStream&nbsp;as&nbsp;opposed&nbsp;to&nbsp;a&nbsp;FileStream.&nbsp;

To&nbsp;setup&nbsp;my&nbsp;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;        }`&nbsp;

&nbsp;Callback&nbsp;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;        }`&nbsp;

However&nbsp;when&nbsp;I&nbsp;run&nbsp;this&nbsp;I&nbsp;receive&nbsp;a&nbsp;divide&nbsp;by&nbsp;zero&nbsp;exception:

Unhandled&nbsp;Exception:&nbsp;System.DivideByZeroException:&nbsp;Attempted&nbsp;to&nbsp;divide&nbsp;by&nbsp;zero.  
&nbsp;&nbsp;&nbsp;at&nbsp;ThinkGeo.MapSuite.WpfDesktopEdition.Tile.DrawException(GeoCanvas&nbsp;geoCanvas,&nbsp;Exception&nbsp;exception)  
&nbsp;&nbsp;&nbsp;at&nbsp;ThinkGeo.MapSuite.WpfDesktopEdition.Tile.Draw(GeoCanvas&nbsp;geoCanvas)  
&nbsp;&nbsp;&nbsp;at&nbsp;ThinkGeo.MapSuite.WpfDesktopEdition.LayerOverlay.DrawTileCore(Tile&nbsp;tile,&nbsp;RectangleShape&nbsp;targetExtent)  
&nbsp;&nbsp;&nbsp;at&nbsp;ThinkGeo.MapSuite.WpfDesktopEdition.TileOverlay.DrawTile(Tile&nbsp;tile,&nbsp;RectangleShape&nbsp;targetExtent)  
&nbsp;&nbsp;&nbsp;at&nbsp;ThinkGeo.MapSuite.WpfDesktopEdition.TileOverlay.vhQ=(RectangleShape&nbsp;vxQ=)  
&nbsp;&nbsp;&nbsp;at&nbsp;ThinkGeo.MapSuite.WpfDesktopEdition.TileOverlay.DrawCore(RectangleShape&nbsp;targetExtent,&nbsp;OverlayRefreshType&nbsp;overlayRefreshType)  
&nbsp;&nbsp;&nbsp;at&nbsp;ThinkGeo.MapSuite.WpfDesktopEdition.LayerOverlay.DrawCore(RectangleShape&nbsp;targetExtent,&nbsp;OverlayRefreshType&nbsp;refreshType)  
&nbsp;&nbsp;&nbsp;at&nbsp;ThinkGeo.MapSuite.WpfDesktopEdition.Overlay.Draw(RectangleShape&nbsp;targetExtent,&nbsp;OverlayRefreshType&nbsp;refreshType)

However,&nbsp;if&nbsp;I&nbsp;replace&nbsp;the&nbsp;e.AlternateStream&nbsp;assignment&nbsp;line&nbsp;in&nbsp;the&nbsp;callback&nbsp;with:

e.AlternateStream&nbsp;=&nbsp;new&nbsp;FileStream(e.AlternateStreamName,&nbsp;e.FileMode,&nbsp;e.FileAccess);&nbsp;&nbsp;

However&nbsp;this&nbsp;obviously&nbsp;defeats&nbsp;the&nbsp;purpose&nbsp;of&nbsp;having&nbsp;a&nbsp;MemoryStream&nbsp;to&nbsp;cache&nbsp;the&nbsp;file.&nbsp;I&nbsp;did&nbsp;also&nbsp;try&nbsp;setting&nbsp;AlternateStream&nbsp;to&nbsp;a&nbsp;newly&nbsp;constructed&nbsp;MemoryStream&nbsp;(from&nbsp;my&nbsp;stored&nbsp;byte&nbsp;buffer&nbsp;cache)&nbsp;however&nbsp;that&nbsp;produced&nbsp;the&nbsp;same&nbsp;exception.

It&nbsp;is&nbsp;strange,&nbsp;both&nbsp;MemoryStream&nbsp;and&nbsp;FileStream&nbsp;are&nbsp;'Stream's&nbsp;so&nbsp;I&nbsp;would&nbsp;expect&nbsp;the&nbsp;substitution&nbsp;to&nbsp;be&nbsp;invisible.

Any&nbsp;ideas?

---

<div class="post-metadata">

**Author:** ![David](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@David](https://community.thinkgeo.com/u/David)\
**Post date:** [December 7, 2011, 3:48am UTC](https://community.thinkgeo.com/t/caching-shapefiles-into-memory/3116/5 "2011-12-07T03:48:17Z")

</div>

Rodney,  
  
  
  
&nbsp;&nbsp;I&nbsp;have&nbsp;it&nbsp;working,&nbsp;so&nbsp;that's&nbsp;good&nbsp;news.&nbsp;&nbsp;There&nbsp;are&nbsp;a&nbsp;few&nbsp;things&nbsp;that&nbsp;you&nbsp;maybe&nbsp;didn't&nbsp;realize&nbsp;lurking&nbsp;right&nbsp;below&nbsp;the&nbsp;surface&nbsp;of&nbsp;this&nbsp;problem.&nbsp;&nbsp;The&nbsp;first&nbsp;is&nbsp;that&nbsp;the&nbsp;event&nbsp;is&nbsp;called&nbsp;multiple&nbsp;times,&nbsp;once&nbsp;for&nbsp;each&nbsp;type&nbsp;of&nbsp;file&nbsp;requested.&nbsp;&nbsp;This&nbsp;mean&nbsp;that&nbsp;for&nbsp;a&nbsp;shapefile&nbsp;the&nbsp;method&nbsp;will&nbsp;be&nbsp;called&nbsp;once&nbsp;for&nbsp;the&nbsp;.shp,&nbsp;another&nbsp;time&nbsp;for&nbsp;the&nbsp;.shx&nbsp;file&nbsp;etc&nbsp;I&nbsp;think&nbsp;it's&nbsp;a&nbsp;total&nbsp;of&nbsp;five&nbsp;times.&nbsp;&nbsp;In&nbsp;your&nbsp;sample&nbsp;you&nbsp;are&nbsp;caching&nbsp;this&nbsp;all&nbsp;to&nbsp;the&nbsp;same&nbsp;buffer&nbsp;which&nbsp;is&nbsp;causing&nbsp;confusion&nbsp;internally.&nbsp;:-)&nbsp;&nbsp;The&nbsp;system&nbsp;thing&nbsp;the&nbsp;.shp&nbsp;is&nbsp;the&nbsp;.shx&nbsp;etc..&nbsp;&nbsp;The&nbsp;alternate&nbsp;stream&nbsp;name&nbsp;will&nbsp;change&nbsp;and&nbsp;will&nbsp;always&nbsp;append&nbsp;the&nbsp;type&nbsp;of&nbsp;file&nbsp;we&nbsp;need&nbsp;to&nbsp;the&nbsp;end.&nbsp;&nbsp;Put&nbsp;in&nbsp;a&nbsp;break&nbsp;point&nbsp;and&nbsp;watch&nbsp;the&nbsp;alternate&nbsp;stream&nbsp;name&nbsp;and&nbsp;you&nbsp;will&nbsp;see&nbsp;it&nbsp;change.&nbsp;&nbsp;You&nbsp;can&nbsp;also&nbsp;look&nbsp;at&nbsp;the&nbsp;e.StreamType&nbsp;to&nbsp;see&nbsp;the&nbsp;type&nbsp;of&nbsp;file&nbsp;being&nbsp;requested.&nbsp;&nbsp;Secondly&nbsp;you&nbsp;don't&nbsp;have&nbsp;to&nbsp;cache&nbsp;the&nbsp;memory&nbsp;stream&nbsp;or&nbsp;buffer&nbsp;to&nbsp;a&nbsp;private&nbsp;field,&nbsp;I&nbsp;saw&nbsp;you&nbsp;used&nbsp;m\_Buffer&nbsp;etc.&nbsp;&nbsp;You&nbsp;can&nbsp;just&nbsp;create&nbsp;the&nbsp;memory&nbsp;stream&nbsp;and&nbsp;pass&nbsp;it&nbsp;in.&nbsp;&nbsp;The&nbsp;layer&nbsp;itself&nbsp;will&nbsp;hold&nbsp;on&nbsp;to&nbsp;the&nbsp;reference&nbsp;of&nbsp;the&nbsp;memory&nbsp;stream&nbsp;and&nbsp;keep&nbsp;it&nbsp;from&nbsp;getting&nbsp;collected.&nbsp;&nbsp;We&nbsp;must&nbsp;also&nbsp;note&nbsp;that&nbsp;this&nbsp;works&nbsp;until&nbsp;the&nbsp;layer&nbsp;is&nbsp;closed&nbsp;so&nbsp;make&nbsp;sure&nbsp;you&nbsp;open&nbsp;the&nbsp;layer&nbsp;first&nbsp;to&nbsp;make&nbsp;sure&nbsp;the&nbsp;Desktop&nbsp;bug&nbsp;doesn't&nbsp;open&nbsp;and&nbsp;close&nbsp;the&nbsp;layer&nbsp;over&nbsp;and&nbsp;over.&nbsp;&nbsp;You&nbsp;should&nbsp;be&nbsp;able&nbsp;to&nbsp;put&nbsp;some&nbsp;debug&nbsp;calls&nbsp;to&nbsp;verify&nbsp;it&nbsp;isn't&nbsp;getting&nbsp;called.&nbsp;&nbsp;The&nbsp;code&nbsp;below&nbsp;is&nbsp;what&nbsp;I&nbsp;modified&nbsp;from&nbsp;the&nbsp;How&nbsp;Do&nbsp;I&nbsp;sample&nbsp;but&nbsp;with&nbsp;a&nbsp;small&nbsp;amount&nbsp;of&nbsp;tweaking&nbsp;it&nbsp;shoudl&nbsp;work&nbsp;for&nbsp;you.  
  
&nbsp;

`        private void LoadAMapFromStreams_StreamLoading(object sender, StreamLoadingEventArgs e)        {            string fileName = Path.GetFileName(e.AlternateStreamName);            MemoryStream stream = null;                                   {                byte[] buffer = File.ReadAllBytes(@"..\..\SampleData\Data\" + fileName);                stream = new MemoryStream(buffer, false);            }                       e.AlternateStream = stream;                    }`&nbsp;

David

---

<div class="post-metadata">

**Author:** ![Rodney](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/r/cab0a1/32.png) [@Rodney](https://community.thinkgeo.com/u/Rodney)\
**Post date:** [December 7, 2011, 8:22pm UTC](https://community.thinkgeo.com/t/caching-shapefiles-into-memory/3116/6 "2011-12-07T20:22:06Z")

</div>

Hi&nbsp;David,&nbsp;  
  
  
  
Thanks&nbsp;for&nbsp;the&nbsp;tip&nbsp;about&nbsp;the&nbsp;differing&nbsp;file&nbsp;types.&nbsp;I&nbsp;implemented&nbsp;the&nbsp;changes&nbsp;you&nbsp;suggested&nbsp;which&nbsp;stopped&nbsp;the&nbsp;exceptions.&nbsp;However&nbsp;I&nbsp;noticed&nbsp;my&nbsp;file&nbsp;IO&nbsp;going&nbsp;through&nbsp;the&nbsp;roof&nbsp;(Up&nbsp;to&nbsp;100MB/s).&nbsp;It&nbsp;seems&nbsp;that&nbsp;despite&nbsp;calling&nbsp;Open()&nbsp;on&nbsp;the&nbsp;layer&nbsp;immediately&nbsp;after&nbsp;construction&nbsp;(and&nbsp;I&nbsp;tried&nbsp;Open()&nbsp;before&nbsp;and&nbsp;after&nbsp;callback&nbsp;assignment)&nbsp;the&nbsp;StreamLoading&nbsp;event&nbsp;callback&nbsp;was&nbsp;being&nbsp;entered&nbsp;several&nbsp;times&nbsp;on&nbsp;each&nbsp;overlay&nbsp;refresh.&nbsp;  
  
  
  
So&nbsp;I&nbsp;formed&nbsp;a&nbsp;hybrid&nbsp;solution&nbsp;whereby&nbsp;I&nbsp;cache&nbsp;the&nbsp;contents&nbsp;of&nbsp;each&nbsp;file&nbsp;in&nbsp;Dictionary\<string,&nbsp;byte[]\>&nbsp;and&nbsp;construct&nbsp;a&nbsp;new&nbsp;MemoryStream&nbsp;each&nbsp;time.&nbsp;I&nbsp;initially&nbsp;tried&nbsp;to&nbsp;cache&nbsp;the&nbsp;MemoryStream&nbsp;for&nbsp;each&nbsp;file&nbsp;but&nbsp;upon&nbsp;re-entering&nbsp;the&nbsp;event&nbsp;callback&nbsp;the&nbsp;stream&nbsp;had&nbsp;been&nbsp;closed,&nbsp;despite&nbsp;having&nbsp;called&nbsp;Open()&nbsp;on&nbsp;the&nbsp;layer&nbsp;initially.&nbsp;

Edit&nbsp;-&nbsp;By&nbsp;the&nbsp;way&nbsp;I&nbsp;have&nbsp;realised&nbsp;I&nbsp;am&nbsp;using&nbsp;WPF&nbsp;Desktop&nbsp;version&nbsp;5.0.0.0.

Edit&nbsp;edit&nbsp;-&nbsp;This&nbsp;WYSIWYG&nbsp;/&nbsp;Source&nbsp;editor&nbsp;interaction&nbsp;is&nbsp;pretty&nbsp;horrible!

Cheers,&nbsp;Rod

`public bool LoadWorldBackground()        {            string backgroundShape = ShapeFileDirectory + @"\Countries02.shp";            string backgroundIndex = ShapeFileDirectory + @"\Countries02.idx";                        if (Directory.Exists( ShapeFileDirectory ) && File.Exists( backgroundShape ))            {                AreaStyle areaStyle = AreaStyles.CreateSimpleAreaStyle( new GeoColor( 255, 0xe2, 0xdb, 0xa4 ));                ShapeFileFeatureLayer.BuildIndexFile( backgroundShape, backgroundIndex, BuildIndexMode.DoNotRebuild);                ShapeFileFeatureLayer layer = new ShapeFileFeatureLayer(backgroundShape, backgroundIndex, ShapeFileReadWriteMode.ReadOnly);                ((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;                                // Open the world map layer to avoid excessive file IO                layer.Open();                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;        }        private void LoadAMapFromStreams_StreamLoading(object sender, StreamLoadingEventArgs e)           {            Console.WriteLine("In Callback - {0}", e.AlternateStreamName);            if (!m_BackgroundCache.ContainsKey(e.AlternateStreamName))            {                byte[] buffer = File.ReadAllBytes(e.AlternateStreamName);                m_BackgroundCache.Add(e.AlternateStreamName, buffer);            }                        e.AlternateStream = new MemoryStream(m_BackgroundCache[e.AlternateStreamName], false);         }`

---

<div class="post-metadata">

**Author:** ![David](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@David](https://community.thinkgeo.com/u/David)\
**Post date:** [December 7, 2011, 8:32pm UTC](https://community.thinkgeo.com/t/caching-shapefiles-into-memory/3116/7 "2011-12-07T20:32:26Z")

</div>

Rodney,&nbsp;  
&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;I&nbsp;will&nbsp;look&nbsp;into&nbsp;the&nbsp;the&nbsp;IO&nbsp;issue&nbsp;however&nbsp;I&nbsp;bet&nbsp;I&nbsp;know&nbsp;what&nbsp;is&nbsp;going&nbsp;on&nbsp;there.&nbsp;&nbsp;The&nbsp;WPF&nbsp;Edition,&nbsp;in&nbsp;order&nbsp;to&nbsp;take&nbsp;advantage&nbsp;of&nbsp;multiple&nbsp;cores,&nbsp;clones&nbsp;the&nbsp;layers&nbsp;so&nbsp;it&nbsp;can&nbsp;draw&nbsp;on&nbsp;multiple&nbsp;tiles&nbsp;at&nbsp;the&nbsp;same&nbsp;time.&nbsp;&nbsp;I&nbsp;bet&nbsp;each&nbsp;clone&nbsp;is&nbsp;getting&nbsp;its&nbsp;open&nbsp;called.&nbsp;&nbsp;If&nbsp;you&nbsp;do&nbsp;not&nbsp;call&nbsp;the&nbsp;Overlay.Refresh&nbsp;then&nbsp;it&nbsp;should&nbsp;keep&nbsp;the&nbsp;clones&nbsp;cached.&nbsp;&nbsp;You&nbsp;should&nbsp;only&nbsp;call&nbsp;the&nbsp;Map.Refresh(),&nbsp;the&nbsp;one&nbsp;without&nbsp;parameters,&nbsp;when&nbsp;some&nbsp;massive&nbsp;things&nbsp;have&nbsp;changed.&nbsp;&nbsp;If&nbsp;not&nbsp;I&nbsp;suggest&nbsp;you&nbsp;call&nbsp;the&nbsp;Map.Refresh(Overlay)&nbsp;and&nbsp;only&nbsp;pass&nbsp;in&nbsp;the&nbsp;overlay&nbsp;that&nbsp;really&nbsp;changed,&nbsp;like&nbsp;you&nbsp;changed&nbsp;the&nbsp;styles.&nbsp;&nbsp;Otherwise,&nbsp;once&nbsp;the&nbsp;styles&nbsp;are&nbsp;set,&nbsp;&nbsp;there&nbsp;is&nbsp;not&nbsp;much&nbsp;need&nbsp;to&nbsp;call&nbsp;the&nbsp;refresh.&nbsp;&nbsp;Anyway&nbsp;we&nbsp;will&nbsp;look&nbsp;into&nbsp;this&nbsp;and&nbsp;see&nbsp;if&nbsp;there&nbsp;is&nbsp;a&nbsp;way&nbsp;we&nbsp;can&nbsp;clone&nbsp;the&nbsp;layer&nbsp;without&nbsp;calling&nbsp;an&nbsp;open&nbsp;on&nbsp;it.&nbsp;:-)&nbsp;&nbsp;Thanks&nbsp;for&nbsp;all&nbsp;of&nbsp;the&nbsp;great&nbsp;information.&nbsp;&nbsp;If&nbsp;you&nbsp;need&nbsp;anything&nbsp;else&nbsp;please&nbsp;let&nbsp;me&nbsp;know.&nbsp;  
&nbsp;&nbsp;  
&nbsp;David

---

<div class="post-metadata">

**Author:** ![Rodney](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/r/cab0a1/32.png) [@Rodney](https://community.thinkgeo.com/u/Rodney)\
**Post date:** [December 7, 2011, 9:22pm UTC](https://community.thinkgeo.com/t/caching-shapefiles-into-memory/3116/8 "2011-12-07T21:22:41Z")

</div>

Hi&nbsp;David,&nbsp;  
&nbsp;&nbsp;  
&nbsp;I&nbsp;have&nbsp;been&nbsp;playing&nbsp;it&nbsp;safe&nbsp;on&nbsp;my&nbsp;calls&nbsp;to&nbsp;refresh&nbsp;(I&nbsp;have&nbsp;been&nbsp;calling&nbsp;Map.Refresh(overlay)&nbsp;but&nbsp;also&nbsp;including&nbsp;the&nbsp;background&nbsp;overlay&nbsp;as&nbsp;a&nbsp;‘just&nbsp;in&nbsp;case’).&nbsp;I’m&nbsp;now&nbsp;stripping&nbsp;back&nbsp;my&nbsp;refresh&nbsp;calls&nbsp;to&nbsp;see&nbsp;what&nbsp;I&nbsp;can&nbsp;get&nbsp;away&nbsp;with.&nbsp;  
&nbsp;&nbsp;  
&nbsp;Thanks&nbsp;for&nbsp;your&nbsp;help&nbsp;with&nbsp;this&nbsp;issue.&nbsp;The&nbsp;tweaks&nbsp;we’ve&nbsp;made&nbsp;are&nbsp;making&nbsp;it&nbsp;perform&nbsp;a&nbsp;lot&nbsp;better&nbsp;and&nbsp;will&nbsp;flow&nbsp;back&nbsp;to&nbsp;my&nbsp;colleagues&nbsp;that&nbsp;are&nbsp;using&nbsp;Map&nbsp;Suite&nbsp;in&nbsp;a&nbsp;proper&nbsp;application&nbsp;(I’m&nbsp;just&nbsp;prototyping&nbsp;to&nbsp;see&nbsp;if&nbsp;it&nbsp;will&nbsp;be&nbsp;suitable&nbsp;to&nbsp;replace&nbsp;the&nbsp;chart&nbsp;I&nbsp;currently&nbsp;use&nbsp;in&nbsp;the&nbsp;project&nbsp;I&nbsp;am&nbsp;attached&nbsp;to).&nbsp;  
&nbsp;&nbsp;  
&nbsp;Cheers&nbsp;  
&nbsp;&nbsp;  
&nbsp;Rod

---

<div class="post-metadata">

**Author:** ![Rodney](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/r/cab0a1/32.png) [@Rodney](https://community.thinkgeo.com/u/Rodney)\
**Post date:** [December 8, 2011, 1:08am UTC](https://community.thinkgeo.com/t/caching-shapefiles-into-memory/3116/9 "2011-12-08T01:08:49Z")

</div>

As&nbsp;an&nbsp;aside,&nbsp;is&nbsp;the&nbsp;StreamLoading&nbsp;technique&nbsp;possible&nbsp;with&nbsp;MultipleShapeFileFeatureLayers?

---

<div class="post-metadata">

**Author:** ![David](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@David](https://community.thinkgeo.com/u/David)\
**Post date:** [December 8, 2011, 3:43am UTC](https://community.thinkgeo.com/t/caching-shapefiles-into-memory/3116/10 "2011-12-08T03:43:48Z")

</div>

Rodney,&nbsp;  
&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;It&nbsp;should&nbsp;work&nbsp;on&nbsp;the&nbsp;individual&nbsp;layers&nbsp;but&nbsp;I&nbsp;am&nbsp;not&nbsp;sure&nbsp;about&nbsp;the&nbsp;midx.&nbsp;&nbsp;I&nbsp;am&nbsp;not&nbsp;that&nbsp;familiar&nbsp;with&nbsp;that&nbsp;layer&nbsp;and&nbsp;need&nbsp;to&nbsp;do&nbsp;a&nbsp;bit&nbsp;more&nbsp;research.&nbsp;&nbsp;What&nbsp;I&nbsp;do&nbsp;know&nbsp;is&nbsp;that&nbsp;we&nbsp;can&nbsp;make&nbsp;it&nbsp;work.&nbsp;:-)&nbsp;  
&nbsp;&nbsp;  
&nbsp;David
