forked from SharpMap/SharpMap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShapeFile.cs
More file actions
1500 lines (1325 loc) · 57.4 KB
/
ShapeFile.cs
File metadata and controls
1500 lines (1325 loc) · 57.4 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
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
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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 System.Runtime.CompilerServices;
using System.Text;
using GeoAPI;
using GeoAPI.Geometries;
using SharpMap.Utilities.Indexing;
using SharpMap.Utilities.SpatialIndexing;
using Common.Logging;
using GeoAPI.CoordinateSystems;
using SharpMap.CoordinateSystems;
using Exception = System.Exception;
namespace SharpMap.Data.Providers
{
/// <summary>
/// Shapefile dataprovider
/// </summary>
/// <remarks>
/// <para>The ShapeFile provider is used for accessing ESRI ShapeFiles. The ShapeFile should at least contain the
/// [filename].shp, [filename].idx, and if feature-data is to be used, also [filename].dbf file.</para>
/// <para>The first time the ShapeFile is accessed, SharpMap will automatically create a spatial index
/// of the shp-file, and save it as [filename].shp.sidx. If you change or update the contents of the .shp file,
/// delete the .sidx file to force SharpMap to rebuilt it. In web applications, the index will automatically
/// be cached to memory for faster access, so to reload the index, you will need to restart the web application
/// as well.</para>
/// <para>
/// M values in a shapefile are ignored by SharpMap.
/// </para>
/// </remarks>
/// <example>
/// Adding a datasource to a layer:
/// <code lang="C#">
/// SharpMap.Layers.VectorLayer myLayer = new SharpMap.Layers.VectorLayer("My layer");
/// myLayer.DataSource = new SharpMap.Data.Providers.ShapeFile(@"C:\data\MyShapeData.shp");
/// </code>
/// </example>
public class ShapeFile : FilterProvider, IProvider
{
readonly ILog _logger = LogManager.GetLogger(typeof(ShapeFile));
//#region Delegates
///// <summary>
///// Filter Delegate Method
///// </summary>
///// <remarks>
///// The FilterMethod delegate is used for applying a method that filters data from the dataset.
///// The method should return 'true' if the feature should be included and false if not.
///// <para>See the <see cref="FilterDelegate"/> property for more info</para>
///// </remarks>
///// <seealso cref="FilterDelegate"/>
///// <param name="dr"><see cref="SharpMap.Data.FeatureDataRow"/> to test on</param>
///// <returns>true if this feature should be included, false if it should be filtered</returns>
//public delegate bool FilterMethod(FeatureDataRow dr);
//#endregion
private ShapeFileHeader _header;
private ShapeFileIndex _index;
private ICoordinateSystem _coordinateSystem;
private bool _coordsysReadFromFile;
private bool _fileBasedIndex;
private string _filename;
private string _dbfFile;
private Encoding _dbfSpecifiedEncoding;
private int _srid = -1;
//private readonly object _shapeFileLock = new object();
private IGeometryFactory _factory;
private static int _memoryCacheLimit = 50000;
private static readonly object _gspLock = new object();
#if USE_MEMORYMAPPED_FILE
private static Dictionary<string,System.IO.MemoryMappedFiles.MemoryMappedFile> _memMappedFiles;
private static Dictionary<string, int> _memMappedFilesRefConter;
private bool _haveRegistredForUsage = false;
private bool _haveRegistredForShxUsage = false;
static ShapeFile()
{
_memMappedFiles = new Dictionary<string, System.IO.MemoryMappedFiles.MemoryMappedFile>();
_memMappedFilesRefConter = new Dictionary<string, int>();
SpatialIndexFactory = new QuadTreeFactory();
#pragma warning disable 618
SpatialIndexCreationOption = SpatialIndexCreation.Recursive;
#pragma warning restore 618
}
#else
static ShapeFile()
{
SpatialIndexFactory = new QuadTreeFactory();
#pragma warning disable 618
SpatialIndexCreationOption = SpatialIndexCreation.Recursive;
#pragma warning restore 618
}
#endif
private readonly bool _useMemoryCache;
private DateTime _lastCleanTimestamp = DateTime.Now;
private readonly TimeSpan _cacheExpireTimeout = TimeSpan.FromMinutes(1);
private readonly object _cacheLock = new object();
private readonly Dictionary <uint,FeatureDataRow> _cacheDataTable = new Dictionary<uint,FeatureDataRow>();
/// <summary>
/// Tree used for fast query of data
/// </summary>
private ISpatialIndex<uint> _tree;
/// <summary>
/// Initializes a ShapeFile DataProvider without a file-based spatial index.
/// </summary>
/// <param name="filename">Path to shape file</param>
public ShapeFile(string filename)
: this(filename, false)
{
}
/// <summary>
/// Initializes a ShapeFile DataProvider.
/// </summary>
/// <remarks>
/// <para>If FileBasedIndex is true, the spatial index will be read from a local copy. If it doesn't exist,
/// it will be generated and saved to [filename] + '.sidx'.</para>
/// <para>Using a file-based index is especially recommended for ASP.NET applications which will speed up
/// start-up time when the cache has been emptied.
/// </para>
/// </remarks>
/// <param name="filename">Path to shape file</param>
/// <param name="fileBasedIndex">Use file-based spatial index</param>
public ShapeFile(string filename, bool fileBasedIndex)
{
_filename = filename;
_fileBasedIndex = fileBasedIndex;
//Parse shape header
ParseHeader();
//Read projection file
ParseProjection();
//If no spatial index is wanted, just build a pseudo tree
if (!fileBasedIndex)
_tree = new AllFeaturesTree(_header.BoundingBox, (uint) _index.FeatureCount);
_dbfFile = Path.ChangeExtension(filename, ".dbf");
//Read projection file
ParseProjection();
//By default, don't enable _MemoryCache if there are a lot of features
_useMemoryCache = GetFeatureCount() <= MemoryCacheLimit;
}
/// <summary>
/// Initializes a ShapeFile DataProvider.
/// </summary>
/// <remarks>
/// <para>If FileBasedIndex is true, the spatial index will be read from a local copy. If it doesn't exist,
/// it will be generated and saved to [filename] + '.sidx'.</para>
/// <para>Using a file-based index is especially recommended for ASP.NET applications which will speed up
/// start-up time when the cache has been emptied.
/// </para>
/// </remarks>
/// <param name="filename">Path to shape file</param>
/// <param name="fileBasedIndex">Use file-based spatial index</param>
/// <param name="useMemoryCache">Use the memory cache. BEWARE in case of large shapefiles</param>
public ShapeFile(string filename, bool fileBasedIndex, bool useMemoryCache)
: this(filename, fileBasedIndex,useMemoryCache,0)
{
}
/// <summary>
/// Initializes a ShapeFile DataProvider.
/// </summary>
/// <remarks>
/// <para>If FileBasedIndex is true, the spatial index will be read from a local copy. If it doesn't exist,
/// it will be generated and saved to [filename] + '.sidx'.</para>
/// <para>Using a file-based index is especially recommended for ASP.NET applications which will speed up
/// start-up time when the cache has been emptied.
/// </para>
/// </remarks>
/// <param name="filename">Path to shape file</param>
/// <param name="fileBasedIndex">Use file-based spatial index</param>
/// <param name="useMemoryCache">Use the memory cache. BEWARE in case of large shapefiles</param>
/// <param name="srid">The spatial reference id</param>
public ShapeFile(string filename, bool fileBasedIndex, bool useMemoryCache,int srid) : this(filename, fileBasedIndex)
{
_useMemoryCache = useMemoryCache;
SRID=srid;
}
/// <summary>
/// Cleans the internal memory cached, expurging the objects that are not in the viewarea anymore
/// </summary>
/// <param name="objectlist">OID of the objects in the current viewarea</param>
private void CleanInternalCache(Collection<uint> objectlist)
{
if (!_useMemoryCache)
{
return;
}
lock (_cacheLock)
{
//Only execute this if the memorycache is active and the expiretimespan has timed out
if (DateTime.Now.Subtract(_lastCleanTimestamp) > _cacheExpireTimeout)
{
var notIntersectOid = new Collection<uint>();
//identify the not intersected oid
foreach (var oid in _cacheDataTable.Keys)
{
if (!objectlist.Contains(oid))
{
notIntersectOid.Add(oid);
}
}
//Clean the cache
foreach (uint oid in notIntersectOid)
{
_cacheDataTable.Remove(oid);
}
//Reset the lastclean timestamp
_lastCleanTimestamp = DateTime.Now;
}
}
}
/// <summary>
/// Gets or sets a value indicating how many features are allowed for memory cache approach
/// </summary>
protected static int MemoryCacheLimit
{
get { return _memoryCacheLimit; }
set { _memoryCacheLimit = value; }
}
//private void ClearingOfCachedDataRequired(object sender, EventArgs e)
//{
// if (_useMemoryCache)
// lock (_cacheLock)
// {
// _cacheDataTable.Clear();
// }
//}
/// <summary>
/// Gets or sets the coordinate system of the ShapeFile. If a shapefile has
/// a corresponding [filename].prj file containing a Well-Known Text
/// description of the coordinate system this will automatically be read.
/// If this is not the case, the coordinate system will default to null.
/// </summary>
/// <exception cref="ApplicationException">An exception is thrown if the coordinate system is read from file.</exception>
public ICoordinateSystem CoordinateSystem
{
get { return _coordinateSystem; }
set
{
if (_coordsysReadFromFile)
throw new ApplicationException("Coordinate system is specified in projection file and is read only");
_coordinateSystem = value;
}
}
/// <summary>
/// Gets the <see cref="SharpMap.Data.Providers.ShapeType">shape geometry type</see> in this shapefile.
/// </summary>
/// <remarks>
/// The property isn't set until the first time the datasource has been opened,
/// and will throw an exception if this property has been called since initialization.
/// <para>All the non-Null shapes in a shapefile are required to be of the same shape
/// type.</para>
/// </remarks>
public ShapeType ShapeType
{
get { return _header.ShapeType; }
}
/// <summary>
/// Gets or sets the filename of the shapefile
/// </summary>
/// <remarks>If the filename changes, indexes will be rebuilt</remarks>
public string Filename
{
get { return _filename; }
set
{
if (value == _filename)
return;
if (string.IsNullOrEmpty(value))
throw new ArgumentNullException("value");
if (IsOpen)
Close();
_filename = value;
_fileBasedIndex = (_fileBasedIndex) && File.Exists(Path.ChangeExtension(value, SpatialIndexFactory.Extension));
_dbfFile = Path.ChangeExtension(value, ".dbf");
ParseHeader();
ParseProjection();
_tree = null;
}
}
/// <summary>
/// Gets or sets the encoding used for parsing strings from the DBase DBF file.
/// </summary>
/// <remarks>
/// The DBase default encoding is <see cref="System.Text.Encoding.UTF8"/>.
/// </remarks>
public Encoding Encoding
{
get
{
if (_dbfSpecifiedEncoding != null)
return _dbfSpecifiedEncoding;
using (var dbf = OpenDbfStream())
return dbf.Encoding;
}
set
{
_dbfSpecifiedEncoding = value;
}
}
#region Disposers and finalizers
private bool _disposed;
private static ISpatialIndexFactory<uint> _spatialIndexFactory = new QuadTreeFactory();
/// <summary>
/// Disposes the object
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
Close();
if (_tree != null)
{
bool disposeTree = true;
// If we are in a web-context we might not be entitled to dispose the spatial index!
if (Web.HttpCacheUtility.IsWebContext)
{
if (Web.HttpCacheUtility.TryGetValue(_filename, out ISpatialIndex<uint> tree))
disposeTree = !ReferenceEquals(tree, _tree);
}
if (disposeTree && _tree is IDisposable disposableTree)
disposableTree.Dispose();
_tree = null;
}
#if USE_MEMORYMAPPED_FILE
if (_memMappedFilesRefConter.ContainsKey(_filename))
{
_memMappedFilesRefConter[_filename]--;
if (_memMappedFilesRefConter[_filename] == 0)
{
_memMappedFiles[_filename].Dispose();
_memMappedFiles.Remove(_filename);
_memMappedFilesRefConter.Remove(_filename);
}
}
string shxFile = Path.ChangeExtension(_filename,".shx");
if (_memMappedFilesRefConter.ContainsKey(shxFile))
{
_memMappedFilesRefConter[shxFile]--;
if (_memMappedFilesRefConter[shxFile] <= 0)
{
_memMappedFiles[shxFile].Dispose();
_memMappedFilesRefConter.Remove(shxFile);
_memMappedFiles.Remove(shxFile);
}
}
#endif
}
_disposed = true;
}
}
/// <summary>
/// Finalizes the object
/// </summary>
~ShapeFile()
{
Dispose();
}
#endregion
#region IProvider Members
private Stream OpenShapefileStream()
{
Stream s;
#if USE_MEMORYMAPPED_FILE
s = CheckCreateMemoryMappedStream(_filename, ref _haveRegistredForUsage);
#else
s = new FileStream(_filename, FileMode.Open, FileAccess.Read);
#endif
return s;
}
private DbaseReader OpenDbfStream()
{
DbaseReader dbfFile = null;
if (File.Exists(_dbfFile))
{
dbfFile = new DbaseReader(_dbfFile);
if (_dbfSpecifiedEncoding != null)
dbfFile.Encoding = _dbfSpecifiedEncoding;
dbfFile.IncludeOid = IncludeOid;
dbfFile.Open();
}
return dbfFile;
}
/// <summary>
/// Gets or sets a value indicating whether the object's id
/// should be included in attribute data or not.
/// <para>The default value is <c>false</c></para>
/// </summary>
public bool IncludeOid { get; set; }
/// <summary>
/// Opens the datasource
/// </summary>
public void Open()
{
if (!File.Exists(_filename))
throw new FileNotFoundException(String.Format("Could not find file \"{0}\"", _filename));
if (!_filename.ToLower().EndsWith(".shp"))
throw (new Exception("Invalid shapefile filename: " + _filename));
//Load SpatialIndexIfNotLoaded
if (_tree == null)
{
if (_fileBasedIndex)
LoadSpatialIndex(false);
else
_tree = new AllFeaturesTree(_header.BoundingBox, (uint)_index.FeatureCount);
//using (Stream s = OpenShapefileStream())
//{
// s.Close();
//}
}
// // TODO:
// // Get a Connector. The connector returned is guaranteed to be connected and ready to go.
// // Pooling.Connector connector = Pooling.ConnectorPool.ConnectorPoolManager.RequestConnector(this,true);
// // if (!_isOpen )
// // {
//// if (File.Exists(shxFile))
//// {
////#if USE_MEMORYMAPPED_FILE
//// _fsShapeIndex = CheckCreateMemoryMappedStream(shxFile, ref _haveRegistredForShxUsage);
////#else
//// _fsShapeIndex = new FileStream(shxFile, FileMode.Open, FileAccess.Read);
////#endif
//// _brShapeIndex = new BinaryReader(_fsShapeIndex, Encoding.Unicode);
//// }
//#if USE_MEMORYMAPPED_FILE
// _fsShapeFile = CheckCreateMemoryMappedStream(_filename, ref _haveRegistredForUsage);
//#else
// _fsShapeFile = new FileStream(_filename, FileMode.Open, FileAccess.Read);
//#endif
// //_brShapeFile = new BinaryReader(_fsShapeFile);
// //// Create array to hold the index array for this open session
// ////_offsetOfRecord = new int[_featureCount];
// //_offsetOfRecord = new ShapeFileIndexEntry[_featureCount];
// //PopulateIndexes(shxFile);
// InitializeShape(_filename, _fileBasedIndex);
// if (DbaseFile != null)
// DbaseFile.Open();
// _isOpen = true;
// }
}
#if USE_MEMORYMAPPED_FILE
private Stream CheckCreateMemoryMappedStream(string filename, ref bool haveRegistredForUsage)
{
if (!_memMappedFiles.ContainsKey(filename))
{
System.IO.MemoryMappedFiles.MemoryMappedFile memMappedFile = System.IO.MemoryMappedFiles.MemoryMappedFile.CreateFromFile(filename, FileMode.Open);
_memMappedFiles.Add(filename, memMappedFile);
}
if (!haveRegistredForUsage)
{
if (_memMappedFilesRefConter.ContainsKey(filename))
_memMappedFilesRefConter[filename]++;
else
_memMappedFilesRefConter.Add(filename, 1);
haveRegistredForUsage = true;
}
return _memMappedFiles[filename].CreateViewStream();
}
#endif
/// <summary>
/// Closes the datasource
/// </summary>
public void Close()
{ }
/// <summary>
/// Returns true if the datasource is currently open
/// </summary>
public bool IsOpen
{
get { return false; }
}
/// <summary>
/// Returns geometries whose bounding box intersects 'bbox'
/// </summary>
/// <remarks>
/// <para>Please note that this method doesn't guarantee that the geometries returned actually intersect 'bbox', but only
/// that their boundingbox intersects 'bbox'.</para>
/// <para>This method is much faster than the QueryFeatures method, because intersection tests
/// are performed on objects simplified by their boundingbox, and using the Spatial Index.</para>
/// </remarks>
/// <param name="bbox"></param>
/// <returns></returns>
public Collection<IGeometry> GetGeometriesInView(Envelope bbox)
{
//Use the spatial index to get a list of features whose boundingbox intersects bbox
var objectlist = GetObjectIDsInView(bbox);
if (objectlist.Count == 0) //no features found. Return an empty set
return new Collection<IGeometry>();
if (FilterDelegate != null)
return GetGeometriesInViewWithFilter(objectlist);
return GetGeometriesInViewWithoutFilter(objectlist);
}
private Collection<IGeometry> GetGeometriesInViewWithFilter(Collection<uint> oids)
{
Collection<IGeometry> result = null;
using (Stream s = OpenShapefileStream())
{
using (BinaryReader br = new BinaryReader(s))
{
using (DbaseReader DbaseFile = OpenDbfStream())
{
result = new Collection<IGeometry>();
var table = DbaseFile.NewTable;
var tmpOids = new Collection<uint>();
foreach (var oid in oids)
{
var fdr = getFeature(oid, table, br, DbaseFile);
if (!FilterDelegate(fdr)) continue;
result.Add(fdr.Geometry);
tmpOids.Add(oid);
}
CleanInternalCache(tmpOids);
DbaseFile.Close();
}
br.Close();
}
s.Close();
}
return result;
}
private Collection<IGeometry> GetGeometriesInViewWithoutFilter(Collection<uint> oids)
{
var result = new Collection<IGeometry>();
using (var s = OpenShapefileStream())
{
using (var br = new BinaryReader(s))
{
using (var dbf = OpenDbfStream())
{
foreach (var oid in oids)
{
result.Add(GetGeometryByID(oid, br, dbf));
}
dbf.Close();
}
br.Close();
}
s.Close();
}
CleanInternalCache(oids);
return result;
}
/// <summary>
/// Returns all objects whose boundingbox intersects bbox.
/// </summary>
/// <remarks>
/// <para>
/// Please note that this method doesn't guarantee that the geometries returned actually intersect 'bbox', but only
/// that their boundingbox intersects 'bbox'.
/// </para>
/// </remarks>
/// <param name="bbox"></param>
/// <param name="ds"></param>
/// <returns></returns>
public void ExecuteIntersectionQuery(Envelope bbox, FeatureDataSet ds)
{
// Do true intersection query
if (DoTrueIntersectionQuery)
{
ExecuteIntersectionQuery(Factory.ToGeometry(bbox), ds);
return;
}
//Use the spatial index to get a list of features whose boundingbox intersects bbox
var objectlist = GetObjectIDsInView(bbox);
using (BinaryReader br = new BinaryReader(OpenShapefileStream()))
{
using (DbaseReader dbaseFile = OpenDbfStream())
{
var dt = dbaseFile.NewTable;
dt.BeginLoadData();
for (var i = 0; i < objectlist.Count; i++)
{
FeatureDataRow fdr;
fdr = (FeatureDataRow)dt.LoadDataRow(dbaseFile.GetValues(objectlist[i]), true);
fdr.Geometry = ReadGeometry(objectlist[i], br, dbaseFile);
//Test if the feature data row corresponds to the FilterDelegate
if (FilterDelegate != null && !FilterDelegate(fdr))
fdr.Delete();
}
dt.EndLoadData();
dt.AcceptChanges();
ds.Tables.Add(dt);
dbaseFile.Close();
}
br.Close();
}
CleanInternalCache(objectlist);
}
/// <summary>
/// Returns geometry Object IDs whose bounding box intersects 'bbox'
/// </summary>
/// <param name="bbox"></param>
/// <returns></returns>
public Collection<uint> GetObjectIDsInView(Envelope bbox)
{
if (!_tree.Box.Intersects(bbox))
return new Collection<uint>();
var needBBoxFiltering = _tree is AllFeaturesTree && !bbox.Contains(_tree.Box);
//Use the spatial index to get a list of features whose boundingbox intersects bbox
var res = _tree.Search(bbox);
if (needBBoxFiltering)
{
var tmp = new Collection<uint>();
using (var dbr = OpenDbfStream())
using (var sbr = new BinaryReader(OpenShapefileStream()))
{
foreach (var oid in res)
{
var geom = ReadGeometry(oid, sbr, dbr);
if (geom != null && bbox.Intersects(geom.EnvelopeInternal)) tmp.Add(oid);
}
}
res = tmp;
}
/*Sort oids so we get a forward only read of the shapefile*/
var ret = new List<uint>(res);
ret.Sort();
return new Collection<uint>(ret);
}
/// <summary>
/// Returns the geometry corresponding to the Object ID
/// </summary>
/// <remarks>FilterDelegate is no longer applied to this ge</remarks>
/// <param name="oid">Object ID</param>
/// <returns>The geometry at the Id</returns>
public IGeometry GetGeometryByID(uint oid)
{
IGeometry geom;
using (Stream s = OpenShapefileStream())
{
using (BinaryReader br = new BinaryReader(s))
{
using (DbaseReader dbf = OpenDbfStream())
{
geom = ReadGeometry(oid, br, dbf);
dbf.Close();
}
br.Close();
}
s.Close();
}
return geom;
}
/// <summary>
/// Returns the geometry corresponding to the Object ID
/// </summary>
/// <remarks>FilterDelegate is no longer applied to this ge</remarks>
/// <param name="oid">Object ID</param>
/// <param name="br">The binary reader for reading</param>
/// <param name="dbf">The dBase reader</param>
/// <returns>The geometry at the Id</returns>
private IGeometry GetGeometryByID(uint oid, BinaryReader br, DbaseReader dbf)
{
if (_useMemoryCache)
{
FeatureDataRow fdr;
lock (_cacheLock)
{
_cacheDataTable.TryGetValue(oid, out fdr);
}
if (fdr == null)
{
fdr = getFeature(oid, dbf.NewTable, br, dbf);
}
return fdr.Geometry;
}
IGeometry geom = ReadGeometry(oid, br, dbf);
return geom;
}
/// <summary>
/// Gets or sets a value indicating that for <see cref="ExecuteIntersectionQuery(GeoAPI.Geometries.Envelope,SharpMap.Data.FeatureDataSet)"/> the intersection of the geometries and the envelope should be tested.
/// </summary>
public bool DoTrueIntersectionQuery { get; set; }
/// <summary>
/// Gets or sets a value indicating that the provider should check if geometry belongs to a deleted record.
/// </summary>
/// <remarks>This really slows rendering performance down</remarks>
public bool CheckIfRecordIsDeleted { get; set; }
/// <summary>
/// Returns the data associated with all the geometries that are intersected by <paramref name="geom"/>.
/// </summary>
/// <param name="geom">The geometry to test intersection for</param>
/// <param name="ds">FeatureDataSet to fill data into</param>
public virtual void ExecuteIntersectionQuery(IGeometry geom, FeatureDataSet ds)
{
var bbox = new Envelope(geom.EnvelopeInternal);
//Get a list of objects that possibly intersect with geom.
var objectlist = GetObjectIDsInView(bbox);
//Get a prepared geometry object
var prepGeom = NetTopologySuite.Geometries.Prepared.PreparedGeometryFactory.Prepare(geom);
using (Stream s = OpenShapefileStream())
{
using (BinaryReader br = new BinaryReader(s))
{
using (DbaseReader DbaseFile = OpenDbfStream())
{
//Get an empty table
var dt = DbaseFile.NewTable;
dt.BeginLoadData();
var tmpOids = new Collection<uint>();
//Cycle through all object ids
foreach (var oid in objectlist)
{
//Get the geometry
var testGeom = ReadGeometry(oid, br, DbaseFile);
//We do not have a geometry => we do not have a feature
if (testGeom == null)
continue;
//Does the geometry really intersect with geom?
if (!prepGeom.Intersects(testGeom))
continue;
//Get the feature data row and assign the geometry
FeatureDataRow fdr;
fdr = (FeatureDataRow)dt.LoadDataRow(DbaseFile.GetValues(oid), true);
fdr.Geometry = testGeom;
//Test if the feature data row corresponds to the FilterDelegate
if (FilterDelegate != null && !FilterDelegate(fdr))
fdr.Delete();
else
tmpOids.Add(oid);
}
dt.EndLoadData();
dt.AcceptChanges();
ds.Tables.Add(dt);
DbaseFile.Close();
CleanInternalCache(tmpOids);
}
br.Close();
}
s.Close();
}
}
/// <summary>
/// Returns the total number of features in the datasource (without any filter applied)
/// </summary>
/// <returns></returns>
public int GetFeatureCount()
{
return _index.FeatureCount;
}
/// <summary>
/// Returns the extents of the datasource
/// </summary>
/// <returns></returns>
public Envelope GetExtents()
{
if (_tree == null)
return _header.BoundingBox;
/*
throw new ApplicationException(
"File hasn't been spatially indexed. Try opening the datasource before retriving extents");
*/
return _tree.Box;
}
/// <summary>
/// Gets the connection ID of the datasource
/// </summary>
/// <remarks>
/// The connection ID of a shapefile is its filename
/// </remarks>
public string ConnectionID
{
get { return _filename; }
}
/// <summary>
/// Gets or sets the spatial reference ID (CRS)
/// </summary>
public virtual int SRID
{
get { return _srid; }
set
{
_srid = value;
lock (_gspLock)
Factory = GeometryServiceProvider.Instance.CreateGeometryFactory(value);
}
}
#endregion
/// <summary>
/// Reads and parses the header of the .shp index file
/// </summary>
private void ParseHeader()
{
_header = ShapeFileHeader.Read(_filename);
var shxPath = Path.ChangeExtension(_filename, ".shx");
if (!File.Exists(shxPath))
ShapeFileIndex.Create(_filename);
_index = ShapeFileIndex.Read(shxPath);
}
/// <summary>
/// Reads and parses the projection if a projection file exists
/// </summary>
private void ParseProjection()
{
var projfile = Path.ChangeExtension(_filename, ".prj");
if (File.Exists(projfile))
{
try
{
var wkt = File.ReadAllText(projfile);
var css = (CoordinateSystemServices) Session.Instance.CoordinateSystemServices;
_coordinateSystem = css.CreateCoordinateSystem(wkt);
SRID = (int)_coordinateSystem.AuthorityCode;
_coordsysReadFromFile = true;
}
catch (Exception ex)
{
Trace.TraceWarning("Coordinate system file '" + projfile +
"' found, but could not be parsed. WKT parser returned:" + ex.Message);
throw;
}
}
else
{
if (_coordinateSystem == null)
SRID = 0;
else
{
SRID = (int) _coordinateSystem.AuthorityCode;
}
}
}
///// <summary>
///// If an index file is present (.shx) it reads the record offsets from the .shx index file and returns the information in an array.
///// IfF an indexd array is not present it works out the indexes from the data file, by going through the record headers, finding the
///// data lengths and workingout the offsets. Which ever method is used a array of index is populated to be use by the other methods.
///// This array is created when the open method is called, and removed when the close method called.
///// </summary>
//private void PopulateIndexes(string shxFile)
//{
// if (File.Exists(shxFile))
// {
// using (var brShapeIndex = new BinaryReader(File.OpenRead(shxFile)))
// {
// brShapeIndex.BaseStream.Seek(100, 0); //skip the header
// for (int x = 0; x < _featureCount; ++x)
// {
// _offsetOfRecord[x] = new ShapeFileIndexEntry(brShapeIndex);
// //_offsetOfRecord[x] = 2 * SwapByteOrder(brShapeIndex.ReadInt32()); //Read shape data position // ibuffer);
// //brShapeIndex.BaseStream.Seek(brShapeIndex.BaseStream.Position + 4, 0); //Skip content length
// }
// }
// }
// //if (_brShapeIndex != null)
// //{
// // _brShapeIndex.BaseStream.Seek(100, 0); //skip the header
// // for (int x = 0; x < _featureCount; ++x)
// // {
// // _offsetOfRecord[x] = 2 * SwapByteOrder(_brShapeIndex.ReadInt32()); //Read shape data position // ibuffer);
// // _brShapeIndex.BaseStream.Seek(_brShapeIndex.BaseStream.Position + 4, 0); //Skip content length
// // }
// //}
// else
// {
// // we need to create an index from the shape file
// // Record the current position pointer for later
// var oldPosition = _brShapeFile.BaseStream.Position;
// // Move to the start of the data
// _brShapeFile.BaseStream.Seek(100, 0); //Skip content length
// long offset = 100; // Start of the data records