-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathImageTileCache.cs
More file actions
102 lines (91 loc) · 4.03 KB
/
ImageTileCache.cs
File metadata and controls
102 lines (91 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Copyright 2006, 2007 - Rory Plaire (codekaizen@gmail.com)
//
// This file is part of SharpMap.
// SharpMap is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// SharpMap is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
// You should have received a copy of the GNU Lesser General Public License
// along with SharpMap; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using GeoAPI.Geometries;
using SharpMap.Indexing.QuadTree;
namespace SharpMap.Presentation.WinForms
{
internal class ImageTileCache : LinearDynamicQuadTree<Bitmap>
{
/// <summary>
/// Creates a new ImageTileCache.
/// </summary>
/// <param name="initialBounds">The extents for the entire index. May grow if tiles are added which exceed these bounds.</param>
public ImageTileCache(IExtents initialBounds)
{
// Set the bounds of the index
Bounds = initialBounds;
}
public void Add(IExtents tileBounds, Bitmap tile)
{
QuadTreeNode<Bitmap> tileNode = new QuadTreeNode<Bitmap>();
tileNode.Value = tile;
#warning This doesn't compile in Orcas B2, but it does in VS2005
//tileNode.BoundingBox = tileBounds;
AddItem(tileNode);
}
public override IEnumerable<Bitmap> Search(IExtents searchBounds)
{
return recursiveNodeSearch(searchBounds, this);
}
private IEnumerable<Bitmap> recursiveNodeSearch(IExtents searchBounds, QuadTreeNode<Bitmap> node)
{
// If search bounds are only partially by node bounds, then return node's value,
// since successive searches will be incomplete.
if (node.Bounds.Intersects(searchBounds) && !node.Bounds.Contains(searchBounds))
{
yield return node.Value;
yield break;
}
// The node must cover the search bounds, so see if it has more than
// one child node which intersects the search bounds. If so, then that must
// be the best resolution tiles to satisfy the search request, as the bitmaps can
// be resampled upward. If there is only one child, recurse downward on that child.
Int32[] intersectNodes = new Int32[4];
Int32 intersectNodeIndex = -1;
for (Int32 nodeIndex = 0; nodeIndex < node.ItemCount; nodeIndex++)
{
QuadTreeNode<Bitmap> testNode = Items[nodeIndex];
if (testNode.Bounds.Intersects(searchBounds))
{
intersectNodes[++intersectNodeIndex] = nodeIndex;
}
}
// This should not be possible with the first bounds-check in this function
Debug.Assert(intersectNodeIndex > -1);
// If more than one node intersects the searchBounds, we must be at the correct resolution.
// Otherwise, keep searching down the index.
if (intersectNodeIndex > 0)
{
foreach (Int32 nodeIndex in intersectNodes)
{
yield return node.Items[nodeIndex].Value;
}
}
else
{
foreach (
Bitmap tile in recursiveNodeSearch(searchBounds, node.Items[intersectNodes[intersectNodeIndex]]))
{
yield return tile;
}
}
}
}
}