forked from SharpMap/SharpMap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeometryFeatureProvider.cs
More file actions
479 lines (433 loc) · 17.4 KB
/
GeometryFeatureProvider.cs
File metadata and controls
479 lines (433 loc) · 17.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
// Copyright 2006 - Morten Nielsen (www.iter.dk)
// Copyright 2007 - Christian Gräfe (www.sharptools.de)
//
// 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.Data;
using GeoAPI.Geometries;
using System.ComponentModel;
namespace SharpMap.Data.Providers
{
/// <summary>
/// Datasource for storing a limited set of geometries.
/// </summary>
/// <remarks>
/// <para>The GeometryProvider doesn’t utilize performance optimizations of spatial indexing,
/// and thus is primarily meant for rendering a limited set of Geometries.</para>
/// <para>A common use of the GeometryProvider is for highlighting a set of selected features.</para>
/// <example>
/// The following example gets data within a BoundingBox of another datasource and adds it to the map.
/// <code lang="C#">
/// List<Geometry> geometries = myMap.Layers[0].DataSource.GetGeometriesInView(myBox);
/// VectorLayer laySelected = new VectorLayer("Selected Features");
/// laySelected.DataSource = new GeometryFeatureProvider(geometries);
/// laySelected.Style.Outline = new Pen(Color.Magenta, 3f);
/// laySelected.Style.EnableOutline = true;
/// myMap.Layers.Add(laySelected);
/// </code>
/// </example>
/// <example>
/// Adding points of interest to the map. This is useful for vehicle tracking etc.
/// <code lang="C#">
/// GeoAPI.Geometries.IGeometryFactory gf = new NetTopologySuite.Geometries.GeometryFactory();
/// List<GeoAPI.Geometries.IGeometry> geometries = new List<GeoAPI.Geometries.IGeometry>();
/// //Add two points
/// geometries.Add(gf.CreatePoint(23.345,64.325));
/// geometries.Add(gf.CreatePoint(23.879,64.194));
/// SharpMap.Layers.VectorLayer layerVehicles = new SharpMap.Layers.VectorLayer("Vehicles");
/// layerVehicles.DataSource = new SharpMap.Data.Providers.GeometryFeatureProvider(geometries);
/// layerVehicles.Style.Symbol = Bitmap.FromFile(@"C:\data\car.gif");
/// myMap.Layers.Add(layerVehicles);
/// </code>
/// </example>
/// </remarks>
public class GeometryFeatureProvider : FilterProvider, IProvider
{
private readonly FeatureDataTable _features;
private int _srid = -1;
private int _oid = -1; // primary key index from fdt schema or subsequently added unique constraint
#region constructors
/// <summary>
/// Initializes a new instance of the <see cref="GeometryProvider"/>
/// </summary>
/// <param name="geometries">Set of geometries that this datasource should contain</param>
public GeometryFeatureProvider(IEnumerable<IGeometry> geometries)
{
_features = new FeatureDataTable();
_features.BeginLoadData();
foreach (var geom in geometries)
{
var fdr = _features.NewRow();
fdr.Geometry = geom;
_features.AddRow(fdr);
}
_features.AcceptChanges();
_features.EndLoadData();
_features.TableCleared += HandleFeaturesCleared;
_features.Constraints.CollectionChanged += HandleConstraintsCollectionChanged;
if (_features.Count > 0 && _features[0].Geometry != null)
SRID = _features[0].Geometry.SRID;
}
/// <summary>
/// Initializes a new instance of the <see cref="GeometryProvider"/>
/// </summary>
/// <param name="features">Features to be included in this datasource</param>
public GeometryFeatureProvider(FeatureDataTable features)
{
_features = features;
_features.TableCleared += HandleFeaturesCleared;
_features.Constraints.CollectionChanged += HandleConstraintsCollectionChanged;
if (_features != null && _features.Count > 0)
if (_features[0].Geometry != null)
SRID = _features[0].Geometry.SRID;
// eg ShapeFile datasource with IncludeOid = true
if (features.PrimaryKey.Length == 1)
_oid = ValidateOidDataType(features.PrimaryKey[0]);
}
/// <summary>
/// Initializes a new instance of the <see cref="GeometryProvider"/>
/// </summary>
/// <param name="geometry">Geometry to be in this datasource</param>
public GeometryFeatureProvider(IGeometry geometry)
{
_features = new FeatureDataTable();
var fdr = _features.NewRow();
fdr.Geometry = geometry;
if (geometry != null)
SRID = geometry.SRID;
_features.AddRow(fdr);
_features.AcceptChanges();
_features.TableCleared += HandleFeaturesCleared;
_features.Constraints.CollectionChanged += HandleConstraintsCollectionChanged;
}
#endregion
private void HandleFeaturesCleared(object sender, DataTableClearEventArgs e)
{
//maybe clear extents, reset SRID
//ok, maybe not
}
private void HandleConstraintsCollectionChanged(object sender, CollectionChangeEventArgs e)
{
// rare cases when PK is explicitly added/removed subsequent to constructor
if (e.Element is UniqueConstraint)
{
UniqueConstraint uc;
uc = (UniqueConstraint)e.Element;
// Ensure we are dealing with PK....
// IsPrimaryKey only True for REMOVE or REFRESH. When ADDing new PK constraint
// IsPrimaryKey is False until after constraint applied, so for this case confirm
// no existing PK and check constraint for single col, !AllowDBNull and Unique
if ((uc.IsPrimaryKey && _features.PrimaryKey.Length == 1) ||
(_features.PrimaryKey.Length == 0 && uc.Columns.Length == 1 &&
!uc.Columns[0].AllowDBNull && uc.Columns[0].Unique))
{
if (e.Action == CollectionChangeAction.Remove)
_oid = -1;
else
_oid = ValidateOidDataType(uc.Columns[0]);
}
}
}
private int ValidateOidDataType(DataColumn pk)
{
switch (Type.GetTypeCode(pk.DataType))
{
case TypeCode.UInt16:
case TypeCode.UInt32:
case TypeCode.UInt64:
case TypeCode.Int16:
case TypeCode.Int32:
case TypeCode.Int64:
return pk.Ordinal;
default:
return -1;
}
}
/// <summary>
/// Access to underlying <see cref="FeatureDataTable"/>
/// </summary>
public FeatureDataTable Features
{
get
{
lock (_features.Rows.SyncRoot)
{
return _features;
}
}
}
#region IProvider Members
/// <summary>
/// Returns features within the specified bounding box
/// </summary>
/// <param name="bbox"></param>
/// <returns></returns>
public Collection<IGeometry> GetGeometriesInView(Envelope bbox)
{
var list = new Collection<IGeometry>();
lock (_features.Rows.SyncRoot)
{
foreach (FeatureDataRow fdr in _features.Rows)
if (fdr.Geometry != null && !fdr.Geometry.IsEmpty)
if (FilterDelegate == null || FilterDelegate(fdr))
{
if (bbox.Intersects(fdr.Geometry.EnvelopeInternal))
list.Add(fdr.Geometry);
}
}
return list;
}
private IEnumerable<KeyValuePair<uint, FeatureDataRow>> EnumerateFeatures(Envelope bbox)
{
lock (_features.Rows.SyncRoot)
{
uint id = 0;
foreach (FeatureDataRow feature in _features.Rows)
{
var geom = feature.Geometry;
if (geom != null && !geom.IsEmpty)
{
if (bbox.Intersects(geom.EnvelopeInternal) && (FilterDelegate == null || FilterDelegate(feature)))
{
if (_oid == -1)
{
yield return new KeyValuePair<uint, FeatureDataRow>(id, feature);
id++;
}
else
{
yield return new KeyValuePair<uint, FeatureDataRow>(Convert.ToUInt32(feature[_oid]), feature);
}
}
}
}
}
}
/// <summary>
/// Returns all objects whose boundingbox intersects 'bbox'.
/// </summary>
/// <param name="bbox"></param>
/// <returns></returns>
public Collection<uint> GetObjectIDsInView(Envelope bbox)
{
var list = new Collection<uint>();
foreach (var idFeature in EnumerateFeatures(bbox))
list.Add(idFeature.Key);
return list;
}
/// <summary>
/// Returns the geometry corresponding to the Object ID
/// </summary>
/// <param name="oid">Object ID</param>
/// <returns>geometry</returns>
public IGeometry GetGeometryByID(uint oid)
{
lock (_features.Rows.SyncRoot)
{
if (_oid == -1)
{
if (oid >= _features.Rows.Count)
return null;
else
return ((FeatureDataRow)_features.Rows[(int)oid]).Geometry;
}
else
{
var dr = _features.Rows.Find(oid);
if (dr != null)
return ((FeatureDataRow)dr).Geometry;
else
return null;
}
}
}
/// <summary>
/// Add datatable to dataset and populate with intersecting features (perform bounding box intersect followed by geom intersect)
/// </summary>
/// <param name="geom"></param>
/// <param name="ds">FeatureDataSet to fill data into</param>
public void ExecuteIntersectionQuery(IGeometry geom, FeatureDataSet ds)
{
FeatureDataTable fdt;
lock (_features.Columns.SyncRoot)
fdt = _features.Clone();
fdt.BeginLoadData();
var pg = new NetTopologySuite.Geometries.Prepared.PreparedGeometryFactory().Create(geom);
foreach (var idFeature in EnumerateFeatures(geom.EnvelopeInternal))
{
var fdr = idFeature.Value;
if (pg.Intersects(fdr.Geometry))
{
fdt.LoadDataRow(fdr.ItemArray, true);
var tmpGeom = fdr.Geometry;
if (tmpGeom != null)
((FeatureDataRow)fdt.Rows[fdt.Rows.Count - 1]).Geometry = (IGeometry)tmpGeom.Clone();
}
}
fdt.EndLoadData();
ds.Tables.Add(fdt);
}
/// <summary>
/// Add datatable to dataset and populate with interesecting features
/// </summary>
/// <param name="box"></param>
/// <param name="ds">FeatureDataSet to fill data into</param>
public void ExecuteIntersectionQuery(Envelope box, FeatureDataSet ds)
{
FeatureDataTable fdt;
lock (_features.Columns.SyncRoot)
fdt = _features.Clone();
fdt.BeginLoadData();
foreach (var idFeature in EnumerateFeatures(box))
{
var fdr = idFeature.Value;
fdt.LoadDataRow(fdr.ItemArray, false);
var geom = fdr.Geometry;
if (geom != null)
((FeatureDataRow)fdt.Rows[fdt.Rows.Count - 1]).Geometry = (IGeometry)geom.Clone();
}
fdt.AcceptChanges();
fdt.EndLoadData();
ds.Tables.Add(fdt);
}
/// <summary>
/// Returns the number of features in the dataset
/// </summary>
/// <returns>number of features</returns>
public int GetFeatureCount()
{
lock (_features.Rows.SyncRoot)
return _features.Rows.Count;
}
/// <summary>
/// Gets a specific feature from the data source by its <paramref name="rowId"/>
/// </summary>
/// <param name="rowId">The row index or OID (if primary key enabled) of the feature</param>
/// <returns>A feature data row</returns>
public FeatureDataRow GetFeature(uint rowId)
{
lock (_features.Rows.SyncRoot)
{
if (_oid == -1)
{
// find by row number
if (rowId >= _features.Rows.Count)
{
return null;
}
else if (FilterDelegate != null && FilterDelegate(_features[(int)rowId]))
{
return _features[(int)rowId];
}
else if (rowId < _features.Rows.Count)
{
return _features[(int)rowId];
}
}
else
{
// find by primary key
DataRow dr;
dr = _features.Rows.Find(rowId);
if (dr == null)
{
return null;
}
else if (FilterDelegate != null && FilterDelegate((FeatureDataRow)dr))
{
return (FeatureDataRow)dr;
}
else
{
return (FeatureDataRow)dr;
}
}
}
return null;
}
/// <summary>
/// Boundingbox of dataset
/// </summary>
/// <returns>boundingbox</returns>
public Envelope GetExtents()
{
lock (_features.Rows.SyncRoot)
{
if (_features.Rows.Count == 0)
return null;
var box = new Envelope();
foreach (FeatureDataRow fdr in _features.Rows)
{
if (fdr.Geometry != null && !fdr.Geometry.IsEmpty)
box.ExpandToInclude(fdr.Geometry.EnvelopeInternal);
}
return box;
}
}
/// <summary>
/// Gets the connection ID of the datasource
/// </summary>
/// <remarks>
/// The ConnectionID is meant for Connection Pooling which doesn't apply to this datasource. Instead
/// <c>String.Empty</c> is returned.
/// </remarks>
public virtual string ConnectionID
{
get { return String.Empty; }
}
/// <summary>
/// Opens the datasource
/// </summary>
public void Open()
{
//Do nothing;
}
/// <summary>
/// Closes the datasource
/// </summary>
public void Close()
{
//Do nothing;
}
/// <summary>
/// Returns true if the datasource is currently open
/// </summary>
public bool IsOpen
{
get { return true; }
}
/// <summary>
/// The spatial reference ID (CRS)
/// </summary>
public int SRID
{
get { return _srid; }
set { _srid = value; }
}
/// <summary>
/// Disposes the object
/// </summary>
public void Dispose()
{
_features.TableCleared -= HandleFeaturesCleared;
_features.Constraints.CollectionChanged -= HandleConstraintsCollectionChanged;
_features.Dispose();
}
#endregion
}
}