-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMiWrapper.cs
More file actions
579 lines (496 loc) · 15.8 KB
/
MiWrapper.cs
File metadata and controls
579 lines (496 loc) · 15.8 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
// $Id: MiWrapper.cs,v 1.2 2005/03/24 17:02:06 dmorissette Exp $
//
using System;
using System.Runtime.InteropServices;
using System.IO;
using System.Collections;
using System.Text;
namespace EBop.MapObjects.MapInfo {
/*
* These classes wrap the mitab c api functions to produce a hierarchy of classes giving readonly
* access to the feature points.
*
* Requires mitab.dll (www.maptools.org)
* See http://mitab.maptools.org/
*
* Graham Sims,
* Environment Bay of Plenty, Whakatane, New Zealand
* http://www.envbop.govt.nz
*/
/// <summary>
/// This is a helper class for our standard enumerator based on EnumImpl. Implementataions
/// (Features, Parts, Fields and Vertices) are array like structures that can provide
/// an object at a given index between 0 and Count - 1.
/// </summary>
interface IObjectProvider {
int Count {
get;
}
object GetObj(int idx);
}
/// <summary>
/// Implementation of an enumeration scheme over an index (array like) structure.
/// This class provides an enumerator that will work over any IObjectProvider implementation
/// (Features, Parts, Fields and Vertices).
/// </summary>
/// <remarks>
/// Calls to the GetEnumerator method of Fields, Parts and Vertices will return
/// an instance of this class. Calls to GetEnumerator of Features will return a descendant
/// of this class (due to the fact that features don't necessarily have a sequential
/// index).
/// </remarks>
public class IndexedEnum : IEnumerator {
public readonly int Count;
protected int eIdx = -1;
private readonly IObjectProvider objProvider;
internal IndexedEnum(IObjectProvider objProvider) {
this.objProvider = objProvider;
}
public virtual void Reset() {
eIdx = -1;
}
public object Current {
get {
return objProvider.GetObj(eIdx);
}
}
public virtual bool MoveNext() {
return (++eIdx < objProvider.Count);
}
}
/// <summary>
/// Partial implementation of IEnumerable over an indexed (aray like) structure.
/// </summary>
/// <remarks>
/// Fields, Vertices, Parts and Features all descend from this class. It serves to
/// provide the common functionality required to generate their enumerators.
/// </remarks>
public abstract class EnumImpl : IEnumerable, IObjectProvider {
public readonly int count;
protected EnumImpl(int count) {
this.count = count;
}
public int Count {
get {
return count;
}
}
public virtual IEnumerator GetEnumerator() {
return new IndexedEnum(this);
}
public abstract object GetObj(int idx);
}
/// <summary>
/// Represents a readonly view of field in a layer.
/// </summary>
/// <remarks>
/// A field instance does not relate explicity to a single feature instance. Rather
/// it represents all the features in the layer. To find the value of a field for a particular
/// feature one of the GetValueAs methods should be called, passing the feature in.
/// </remarks>
public class Field {
/// <summary>
/// The field name
/// </summary>
public readonly string Name;
/// <summary>
/// The field type
/// </summary>
public FieldType Type;
/// <summary>
/// The index of the field within the layers set of fields.
/// </summary>
public readonly int Index;
/// <summary>
/// The field width
/// </summary>
public readonly int Width;
/// <summary>
/// The field precision
/// </summary>
public readonly short Precision;
/// <summary>
/// The layer this field belongs to.
/// </summary>
public readonly Layer Layer;
protected internal Field(Layer layer, int i) {
this.Layer = layer;
this.Index = i;
this.Name = MiApi.mitab_c_get_field_name(layer.Handle, i);
this.Type = MiApi.mitab_c_get_field_type(layer.Handle, i);
this.Precision = (short) MiApi.mitab_c_get_field_precision(layer.Handle, i);
this.Width = MiApi.mitab_c_get_field_width(layer.Handle, i);
}
/// <summary>
/// Returns a string representation of this fields value for the given feature.
/// </summary>
/// <param name="feature">The feature to find the fields value for.</param>
/// <returns>A string representation of this fields value for the given feature</returns>
public string GetValueAsString(Feature feature) {
return MiApi.mitab_c_get_field_as_string(feature.Handle, this.Index);
}
/// <summary>
/// Returns a double representation of this fields value for the given feature.
/// </summary>
/// <param name="feature">The feature to find the fields value for.</param>
/// <returns>A double representation of this fields value for the given feature</returns>
public double GetValueAsDouble(Feature feature) {
return MiApi.mitab_c_get_field_as_double(feature.Handle, this.Index);
}
public override string ToString() {
return this.Name+", "+this.Type;
}
}
/// <summary>
/// Contains the set of fields belonging to a layer.
/// </summary>
/// <remarks>
/// This class descends EnumImpl, meaning the fields in the
/// set can be iterated using foreach.
/// It also has an index property allowing any field between 0 and Fields.Count-1
/// to be accessed directly with Fields[idx]
/// </remarks>
public class Fields : EnumImpl {
private Field[] fields;
protected internal Fields(Layer layer):base(MiApi.mitab_c_get_field_count(layer.Handle)) {
fields = new Field[Count];
for (int i=0; i<Count; i++)
fields[i] = CreateField(layer, i);
}
/// <summary>
/// Override this to support descendants of the Field class.
/// </summary>
/// <returns>A Field, with the given index, belonging to the given Layer</returns>
protected internal virtual Field CreateField(Layer layer, int index) {
return new Field(layer, index);
}
public virtual Field this[int index] {
get {
return index < Count ? fields[index] : null;
}
}
public override object GetObj(int idx) {
return this[idx];
}
public override string ToString() {
StringBuilder str = new StringBuilder();
str.Append("Columns:\n");
foreach (Field field in this)
str.Append(field.ToString()+"\t");
return str.ToString();
}
}
/// <summary>
/// Represents a single vertex with an X,Y point in geometric space.
/// </summary>
/// <remarks>
/// This is the base building block of a feature
/// </remarks>
public class Vertex {
public readonly double X,Y;
protected internal Vertex(double x, double y) {
this.X = x;
this.Y = y;
}
public override string ToString() {
return this.X+", "+this.Y;
}
}
/// <summary>
/// Contains the set of Vertices belonging to a single Part.
/// </summary>
/// <remarks>
/// This class descends EnumImpl, meaning the vertices in the
/// set can be iterated using foreach.
/// It also has an index property allowing any vertice between 0 and Vertices.Count-1
/// to be accessed directly with Vertices[idx]
/// </remarks>
public class Vertices : EnumImpl {
public readonly Part Part;
protected internal Vertices(Part part):
base(MiApi.mitab_c_get_vertex_count(part.Feature.Handle, part.Index)) {
this.Part = part;
}
/// <summary>
/// Override this to support descendants of the Vertex class.
/// </summary>
/// <returns>A vertex with the given X,Y coordinates</returns>
protected internal virtual Vertex CreateVertex(double x, double y) {
return new Vertex(x ,y);
}
public virtual Vertex this[int index] {
get {
return index < Count ? CreateVertex(
MiApi.mitab_c_get_vertex_x(Part.Feature.Handle, Part.Index, index),
MiApi.mitab_c_get_vertex_y(Part.Feature.Handle, Part.Index, index)) : null;
}
}
public override object GetObj(int idx) {
return this[idx];
}
public override string ToString() {
StringBuilder str = new StringBuilder();
foreach (Vertex v in this)
str.Append(v+"\t");
return str.ToString();
}
}
/// <summary>
/// Represents a Part.
/// </summary>
/// <remarks>A feature will contain one or more parts.</remarks>
public class Part {
public readonly Feature Feature;
public readonly Vertices Vertices;
public readonly int Index;
protected internal Part(Feature feature, int partIdx) {
this.Index = partIdx;
this.Feature = feature;
this.Vertices = CreateVertices(this);
}
/// <summary>
/// Override this to support descendants of the Vertices class.
/// </summary>
/// <returns>This parts vertices.</returns>
protected internal virtual Vertices CreateVertices(Part part) {
return new Vertices(this);
}
public override string ToString() {
return "Part: "+Index+"\nVertices:\n"+this.Vertices.ToString();
}
}
/// <summary>
/// Contains the set of Parts belonging to a single Feature.
/// </summary>
/// <remarks>This class descends EnumImple, meaning the Parts in the
/// set can be iterated using foreach.
/// It also has an index property allowing any Part between 0 and Parts.Count-1
/// to be accessed directly with Parts[idx]
/// </remarks>
public class Parts : EnumImpl {
/// <summary>
/// The feature these parts belong to.
/// </summary>
public readonly Feature Feature;
protected internal Parts(Feature feature):base(MiApi.mitab_c_get_parts(feature.Handle)) {
this.Feature = feature;
}
/// <summary>
/// Override this to support descendants of the Part class.
/// </summary>
/// <returns>A part with the given index</returns>
protected internal virtual Part CreatePart(int partIdx) {
return new Part(this.Feature, partIdx);
}
public Part this[int index] {
get {
return index < Count ? CreatePart(index) : null;
}
}
public override object GetObj(int idx) {
return this[idx];
}
public override string ToString() {
StringBuilder str = new StringBuilder();
str.Append("Part Count: "+this.Count+"\n");
foreach (Part part in this)
str.Append(part.ToString()+"\n");
return str.ToString();
}
}
/// <summary>
/// Represents a single feature beloning to a layer.
/// </summary>
public class Feature : IDisposable {
/// <summary>
/// Handle used to manipulate the object in the C API.
/// </summary>
public readonly IntPtr Handle;
/// <summary>
/// The id of this feature.
/// </summary>
public readonly int Id;
/// <summary>
/// The feature type.
/// </summary>
public readonly FeatureType Type;
/// <summary>
/// The layer the Feature belongs to.
/// </summary>
public readonly Layer Layer;
/// <summary>
/// The set of parts comprising this feature.
/// </summary>
public readonly Parts Parts;
protected internal Feature(Layer layer, int featureId) {
this.Id = featureId;
this.Layer = layer;
this.Handle = MiApi.mitab_c_read_feature(layer.Handle, featureId);
this.Type = MiApi.mitab_c_get_type(Handle);
this.Parts = CreateParts(this);
}
/// <summary>
/// Override this to support descendants of the Parts class.
/// </summary>
/// <returns>This layers fields</returns>
protected internal virtual Parts CreateParts(Feature feature) {
return new Parts(this);
}
/// <summary>
/// Returns text associated with this feature
/// </summary>
/// <remarks>This will return an empty string, unless this features type is
/// TABFC_Text.</remarks>
public string Text {
get {
return (this.Type == FeatureType.TABFC_Text) ?
MiApi.mitab_c_get_text(this.Handle) :
"";
}
}
public override string ToString() {
StringBuilder str = new StringBuilder();
str.Append("Feature: "+Id+"\nFields:\n");
foreach (Field field in this.Layer.Fields)
str.Append(field.GetValueAsString(this).Trim()+"\t");
str.Append("\n"+this.Parts.ToString());
return str.ToString();
}
/// <summary>
/// Convenience method to return the next Feature in the file.
/// </summary>
/// <returns>A following feature in the file.</returns>
public Feature GetNext() {
return new Feature(this.Layer, MiApi.mitab_c_next_feature_id(Layer.Handle, this.Id));
}
private bool disposed = false;
public void Dispose(bool disposing) {
if (disposing && !disposed) {
MiApi.mitab_c_destroy_feature(this.Handle);
disposed = true;
}
}
public void Dispose() {
Dispose(true);
}
~Feature() {
Dispose(false);
}
}
/// <summary>
/// Unlike the other enumerators. The feature id set isn't guaranteed to be sequential.
/// So we override the default seqeuntial iterator.
/// </summary>
internal class FeaturesEnum : IndexedEnum {
private readonly Layer layer;
internal FeaturesEnum(IObjectProvider objProvider, Layer layer) : base(objProvider) {
this.layer = layer;
}
public override bool MoveNext() {
return (eIdx = MiApi.mitab_c_next_feature_id(layer.Handle, eIdx)) != -1;
}
}
/// <summary>
/// Contains the set of features belonging to a single layer.
/// </summary>
/// <remarks>This class descends EnumImpl, meaning the features in the
/// set can be iterated using foreach.
/// It also has an index property allowing any feature between 0 and Features.Count-1
/// to be accessed directly with Features[idx]</remarks>
public class Features : EnumImpl {
/// <summary>
/// The layer the features belong to
/// </summary>
public readonly Layer Layer;
protected internal Features(Layer layer) :
base(MiApi.mitab_c_get_feature_count(layer.Handle)) {
this.Layer = layer;
}
/// <summary>
/// Override this to support descendants of the Feature class.
/// </summary>
/// <returns>This layers fields</returns>
protected internal virtual Feature CreateFeature(int index) {
return new Feature(this.Layer, index);
}
public Feature this[int index] {
get {
return (index != -1) ? CreateFeature(index) : null;
}
}
public Feature GetFirst() {
return this[MiApi.mitab_c_next_feature_id(Layer.Handle, -1)];
}
public override object GetObj(int idx) {
return this[idx];
}
public override IEnumerator GetEnumerator() {
return new FeaturesEnum(this, Layer);
}
public override string ToString() {
StringBuilder str = new StringBuilder();
str.Append("Feature Count: "+this.Count+"\n");
foreach (Feature feature in this)
str.Append(feature.ToString()+"\n");
return str.ToString();
}
}
public class Layer {
/// <summary>
/// Handle used to manipulate the object in the C API
/// </summary>
public readonly IntPtr Handle;
public readonly Fields Fields;
public readonly Features Features;
public readonly string FileName;
protected internal Layer(string fileName) {
this.Handle = MiApi.mitab_c_open(fileName);
if (this.Handle == IntPtr.Zero)
throw new FileNotFoundException("File "+fileName+" not found", fileName);
this.Fields = CreateFields();
this.Features = CreateFeatures();
this.FileName = fileName;
}
/// <summary>
/// Override this to support descendants of the Fields class.
/// </summary>
/// <returns>This layers fields</returns>
protected internal virtual Fields CreateFields() {
return new Fields(this);
}
/// <summary>
/// Override this to support descendants of the Feature class.
/// </summary>
/// <returns>This layers features</returns>
protected internal virtual Features CreateFeatures() {
return new Features(this);
}
/// <summary>
/// Factory method to return the layer with a given name.
/// </summary>
/// <param name="tabFileName"></param>
/// <returns></returns>
public static Layer GetByName(string tabFileName) {
return new Layer(tabFileName);
}
public override string ToString() {
return "Layer: "+this.FileName;
}
/// <summary>
/// Writes this layers features to the given textwriter
/// </summary>
/// <param name="writer">Destintation for the layers features</param>
public void ToText(TextWriter writer) {
writer.WriteLine(this);
writer.WriteLine(this.Fields+"\n");
writer.WriteLine(this.Features);
}
/// <summary>
/// Writes this layers features as a text file.
/// </summary>
/// <param name="fileName">The name of the file that will be created.</param>
public void ToText(string fileName) {
ToText(new StreamWriter(fileName));
}
}
}