-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathFields.cs
297 lines (277 loc) · 13.3 KB
/
Fields.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
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace ArcShapeFile
{
/// <summary>
/// The Collection of all Database field information and data values
/// </summary>
/// <remarks>
/// <para>The Fields collection represents a single row from the Shape DBF. It always points to the <see cref="ArcShapeFile.ShapeFile.CurrentRecord">current record</see> of the ShapeFiles object.</para>
/// <para>You can refer to each Field object within the collection by:
/// <ul>
/// <li>Iteration by using the 0 based ordinal - i.e. for(int i=0;i < shp.Fields.Count; i++) .</li>
/// <li>Iteration by reference - i.e. foreach(Field fd in shp.Fields) .</li>
/// <li>By referencing the item by FieldName - i.e. shp.Fields["myname"] .</li>
/// </ul>
/// </para></remarks>
[ClassInterface(ClassInterfaceType.AutoDual)]
[Guid("1BBEA337-0B68-418c-B380-BB54B38866B0")]
public class Fields : System.Collections.CollectionBase
{
#region ********** Local Variables **********
bool mvarIsDeleted = false;
static bool mvarFixFieldDupls = false;
// Database Variables;
private Int16 dbfHeaderLength;
private Int16 dbfRecordlength;
private eLanguage dbfLanguage;
private string dbfDelimiter = ".";
private int dbfRecordCount;
internal Int16 HeaderLength
{
get { return dbfHeaderLength; }
set { dbfHeaderLength = value; }
}
internal Int16 Recordlength
{
get { return dbfRecordlength; }
set { dbfRecordlength = value; }
}
internal eLanguage Language
{
get { return (eLanguage)dbfLanguage; }
set { dbfLanguage = value; }
}
internal string Delimiter
{
get{return dbfDelimiter;}
set {dbfDelimiter=value;}
}
internal int RecordCount
{
get { return dbfRecordCount; }
set { dbfRecordCount = value; }
}
#endregion
#region ********** Fields Methods **********
/// <summary>
/// Creates a new ShapeFile Database field by defining the field in detail
/// </summary>
/// <param name="Name">Field name of the ShapeFile Database record</param>
/// <param name="Type">Type of field to be created</param>
/// <param name="Size">The length of the field</param>
/// <param name="Decimal">The number of digits to be stored right of the decimal point</param>
/// <remarks>
/// Use the Add method to append a field definition into the collection. To physically write the definition to the DBF file you must use the
/// <see cref="ArcShapeFile.ShapeFile.WriteFieldDefs">WriteFieldDefs</see> method. You can aren't limited to adding field definitions to new shapefiles - you can append them to existing ones too.
/// </remarks>
public void Add(string Name, eFieldType Type, short Size, short Decimal)
{ CreateField(Name, Type, Size, Decimal); }
/// <summary>
/// Creates a new ShapeFile Database field using name, type and size
/// </summary>
/// <param name="Name">Field name of the ShapeFile Database record</param>
/// <param name="Type">Type of field to be created</param>
/// <param name="Size">The length of the field</param>
/// <remarks>
/// Use the Add method to append a field definition into the collection. To physically write the definition to the DBF file you must use the
/// <see cref="ArcShapeFile.ShapeFile.WriteFieldDefs">WriteFieldDefs</see> method. You can aren't limited to adding field definitions to new shapefiles - you can append them to existing ones too.
/// </remarks>
public void Add(string Name, eFieldType Type, short Size)
{ CreateField(Name, Type, Size, -1); }
/// <summary>
/// Creates a new ShapeFile Database field using name and predetermined type
/// </summary>
/// <param name="Name">Field name of the ShapeFile Database record</param>
/// <param name="Type">Type of field to be created</param>
/// <remarks>
/// Use the Add method to append a field definition into the collection. To physically write the definition to the DBF file you must use the
/// <see cref="ArcShapeFile.ShapeFile.WriteFieldDefs">WriteFieldDefs</see> method. You can aren't limited to adding field definitions to new shapefiles - you can append them to existing ones too.
/// </remarks>
public void Add(string Name, eFieldType Type)
{ CreateField(Name, Type, -1, -1); }
/// <summary>
/// Creates a new ShapeFile Database field from a field definition
/// </summary>
/// <param name="thisField">The ShapeFile Field definition to be added</param>
/// <remarks>
/// Use the Add method to append a field definition into the collection. To physically write the definition to the DBF file you must use the
/// <see cref="ArcShapeFile.ShapeFile.WriteFieldDefs">WriteFieldDefs</see> method. You can aren't limited to adding field definitions to new shapefiles - you can append them to existing ones too.
/// </remarks>
public void Add(Field thisField)
{ CreateField(thisField.Name, thisField.Type, thisField.Size, thisField.Decimal); }
private void CreateField(string Name, eFieldType Type, short Size, short Decimal)
{
//Check existance of Field Name
if (Name.Length>10){Name=Name.Substring(0,10);}
foreach(Field testField in List)
{
if(testField.Name==Name.ToUpper() & testField.Name!="SHAPE_ID")
{
if (mvarFixFieldDupls == false) { throw new System.ArgumentException("A Field already exists with this name", Name); }
else
{
bool lvarFoundName = true;
int i = 0;
string newName = null;
while (lvarFoundName == true)
{
lvarFoundName = false;
i++;
newName = Name.Substring(0, Name.Length - i.ToString().Length) + i.ToString();
foreach (Field nameField in List)
{
if (nameField.Name == newName) { break; }
}
}
Name = newName;
}
}
}
//create a new object
Field objNewMember = default(Field);
objNewMember = new Field();
//set the properties passed into the method
objNewMember.Name = Name.ToUpper();
objNewMember.Status = "A";
objNewMember.Type = Type;
switch (Type)
{case eFieldType.shpBoolean:
objNewMember.Decimal = 0;
objNewMember.Size = 1;
break;
case eFieldType.shpDate:
objNewMember.Decimal = 0;
objNewMember.Size = 8;
break;
case eFieldType.shpDouble:
if (Decimal == -1) { objNewMember.Decimal = 10; }
else { objNewMember.Decimal = Decimal; }
if (Size == -1) { objNewMember.Size = 30; }
else { objNewMember.Size = Size; }
break;
case eFieldType.shpLong:
objNewMember.Decimal = 0;
if (Size == -1) { objNewMember.Size = 10; }
else { objNewMember.Size = Size; }
break;
case eFieldType.shpInteger:
objNewMember.Decimal = 0;
if (Size == -1) { objNewMember.Size = 5; }
else { objNewMember.Size = Size; }
break;
case eFieldType.shpFloat:
if (Decimal == -1) { objNewMember.Decimal = 11; }
else { objNewMember.Decimal = Decimal; }
if (Size == -1) { objNewMember.Size = 19; }
else { objNewMember.Size = Size; }
break;
case eFieldType.shpSingle:
if (Decimal == -1) { objNewMember.Decimal = 5; }
else { objNewMember.Decimal = Decimal; }
if (Size == -1) { objNewMember.Size = 20; }
else { objNewMember.Size = Size; }
break;
case eFieldType.shpText:
objNewMember.Decimal = 0;
if (Size == -1) { objNewMember.Size = 10; }
else { objNewMember.Size = Size; }
break;
default:
if (Decimal == -1) { objNewMember.Decimal = 0; }
else { objNewMember.Decimal = Decimal; }
if (Size == -1) { objNewMember.Size = 10; }
else { objNewMember.Size = Size; }
break;
}
List.Add(objNewMember);
objNewMember = null;
}
/// <summary>
/// Defines if duplicate field names should be corrected by adding a number to the end of the field name
/// </summary>
/// <remarks>As with all databases - your field names need to be unique but as the .DBF data format limits the field name to 10 characters this can sometimes be a bit of a problem.
/// When you set this property to true every field name is checked when added. If it isn't unique then the field name has a counter added to it (e.g. MYNAME1, MYNAME2). If the field name is larger than
/// 10 charcters in length then it will be truncated before the counter is added.</remarks>
public bool FixFieldNames
{
get { return mvarFixFieldDupls; }
set { mvarFixFieldDupls = value; }
}
/// <summary>
/// Marks a field for deletion
/// </summary>
/// <param name="vntIndexKey">The Index if the field to be removed</param>
/// <remarks>
/// This method marks the indicated Field for removal from the collection. This uses the same process as the Field <see cref="ArcShapeFile.Field.Delete">Delete</see> method in the Field object. When the <see cref="ArcShapeFile.ShapeFile.WriteFieldDefs">WriteFieldDefs</see> method is used the removal is made permanent.
/// </remarks>
/// <seealso cref="ArcShapeFile.Field.Delete">Field Delete</seealso>
/// <seealso cref="ArcShapeFile.Field.UnDelete">Field UnDelete</seealso>
/// <seealso cref="ArcShapeFile.ShapeFile.Pack">Pack</seealso>
public new void RemoveAt(int vntIndexKey)
{
Field mField = (Field)List[vntIndexKey];
mField.Delete();
}
#endregion
#region ********** Fields Properties **********
/// <summary>
/// Reports if the database record is Deleted
/// </summary>
/// <remarks>Deleted records are often associated with Null shape records. Creating a Null record using <see cref="ArcShapeFile.ShapeFile.AddNullShape">AddNullShape</see> will automatically add a new record to the database, but set the
/// delete flag. This property will tell you the status of the current record.</remarks>
public bool isDeleted
{
get { return mvarIsDeleted; }
set { mvarIsDeleted = value; }
}
/// <summary>
/// Grabs the Field record from the collection by using its ordinal position within the collection
/// </summary>
/// <param name="Index">The index within the collection</param>
public Field this[int Index]
{
get
{
return (Field)List[Index];
}
}
/// <summary>
/// Grabs the Field record from the collection by using the FieldName
/// </summary>
/// <param name="FieldName">The field name listed in the collection</param>
public Field this[string FieldName]
{
get
{
int retIndex = -1;
for (int Index = 0; Index < List.Count; Index++)
{
Field testField = (Field)List[Index];
if (testField.Name == FieldName.ToUpper())
{
retIndex = Index;
break;
}
}
if (retIndex > -1)
{ return (Field)List[retIndex]; }
else
{ return null; }
}
}
#endregion
#region ********** Internal Methods **********
/// <summary>
/// Removes the data values from the Fields Collection but leaves the structure behind
/// </summary>
internal void Strip()
{
foreach (Field mField in List)
{ mField.Value = null; }
}
#endregion
}
}