# Using in-memory spatial index

**URL:** <https://community.thinkgeo.com/t/using-in-memory-spatial-index/7179>\
**Category:** Services\
**Created:** [February 20, 2013, 12:11pm UTC](https://community.thinkgeo.com/t/using-in-memory-spatial-index/7179 "2013-02-20T12:11:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Raul](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/r/958977/32.png) [@Raul](https://community.thinkgeo.com/u/Raul)\
**Post date:** [February 20, 2013, 12:11pm UTC](https://community.thinkgeo.com/t/using-in-memory-spatial-index/7179/1 "2013-02-20T12:11:37Z")

</div>

Hello,

I&nbsp;am&nbsp;trying&nbsp;to&nbsp;use&nbsp;an&nbsp;in-memory&nbsp;spatial&nbsp;index&nbsp;and&nbsp;got&nbsp;into&nbsp;trouble.

File-based&nbsp;RtreeSpatialIndex&nbsp;works&nbsp;without&nbsp;errors&nbsp;but&nbsp;it&nbsp;is&nbsp;too&nbsp;slow&nbsp;for&nbsp;our&nbsp;needs,&nbsp;so&nbsp;I&nbsp;thought&nbsp;of&nbsp;handling&nbsp;StreamLoading&nbsp;event&nbsp;and&nbsp;assigning&nbsp;a&nbsp;MemoryStream&nbsp;to&nbsp;args.AlternateStream.&nbsp;This&nbsp;approach&nbsp;fails&nbsp;when&nbsp;I&nbsp;try&nbsp;to&nbsp;open&nbsp;the&nbsp;index:

&nbsp;

&nbsp;&nbsp;System.IndexOutOfRangeException&nbsp;occurred  
&nbsp;&nbsp;HResult=-2146233080  
&nbsp;&nbsp;Message=Index&nbsp;was&nbsp;outside&nbsp;the&nbsp;bounds&nbsp;of&nbsp;the&nbsp;array.  
&nbsp;&nbsp;Source=MapSuiteCore  
&nbsp;&nbsp;StackTrace:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at&nbsp;hxQ=.RU4=.khQ=(BinaryReader&nbsp;SU4=)  
  
Extended&nbsp;stack&nbsp;trace:  
&nbsp; MapSuiteCore.dll!hxQ=.RU4=.khQ=(System.IO.BinaryReader&nbsp;SU4=)&nbsp;+&nbsp;0x10c&nbsp;bytes   
&nbsp; MapSuiteCore.dll!hxQ=.h0A=.khQ=()&nbsp;+&nbsp;0x90&nbsp;bytes   
&nbsp; MapSuiteCore.dll!hxQ=.PjY=.VzY=()&nbsp;+&nbsp;0x14&nbsp;bytes   
&nbsp; MapSuiteCore.dll!hxQ=.PjY=.Dxg=(string&nbsp;UzY=,&nbsp;bool&nbsp;VDY=,&nbsp;ThinkGeo.MapSuite.Core.RtreeSpatialIndex.q0Y=&nbsp;VTY=)&nbsp;+&nbsp;0x97&nbsp;bytes   
&nbsp; MapSuiteCore.dll!ThinkGeo.MapSuite.Core.RtreeSpatialIndex.OpenCore()&nbsp;+&nbsp;0x7f&nbsp;bytes   
&nbsp; MapSuiteCore.dll!ThinkGeo.MapSuite.Core.SpatialIndex.Open()&nbsp;+&nbsp;0x22&nbsp;bytes  
&nbsp;  
I&nbsp;also&nbsp;tried&nbsp;to&nbsp;use&nbsp;RtreeSpatialIndex.CreatePointSpatialIndex(filePath,...)&nbsp;and&nbsp;initialize&nbsp;the&nbsp;memory&nbsp;stream&nbsp;with&nbsp;the&nbsp;content&nbsp;of&nbsp;the&nbsp;.idx&nbsp;file.&nbsp;That&nbsp;got&nbsp;me&nbsp;past&nbsp;Open()&nbsp;but&nbsp;failed&nbsp;on&nbsp;Add():  
  
&nbsp;&nbsp;System.NullReferenceException&nbsp;occurred  
&nbsp;&nbsp;HResult=-2147467261  
&nbsp;&nbsp;Message=Object&nbsp;reference&nbsp;not&nbsp;set&nbsp;to&nbsp;an&nbsp;instance&nbsp;of&nbsp;an&nbsp;object.  
&nbsp;&nbsp;Source=MapSuiteCore  
&nbsp;&nbsp;StackTrace:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at&nbsp;hxQ=.YE4=.bE4=(String&nbsp;bU4=)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at&nbsp;ThinkGeo.MapSuite.Core.RtreeSpatialIndex.bUY=(Double&nbsp;bkY=,&nbsp;Double&nbsp;b0Y=,&nbsp;String&nbsp;cEY=)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at&nbsp;ThinkGeo.MapSuite.Core.RtreeSpatialIndex.bUY=(PointShape&nbsp;cUY=,&nbsp;String&nbsp;ckY=)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at&nbsp;ThinkGeo.MapSuite.Core.RtreeSpatialIndex.AddCore(Feature&nbsp;feature)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at&nbsp;ThinkGeo.MapSuite.Core.SpatialIndex.Add(BaseShape&nbsp;baseShape)  
&nbsp;  
I&nbsp;also&nbsp;looked&nbsp;for&nbsp;samples&nbsp;or&nbsp;at&nbsp;least&nbsp;posts&nbsp;on&nbsp;handling&nbsp;Rtree's&nbsp;StreamLoading&nbsp;event&nbsp;but&nbsp;didn't&nbsp;find&nbsp;any.  
Any&nbsp;suggestions&nbsp;would&nbsp;be&nbsp;appreciated.&nbsp;  
&nbsp;  
Thanks&nbsp;in&nbsp;advance,  
Raul  
&nbsp;

&nbsp;

---

<div class="post-metadata">

**Author:** ![Johnny](https://community.thinkgeo.com/user_avatar/community.thinkgeo.com/johnny/32/19044_2.png) [@Johnny](https://community.thinkgeo.com/u/Johnny)\
**Post date:** [February 21, 2013, 2:37am UTC](https://community.thinkgeo.com/t/using-in-memory-spatial-index/7179/2 "2013-02-21T02:37:54Z")

</div>

&nbsp;

Hi&nbsp;Raul,  
Welcome&nbsp;to&nbsp;Map&nbsp;Suite&nbsp;discussion&nbsp;forum&nbsp;and&nbsp;thanks&nbsp;for&nbsp;your&nbsp;posts.  
I&nbsp;guess&nbsp;maybe&nbsp;there&nbsp;are&nbsp;2&nbsp;problems&nbsp;here&nbsp;shown&nbsp;as&nbsp;following:  
1.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;In&nbsp;MapSuiteCore.dll,&nbsp;all&nbsp;the&nbsp;MemoryStream&nbsp;used&nbsp;here&nbsp;should&nbsp;follow&nbsp;the&nbsp;.idx&nbsp;data&nbsp;format.&nbsp;Otherwise,&nbsp;it&nbsp;will&nbsp;throw&nbsp;the&nbsp;exception.  
2.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;I&nbsp;guess&nbsp;your&nbsp;workaround&nbsp;is&nbsp;really&nbsp;a&nbsp;workable&nbsp;way,&nbsp;but&nbsp;missed&nbsp;setting&nbsp;the&nbsp;“rTreeIndex.ReadWriteMode&nbsp;=RtreeSpatialIndexReadWriteMode.ReadWrite;”.&nbsp;Please&nbsp;check&nbsp;the&nbsp;demo&nbsp;code&nbsp;shown&nbsp;below:  
&nbsp;  
  
`        public TestForm()        {            InitializeComponent();        }        string idxFilePath;        private void TestForm_Load(object sender, EventArgs e)        {            idxFilePath =  @"..\..\~.idx";            RtreeSpatialIndex rTreeIndex = new RtreeSpatialIndex(idxFilePath, RtreeSpatialIndexReadWriteMode.ReadOnly);            rTreeIndex.StreamLoading += new EventHandler<StreamLoadingEventArgs>(rTreeIndex_StreamLoading);            RtreeSpatialIndex.CreateRectangleSpatialIndex(idxFilePath, RtreeSpatialIndexPageSize.EightKilobytes);            rTreeIndex.ReadWriteMode = RtreeSpatialIndexReadWriteMode.ReadWrite;            rTreeIndex.Open();            rTreeIndex.Add(new PointShape(0, 0));        }        void rTreeIndex_StreamLoading(object sender, StreamLoadingEventArgs e)        {            FileStream fs = new FileStream(idxFilePath, FileMode.Open, FileAccess.ReadWrite);            byte[] bytes = new byte[fs.Length];            fs.Read(bytes, 0, (int)fs.Length);            MemoryStream memoryStream = new MemoryStream(bytes);            e.AlternateStream = memoryStream;        }`  
&nbsp;  
Thanks,  
Johnny

---

<div class="post-metadata">

**Author:** ![Raul](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/r/958977/32.png) [@Raul](https://community.thinkgeo.com/u/Raul)\
**Post date:** [February 22, 2013, 1:40pm UTC](https://community.thinkgeo.com/t/using-in-memory-spatial-index/7179/3 "2013-02-22T13:40:20Z")

</div>

Hey&nbsp;Johnny,  
  
Thanks&nbsp;a&nbsp;lot&nbsp;for&nbsp;your&nbsp;reply,&nbsp;the&nbsp;sample&nbsp;code&nbsp;you&nbsp;provided&nbsp;worked&nbsp;with&nbsp;a&nbsp;small&nbsp;correction:&nbsp;creating&nbsp;MemoryStream(byte[])&nbsp;creates&nbsp;a&nbsp;non-resizable&nbsp;stream&nbsp;based&nbsp;on&nbsp;that&nbsp;byte&nbsp;array.&nbsp;Instead,&nbsp;we'd&nbsp;like&nbsp;something&nbsp;like&nbsp;  
  
`    var memoryStream = new MemoryStream();    memoryStream.Write(bytes, 0, bytes.Length);`

Regards,  
  
Raul

---

<div class="post-metadata">

**Author:** ![Johnny](https://community.thinkgeo.com/user_avatar/community.thinkgeo.com/johnny/32/19044_2.png) [@Johnny](https://community.thinkgeo.com/u/Johnny)\
**Post date:** [February 25, 2013, 3:18am UTC](https://community.thinkgeo.com/t/using-in-memory-spatial-index/7179/4 "2013-02-25T03:18:11Z")

</div>

That’s&nbsp;great,&nbsp;any&nbsp;question&nbsp;please&nbsp;feel&nbsp;free&nbsp;to&nbsp;let&nbsp;us&nbsp;know.&nbsp;  
&nbsp;&nbsp;  
&nbsp;Thanks,&nbsp;  
&nbsp;Johnny
