-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArcStuff.cs
384 lines (360 loc) · 16.7 KB
/
ArcStuff.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
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.ADF.COMSupport;
using ESRI.ArcGIS.CatalogUI;
using ESRI.ArcGIS.Catalog;
using ESRI.ArcGIS.Display;
namespace fulcrum
{
public class ArcStuff
{
private const string photoFieldName = "Photos";
public static bool DoArcStuff(fulcrumform form, fulcrumrecords records)
{
try
{
IFeatureClass newFeatureClass = CreateFeatureClass(form, Properties.Settings.Default.pathToGeoDB);
InsertFeatures(records, newFeatureClass);
IFeatureLayer layer = CreateFeatureLayer(newFeatureClass, form.name);
IMap map = ArcMap.Document.FocusMap;
map.AddLayer((ILayer)layer);
ArcMap.Document.ActiveView.Refresh();
return true;
}
catch (Exception e)
{
string bomb = e.Message;
return false;
}
}
public static IFeatureClass CreateFeatureClass(fulcrumform form, string pathToGeoDB)
{
try
{
ISpatialReferenceFactory spatialReferenceFactory = new SpatialReferenceEnvironmentClass();
IGeographicCoordinateSystem geographicCoordinateSystem = spatialReferenceFactory.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_WGS1984);
ISpatialReference sr = geographicCoordinateSystem;
IWorkspaceFactory2 wsf = new FileGDBWorkspaceFactoryClass();
IWorkspace2 workspace = (IWorkspace2)wsf.OpenFromFile(pathToGeoDB, 0);
IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspace;
string fcName = GetUniqueFeatureClassName(form.name, workspace);
IFields fieldsCollection = new FieldsClass();
IFieldEdit newField = fieldsCollection as IFieldEdit;
IFeatureClassDescription fcDesc = new ESRI.ArcGIS.Geodatabase.FeatureClassDescriptionClass();
IObjectClassDescription ocDesc = (IObjectClassDescription)fcDesc;
// create required fields using the required fields method
IFields fields = ocDesc.RequiredFields;
//Grab the GeometryDef from the shape field, edit it and give it back
int shapeFieldIndex = fields.FindField(fcDesc.ShapeFieldName);
IField shapeField = fields.get_Field(shapeFieldIndex);
IGeometryDef geomDef = shapeField.GeometryDef;
IGeometryDefEdit geomDefEdit = (IGeometryDefEdit)geomDef;
geomDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint;
geomDefEdit.GridCount_2 = 1;
geomDefEdit.set_GridSize(0, 0);
geomDefEdit.SpatialReference_2 = sr;
IFields newFields = CreateNewFields(form, fields, sr);
IFeatureClass featureClass = featureWorkspace.CreateFeatureClass
(fcName, newFields, ocDesc.InstanceCLSID, ocDesc.ClassExtensionCLSID, esriFeatureType.esriFTSimple, fcDesc.ShapeFieldName, "");
return featureClass;
}
catch (Exception e)
{
string errMsg = e.Message;
return null;
}
}
public static IFeatureLayer CreateFeatureLayer(IFeatureClass featureClass, string layerName)
{
try
{
IFeatureLayer featureLayer = new FeatureLayer();
featureLayer.Name = layerName;
featureLayer.FeatureClass = featureClass;
return featureLayer;
}
catch (Exception e)
{
string ouch = e.Message;
return null;
}
}
private static IFields CreateNewFields(fulcrumform form, IFields fields, ISpatialReference sr)
{
try
{
IFieldsEdit fieldsEdit = (IFieldsEdit)fields;
foreach (fulcrumelement element in form.elements)
{
//!!! ToDo: Add data types as needed (numeric, etc)
if (element.data_name != "")
{
IField field = new FieldClass();
IFieldEdit2 fieldEdit = (IFieldEdit2)field;
switch (element.type)
{
case "Section":
//a Section is a whole different animal it will have sub elements (children) like so, element.elements
//!!!~but not dealing with them for now
fieldEdit.Name_2 = element.data_name;
fieldEdit.IsNullable_2 = true;
fieldEdit.AliasName_2 = element.label;
fieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
fieldEdit.Length_2 = 100;
// add field to field collection
fieldsEdit.AddField(field);
break;
case "ClassificationField":
fieldEdit.Name_2 = element.data_name;
fieldEdit.IsNullable_2 = true;
fieldEdit.AliasName_2 = element.label;
fieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
fieldEdit.Length_2 = 100;
fieldsEdit.AddField(field);
break;
case "ChoiceField":
fieldEdit.Name_2 = element.data_name;
fieldEdit.IsNullable_2 = true;
fieldEdit.AliasName_2 = element.label;
fieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
fieldEdit.Length_2 = 100;
fieldsEdit.AddField(field);
break;
case "TextField":
fieldEdit.Name_2 = element.data_name;
fieldEdit.IsNullable_2 = true;
fieldEdit.AliasName_2 = element.label;
fieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
fieldEdit.Length_2 = 255;
fieldsEdit.AddField(field);
break;
case "DateTimeField":
//if the name happens to be Date we need to override it
if (element.data_name == "date")
{
fieldEdit.Name_2 = "record_date";
}
else
{
fieldEdit.Name_2 = element.data_name;
}
fieldEdit.IsNullable_2 = true;
fieldEdit.AliasName_2 = element.label;
fieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
fieldEdit.Length_2 = 50;
fieldsEdit.AddField(field);
break;
case "PhotoField":
fieldEdit.Name_2 = photoFieldName;
fieldEdit.IsNullable_2 = true;
fieldEdit.AliasName_2 = element.label;
fieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
fieldEdit.Length_2 = 200;
//If we want to stuff the image in a raster field, here's a start
/*!!!
IRasterDef rasterDef = new RasterDefClass();
rasterDef.Description = "PictureField";
rasterDef.IsManaged = true;
rasterDef.IsRasterDataset = true;
//rasterDef.SpatialReference = sr;
This is throwing an error
fieldEdit.RasterDef = rasterDef;
fieldEdit.Type_2 = esriFieldType.esriFieldTypeRaster;
*/
fieldsEdit.AddField(field);
break;
case "Label":
//skip it
default:
//a data type we haven't accounted for...
//don't create a field for it
break;
}
field = null;
fieldEdit = null;
}
}
fields = (IFields)fieldsEdit;
// Use IFieldChecker to create a validated fields collection.
IFieldChecker fieldChecker = new FieldCheckerClass();
IEnumFieldError enumFieldError = null;
IFields validatedFields = null;
fieldChecker.Validate(fields, out enumFieldError, out validatedFields);
// The enumFieldError enumerator can be inspected at this point to determine
// which fields were modified during validation.
if (enumFieldError != null)
{
enumFieldError.Reset();
IFieldError err = enumFieldError.Next();
while (!(err == null))
{
//!!!ToDo:
//we need to do something here cause the feature class will not be created with invalid fields
Console.WriteLine(err.FieldError + " at index " + err.FieldIndex);
err = enumFieldError.Next();
}
}
return validatedFields;
}
catch (Exception e)
{
string choke = e.Message;
return null;
}
}
private static bool InsertFeatures(fulcrumrecords fulcrumRecords, IFeatureClass featureClass)
{
try
{
// Create an insert cursor.
IFeatureCursor insertCursor = featureClass.Insert(false);
//comReleaser.ManageLifetime(insertCursor);
foreach (fulcrumrecord record in fulcrumRecords.records)
{
// Create a feature buffer.
IFeatureBuffer featureBuffer = featureClass.CreateFeatureBuffer();
//comReleaser.ManageLifetime(featureBuffer);
IPoint pt = new Point();
pt.PutCoords(record.longitude, record.latitude);
featureBuffer.Shape = pt as IGeometry;
//handle photos separately
if (record.photos.Count >= 1)
{
try
{
//only link to the first photo for now
//ToDo: how to put the actual image in the photos field (the actual image is held in the photos property of the record)
//like so...
//featureBuffer.set_Value(featureClass.FindField("photos"), record.photos[0]);
featureBuffer.set_Value(featureClass.FindField(photoFieldName), record.photolinks[0]);
//ToDo: enhancement: multiple photo support; could create a separate table to store the photo links and join it back to resulting feature class via record id
}
catch (Exception e)
{
string photoInsertError = e.Message;
}
}
foreach (fulcrumattribute attr in record.attributes)
{
if (attr.fieldName != "photos")
{
//ToDo: why are some (first?) field names empty strings?
if (attr.fieldName != "" && attr.fieldName != null)
{
try
{
featureBuffer.set_Value(featureClass.FindField(attr.fieldName), attr.fieldValue);
}
catch(Exception e)
{
string ouch = e.Message;
}
}
}
}
try
{
insertCursor.InsertFeature(featureBuffer);
featureBuffer = null;
GC.Collect();
}
catch (Exception e)
{
string insertFailed = e.Message;
}
}
// Flush the buffer to the geodatabase.
insertCursor.Flush();
return true;
}
catch (Exception)
{
return false;
}
}
public static string GetGeodatabaseFromUser()
{
try
{
IEnumGxObject enumGxObject = null;
IGxObjectFilter gxObjectFilterFGBs = new GxFilterFileGeodatabasesClass();
IGxDialog gxDialog = new GxDialogClass();
IGxObjectFilterCollection gxObjectFilterCollection = (IGxObjectFilterCollection)gxDialog;
gxObjectFilterCollection.AddFilter(gxObjectFilterFGBs, false);
gxDialog.Title = "Locate Geodatabase";
gxDialog.AllowMultiSelect = false;
gxDialog.DoModalOpen(0, out enumGxObject);
if (!(enumGxObject == null))
{
//we don't allow multiple select so grab the first one
IGxObject gxObj = enumGxObject.Next();
string pathToFGDB = gxObj.FullName;
return pathToFGDB;
}
else
{
//user cancelled
return "";
}
}
catch
{
return "";
}
}
public static IFeatureLayer GetFeatureLayerByName(string layerName)
{
IEnumLayer enumLayer;
try
{
enumLayer = ArcMap.Document.FocusMap.Layers;
enumLayer.Reset();
ILayer layer;
while ((layer = enumLayer.Next()) != null)
{
if (layer is FeatureLayer)
{
if (layer.Name == layerName)
{
return (IFeatureLayer)layer;
}
}
}
//if we made it here we didn't find a layer with the passed name
return null;
}
catch (Exception)
{
return null;
}
}
public static string GetUniqueFeatureClassName(string name, IWorkspace2 workspace)
{
//ToDo: This works to get a unique name but it's goobery, need to implement recursion here...
try
{
IFieldChecker fieldChecker = new FieldChecker();
string fcName;
fieldChecker.ValidateTableName(name, out fcName);
int i = 1;
while(workspace.get_NameExists(esriDatasetType.esriDTFeatureClass, fcName))
{
fcName = fcName + i;
i++;
}
return fcName;
}
catch
{
return "";
}
}
}
}