forked from SharpMap/SharpMap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileGdbProvider.cs
More file actions
482 lines (427 loc) · 17.3 KB
/
FileGdbProvider.cs
File metadata and controls
482 lines (427 loc) · 17.3 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
// Copyright 2012 - Felix Obermaier (www.ivv-aachen.de)
//
// This file is part of SharpMap.Data.Providers.FileGdb.
// SharpMap.Data.Providers.FileGdb 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.Data.Providers.FileGdb 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.Configuration;
using System.Data;
using System.Globalization;
using System.IO;
using GeoAPI.Geometries;
using SharpMap.Data.Providers.Converter;
using EsriGdb = Esri.FileGDB.Geodatabase;
using EsriTable = Esri.FileGDB.Table;
namespace SharpMap.Data.Providers
{
/// <summary>
/// Provider implementation for ESRI File Gdb database
/// </summary>
[Serializable]
public class FileGdbProvider : IProvider
{
static FileGdbProvider()
{
var asr = new AppSettingsReader();
var fileGdbPath = (string)asr.GetValue("FileGdbNativeDirectory", typeof(string));
//ensure directory
if (!Directory.Exists(fileGdbPath))
throw new DirectoryNotFoundException();
var path = Environment.GetEnvironmentVariable("PATH");
var pathSet = false;
if (!string.IsNullOrEmpty(path))
pathSet = path.ToLowerInvariant().Contains(fileGdbPath.ToLowerInvariant());
if (!pathSet)
{
path = fileGdbPath + ";" + path;
Environment.SetEnvironmentVariable("PATH", path);
}
}
[NonSerialized]
private EsriGdb _esriGdb;
private DirectoryInfo _esriGdbLocation;
private EsriGdb EsriGdbInstance
{
get
{
if (_esriGdbLocation == null)
throw new InvalidOperationException();
return _esriGdb ?? (_esriGdb = EsriGdb.Open(_esriGdbLocation.FullName));
}
}
[NonSerialized]
private EsriTable _esriTable;
/// <summary>
/// Creates an instance of this class
/// </summary>
public FileGdbProvider()
{
}
/// <summary>
/// Creates an instance of this class, assigning the location of the database
/// </summary>
/// <param name="path">A path to the directory containing the database files.</param>
public FileGdbProvider(string path)
{
_esriGdbLocation = new DirectoryInfo(path);
}
#region Implementation of IDisposable
/// <summary>
/// Führt anwendungsspezifische Aufgaben durch, die mit der Freigabe, der Zurückgabe oder dem Zurücksetzen von nicht verwalteten Ressourcen zusammenhängen.
/// </summary>
/// <filterpriority>2</filterpriority>
public void Dispose()
{
Dispose(true);
}
/// <summary>
/// Actual implementation for disposing the provider.
/// </summary>
/// <param name="disposing">A flag indicating if this function is called from <see cref="IDisposable.Dispose()"/> or the finalizer.</param>
protected virtual void Dispose(bool disposing)
{
if (IsDisposed)
return;
if (disposing)
{
if (_esriGdb != null)
_esriGdb.Dispose();
if (_esriTable != null)
_esriTable.Dispose();
IsDisposed = true;
GC.SuppressFinalize(this);
}
}
/// <summary>
/// Gets a value indicating if this provider has been disposed.
/// </summary>
public bool IsDisposed { get; private set; }
#endregion
#region Implementation of IProvider
/// <summary>
/// Gets the connection ID of the datasource
/// </summary>
/// <remarks>
/// <para>The ConnectionID should be unique to the datasource (for instance the filename or the
/// connectionstring), and is meant to be used for connection pooling.</para>
/// <para>If connection pooling doesn't apply to this datasource, the ConnectionID should return String.Empty</para>
/// </remarks>
public string ConnectionID
{
get { return _esriGdbLocation.FullName; }
set
{
if (_esriGdbLocation != null)
throw new InvalidOperationException("Only set 'ConnectionID' once!");
_esriGdbLocation = new DirectoryInfo(value);
}
}
private string _table;
/// <summary>
/// Gets or sets a value indicating the name of the data table.
/// </summary>
public string Table
{
get { return _table; }
set
{
if (!String.Equals(value, _table, StringComparison.InvariantCultureIgnoreCase))
{
var esriTable = EsriGdbInstance.OpenTable(value);
if (esriTable != null)
{
_esriTable = esriTable;
_table = value;
}
}
}
}
/// <summary>
/// Returns true if the datasource is currently open
/// </summary>
public bool IsOpen
{
get { return _esriTable != null; }
}
/// <summary>
/// The spatial reference ID (CRS)
/// </summary>
public int SRID
{
get;
set;
}
/// <summary>
/// Gets the features within the specified <see cref="Envelope"/>
/// </summary>
/// <param name="bbox"></param>
/// <returns>Features within the specified <see cref="Envelope"/></returns>
public Collection<IGeometry> GetGeometriesInView(Envelope bbox)
{
var res = new Collection<IGeometry>();
foreach (var row in _esriTable.Search("*", string.Empty, FileGdbGeometryConverter.ToEsriExtent(bbox), Esri.FileGDB.RowInstance.Recycle))
{
using (var buffer = row.GetGeometry())
{
var geom = FileGdbGeometryConverter.ToSharpMapGeometry(buffer);
if (geom != null)
res.Add(geom);
}
}
return res;
}
/// <summary>
/// Returns all objects whose <see cref="Envelope"/> intersects 'bbox'.
/// </summary>
/// <remarks>
/// This method is usually much faster than the QueryFeatures method, because intersection tests
/// are performed on objects simplified by their <see cref="Envelope"/>, and using the Spatial Index
/// </remarks>
/// <param name="bbox">Box that objects should intersect</param>
/// <returns></returns>
public Collection<uint> GetObjectIDsInView(Envelope bbox)
{
var res = new Collection<uint>();
foreach (var row in _esriTable.Search(string.Empty, string.Empty, FileGdbGeometryConverter.ToEsriExtent(bbox), Esri.FileGDB.RowInstance.Recycle))
res.Add((uint)row.GetOID());
return res;
}
/// <summary>
/// Returns the geometry corresponding to the Object ID
/// </summary>
/// <param name="oid">Object ID</param>
/// <returns>geometry</returns>
public IGeometry GetGeometryByID(uint oid)
{
var fdt = CreateTable(_esriTable);
var where = string.Format(CultureInfo.InvariantCulture, "{0}={1}", fdt.PrimaryKey[0].ColumnName, oid);
foreach (var row in _esriTable.Search("*", where, Esri.FileGDB.RowInstance.Recycle))
{
return FileGdbGeometryConverter.ToSharpMapGeometry(row.GetGeometry());
}
return null;
}
/// <summary>
/// Returns the data associated with all the geometries that are intersected by 'geom'
/// </summary>
/// <param name="geom">Geometry to intersect with</param>
/// <param name="ds">FeatureDataSet to fill data into</param>
public void ExecuteIntersectionQuery(IGeometry geom, FeatureDataSet ds)
{
ExecuteIntersectionQuery(geom.EnvelopeInternal, ds);
}
/// <summary>
/// Returns the data associated with all the geometries that are intersected by 'geom'
/// </summary>
/// <param name="box">Geometry to intersect with</param>
/// <param name="ds">FeatureDataSet to fill data into</param>
public void ExecuteIntersectionQuery(Envelope box, FeatureDataSet ds)
{
var fdt = CreateTable(_esriTable);
fdt.BeginLoadData();
var fi = _esriTable.FieldInformation;
foreach (var row in _esriTable.Search("*", string.Empty, FileGdbGeometryConverter.ToEsriExtent(box), Esri.FileGDB.RowInstance.Recycle))
{
var tmp = new List<object>(_esriTable.FieldInformation.Count - 1);
for (var i = 0; i < fi.Count; i++)
{
if (fi.GetFieldType(i) == Esri.FileGDB.FieldType.Geometry)
continue;
tmp.Add(row[fi.GetFieldName(i)]);
}
var fdr = (FeatureDataRow)fdt.LoadDataRow(tmp.ToArray(), true);
using (var buffer = row.GetGeometry())
fdr.Geometry = FileGdbGeometryConverter.ToSharpMapGeometry(buffer);
}
fdt.EndLoadData();
ds.Tables.Add(fdt);
}
/// <summary>
/// Returns the number of features in the dataset
/// </summary>
/// <returns>number of features</returns>
public int GetFeatureCount()
{
return _esriTable.RowCount;
}
/// <summary>
/// Returns a <see cref="SharpMap.Data.FeatureDataRow"/> based on a RowID
/// </summary>
/// <param name="rowId"></param>
/// <returns>datarow</returns>
public FeatureDataRow GetFeature(uint rowId)
{
var fdt = CreateTable(_esriTable);
fdt.BeginLoadData();
var where = string.Format(CultureInfo.InvariantCulture, "{0}={1}", fdt.PrimaryKey[0].ColumnName, rowId);
var fi = _esriTable.FieldInformation;
foreach (var row in _esriTable.Search("*", where, Esri.FileGDB.RowInstance.Recycle))
{
var tmp = new List<object>(_esriTable.FieldInformation.Count - 1);
for (var i = 0; i < fi.Count; i++)
{
if (fi.GetFieldType(i) == Esri.FileGDB.FieldType.Geometry)
continue;
tmp.Add(row[fi.GetFieldName(i)]);
}
var fdr = (FeatureDataRow)fdt.LoadDataRow(tmp.ToArray(), true);
fdr.Geometry = FileGdbGeometryConverter.ToSharpMapGeometry(row.GetGeometry());
}
fdt.EndLoadData();
return (FeatureDataRow)fdt.Rows[0];
}
private static FeatureDataTable CreateTable(EsriTable table)
{
var fdt = new FeatureDataTable();
var columns = fdt.Columns;
var fi = table.FieldInformation;
for (var i = 0; i < fi.Count; i++)
{
var ft = fi.GetFieldType(i);
Type netType;
var primaryKey = false;
switch (ft)
{
case Esri.FileGDB.FieldType.OID:
netType = typeof(int);
primaryKey = true;
break;
case Esri.FileGDB.FieldType.Single:
netType = typeof(float);
break;
case Esri.FileGDB.FieldType.XML:
netType = typeof(string);
break;
case Esri.FileGDB.FieldType.GlobalID:
netType = typeof(int);
break;
case Esri.FileGDB.FieldType.GUID:
netType = typeof(Guid);
break;
case Esri.FileGDB.FieldType.String:
netType = typeof(string);
break;
case Esri.FileGDB.FieldType.Double:
netType = typeof(double);
break;
case Esri.FileGDB.FieldType.Blob:
case Esri.FileGDB.FieldType.Raster:
netType = typeof(byte[]);
break;
case Esri.FileGDB.FieldType.Integer:
netType = typeof(long);
break;
case Esri.FileGDB.FieldType.SmallInteger:
netType = typeof(int);
break;
case Esri.FileGDB.FieldType.Date:
netType = typeof(DateTime);
break;
case Esri.FileGDB.FieldType.Geometry:
//Do not add geometry column
continue;
default:
throw new InvalidOperationException("Unknown field type" + ft);
}
var dc = new DataColumn(fi.GetFieldName(i), netType)
{
AllowDBNull = fi.GetFieldIsNullable(i),
//MaxLength = fi.GetFieldLength(i)
};
if (netType == typeof(string))
dc.MaxLength = fi.GetFieldLength(i);
columns.Add(dc);
if (primaryKey)
fdt.PrimaryKey = new[] { dc };
}
return fdt;
}
/// <summary>
/// <see cref="Envelope"/> of dataset
/// </summary>
/// <returns>Envelope</returns>
public Envelope GetExtents()
{
var extent = _esriTable.Extent;
return new Envelope(extent.xMin, extent.xMax, extent.yMin, extent.yMax);
}
/// <summary>
/// Opens the datasource
/// </summary>
public void Open()
{
//Nothing to do
}
/// <summary>
/// Closes the datasource
/// </summary>
public void Close()
{
//Nothing to do
}
/// <summary>
/// Gets an array of feature dataset names
/// </summary>
/// <param name="path">The root path (default = "\")</param>
/// <returns>An array of feature dataset names.</returns>
public string[] GetFeatureDatasets(string path = "\\")
{
var e = EsriGdbInstance;
return e.GetChildDatasets(path, "Feature Dataset");
}
/// <summary>
/// Gets an array of feature class names
/// </summary>
/// <param name="path">The root path (default = "\")</param>
/// <returns>An array of feature class names.</returns>
public string[] GetFeatureClasses(string path = "\\")
{
var e = EsriGdbInstance;
return e.GetChildDatasets(path, "Feature Class");
}
/// <summary>
/// Gets an array of table names
/// </summary>
/// <param name="path">The root path (default = "\")</param>
/// <returns>An array of table names.</returns>
public string[] GetTables(string path = "\\")
{
var e = EsriGdbInstance;
return e.GetChildDatasets(path, "Table");
}
#endregion
#if DEBUG
#pragma warning disable 1591
public IEnumerable<string> EnumerateTables(string path = "\\")
{
var e = EsriGdbInstance;
foreach (var table in e.GetChildDatasets(path, "Table"))
yield return "Table: " + table;
foreach (var table in e.GetChildDatasets(path, "Feature Class"))
yield return "Feature Class: " + table;
foreach (var fds in e.GetChildDatasets(path, "Feature Dataset"))
{
yield return "Feature Dataset: " + fds;
foreach (var fd in EnumerateTables(fds))
{
yield return fd;
}
}
}
#pragma warning restore 1591
#endif
}
}