protected DynamicRTree(SerializationInfo info, StreamingContext context) { byte[] data = (byte[])info.GetValue("data", typeof(byte[])); MemoryStream buffer = new MemoryStream(data); DynamicRTree tree = FromStream(buffer); Root = tree.Root; }
/// <summary> /// Reads a node from a stream recursively /// </summary> /// <param name="tree">R-Tree instance</param> /// <param name="br">Binary reader reference</param> private static Node ReadNode(DynamicRTree tree, BinaryReader br) { if (br.BaseStream.Position == br.BaseStream.Length) { return(null); } Node node; bool isLeaf = br.ReadBoolean(); if (isLeaf) { node = tree.CreateLeafNode(); } else { node = tree.CreateIndexNode(); } node.Box = new SharpMap.Converters.Geometries.BoundingBox(br.ReadDouble(), br.ReadDouble(), br.ReadDouble(), br.ReadDouble()); if (isLeaf) { LeafNode leaf = node as LeafNode; int featureCount = br.ReadInt32(); for (int i = 0; i < featureCount; i++) { RTreeIndexEntry entry = new RTreeIndexEntry(); entry.Box = new SharpMap.Converters.Geometries.BoundingBox(br.ReadDouble(), br.ReadDouble(), br.ReadDouble(), br.ReadDouble()); entry.Id = br.ReadUInt32(); leaf.Add(entry); } } else { IndexNode index = node as IndexNode; uint childNodes = br.ReadUInt32(); for (int c = 0; c < childNodes; c++) { Node child = ReadNode(tree, br); if (child != null) { index.Add(child); } } } return(node); }
/// <summary> /// Loads an R-Tree structure from a stream /// </summary> /// <param name="filename">Stream which holds the R-Tree spatial index</param> /// <returns>A <see cref="DynamicRTree"/> instance which had been persisted to the <see cref="System.IO.Stream">stream</see>.</returns> public static DynamicRTree FromStream(Stream data) { DynamicRTree tree = new DynamicRTree(); using (System.IO.BinaryReader br = new System.IO.BinaryReader(data)) { System.Version fileVersion = new System.Version(br.ReadInt32(), br.ReadInt32(), br.ReadInt32(), br.ReadInt32()); if (fileVersion != IndexFileVersion) //Check fileindex version { throw new ObsoleteFileFormatException(IndexFileVersion, fileVersion, "Invalid index file version. Please rebuild the spatial index by deleting the index."); } tree.Root = ReadNode(tree, br); } return(tree); }