-
Notifications
You must be signed in to change notification settings - Fork 1
/
FeatureClassUtilities.cs
6445 lines (5310 loc) · 277 KB
/
FeatureClassUtilities.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
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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
namespace PDX.BTS.DataMaintenance.MaintTools
{
// Set the GUID that will be used when this Library is referenced by other applications. Specifying a
// GUID insures that the library will maintain binary compatibility through all build operations.
[System.Runtime.InteropServices.Guid("0382ba15-d663-4efa-978b-d093ef684006")]
public class FeatureClassUtilities:System.IDisposable
{
/// <summary>
/// A public event for sending process messages back to the calling application.
/// </summary>
public event SendProcessMessage ProcessMessage;
/// <summary>
/// A public event for sending error messages back to the calling application.
/// </summary>
public event SendErrorMessage ErrorMessage;
/// <summary>
/// Class Constructor.
/// </summary>
public FeatureClassUtilities()
{
PDX.BTS.DataMaintenance.MaintTools.XMLSetttingsFileManager xmlFileManager = null;
PDX.BTS.DataMaintenance.MaintTools.EncryptionManager encryptionManager = null;
try
{
// Set the Name of the XML Settings File for this Application.
string applicationDirectory = System.Reflection.Assembly.GetExecutingAssembly().Location.ToString();
while (applicationDirectory.EndsWith(@"\") == false)
{
// Drop the right-most character from the string.
applicationDirectory = applicationDirectory.Substring(0, (applicationDirectory.Length - 1));
}
string xmlSettingsFile = applicationDirectory + @"DataMaintenanceUtilitiesSettings.xml";
// Initialize the XML File Manager.
xmlFileManager = new PDX.BTS.DataMaintenance.MaintTools.XMLSetttingsFileManager();
xmlFileManager.XMLFile(xmlSettingsFile);
if (!System.IO.File.Exists(xmlSettingsFile))
{
// Let the user know that the Parameter Table could not be initialized.
if (ErrorMessage != null)
{
ErrorMessage(" Could not find the XML Settings File! Initialization Failed!");
}
// Exit this process.
return;
}
// Get the connection parameters for the Monitor Database from the XML Settings File.
_monitorDatabaseServerName = xmlFileManager.ReadUserSetting("DatabaseDefinitions", "MonitorDatabaseServer");
_monitorDatabaseName = xmlFileManager.ReadUserSetting("DatabaseDefinitions", "MonitorDatabase");
// Get the Name of the Load Monitor Table in the Monitor Database.
_parameterTableName = xmlFileManager.ReadUserSetting("DatabaseDefinitions", "LoadParemetersTableName");
// Set the Connection String that will be used to Connect to the Parameter Database.
_parameterDatabaseConnectionString = "Data Source=" + _monitorDatabaseServerName +";"
+ "Initial Catalog=" + _monitorDatabaseName + ";"
+ "Integrated Security=SSPI";
// Instantiate the Component that will hold the Managed Resources used by this method.
_component = new System.ComponentModel.Component();
// Populate the Shapefile Extension Array.
_shapefileExtensions = new System.Collections.ArrayList();
_shapefileExtensions.Add("aih");
_shapefileExtensions.Add("ain");
_shapefileExtensions.Add("cpg");
_shapefileExtensions.Add("dbf");
_shapefileExtensions.Add("gcd");
_shapefileExtensions.Add("ixs");
_shapefileExtensions.Add("mxs");
_shapefileExtensions.Add("prj");
_shapefileExtensions.Add("sbn");
_shapefileExtensions.Add("sbx");
_shapefileExtensions.Add("shp");
_shapefileExtensions.Add("shp.xml");
_shapefileExtensions.Add("shx");
_shapefileExtensions.Add("xml");
// Populate the Array of Shapefile Extensions that are necessary to have a valid Shapefile.
_shapefileNecessaryExtensions = new System.Collections.ArrayList();
_shapefileNecessaryExtensions.Add("shp");
_shapefileNecessaryExtensions.Add("shx");
_shapefileNecessaryExtensions.Add("dbf");
}
catch
{
// Exit this method.
return;
}
finally
{
// If the CGIS Data Maintenance Encryption Manager Object was instantiated, close it.
if (encryptionManager != null)
{
encryptionManager = null;
}
// If the CGIS Data Maintenance XML File Manager Object was instantiated, close it.
if (xmlFileManager != null)
{
xmlFileManager = null;
}
}
} // FeatureClassUtilities()
#region Shapefile Spatial Index Management
public System.Boolean BuildShapefileSpatialIndex(System.String ShapefileName, System.String ShapefileDirectory)
{
System.Type indexShapefileFactoryType = null;
System.Object indexShapefileFactoryObject = null;
ESRI.ArcGIS.Geodatabase.IWorkspaceFactory indexShapefileWorkspaceFactory = null;
ESRI.ArcGIS.Geodatabase.IWorkspace indexShapefileWorkspace = null;
ESRI.ArcGIS.Geodatabase.IFeatureWorkspace indexShapefileFeatureWorkspace = null;
ESRI.ArcGIS.Geodatabase.IFeatureClass indexShapefileFeatureClass = null;
try
{
// Make sure the Shapefile Name was passed correctly before moving on.
if (ShapefileName == null)
{
// Let the User know that a valid Shapefile Name MUST be passed before the index can
// be created.
if (ErrorMessage != null)
{
ErrorMessage("A NULL Value cannot be passed for the Shapefile Name! The MaintTools.FeatureClassUtilities.BuildShapefileSpatialIndex() Method failed!");
}
// Return FALSE to the calling method to indicate that this method failed.
return false;
}
else
{
if (ShapefileName.Length == 0)
{
// Let the User know that a valid Shapefile Name MUST be passed before the index can
// be created.
if (ErrorMessage != null)
{
ErrorMessage("An Empty String cannot be passed for the Shapefile Name! The MaintTools.FeatureClassUtilities.BuildShapefileSpatialIndex() Method failed!");
}
// Return FALSE to the calling method to indicate that this method failed.
return false;
}
}
// Make sure the Shapefile Directory was passed correctly before moving on.
if (ShapefileDirectory == null)
{
// Let the User know that a valid Shapefile Directory MUST be passed before the index
// can be created.
if (ErrorMessage != null)
{
ErrorMessage("A NULL Value cannot be passed for the Shapefile Directory! The MaintTools.FeatureClassUtilities.BuildShapefileSpatialIndex() Method failed!");
}
// Return FALSE to the calling method to indicate that this method failed.
return false;
}
else
{
if (ShapefileDirectory.Length == 0)
{
// Let the User know that a valid Shapefile Directory MUST be passed before the index
// can be created.
if (ErrorMessage != null)
{
ErrorMessage("An Empty String cannot be passed for the Shapefile Directory! The MaintTools.FeatureClassUtilities.BuildShapefileSpatialIndex() Method failed!");
}
// Return FALSE to the calling method to indicate that this method failed.
return false;
}
}
// If the Process Message Event has been instantiated, let the user know what is happening.
if (ProcessMessage != null)
{
ProcessMessage(" - Opening the Workspace (directory) in which the Shapefile resides...");
}
// Open the Workspace that houses the Feature Class in a try block so that any errors
// that are encountered can be handled.
try
{
// Attempt to open the Shapefile Directory.
indexShapefileFactoryType = System.Type.GetTypeFromProgID("esriDataSourcesFile.ShapefileWorkspaceFactory");
indexShapefileFactoryObject = System.Activator.CreateInstance(indexShapefileFactoryType);
indexShapefileWorkspaceFactory = (ESRI.ArcGIS.Geodatabase.IWorkspaceFactory)indexShapefileFactoryObject;
indexShapefileWorkspace = indexShapefileWorkspaceFactory.OpenFromFile(ShapefileDirectory, 0);
indexShapefileFeatureWorkspace = (ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)indexShapefileWorkspace;
}
catch (System.Runtime.InteropServices.COMException comException)
{
// Determine the Line Number from which the exception was thrown.
System.Diagnostics.StackTrace stackTrace = new System.Diagnostics.StackTrace(comException, true);
System.Diagnostics.StackFrame stackFrame = stackTrace.GetFrame(stackTrace.FrameCount - 1);
int lineNumber = stackFrame.GetFileLineNumber();
// Let the User know that this method failed.
if (ErrorMessage != null)
{
ErrorMessage("The MaintTools.FeatureClassUtilities.BuildShapefileSpatialIndex() Method failed while opening the Shapefile Workspace with COM Exception - " + comException.Message + " (" + comException.ErrorCode + " Line: " + lineNumber.ToString() + ")!");
}
// Return FALSE to the calling method to indicate that this methdo failed.
return false;
}
// If the Process Message Event has been instantiated, let the user know what is happening.
if (ProcessMessage != null)
{
ProcessMessage(" - Opening the Shapefile Feature Class on which the Index will be created...");
}
// Open the Feature Class in a try block so that any errors that are encountered can
// be handled.
try
{
// Attempt to open the Shapefile Feature Class.
indexShapefileFeatureClass = indexShapefileFeatureWorkspace.OpenFeatureClass(ShapefileName);
}
catch (System.Runtime.InteropServices.COMException comException)
{
// Determine the Line Number from which the exception was thrown.
System.Diagnostics.StackTrace stackTrace = new System.Diagnostics.StackTrace(comException, true);
System.Diagnostics.StackFrame stackFrame = stackTrace.GetFrame(stackTrace.FrameCount - 1);
int lineNumber = stackFrame.GetFileLineNumber();
// Let the User know that this method failed.
if (ErrorMessage != null)
{
ErrorMessage("The MaintTools.FeatureClassUtilities.BuildShapefileSpatialIndex() Method failed while opening the Shapefile Feature Class with COM Exception - " + comException.Message + " (" + comException.ErrorCode + " Line: " + lineNumber.ToString() + ")!");
}
// Return FALSE to the calling method to indicate that this methdo failed.
return false;
}
// Call the overload that requires that a Feature Class Pointer be passed to create the
// Spatial Index on the Feature Class that was just opened.
System.Boolean createdIndex = BuildShapefileSpatialIndex(indexShapefileFeatureClass);
// Return the result of the attempt to create the index to the calling method.
return createdIndex;
}
catch (System.Exception caught)
{
// Determine the Line Number from which the exception was thrown.
System.Diagnostics.StackTrace stackTrace = new System.Diagnostics.StackTrace(caught, true);
System.Diagnostics.StackFrame stackFrame = stackTrace.GetFrame(stackTrace.FrameCount - 1);
int lineNumber = stackFrame.GetFileLineNumber();
// Let the user know that this method failed.
if (ErrorMessage != null)
{
ErrorMessage("The MaintTools.FeatureClassUtilities.BuildShapefileSpatialIndex() Method failed with error message - " + caught.Message + " (Line: " + lineNumber.ToString() + ")!");
}
// Return FALSE to the calling method to indicate that this method Failed.
return false;
}
finally
{
// Close any Object Pointers that were instantiated by this method.
if (indexShapefileFactoryType != null)
{
// Set the Object Pointer to NULL.
indexShapefileFactoryType = null;
}
if (indexShapefileFactoryObject != null)
{
// Set the Object Pointer to NULL.
indexShapefileFactoryObject = null;
}
if (indexShapefileWorkspaceFactory != null)
{
// Set the Object Pointer to NULL.
indexShapefileWorkspaceFactory = null;
}
if (indexShapefileWorkspace != null)
{
// Set the Object Pointer to NULL.
indexShapefileWorkspace = null;
}
if (indexShapefileFeatureWorkspace != null)
{
// Set the Object Pointer to NULL.
indexShapefileFeatureWorkspace = null;
}
if (indexShapefileFeatureClass != null)
{
// Set the Object Pointer to NULL.
indexShapefileFeatureClass = null;
}
}
} // BuildShapefileSpatialIndex()
public System.Boolean BuildShapefileSpatialIndex(ESRI.ArcGIS.Geodatabase.IFeatureClass ShapefileFeatureClass)
{
ESRI.ArcGIS.Geodatabase.IFields indexShapefileFields = null;
ESRI.ArcGIS.Geodatabase.IFieldsEdit indexShapefileFieldsEdit = null;
ESRI.ArcGIS.Geodatabase.IField shapefileGeometryField = null;
ESRI.ArcGIS.Geodatabase.IIndex shapefileSpatialIndex = null;
ESRI.ArcGIS.Geodatabase.IIndexEdit shapefileSpatialIndexEdit = null;
ESRI.ArcGIS.Geodatabase.IEnumIndex shapefileExistingIndices = null;
ESRI.ArcGIS.Geodatabase.IIndex shapefileDeleteIndex = null;
try
{
// Make sure the Feature Class Object is not a NULL Pointer before moving on.
if (ShapefileFeatureClass == null)
{
// Let the User know that a valid Shapefile Name MUST be passed before the index can
// be created.
if (ErrorMessage != null)
{
ErrorMessage("A Valid Shapefile Object must be passed to this method! The MaintTools.FeatureClassUtilities.BuildShapefileSpatialIndex() Method failed!");
}
// Return FALSE to the calling method to indicate that this method failed.
return false;
}
// If the Process Message Event has been instantiated, let the user know what is happening.
if (ProcessMessage != null)
{
ProcessMessage(" - Opening the Shapefile Geometry Field and preparing it to be indexed...");
}
// Create a Fields Object and open an Editing Object on it so that new fields can be added.
indexShapefileFields = new ESRI.ArcGIS.Geodatabase.FieldsClass();
indexShapefileFieldsEdit = (ESRI.ArcGIS.Geodatabase.IFieldsEdit)indexShapefileFields;
// Find the Geometry Field in the Field in the Shapefile Fields Object and add it to the
// Fields Collection.
indexShapefileFieldsEdit.FieldCount_2 = 1;
int l = ShapefileFeatureClass.FindField(ShapefileFeatureClass.ShapeFieldName);
if (l < 0)
{
// Let the user know that the Geometry Field of the Shapefile Feature Class could not
// be found.
if (ErrorMessage != null)
{
ErrorMessage("The Shapefile Geometry Field could not be found! The MaintTools.FeatureClassUtilities.BuildShapefileSpatialIndex() Method failed!");
}
// Return FALSE to the calling method to indicate that this process failed.
return false;
}
shapefileGeometryField = ShapefileFeatureClass.Fields.get_Field(l);
indexShapefileFieldsEdit.set_Field(0, shapefileGeometryField);
// If the Process Message Event has been instantiated, let the user know what is happening.
if (ProcessMessage != null)
{
ProcessMessage(" - Creating the Index and applying it to the Shapefile Feature Class Geometry Field...");
}
// Create a new index and prepare it to be added to the table.
shapefileSpatialIndex = new ESRI.ArcGIS.Geodatabase.IndexClass();
shapefileSpatialIndexEdit = (ESRI.ArcGIS.Geodatabase.IIndexEdit)shapefileSpatialIndex;
shapefileSpatialIndexEdit.Fields_2 = indexShapefileFields;
shapefileSpatialIndexEdit.Name_2 = "Idx_1";
// Remove all Indices from the table.
shapefileExistingIndices = ShapefileFeatureClass.Indexes.FindIndexesByFieldName(ShapefileFeatureClass.ShapeFieldName);
shapefileDeleteIndex = shapefileExistingIndices.Next();
while (shapefileDeleteIndex != null)
{
ShapefileFeatureClass.DeleteIndex(shapefileDeleteIndex);
shapefileDeleteIndex = shapefileExistingIndices.Next();
}
// Add the index to the Shapefile.
ShapefileFeatureClass.AddIndex(shapefileSpatialIndex);
// If the process made it to here it was successful so return TRUE to the calling method.
return true;
}
catch (System.Exception caught)
{
// Determine the Line Number from which the exception was thrown.
System.Diagnostics.StackTrace stackTrace = new System.Diagnostics.StackTrace(caught, true);
System.Diagnostics.StackFrame stackFrame = stackTrace.GetFrame(stackTrace.FrameCount - 1);
int lineNumber = stackFrame.GetFileLineNumber();
// Let the user know that this method failed.
if (ErrorMessage != null)
{
ErrorMessage("The MaintTools.FeatureClassUtilities.BuildShapefileSpatialIndex() Method failed with error message - " + caught.Message + " (Line: " + lineNumber.ToString() + ")!");
}
// Return FALSE to the calling method to indicate that this method Failed.
return false;
}
finally
{
// Close any Object Pointers that were created by this method.
if (indexShapefileFields != null)
{
// Close the Object Pointer.
indexShapefileFields = null;
}
if (indexShapefileFieldsEdit != null)
{
// Close the Object Pointer.
indexShapefileFieldsEdit = null;
}
if (shapefileGeometryField != null)
{
// Close the Object Pointer.
shapefileGeometryField = null;
}
if (shapefileSpatialIndex != null)
{
// Close the Object Pointer.
shapefileSpatialIndex = null;
}
if (shapefileSpatialIndexEdit != null)
{
// Close the Object Pointer.
shapefileSpatialIndexEdit = null;
}
if (shapefileExistingIndices != null)
{
// Close the Object Pointer.
shapefileExistingIndices = null;
}
if (shapefileDeleteIndex != null)
{
// Close the Object Pointer.
shapefileDeleteIndex = null;
}
}
} // BuildShapefileSpatialIndex()
#endregion Shapefile Spatial Index Management
#region Delete Shapefile
public System.Boolean DeleteExistingShapefile(System.String ShapefileDirectory, System.String ShapefileName)
{
try
{
// If the Shapefile Name that was passed to this Method includes a Shapefile Part
// Extension, strip the extension before attempting to delete the Shapefile Parts.
System.String deleteShapefileName = ShapefileName;
// If the Extension is included in the Delete Shapefile Name, strip it off.
if (deleteShapefileName.IndexOf(".") != -1)
{
// Strip the Extension from the Delete Shapefile Name.
deleteShapefileName = deleteShapefileName.Substring(0, deleteShapefileName.IndexOf(".") - 1);
}
// Build the Full Path to the Shapefile that is to be deleted.
System.String deleteFullShapefilePath = System.IO.Path.Combine(ShapefileDirectory, deleteShapefileName);
// Call the Overload of this Method that expects a full Shapefile Path to delete the
// existing Shapefile.
System.Boolean deletedShapefile = DeleteExistingShapefile(deleteFullShapefilePath);
// Return the result of the delete attempt to the calling method.
return deletedShapefile;
}
catch (System.Exception caught)
{
// Determine the Line Number from which the exception was thrown.
System.Diagnostics.StackTrace stackTrace = new System.Diagnostics.StackTrace(caught, true);
System.Diagnostics.StackFrame stackFrame = stackTrace.GetFrame(stackTrace.FrameCount - 1);
int lineNumber = stackFrame.GetFileLineNumber();
// Let the User know that this method failed.
if (ErrorMessage != null)
{
// Let the User know that the Method faild.
ErrorMessage("The Maintools.FeatureClassUtilities.DeleteExistingShapefile(Directory, Name) Method failed with error message - " + caught.Message + " (Line: " + lineNumber.ToString() + ")!");
}
// Return FALSE to the calling Method to indicate that this Method failed.
return false;
}
} // DeleteExistingShapefile()
public System.Boolean DeleteExistingShapefile(System.String DeleteShapefileName)
{
try
{
// If the Shapefile Path that was passed to this Method includes a Shapefile Part
// Extension, strip the extension before attempting to delete the Shapefile Parts.
System.String deleteShapefilePath = DeleteShapefileName;
foreach (System.String currentSearchShapefileExtension in _shapefileExtensions)
{
// If the Extension is included in the Delete Shapefile Path, strip it off.
if (deleteShapefilePath.IndexOf("." + currentSearchShapefileExtension) != -1)
{
// Strip the Extension from the Delete Shapefile Path.
deleteShapefilePath = deleteShapefilePath.Substring(0, (deleteShapefilePath.Length - (currentSearchShapefileExtension.Length + 1)));
}
}
// If any pieces of the Shapefile exist, delete them.
foreach (string currentShapefileExtension in _shapefileExtensions)
{
// If the Current Shapefile part exists, delete it.
if (System.IO.File.Exists(deleteShapefilePath + "." + currentShapefileExtension))
{
// Attempt to delete the Shapefile Part.
System.IO.File.Delete(deleteShapefilePath + "." + currentShapefileExtension);
}
}
// If any parts of the Shapefile exist in the Output directory, the delete failed so
// return FALSE to the calling method.
foreach (string currentTestShapefileExtension in _shapefileExtensions)
{
// if the Current Shapefile part exists, the delete failed so return FALSE to the
// calling method.
if (System.IO.File.Exists(deleteShapefilePath + "." + currentTestShapefileExtension))
{
// Return FALSE to the calling method.
return false;
}
}
// If the process made it to here, it was successful so return TRUE to the calling method.
return true;
}
catch (System.Exception caught)
{
// Determine the Line Number from which the exception was thrown.
System.Diagnostics.StackTrace stackTrace = new System.Diagnostics.StackTrace(caught, true);
System.Diagnostics.StackFrame stackFrame = stackTrace.GetFrame(stackTrace.FrameCount - 1);
int lineNumber = stackFrame.GetFileLineNumber();
// Let the User know that this method failed.
if (ErrorMessage != null)
{
// Let the User know that the Method faild.
ErrorMessage("The Maintools.FeatureClassUtilities.DeleteExistingShapefile(FullName) Method failed with error message - " + caught.Message + " (Line: " + lineNumber.ToString() + ")!");
}
// Return FALSE to the calling Method to indicate that this Method failed.
return false;
}
} // DeleteExistingShapefile()
#endregion Delete Shapefile
#region Copy Shapefile
public System.Boolean CopyShapefile(System.String InputShapefileDirectory, System.String InputShapefileName, System.String OutputDirectory, System.String OutputName)
{
try
{
// Call the Override of this Method that requires an Override value defaulting the value
// to false.
System.Boolean copiedShapefile = CopyShapefile(InputShapefileDirectory, InputShapefileName, OutputDirectory, OutputName, false);
// Return the results of the Copy Attempt to the calling method.
return copiedShapefile;
}
catch (System.Exception caught)
{
// Determine the Line Number from which the exception was thrown.
System.Diagnostics.StackTrace stackTrace = new System.Diagnostics.StackTrace(caught, true);
System.Diagnostics.StackFrame stackFrame = stackTrace.GetFrame(stackTrace.FrameCount - 1);
int lineNumber = stackFrame.GetFileLineNumber();
// Let the User know that this method failed.
if (ErrorMessage != null)
{
// Let the User know that the Method faild.
ErrorMessage("The Maintools.FeatureClassUtilities.CopyShapefile(InputDirectory, InputName, OutputDirectory, OutputName) Method failed with error message - " + caught.Message + " (Line: " + lineNumber.ToString() + ")!");
}
// Return FALSE to the calling Method to indicate that this Method failed.
return false;
}
} // CopyShapefile()
public System.Boolean CopyShapefile(System.String InputShapefileDirectory, System.String InputShapefileName, System.String OutputDirectory, System.String OutputName, System.Boolean OverwriteExixting)
{
try
{
// Make sure the Input Shapefile Directory Exists.
if (!System.IO.Directory.Exists(InputShapefileDirectory))
{
// Let the User know that the Specified Input Directory does not exist.
if (ErrorMessage != null)
{
ErrorMessage("The specified Input Shapefile Directory - " + InputShapefileDirectory + " - does not Exist. Cannot Copy the Shapefile from that location!");
}
// Return FALSE to the calling method to indicate that this method failed.
return false;
}
// Make sure that the Specified Input Shapefile Name is valid.
if (InputShapefileName.Length == 0)
{
// Let the User know that the Specified Input Shapefile Name is not valid.
if (ErrorMessage != null)
{
ErrorMessage("The specified Input Shapefile Name - " + InputShapefileName + " - is not valid.");
}
// Return FALSE to the calling method to indicate that this method failed.
return false;
}
// If the Input Shapefile Name Includes an Extension, remove the extension.
System.String currentShapefileName = InputShapefileName;
if (currentShapefileName.IndexOf(".") != -1)
{
foreach (System.String currentExtension in _shapefileExtensions)
{
// Check to see if the Extension is part of the Shapefile Name.
if (currentShapefileName.IndexOf(currentExtension, System.StringComparison.CurrentCultureIgnoreCase) != -1)
{
// Remove the Extension from the Shapefile name.
currentShapefileName = currentShapefileName.Substring(0, (currentShapefileName.Length - (currentExtension.Length + 1)));
}
}
}
// Make sure the Specified Input Shapefile Exists.
System.String inputShapefilePath = System.IO.Path.Combine(InputShapefileDirectory, currentShapefileName);
if (!System.IO.File.Exists(inputShapefilePath + @".shp"))
{
// Let the User know that the Specified Input Shapefile Name does not exist.
if (ErrorMessage != null)
{
ErrorMessage("The specified Input Shapefile - " + inputShapefilePath + " - does not exist and cannot be copied.");
}
// Return FALSE to the calling method to indicate that this method failed.
return false;
}
// Make sure the Output Shapefile Directory Exists.
if (!System.IO.Directory.Exists(OutputDirectory))
{
// Attempt to create the Output Directory.
System.IO.Directory.CreateDirectory(OutputDirectory);
// Make sure that the directory exists now.
if (!System.IO.Directory.Exists(OutputDirectory))
{
// Let the User know that the Specified Input Shapefile Name does not exist.
if (ErrorMessage != null)
{
ErrorMessage("The specified Output Directory - " + OutputDirectory + " - does not exist the Shapefile cannot be copied to the directory.");
}
// Return FALSE to the calling method to indicate that this method failed.
return false;
}
}
// Call the override of this Method to requires a Full Input Shapefile Directory and a
// Full Output Shapefile Directory to actually perform the copy.
System.String outputShapefilePath = System.IO.Path.Combine(OutputDirectory, OutputName);
System.Boolean copiedShapefile = CopyShapefile(inputShapefilePath, outputShapefilePath, OverwriteExixting);
// Return the result of the Copy attempt to the calling method.
return copiedShapefile;
}
catch (System.Exception caught)
{
// Determine the Line Number from which the exception was thrown.
System.Diagnostics.StackTrace stackTrace = new System.Diagnostics.StackTrace(caught, true);
System.Diagnostics.StackFrame stackFrame = stackTrace.GetFrame(stackTrace.FrameCount - 1);
int lineNumber = stackFrame.GetFileLineNumber();
// Let the User know that this method failed.
if (ErrorMessage != null)
{
// Let the User know that the Method faild.
ErrorMessage("The Maintools.FeatureClassUtilities.CopyShapefile(InputDirectory, InputName, OutputDirectory, OutputName, Overwrite) Method failed with error message - " + caught.Message + " (Line: " + lineNumber.ToString() + ")!");
}
// Return FALSE to the calling Method to indicate that this Method failed.
return false;
}
} // CopyShapefile()
public System.Boolean CopyShapefile(System.String InputShapefilePath, System.String OutputShapefilePath)
{
try
{
// Call the Override of this Method that requires and Overwrite Existing value, defaulting
// the value to false.
System.Boolean copiedShapefile = CopyShapefile(InputShapefilePath, OutputShapefilePath, false);
// Return the Results of the Copy Attempt to the calling method.
return copiedShapefile;
}
catch (System.Exception caught)
{
// Determine the Line Number from which the exception was thrown.
System.Diagnostics.StackTrace stackTrace = new System.Diagnostics.StackTrace(caught, true);
System.Diagnostics.StackFrame stackFrame = stackTrace.GetFrame(stackTrace.FrameCount - 1);
int lineNumber = stackFrame.GetFileLineNumber();
// Let the User know that this method failed.
if (ErrorMessage != null)
{
// Let the User know that the Method faild.
ErrorMessage("The Maintools.FeatureClassUtilities.CopyShapefile(FullInputName, FullOutputName) Method failed with error message - " + caught.Message + " (Line: " + lineNumber.ToString() + ")!");
}
// Return FALSE to the calling Method to indicate that this Method failed.
return false;
}
} // CopyShapefile()
public System.Boolean CopyShapefile(System.String InputShapefilePath, System.String OutputShapefilePath, System.Boolean OverwriteExisting)
{
try
{
// Make sure the Input Shapefile Path value is not zero length.
if (InputShapefilePath.Length == 0)
{
// Let the user know that the Input Shapefile Path is not valid.
if (ErrorMessage != null)
{
ErrorMessage("The Specified Input Shapefile Path cannot be zero length. The Copy will be aborted!");
}
// Return FALSE to the calling method to indicate that the Copy failed.
return false;
}
// Retrieve the Input Shapefile Name from the Input Shapefile Path.
System.String inputShapefileName = InputShapefilePath;
while (inputShapefileName.IndexOf(@"\") != -1)
{
inputShapefileName = inputShapefileName.Substring(1);
}
// Retrieve the Output Shapefile Name from the Output Shapefile Path.
System.String outputShapefileName = OutputShapefilePath;
while (outputShapefileName.IndexOf(@"\") != -1)
{
outputShapefileName = outputShapefileName.Substring(1);
}
// If the Input Shapefile Name Includes an Extension, remove the extension.
System.String currentInputShapefilePath = InputShapefilePath;
if (currentInputShapefilePath.IndexOf(".") != -1)
{
foreach (System.String currentExtension in _shapefileExtensions)
{
// Check to see if the Extension is part of the Input Shapefile Path.
if (currentInputShapefilePath.IndexOf(currentExtension, System.StringComparison.CurrentCultureIgnoreCase) != -1)
{
// Remove the Extension from the Input Shapefile Path.
currentInputShapefilePath = currentInputShapefilePath.Substring(0, (currentInputShapefilePath.Length - (currentExtension.Length + 1)));
}
}
}
// Make sure the Input Shapefile Path Exists before Attempting to copy it to the Output
// Directory.
if (!System.IO.File.Exists(currentInputShapefilePath + @".shp"))
{
// Let the user know that the Input Shapefile does not exist.
if (ErrorMessage != null)
{
ErrorMessage("The Specified Input Shapefile Path - " + currentInputShapefilePath + ".shp - does not exist. The Copy will be aborted!");
}
// Return FALSE to the calling method to indicate that the Copy failed.
return false;
}
// Strip the Output Shapefile Name from the Output Shapefile Directory and Make sure the
// Output Shapefile Directory exists before attempting to copy the Input Shapefile to it.
System.String outputShapefileDirectory = OutputShapefilePath;
while (!outputShapefileDirectory.EndsWith(@"\"))
{
// Strip the last character from the Shapefile Path until the Output Shapefile Name has
// been removed from the path.
outputShapefileDirectory = outputShapefileDirectory.Substring(0, outputShapefileDirectory.Length - 1);
}
// Strip the final Backslash from the Output Directory Path.
outputShapefileDirectory = outputShapefileDirectory.Substring(0, outputShapefileDirectory.Length - 1);
// Now make sure the output Directory exists.
if (!System.IO.Directory.Exists(outputShapefileDirectory))
{
// Let the user know that the directory is being created.
if (ProcessMessage != null)
{
ProcessMessage(" - Creating the Output Directory - " + outputShapefileDirectory + "...");
}
// Attempt to create the Output Directory.
System.IO.Directory.CreateDirectory(outputShapefileDirectory);
// Now if the Output Directory does not exist, abort the copy.
if (!System.IO.Directory.Exists(outputShapefileDirectory))
{
// Let the user know that the Output Directory does not exist.
if (ErrorMessage != null)
{
ErrorMessage("The Specified Output Directory - " + outputShapefileDirectory + " - does not exist. The Copy will be aborted!");
}
// Return FALSE to the calling method to indicate that the Copy failed.
return false;
}
}
// If the Output Shapefile Path includes an extension, remove it.
string outputShapefileCopyPath = OutputShapefilePath;
if (outputShapefileCopyPath.IndexOf(".") != -1)
{
foreach (System.String currentExtension in _shapefileExtensions)
{
// Check to see if the Extension is part of the Shapefile Name.
if (outputShapefileCopyPath.IndexOf(currentExtension, System.StringComparison.CurrentCultureIgnoreCase) != -1)
{
// Remove the Extension from the Shapefile name.
outputShapefileCopyPath = outputShapefileCopyPath.Substring(0, (outputShapefileCopyPath.Length - (currentExtension.Length + 1)));
}
}
}
// If the Output Shapefile already exists and overwrite is set to false, this process
// cannot continue. If Overwrite is set to TRUE, delete the existing output Files.
foreach (System.String currentextension in _shapefileExtensions)
{
// Determine if the output file already exists. If it does and Overwrite Existing
// is set to TRUE, delete teh existing file. Otherwise, abort the copy.
if (System.IO.File.Exists(outputShapefileCopyPath + @"." + currentextension))
{
// If Overwrite Existing is set to TRUE, attempt to delete the file.
if (OverwriteExisting)
{
// Make sure the File is not set to Read-Only.
System.IO.FileAttributes currentFileAttributes = System.IO.File.GetAttributes(outputShapefileCopyPath + @"." + currentextension);
if ((currentFileAttributes & System.IO.FileAttributes.ReadOnly) == System.IO.FileAttributes.ReadOnly)
{
// Set the file to Read-Write.
System.IO.File.SetAttributes(outputShapefileCopyPath + @"." + currentextension, System.IO.FileAttributes.Normal);
}
// Attempt to delete the file.
System.IO.File.Delete(outputShapefileCopyPath + @"." + currentextension);
// If the Output File Still Exists, abort the copy.
if (System.IO.File.Exists(outputShapefileCopyPath + @"." + currentextension))
{
// Let the user know that the Output file could not be over-written.
if (ErrorMessage != null)
{
ErrorMessage("Could not Over-Write the - " + outputShapefileCopyPath + @"." + currentextension + " - existing file. Aborting the Copy!");
}
// Return FALSE to the calling method to indicate that the copy failed.
return false;
}
}
else
{
// Let the User know that the Output File already exists so the copy cannot continue.
if (ErrorMessage != null)
{
ErrorMessage("The Output Shapefile - " + outputShapefileCopyPath + ".shp - Already Exists. The copy cannot continue.");
}
// Return FALSE to the calling method to indicate that this copy failed.
return false;
}
}
}
// Attempt to copy the Input Shapefile to the Output Directory.
foreach (System.String currentExtension in _shapefileExtensions)
{
// If the Input File Exists, attempt to copy it to the destination Directory.
if (System.IO.File.Exists(InputShapefilePath + @"." + currentExtension))
{
// Attempt to Copy the File to the Destination Directory.
System.IO.File.Copy(InputShapefilePath + @"." + currentExtension, outputShapefileCopyPath + @"." + currentExtension);
// Make sure the file was copied successfully.
if (!System.IO.File.Exists(outputShapefileCopyPath + @"." + currentExtension))
{
// Let the User know that the Current Shapefile Part could not be copied.
if (ErrorMessage != null)
{
ErrorMessage("Could not copy the - " + InputShapefilePath + @"." + currentExtension + " - file to the Ouput Directory. Aborting the copy!");