/
SpatialIndexing.cs
866 lines (754 loc) · 29.7 KB
/
SpatialIndexing.cs
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
// Copyright 2005, 2006 - Morten Nielsen (www.iter.dk)
//
// 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.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using Common.Logging;
using GeoAPI.Geometries;
using NetTopologySuite.Utilities;
using SharpMap.Data.Providers;
using SharpMap.Utilities.Indexing;
namespace SharpMap.Utilities.Indexing
{
/// <summary>
/// Interface for items stored in a spatial index
/// </summary>
/// <typeparam name="TOid">The type of the object identifier</typeparam>
public interface ISpatialIndexItem<out TOid>
{
/// <summary>
/// Gets the object's identifier
/// </summary>
TOid ID { get; }
/// <summary>
/// Gets the spatial extent of the object
/// </summary>
Envelope Box { get; }
}
/// <summary>
/// Interface for a spatial index
/// </summary>
/// <typeparam name="TOid">The type of the object identifier</typeparam>
public interface ISpatialIndex<TOid>
{
/// <summary>
/// Method to search for
/// </summary>
/// <param name="extent">The extent to search</param>
/// <returns>A collection of items</returns>
Collection<TOid> Search(Envelope extent);
/// <summary>
/// Gets a value indicating the area covered by this spatial index
/// </summary>
Envelope Box { get; }
/// <summary>
/// Method to save the spatial index to disk
/// </summary>
/// <param name="filename">The filename</param>
void SaveIndex(string filename);
/// <summary>
/// Method to delete the spatial index from the disk
/// </summary>
/// <param name="filename">The filename</param>
void DeleteIndex(string filename);
}
/// <summary>
/// Interface for all classes that can create a spatial index
/// </summary>
/// <typeparam name="TOid">The type of the object identifier</typeparam>
public interface ISpatialIndexFactory<TOid>
{
/// <summary>
/// Method to create a spatial index item
/// </summary>
/// <param name="oid">The object's identifier</param>
/// <param name="box">The extent</param>
/// <returns>A new spatial index item</returns>
ISpatialIndexItem<TOid> Create(TOid oid, Envelope box);
/// <summary>
/// Method to create a spatial index
/// </summary>
/// <param name="extent">The extent covered by the spatial index</param>
/// <param name="expectedNumberOfEntries"></param>
/// <param name="entries">The entries</param>
/// <returns>A spatial index</returns>
ISpatialIndex<TOid> Create(Envelope extent, int expectedNumberOfEntries,
IEnumerable<ISpatialIndexItem<TOid>> entries);
/// <summary>
/// Method to create a spatial index by loading it from file
/// </summary>
/// <param name="fileName">The filename of the file, the spatial index
/// is associated with. The implementation has to take care of the
/// renaming strategy, e.g. change the extension.</param>
/// <returns>The loaded index</returns>
ISpatialIndex<TOid> Load(string fileName);
/// <summary>
/// Gets a value indicating the file extension
/// </summary>
string Extension { get; }
}
/// <summary>
/// A pseudo tree.
/// </summary>
internal class AllFeaturesTree : ISpatialIndex<uint>
{
private readonly uint _featureCount;
private readonly Envelope _box;
public AllFeaturesTree(Envelope box, uint featureCount)
{
_box = box;
_featureCount = featureCount;
}
Collection<uint> ISpatialIndex<uint>.Search(Envelope extent)
{
var res = new Collection<uint>();
for (uint i = 0; i < _featureCount; i++)
res.Add(i);
return res;
}
Envelope ISpatialIndex<uint>.Box
{
get { return _box; }
}
void ISpatialIndex<uint>.SaveIndex(string filename) { }
void ISpatialIndex<uint>.DeleteIndex(string filename) { }
}
}
namespace SharpMap.Utilities.SpatialIndexing
{
/// <summary>
/// Heuristics used for tree generation
/// </summary>
public struct Heuristic
{
/// <summary>
/// Maximum tree depth
/// </summary>
public int maxdepth;
/// <summary>
/// Minimum Error metric – the volume of a box + a unit cube.
/// The unit cube in the metric prevents big boxes that happen to be flat having a zero result and muddling things up.
/// </summary>
public int minerror;
/// <summary>
/// Minimum object count at node
/// </summary>
public int mintricnt;
/// <summary>
/// Target object count at node
/// </summary>
public int tartricnt;
}
/// <summary>
/// Constructs a Quad-tree node from a object list and creates its children recursively
/// </summary>
public class QuadTree : IDisposable, ISpatialIndex<uint>
{
private Envelope _box;
private QuadTree _child0;
private QuadTree _child1;
/// <summary>
/// Gets the number of Nodes
/// </summary>
public int NodeCount
{
get
{
if (_child0 != null)
return _child0.NodeCount + _child1.NodeCount;
return _objList != null ? _objList.Count : 0;
}
}
/// <summary>
/// Gets a value indicating whether this branch is prunable
/// </summary>
public bool IsPrunable
{
get { return NodeCount == 0; }
}
/// <summary>
/// Nodes depth in a tree
/// </summary>
private readonly uint _depth;
/// <summary>
/// Node ID
/// </summary>
public uint? _ID;
private List<BoxObjects> _objList;
/// <summary>
/// Creates a node and either splits the objects recursively into sub-nodes, or stores them at the node depending on the heuristics.
/// Tree is built top->down
/// </summary>
/// <param name="objList">Geometries to index</param>
/// <param name="depth">Current depth of tree</param>
/// <param name="heurdata">Heuristics data</param>
public QuadTree(List<BoxObjects> objList, uint depth, Heuristic heurdata)
{
_depth = depth;
_box = new Envelope(objList[0].Box);
for (var i = 1; i < objList.Count; i++)
_box.ExpandToInclude(objList[i].Box);
// test our build heuristic - if passes, make children
if (depth < heurdata.maxdepth && objList.Count > heurdata.mintricnt &&
(objList.Count > heurdata.tartricnt || ErrorMetric(_box) > heurdata.minerror))
{
var objBuckets = new List<BoxObjects>[2]; // buckets of geometries
objBuckets[0] = new List<BoxObjects>();
objBuckets[1] = new List<BoxObjects>();
var longaxis = _box.LongestAxis(); // longest axis
var geoavg = 0d; // geometric average - midpoint of ALL the objects
// go through all bbox and calculate the average of the midpoints
var frac = 1.0d/objList.Count;
for (var i = 0; i < objList.Count; i++)
geoavg += objList[i].Box.Centre[longaxis]*frac;
// bucket bbox based on their midpoint's side of the geo average in the longest axis
for (var i = 0; i < objList.Count; i++)
objBuckets[geoavg > objList[i].Box.Centre[longaxis] ? 1 : 0].Add(objList[i]);
//If objects couldn't be splitted, just store them at the leaf
//TODO: Try splitting on another axis
if (objBuckets[0].Count == 0 || objBuckets[1].Count == 0)
{
_child0 = null;
_child1 = null;
// copy object list
_objList = objList;
}
else
{
//We don't need the list anymore;
objList.Clear();
// create new children using the buckets
_child0 = new QuadTree(objBuckets[0], depth + 1, heurdata);
_child1 = new QuadTree(objBuckets[1], depth + 1, heurdata);
}
}
else
{
// otherwise the build heuristic failed, this is
// set the first child to null (identifies a leaf)
_child0 = null;
_child1 = null;
// copy object list
_objList = objList;
}
}
/// <summary>
/// This means areas overlap by 5%
/// </summary>
private const double SplitRatio = 0.55d;
private static void SplitBoundingBox(Envelope input, out Envelope out1, out Envelope out2)
{
double range;
/* -------------------------------------------------------------------- */
/* Split in X direction. */
/* -------------------------------------------------------------------- */
if (input.Width > input.Height)
{
range = input.Width*SplitRatio;
out1 = new Envelope(input.BottomLeft(), new Coordinate(input.MinX + range, input.MaxY));
out2 = new Envelope(new Coordinate(input.MaxX - range, input.MinY), input.TopRight());
}
/* -------------------------------------------------------------------- */
/* Otherwise split in Y direction. */
/* -------------------------------------------------------------------- */
else
{
range = input.Height*SplitRatio;
out1 = new Envelope(input.BottomLeft(), new Coordinate(input.MaxX, input.MinY + range));
out2 = new Envelope(new Coordinate(input.MinX, input.MaxY - range), input.TopRight());
}
//Debug.Assert(out1.Intersects(out2));
}
/// <summary>
/// Adds a new <see cref="BoxObjects"/> to this node.
/// </summary>
/// <param name="o">The boxed object</param>
/// <param name="h">The child node creation heuristic</param>
public void AddNode(BoxObjects o, Heuristic h)
{
/* -------------------------------------------------------------------- */
/* If there are subnodes, then consider whether this object */
/* will fit in them. */
/* -------------------------------------------------------------------- */
if (_child0 != null && _depth < h.maxdepth)
{
if (_child0.Box.Contains(o.Box.Centre))
_child0.AddNode(o, h);
else if (_child1.Box.Contains(o.Box.Centre))
_child1.AddNode(o, h);
return;
}
/* -------------------------------------------------------------------- */
/* Otherwise, consider creating two subnodes if could fit into */
/* them, and adding to the appropriate subnode. */
/* -------------------------------------------------------------------- */
if (h.maxdepth > _depth && !IsLeaf)
{
Envelope half1, half2;
SplitBoundingBox(Box, out half1, out half2);
if (half1.Contains(o.Box.Centre))
{
_child0 = new QuadTree(half1, _depth + 1);
_child1 = new QuadTree(half2, _depth + 1);
_child0.AddNode(o, h);
return;
}
if (half2.Contains(o.Box.Centre))
{
_child0 = new QuadTree(half1, _depth + 1);
_child1 = new QuadTree(half2, _depth + 1);
_child1.AddNode(o, h);
return;
}
}
/* -------------------------------------------------------------------- */
/* If none of that worked, just add it to this nodes list. */
/* -------------------------------------------------------------------- */
//Debug.Assert(_child0 == null);
if (_objList == null)
_objList = new List<BoxObjects>();
Box.ExpandToInclude(o.Box);
_objList.Add(o);
}
/// <summary>
/// This instantiator is used internally for loading a tree from a file
/// </summary>
private QuadTree()
{
}
/// <summary>
/// This instantiator is used internally for linear creation of quadtrees
/// </summary>
/// <param name="box">The initial bounding box</param>
/// <param name="depth">The depth</param>
private QuadTree(Envelope box, uint depth)
{
_box = box;
_depth = depth;
}
#region Read/Write index to/from a file
private const double INDEXFILEVERSION = 1.1;
/// <summary>
/// Loads a quadtree from a file
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
public static QuadTree FromFile(string filename)
{
var fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
using (var br = new BinaryReader(fs))
{
if (br.ReadDouble() != INDEXFILEVERSION) //Check fileindex version
{
fs.Close();
fs.Dispose();
throw new ObsoleteFileFormatException(
"Invalid index file version. Please rebuild the spatial index by either deleting the index");
}
var node = ReadNode(0, br);
return node;
}
}
/// <summary>
/// Reads a node from a stream recursively
/// </summary>
/// <param name="depth">Current depth</param>
/// <param name="br">Binary reader reference</param>
/// <returns></returns>
private static QuadTree ReadNode(uint depth, BinaryReader br)
{
var bbox = new Envelope(new Coordinate(br.ReadDouble(), br.ReadDouble()),
new Coordinate(br.ReadDouble(), br.ReadDouble()));
var node = new QuadTree(bbox, depth);
var isLeaf = br.ReadBoolean();
if (isLeaf)
{
var featureCount = br.ReadInt32();
node._objList = new List<BoxObjects>();
for (int i = 0; i < featureCount; i++)
{
var box = new BoxObjects();
box.Box = new Envelope(new Coordinate(br.ReadDouble(), br.ReadDouble()),
new Coordinate(br.ReadDouble(), br.ReadDouble()));
box.ID = (uint) br.ReadInt32();
node._objList.Add(box);
}
}
else
{
node.Child0 = ReadNode(depth + 1, br);
node.Child1 = ReadNode(depth + 1, br);
}
return node;
}
/// <summary>
/// Saves the Quadtree to a file
/// </summary>
/// <param name="filename"></param>
public void SaveIndex(string filename)
{
try
{
using (var fs = new FileStream(filename + ".sidx", FileMode.Create))
{
using (var bw = new BinaryWriter(fs))
{
bw.Write(INDEXFILEVERSION); //Save index version
SaveNode(this, bw);
}
}
}
catch (Exception)
{
}
}
/// <summary>
/// Method to delete the spatial index from the disk
/// </summary>
/// <param name="filename">The filename</param>
public void DeleteIndex(string filename)
{
if (File.Exists(filename + ".sidx"))
File.Delete(filename + ".sidx");
}
/// <summary>
/// Saves a node to a stream recursively
/// </summary>
/// <param name="node">Node to save</param>
/// <param name="sw">Reference to BinaryWriter</param>
private static void SaveNode(QuadTree node, BinaryWriter sw)
{
//Write node boundingbox
var box = node.Box;
sw.Write(box.MinX);
sw.Write(box.MinY);
sw.Write(box.MaxX);
sw.Write(box.MaxY);
sw.Write(node.IsLeaf);
if (node.IsLeaf || node.Child0 == null)
{
if (node._objList == null)
{
sw.Write(0);
return;
}
sw.Write(node._objList.Count); //Write number of features at node
for (int i = 0; i < node._objList.Count; i++) //Write each featurebox
{
var bo = node._objList[i];
box = bo.Box;
sw.Write(box.MinX);
sw.Write(box.MinY);
sw.Write(box.MaxX);
sw.Write(box.MaxY);
sw.Write(bo.ID);
}
}
else if (!node.IsLeaf) //Save next node
{
SaveNode(node.Child0, sw);
SaveNode(node.Child1, sw);
}
}
/// <summary>
/// Creates a quadtree root node.
/// </summary>
/// <param name="b">The root bounding box</param>
/// <returns>The root node for the quadtree</returns>
public static QuadTree CreateRootNode(Envelope b)
{
return new QuadTree(b, 0);
}
internal class ObsoleteFileFormatException : Exception
{
/// <summary>
/// Exception thrown when layer rendering has failed
/// </summary>
/// <param name="message"></param>
public ObsoleteFileFormatException(string message)
: base(message)
{
}
}
#endregion
/// <summary>
/// Determines whether the node is a leaf (if data is stored at the node, we assume the node is a leaf)
/// </summary>
public bool IsLeaf
{
get { return (_objList != null); }
}
/// <summary>
/// Gets/sets the Axis Aligned Bounding Box
/// </summary>
public Envelope Box
{
get { return _box; }
set { _box = value; }
}
/// <summary>
/// Gets/sets the left child node
/// </summary>
public QuadTree Child0
{
get { return _child0; }
set { _child0 = value; }
}
/// <summary>
/// Gets/sets the right child node
/// </summary>
public QuadTree Child1
{
get { return _child1; }
set { _child1 = value; }
}
/// <summary>
/// Gets the depth of the current node in the tree
/// </summary>
public uint Depth
{
get { return _depth; }
}
#region IDisposable Members
/// <summary>
/// Disposes the node
/// </summary>
public void Dispose()
{
//this._box = null;
_child0?.Dispose();
_child0 = null;
_child1?.Dispose();
_child1 = null;
_objList = null;
}
#endregion
/// <summary>
/// Calculate the floating point error metric
/// </summary>
/// <returns></returns>
public static double ErrorMetric(Envelope box)
{
var temp = new Coordinate(1, 1).Add(box.Max().Subtract(box.Min()));
return temp.X*temp.Y;
}
/// <summary>
/// Searches the tree and looks for intersections with the boundingbox 'bbox'
/// </summary>
/// <param name="box">Boundingbox to intersect with</param>
public Collection<uint> Search(Envelope box)
{
var objectlist = new Collection<uint>();
IntersectTreeRecursive(box, this, /*ref*/ objectlist);
return objectlist;
}
/// <summary>
/// Recursive function that traverses the tree and looks for intersections with a boundingbox
/// </summary>
/// <param name="box">Boundingbox to intersect with</param>
/// <param name="node">Node to search from</param>
/// <param name="list">List of found intersections</param>
private static void IntersectTreeRecursive(Envelope box, QuadTree node, /*ref*/ ICollection<uint> list)
{
if (node.IsLeaf) //Leaf has been reached
{
foreach (var boxObject in node._objList)
{
if (box.Intersects(boxObject.Box))
{
list.Add(boxObject.ID);
}
}
/*
for (int i = 0; i < node._objList.Count; i++)
{
list.Add(node._objList[i].ID);
}
*/
}
else
{
if (node.Box.Intersects(box))
{
if (node.Child0 != null)
IntersectTreeRecursive(box, node.Child0, /*ref*/ list);
if (node.Child1 != null)
IntersectTreeRecursive(box, node.Child1, /*ref*/ list);
}
}
}
#region Nested type: BoxObjects
/// <summary>
/// BoundingBox and Feature ID structure used for storing in the quadtree
/// </summary>
public struct BoxObjects : ISpatialIndexItem<uint>
{
/// <summary>
/// Boundingbox
/// </summary>
public Envelope Box { get; set; }
/// <summary>
/// Feature ID
/// </summary>
public uint ID { get; set; }
}
#endregion
}
/// <summary>
/// A factory to create <see cref="QuadTree"/> spatial indices.
/// </summary>
public class QuadTreeFactory : ISpatialIndexFactory<uint>
{
private static readonly ILog _logger = LogManager.GetLogger(typeof (QuadTreeFactory));
private static ShapeFile.SpatialIndexCreation _spatialIndexSpatialIndexCreationOption;
/// <summary>
/// Gets or sets the default spatial index creation option
/// </summary>
public static ShapeFile.SpatialIndexCreation SpatialIndexCreationOption
{
get { return _spatialIndexSpatialIndexCreationOption; }
set
{
if (
!((value == ShapeFile.SpatialIndexCreation.Linear) ||
(value == ShapeFile.SpatialIndexCreation.Recursive)))
throw new ArgumentException("value");
_spatialIndexSpatialIndexCreationOption = value;
}
}
/// <summary>
/// Method to create a spatial index item
/// </summary>
/// <param name="oid">The object's identifier</param>
/// <param name="box">The extent</param>
/// <returns>A new spatial index item</returns>
public ISpatialIndexItem<uint> Create(uint oid, Envelope box)
{
return new QuadTree.BoxObjects {ID = oid, Box = box};
}
/// <summary>
/// Method to create a spatial index
/// </summary>
/// <param name="extent">The extent covered by the spatial index</param>
/// <param name="expectedNumberOfEntries"></param>
/// <param name="entries">The entries</param>
/// <returns>A spatial index</returns>
public ISpatialIndex<uint> Create(Envelope extent, int expectedNumberOfEntries,
IEnumerable<ISpatialIndexItem<uint>> entries)
{
switch (SpatialIndexCreationOption)
{
case ShapeFile.SpatialIndexCreation.Linear:
return CreateSpatialIndexLinear(extent, expectedNumberOfEntries, entries);
default:
return CreateSpatialIndexRecursive(extent, expectedNumberOfEntries, entries);
}
}
/// <summary>
/// Method to create a spatial index by loading it from file
/// </summary>
/// <param name="fileName">The filename of the spatial index</param>
/// <returns>The loaded index</returns>
public ISpatialIndex<uint> Load(string fileName)
{
var sidxFileName = fileName + ".sidx";
if (!File.Exists(sidxFileName))
return null;
try
{
var sw = new Stopwatch();
sw.Start();
var tree = QuadTree.FromFile(sidxFileName);
sw.Stop();
if (_logger.IsDebugEnabled)
_logger.DebugFormat("Loading QuadTree took {0}ms", sw.ElapsedMilliseconds);
return tree;
}
catch (QuadTree.ObsoleteFileFormatException)
{
File.Delete(sidxFileName);
return null;
}
catch (Exception ex)
{
_logger.Error(ex);
throw ex;
}
}
public string Extension
{
get { return ".shp.sidx"; }
}
/// <summary>
/// Generates a spatial index for a specified shape file.
/// </summary>
private static QuadTree CreateSpatialIndexLinear(Envelope extent, int expectedNumberOfEntries,
IEnumerable<ISpatialIndexItem<uint>> entries)
{
var sw = new Stopwatch();
sw.Start();
var root = QuadTree.CreateRootNode(extent);
var h = new Heuristic
{
maxdepth = (int) Math.Ceiling(Math.Log(expectedNumberOfEntries, 2)),
// These are not used for this approach
minerror = 10,
tartricnt = 5,
mintricnt = 2
};
uint i = 0;
foreach (var entry in entries)
{
//is the box valid?
if (!entry.Box.IsNull) continue;
root.AddNode((QuadTree.BoxObjects)entry, h);
i++;
}
sw.Stop();
if (_logger.IsDebugEnabled)
_logger.DebugFormat("Linear creation of QuadTree took {0}ms", sw.ElapsedMilliseconds);
return root;
}
/// <summary>
/// Generates a spatial index for a specified shape file.
/// </summary>
private static QuadTree CreateSpatialIndexRecursive(Envelope extent, int expectedNumberOfEntries,
IEnumerable<ISpatialIndexItem<uint>> entries)
{
var sw = new Stopwatch();
sw.Start();
var objList = new List<QuadTree.BoxObjects>();
foreach (var entry in entries)
{
if (entry.Box.IsNull) continue;
//var g = new QuadTree.BoxObjects { Box = box, ID = i };
objList.Add((QuadTree.BoxObjects)entry);
}
Heuristic heur;
heur.maxdepth = (int) Math.Ceiling(Math.Log(objList.Count, 2));
heur.minerror = 10;
heur.tartricnt = 5;
heur.mintricnt = 2;
var root = new QuadTree(objList, 0, heur);
sw.Stop();
if (_logger.IsDebugEnabled)
_logger.DebugFormat("Recursive creation of QuadTree took {0}ms", sw.ElapsedMilliseconds);
return root;
}
}
}