-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGDB2GDB.cs
1180 lines (1039 loc) · 52 KB
/
GDB2GDB.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;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Windows.Forms;
using System.Globalization;
using Microsoft.VisualBasic;
using ESRI.ArcGIS.ArcMapUI;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.CatalogUI;
using ESRI.ArcGIS.Catalog;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Geoprocessor;
using ESRI.ArcGIS.Geoprocessing;
using ESRI.ArcGIS.AnalysisTools;
using ESRI.ArcGIS.Framework;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.GeoSurvey;
using ESRI.ArcGIS.Editor;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.DataManagementTools;
using ESRI.ArcGIS.EditorExt;
namespace CoordinateTransformation
{
//GDB2GDB类的功能:实现Geodatabase数据库的拷贝
//拷贝过程中可以设置Z、M是否有效
public partial class GDB2GDB : Form
{
IWorkspaceFactory workspaceFactory = null;
IWorkspace sourceWorkspace = null;
IWorkspace targetWorkspace = null;
string sourceGDBDirectory;
string sourceGDBName;
string targetGDBDirectory;
bool bHasZ = false;
bool bHasM = false;
public GDB2GDB()
{
InitializeComponent();
}
//选择用于拷贝的源数据库:可以是*.gdb或*.mdb
private void btnSelectSourceDB_Click(object sender, EventArgs e)
{
IEnumGxObject gxObjects = AddDatasetWithGxDialog();
IGxObject gxObject = gxObjects.Next();
if (gxObject == null) return;
sourceGDBDirectory = gxObject.FullName;
sourceGDBName = System.IO.Path.GetFileNameWithoutExtension(sourceGDBDirectory);
string gdbExt = System.IO.Path.GetExtension(sourceGDBDirectory);
switch (gdbExt)
{
case ".gdb":
workspaceFactory = new FileGDBWorkspaceFactoryClass();
sourceWorkspace = workspaceFactory.OpenFromFile(sourceGDBDirectory, 0);
break;
case ".mdb":
workspaceFactory = new AccessWorkspaceFactoryClass();
sourceWorkspace = workspaceFactory.OpenFromFile(sourceGDBDirectory, 0);
break;
default:
break;
}
if (sourceWorkspace == null) return;
trvSourceGDB.Nodes.Clear();
trvTargetGDB.Nodes.Clear();
TreeNode rootnode = new TreeNode("源数据库" + "--" + sourceGDBDirectory);
trvSourceGDB.Nodes.Add(rootnode);
IEnumDataset featureDatasets = sourceWorkspace.get_Datasets(esriDatasetType.esriDTFeatureDataset);
TreeViewAddNodes(featureDatasets, rootnode);
IEnumDataset featureClasses = sourceWorkspace.get_Datasets(esriDatasetType.esriDTFeatureClass);
TreeViewAddNodes(featureClasses, rootnode);
trvSourceGDB.Sort();
}
//选择目标数据库
private void btnSelectTargetDB_Click(object sender, EventArgs e)
{
targetWorkspace = OpenTargetGDB();
}
//目标数据库:可以是*.gdb或*.mdb,如果选择的目标路径不含Geodatabase数据库,则新建一gdb
private IWorkspace OpenTargetGDB()
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
IWorkspace workspace = null;
targetGDBDirectory = folderBrowserDialog1.SelectedPath;
string gdbExt = System.IO.Path.GetExtension(targetGDBDirectory);
switch (gdbExt)
{
case ".gdb":
workspaceFactory = new FileGDBWorkspaceFactoryClass();
workspace = workspaceFactory.OpenFromFile(targetGDBDirectory, 0);
break;
case ".mdb":
workspaceFactory = new AccessWorkspaceFactoryClass();
workspace = workspaceFactory.OpenFromFile(targetGDBDirectory, 0);
break;
case "":
workspaceFactory = new FileGDBWorkspaceFactoryClass();
string gdbName = sourceGDBName + ".gdb";
string gdbFullPath = System.IO.Path.Combine(targetGDBDirectory, gdbName);
if (System.IO.Directory.Exists(gdbFullPath))
{
workspace = workspaceFactory.OpenFromFile(gdbFullPath, 0);
}
else
{
IWorkspaceName workspaceName = workspaceFactory.Create(targetGDBDirectory, gdbName, null, 0);
IName name = (ESRI.ArcGIS.esriSystem.IName)workspaceName;
workspace = (IWorkspace)name.Open();
}
break;
default:
break;
}
return workspace;
}
return null;
}
//拷贝数据库的属性域
public void CopyDomains(IWorkspace sourceWorkspace, IWorkspace targetWorkspace)
{
IWorkspaceDomains targetWorkspaceDomains = (IWorkspaceDomains)targetWorkspace;
IWorkspaceDomains sourceWorkspaceDomains = (IWorkspaceDomains)sourceWorkspace;
IEnumDomain sourceDomains = sourceWorkspaceDomains.Domains;
if (sourceDomains == null) return;
IDomain sourceDomain = sourceDomains.Next();
IDomain targetDomain = null;
while (sourceDomain != null)
{
if (sourceDomain is IRangeDomain)
{
IRangeDomain rangeDomain = new RangeDomainClass();
rangeDomain.MinValue = (sourceDomain as IRangeDomain).MinValue;
rangeDomain.MaxValue = (sourceDomain as IRangeDomain).MaxValue;
targetDomain = rangeDomain as IDomain;
}
if (sourceDomain is ICodedValueDomain)
{
ICodedValueDomain sourceCodedDomain =sourceDomain as ICodedValueDomain;
ICodedValueDomain codedDomain = new CodedValueDomainClass();
for (int i = 0; i < sourceCodedDomain.CodeCount; i++)
{
codedDomain.AddCode(sourceCodedDomain.get_Value(i),sourceCodedDomain.get_Name(i));
}
targetDomain = codedDomain as IDomain;
}
if (targetDomain == null) return;
targetDomain.Name = sourceDomain.Name;
targetDomain.FieldType = sourceDomain.FieldType;
targetDomain.MergePolicy = sourceDomain.MergePolicy;
targetDomain.SplitPolicy = sourceDomain.SplitPolicy;
targetWorkspaceDomains.AddDomain(targetDomain);
sourceDomain = sourceDomains.Next();
}
}
//拷贝数据库模式:包括属性域、要素集、要素类(包括要素集中的要素类和独立的要素类)、表
private void CreateSchema()
{
CopyDomains(sourceWorkspace, targetWorkspace);
IEnumDataset sourceFeatureDatasets = sourceWorkspace.get_Datasets(esriDatasetType.esriDTFeatureDataset);
CreateFeatureDatasetsAndFeatureClasses(sourceFeatureDatasets);
IEnumDataset featureClasses = sourceWorkspace.get_Datasets(esriDatasetType.esriDTFeatureClass);
CreateFeatureClassesInWorkspace(featureClasses);
IEnumDataset tables = sourceWorkspace.get_Datasets(esriDatasetType.esriDTTable);
CreateTablesInWorkspace(tables);
}
private void btnCopy_Click(object sender, EventArgs e)
{
txtMessages.Text += "数据库拷贝, 请稍候..." + "\r\n";
txtMessages.Text += "\r\n创建数据库模式开始...\r\n";
DateTime startTime = DateTime.Now;
txtMessages.Text += startTime.ToString() + "\r\n";
txtMessages.Update();
CreateSchema();
txtMessages.Text += "\r\n创建数据库模式完成.\r\n";
txtMessages.Text += DateAndTime.Now.ToString() + "\r\n";
txtMessages.Text += "-------------------------------------------------------\r\n";
txtMessages.Text += "\r\n开始数据拷贝...\r\n";
txtMessages.Text += DateAndTime.Now.ToString() + "\r\n";
txtMessages.Text += "-------------------------------------------------------\r\n";
CopyFeatureDatasetAndFeatureClass();
txtMessages.Text += "\r\n数据拷贝完成...\r\n";
DateTime endTime = DateTime.Now;
txtMessages.Text += endTime.ToString() + "\r\n";
TimeSpan time = endTime - startTime;
txtMessages.Text += "总用时:" +time.ToString()+" \r\n";
txtMessages.Text += "-------------------------------------------------------\r\n";
ScrollToBottom(txtMessages);
txtMessages.Update();
//填充目标数据库树视图
trvTargetGDB.Nodes.Clear();
string sourceGDBName2 = sourceGDBName + ".gdb";
string targetGDBFullName = System.IO.Path.Combine(targetGDBDirectory, sourceGDBName2);
TreeNode rootnode = new TreeNode("目标数据库" + "--" + targetGDBFullName);
trvTargetGDB.Nodes.Add(rootnode);
IEnumDataset targetFeatureDatasets = targetWorkspace.get_Datasets(esriDatasetType.esriDTFeatureDataset);
TreeViewAddNodes(targetFeatureDatasets, rootnode);
IEnumDataset targetFeatureClasses = targetWorkspace.get_Datasets(esriDatasetType.esriDTFeatureClass);
TreeViewAddNodes(targetFeatureClasses, rootnode);
trvTargetGDB.Sort();
}
private void CreateFeatureDatasetsAndFeatureClasses(IEnumDataset sourceFeatureDatasets)
{
if (sourceWorkspace == null || targetWorkspace == null) return;
ISpatialReference spatialRference;
sourceFeatureDatasets.Reset();
IDataset dataset = sourceFeatureDatasets.Next();
while (dataset != null)
{
IGeoDataset geoDataset = dataset as IGeoDataset;
spatialRference = geoDataset.SpatialReference;
IFeatureDataset sourceFeatureDataset = dataset as IFeatureDataset;
IFeatureWorkspace featureWorkspace = targetWorkspace as IFeatureWorkspace;
if (((IWorkspace2)targetWorkspace).get_NameExists(esriDatasetType.esriDTFeatureDataset, ((IDataset)sourceFeatureDataset).Name))
dataset = sourceFeatureDatasets.Next();
IFeatureDataset targetFeatureDataset = featureWorkspace.CreateFeatureDataset(dataset.Name, spatialRference);
CreateFeatureClassesInDataset(sourceFeatureDataset, targetFeatureDataset);
dataset = sourceFeatureDatasets.Next();
}
}
//创建要素集中的要素类:要素类、拓扑关系
#region "CreateFeatureClassesInDataset"
public void CreateFeatureClassesInDataset(IFeatureDataset sourceFeatureDataset, IFeatureDataset targetFeatureDataset)
{
if (sourceFeatureDataset == null || targetFeatureDataset == null) return;
IEnumDataset datasets = sourceFeatureDataset.Subsets;
IDataset sourceDataset = datasets.Next();
while (sourceDataset != null)
{
switch (sourceDataset.Type)
{
case esriDatasetType.esriDTFeatureClass:
if (((IWorkspace2)targetWorkspace).get_NameExists(esriDatasetType.esriDTFeatureClass, sourceDataset.Name))
break;
IFeatureClass sourceFeatureClass = sourceDataset as IFeatureClass;
CreateFeatureClassInThisDataset(sourceFeatureClass, targetFeatureDataset);
break;
case esriDatasetType.esriDTGeometricNetwork:
break;
case esriDatasetType.esriDTNetworkDataset:
INetworkDataset sourceNetworkDataset = sourceDataset as INetworkDataset;
break;
case esriDatasetType.esriDTRelationshipClass:
CreateRelationshipClassSchema(sourceFeatureDataset, targetFeatureDataset);
break;
case esriDatasetType.esriDTTopology:
CreateTopology(sourceFeatureDataset, targetFeatureDataset);
break;
default:
break;
}
sourceDataset = datasets.Next();
}
}
#endregion
private void CreateFeatureClassInThisDataset(IFeatureClass sourceFeatureClass, IFeatureDataset targetFeatureDataset)
{
IFields fields = sourceFeatureClass.Fields;
if (fields == null) return;
IFieldsEdit fieldsEdit = (IFieldsEdit)fields;
IField fieldUserDefined = new Field();
IFieldEdit fieldEdit = (IFieldEdit)fieldUserDefined;
IGeometryDef geometryDef = new GeometryDefClass();
IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit)geometryDef;
geometryDefEdit.GeometryType_2 = sourceFeatureClass.ShapeType;
// By setting the grid size to 0, you're allowing ArcGIS to determine the appropriate grid sizes for the feature class.
// If in a personal geodatabase, the grid size will be 1000. If in a file or ArcSDE geodatabase, the grid size
// will be based on the initial loading or inserting of features.
geometryDefEdit.GridCount_2 = 1;
geometryDefEdit.set_GridSize(0, 0);
geometryDefEdit.HasM_2 = bHasM;
geometryDefEdit.HasZ_2 = bHasZ;
// Set standard field properties.
fieldEdit.Name_2 = "SHAPE";
fieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
fieldEdit.GeometryDef_2 = geometryDef;
fieldEdit.IsNullable_2 = true;
fieldEdit.Required_2 = true;
fieldsEdit.set_Field(fields.FindField("SHAPE"), fieldUserDefined);
UID CLSID = new UIDClass();
CLSID.Value = "esriGeoDatabase.Feature";
string featureClassName = ((IDataset)sourceFeatureClass).Name;
//创建目标要素类
IFeatureClass targetClass = targetFeatureDataset.CreateFeatureClass(featureClassName, fields, CLSID, null, esriFeatureType.esriFTSimple, "Shape", "");
//设置要素类子类型
SetSubtypes(sourceFeatureClass, targetClass);
IClassSchemaEdit targetFeatureClassSchema = targetClass as IClassSchemaEdit;
targetFeatureClassSchema.AlterAliasName(sourceFeatureClass.AliasName);
}
private void CreateRelationshipClassSchema(IFeatureDataset sourceFeatureDataset, IFeatureDataset targetFeatureDataset)
{
IRelationshipClassContainer fromRelClassContainer = sourceFeatureDataset as IRelationshipClassContainer;
IEnumRelationshipClass fromRelClasses = fromRelClassContainer.RelationshipClasses;
fromRelClasses.Reset();
IRelationshipClass fromRelClass = fromRelClasses.Next();
while (fromRelClass != null)
{
}
}
//拷贝拓扑关系
private void CreateTopology(IFeatureDataset sourceFeatureDataset, IFeatureDataset targetFeatureDataset)
{
IFeatureWorkspace targetFeatureWorkspace = targetWorkspace as IFeatureWorkspace;
ITopologyContainer toTopoContainer = (ITopologyContainer)targetFeatureDataset;
ITopologyContainer fromTopoContainer = (ITopologyContainer)sourceFeatureDataset;
int topoCount = fromTopoContainer.TopologyCount;
ITopology fromTopology;
ITopology toTopology;
for (int i = 0; i < topoCount; i++)
{
fromTopology = fromTopoContainer.get_Topology(i);
string topologyName = (fromTopology as IDataset).Name;
if (((IWorkspace2)targetWorkspace).get_NameExists(esriDatasetType.esriDTTopology, topologyName))
continue;
ITopologyProperties fromTopoProps = fromTopology as ITopologyProperties;
IEnumFeatureClass fromTopoClasses = fromTopoProps.Classes;
ITopologyRuleContainer fromTopoRuleContainer = fromTopology as ITopologyRuleContainer;
IEnumRule fromRules = fromTopoRuleContainer.Rules;
toTopology = toTopoContainer.CreateTopology(topologyName, toTopoContainer.DefaultClusterTolerance, -1, "");
IFeatureClass fromFeatureClass = fromTopoClasses.Next();
while (fromFeatureClass != null)
{
ITopologyClass fromTopoClass = fromFeatureClass as ITopologyClass;
IFeatureClass toFeatureClass = targetFeatureWorkspace.OpenFeatureClass(((IDataset)fromFeatureClass).Name);
toTopology.AddClass(toFeatureClass, fromTopoClass.Weight, fromTopoClass.XYRank, fromTopoClass.ZRank, false);
fromFeatureClass = fromTopoClasses.Next();
}
IRule fromRule = fromRules.Next();
ITopologyRuleContainer toTopoRuleContainer = toTopology as ITopologyRuleContainer;
while (fromRule != null)
{
ITopologyRule fromTopoRule = fromRule as ITopologyRule;
ITopologyRule topologyRule = new TopologyRuleClass();
topologyRule.AllDestinationSubtypes = fromTopoRule.AllDestinationSubtypes;
topologyRule.AllOriginSubtypes = fromTopoRule.AllOriginSubtypes;
topologyRule.DestinationClassID = fromTopoRule.DestinationClassID;
topologyRule.DestinationSubtype = fromTopoRule.DestinationSubtype;
topologyRule.Name = fromTopoRule.Name;
topologyRule.OriginClassID = fromTopoRule.OriginClassID;
topologyRule.OriginSubtype = fromTopoRule.OriginSubtype;
topologyRule.TopologyRuleType = fromTopoRule.TopologyRuleType;
if (toTopoRuleContainer.get_CanAddRule(topologyRule))
{
toTopoRuleContainer.AddRule(topologyRule);
}
fromRule = fromRules.Next();
}
}
}
//拷贝独立要素类的模式
private void CreateFeatureClassesInWorkspace(IEnumDataset datasets)
{
IDataset dataset = datasets.Next();
while (dataset != null)
{
IFeatureClass sourceClass = dataset as IFeatureClass;
if (((IWorkspace2)targetWorkspace).get_NameExists(esriDatasetType.esriDTFeatureClass, ((IDataset)sourceClass).Name))
dataset = datasets.Next();
else
{
IFields fields = sourceClass.Fields;
if (fields == null) return;
IFieldsEdit fieldsEdit = (IFieldsEdit)fields;
IField fieldUserDefined = new Field();
IFieldEdit fieldEdit = (IFieldEdit)fieldUserDefined;
IGeometryDef geometryDef = new GeometryDefClass();
IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit)geometryDef;
geometryDefEdit.GeometryType_2 = sourceClass.ShapeType;
geometryDefEdit.SpatialReference_2 = ((IGeoDataset)dataset).SpatialReference;
// By setting the grid size to 0, you're allowing ArcGIS to determine the appropriate grid sizes for the feature class.
// If in a personal geodatabase, the grid size will be 1000. If in a file or ArcSDE geodatabase, the grid size
// will be based on the initial loading or inserting of features.
geometryDefEdit.GridCount_2 = 1;
geometryDefEdit.set_GridSize(0, 0);
geometryDefEdit.HasM_2 = bHasM;
geometryDefEdit.HasZ_2 = bHasZ;
// Set standard field properties.
fieldEdit.Name_2 = "SHAPE";
fieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
fieldEdit.GeometryDef_2 = geometryDef;
fieldEdit.IsNullable_2 = true;
fieldEdit.Required_2 = true;
fieldsEdit.set_Field(fields.FindField("SHAPE"), fieldUserDefined);
UID CLSID = new UIDClass();
CLSID.Value = "esriGeoDatabase.Feature";
string featureClassName = ((IDataset)sourceClass).Name;
IFeatureWorkspace featureWorkspace = targetWorkspace as IFeatureWorkspace;
IFeatureClass targetClass = featureWorkspace.CreateFeatureClass(featureClassName, fields, CLSID, null, esriFeatureType.esriFTSimple, "SHAPE", "");
//设置要素类子类型
SetSubtypes(sourceClass, targetClass);
IClassSchemaEdit targetFeatureClassSchema = targetClass as IClassSchemaEdit;
targetFeatureClassSchema.AlterAliasName(sourceClass.AliasName);
}
dataset = datasets.Next();
}
}
//拷贝表模式
private void CreateTablesInWorkspace(IEnumDataset tables)
{
IDataset dataset = tables.Next();
while (dataset != null)
{
ITable sourceTable = dataset as ITable;
if (((IWorkspace2)targetWorkspace).get_NameExists(esriDatasetType.esriDTTable, ((IDataset)sourceTable).Name))
dataset = tables.Next();
else
{
IFields fields = sourceTable.Fields;
if (fields == null) return;
UID CLSID = new UIDClass();
CLSID.Value = "esriGeoDatabase.Object";
string featureClassName = ((IDataset)sourceTable).Name;
IFeatureWorkspace featureWorkspace = targetWorkspace as IFeatureWorkspace;
ITable targetTable = featureWorkspace.CreateTable(featureClassName, fields, CLSID, null, "");
}
dataset = tables.Next();
}
}
//拷贝要素类的子类型定义
private void SetSubtypes(IFeatureClass fromFeatureClass, IFeatureClass toFeatureClass)
{
IWorkspaceDomains targetWorkspaceDomains = (IWorkspaceDomains)targetWorkspace;
ISubtypes toSubtypes = (ISubtypes)toFeatureClass;
ISubtypes fromSubtypes = (ISubtypes)fromFeatureClass;
IEnumSubtype fromEnumSubtype;
string subtypeFieldName;
int subtypeCode;
string subtypeName;
IDomain fromDomain;
if (fromSubtypes.HasSubtype)
{
fromEnumSubtype = fromSubtypes.Subtypes;
subtypeFieldName = fromSubtypes.SubtypeFieldName;
toSubtypes.SubtypeFieldName = subtypeFieldName;
subtypeName = fromEnumSubtype.Next(out subtypeCode);
while (subtypeName != null)
{
fromDomain = fromSubtypes.get_Domain(subtypeCode, subtypeName);
toSubtypes.AddSubtype(subtypeCode, subtypeName);
if (fromDomain != null)
toSubtypes.set_Domain(subtypeCode, subtypeFieldName, targetWorkspaceDomains.get_DomainByName(fromDomain.Name));
subtypeName = fromEnumSubtype.Next(out subtypeCode);
}
}
}
//拷贝数据
private void CopyFeatureDatasetAndFeatureClass()
{
CopyFeatureDatasets();
CopyAloneFeatureClasses();
CopyTablesInWorkspace();
}
private void CopyFeatureDatasets()
{
if (sourceWorkspace == null || targetWorkspace == null) return;
IFeatureWorkspace targetFeatureWorkspace = targetWorkspace as IFeatureWorkspace;
IFeatureWorkspace sourceFeatureWorkspace = sourceWorkspace as IFeatureWorkspace;
IEnumDataset sourceFeatureDatasets = sourceWorkspace.get_Datasets(esriDatasetType.esriDTFeatureDataset);
sourceFeatureDatasets.Reset();
IDataset dataset = sourceFeatureDatasets.Next();
while (dataset != null)
{
IWorkspace workspace = dataset.Workspace;
IWorkspaceEdit workspaceEdit = (IWorkspaceEdit)workspace;
workspaceEdit.StartEditing(true);
workspaceEdit.StartEditOperation();
IFeatureDataset sourceFeatureDataset = sourceFeatureWorkspace.OpenFeatureDataset(dataset.Name);
IEnumDataset sourceFeatureClasses = sourceFeatureDataset.Subsets;
sourceFeatureClasses.Reset();
IDataset sourceDataset = sourceFeatureClasses.Next();
while (sourceDataset != null)
{
switch (sourceDataset.Type)
{
case esriDatasetType.esriDTFeatureClass:
IFeatureClass sourceFeatureClass = sourceDataset as IFeatureClass;
IFeatureClass targetFeatureClass = targetFeatureWorkspace.OpenFeatureClass(sourceDataset.Name);
CopyFeatureClass(sourceFeatureClass, targetFeatureClass);
break;
case esriDatasetType.esriDTGeometricNetwork:
break;
case esriDatasetType.esriDTNetworkDataset:
break;
case esriDatasetType.esriDTRelationshipClass:
break;
case esriDatasetType.esriDTTopology:
//在模式拷贝部分已实现
break;
default:
break;
}
sourceDataset = sourceFeatureClasses.Next();
}
workspaceEdit.StopEditOperation();
workspaceEdit.StopEditing(true);
dataset = sourceFeatureDatasets.Next();
}
}
private void CopyAloneFeatureClasses()
{
if (sourceWorkspace == null || targetWorkspace == null) return;
IFeatureWorkspace targetFeatureWorkspace = targetWorkspace as IFeatureWorkspace;
IFeatureWorkspace sourceFeatureWorkspace = sourceWorkspace as IFeatureWorkspace;
IEnumDataset sourceFeatureClasses = sourceWorkspace.get_Datasets(esriDatasetType.esriDTFeatureClass);
sourceFeatureClasses.Reset();
IDataset dataset = sourceFeatureClasses.Next();
while (dataset != null)
{
IWorkspace workspace = dataset.Workspace;
IWorkspaceEdit workspaceEdit = (IWorkspaceEdit)workspace;
workspaceEdit.StartEditing(true);
workspaceEdit.StartEditOperation();
IFeatureClass sourceFeatureClass = dataset as IFeatureClass;
IFeatureClass targetFeatureClass = targetFeatureWorkspace.OpenFeatureClass(dataset.Name);
CopyFeatureClass(sourceFeatureClass, targetFeatureClass);
dataset = sourceFeatureClasses.Next();
workspaceEdit.StopEditOperation();
workspaceEdit.StopEditing(true);
}
}
private void CopyTablesInWorkspace()
{
if (sourceWorkspace == null || targetWorkspace == null) return;
IFeatureWorkspace targetFeatureWorkspace = targetWorkspace as IFeatureWorkspace;
IFeatureWorkspace sourceFeatureWorkspace = sourceWorkspace as IFeatureWorkspace;
IEnumDataset sourceTables = sourceWorkspace.get_Datasets(esriDatasetType.esriDTTable);
sourceTables.Reset();
IDataset dataset = sourceTables.Next();
while (dataset != null)
{
IWorkspace workspace = dataset.Workspace;
IWorkspaceEdit workspaceEdit = (IWorkspaceEdit)workspace;
workspaceEdit.StartEditing(true);
workspaceEdit.StartEditOperation();
ITable sourceTable = dataset as ITable;
ITable targetTable = targetFeatureWorkspace.OpenTable(dataset.Name);
CopyTables(sourceTable, targetTable);
dataset = sourceTables.Next();
workspaceEdit.StopEditOperation();
workspaceEdit.StopEditing(true);
}
}
private void CopyFeatureClass(IFeatureClass sourceClass, IFeatureClass targetClass)
{
IFeatureBuffer targetFeatureBuffer = targetClass.CreateFeatureBuffer();
IFeatureCursor targetFeatureCursor = targetClass.Insert(true);
txtMessages.Text += "要素类" + ((IDataset)sourceClass).Name + "拷贝开始..." + "\r\n";
txtMessages.Text += DateAndTime.Now.ToString() + "\r\n";
ScrollToBottom(txtMessages);
txtMessages.Update();
IFields sourceFields = sourceClass.Fields;
IFields targetFields = targetClass.Fields;
IFeatureCursor sourceFeatureCursor = sourceClass.Search(null, false);
IFeature sourceFeature = sourceFeatureCursor.NextFeature();
int j = 0;
while (sourceFeature != null)
{
IGeometry geometry = sourceFeature.ShapeCopy;
if (geometry == null) goto nextFeature;
if (geometry is ITopologicalOperator2)
{
ITopologicalOperator2 topo = geometry as ITopologicalOperator2;
topo.IsKnownSimple_2 = false;
topo.Simplify();
}
IZAware za = geometry as IZAware;
za.ZAware = bHasZ;
IMAware ma = geometry as IMAware;
ma.MAware = bHasM;
targetFeatureBuffer.Shape = geometry;
try
{
for (int i = 0; i < targetFields.FieldCount; i++)
{
IField field = new FieldClass();
field = targetFields.get_Field(i);
if (field.Type == esriFieldType.esriFieldTypeOID)
continue;
if (field.Type == esriFieldType.esriFieldTypeGeometry)
continue;
if (field.Name == "Shape_Length")
continue;
if (field.Name == "Shape_Area")
continue;
int fieldIdx = sourceFields.FindField(field.Name);
object fieldValue = sourceFeature.get_Value(fieldIdx);
targetFeatureBuffer.set_Value(i, fieldValue);
}
targetFeatureCursor.InsertFeature(targetFeatureBuffer);
sourceFeature = null;
j++;
}
catch (Exception e)
{
txtMessages.Text += "要素类:" + ((IDataset)sourceClass).Name + " 拷贝有问题" + "\r\n";
txtMessages.Text += e.Message + "\r\n";
txtMessages.Text += DateAndTime.Now.ToString() + "\r\n";
ScrollToBottom(txtMessages);
txtMessages.Update();
}
nextFeature: sourceFeature = sourceFeatureCursor.NextFeature();
}
targetFeatureCursor.Flush();
System.Runtime.InteropServices.Marshal.ReleaseComObject(targetFeatureCursor);
System.Runtime.InteropServices.Marshal.ReleaseComObject(sourceFeatureCursor);
txtMessages.Text += "要素类 " + ((IDataset)sourceClass).Name + " 拷贝结束" + "\r\n";
txtMessages.Text += "总要素个数: " + sourceClass.FeatureCount(null).ToString() + "\r\n";
txtMessages.Text += "拷贝要素个数: " + j.ToString() + "\r\n";
txtMessages.Text += DateAndTime.Now.ToString() + "\r\n\r\n\r\n";
ScrollToBottom(txtMessages);
txtMessages.Update();
}
private void CopyTables(ITable sourceTable, ITable targetTable)
{
IRowBuffer targetRowBuffer = targetTable.CreateRowBuffer();
ICursor targetRowCursor = targetTable.Insert(true);
txtMessages.Text += "要素类" + ((IDataset)sourceTable).Name + "拷贝开始..." + "\r\n";
txtMessages.Text += DateAndTime.Now.ToString() + "\r\n";
ScrollToBottom(txtMessages);
txtMessages.Update();
IFields sourceFields = new FieldsClass();
sourceFields = sourceTable.Fields;
IFields targetFields = new FieldsClass();
targetFields = targetTable.Fields;
ICursor sourceRowCursor = sourceTable.Search(null, false);
IRow sourceRow = sourceRowCursor.NextRow();
int j = 0;
while (sourceRow != null)
{
for (int i = 0; i < targetFields.FieldCount; i++)
{
IField field = new FieldClass();
field = targetFields.get_Field(i);
if (field.Type == esriFieldType.esriFieldTypeOID)
continue;
int fieldIdx = sourceFields.FindField(field.Name);
object fieldValue = sourceRow.get_Value(fieldIdx);
targetRowBuffer.set_Value(i, fieldValue);
}
try
{
targetRowCursor.InsertRow(targetRowBuffer);
sourceRow = null;
j++;
}
catch (Exception e)
{
txtMessages.Text += "表:" + ((IDataset)sourceTable).Name + " 拷贝有问题" + "\r\n";
txtMessages.Text += e.Message + "\r\n";
txtMessages.Text += DateAndTime.Now.ToString() + "\r\n";
ScrollToBottom(txtMessages);
txtMessages.Update();
}
sourceRow = sourceRowCursor.NextRow();
}
targetRowCursor.Flush();
txtMessages.Text += "表 " + ((IDataset)sourceTable).Name + " 拷贝结束" + "\r\n";
txtMessages.Text += "总对象个数: " + sourceTable.RowCount(null).ToString() + "\r\n";
txtMessages.Text += "拷贝对象个数: " + j.ToString() + "\r\n";
txtMessages.Text += DateAndTime.Now.ToString() + "\r\n\r\n\r\n";
ScrollToBottom(txtMessages);
txtMessages.Update();
}
private void CopyFeatureClass2(IFeatureClass sourceClass, IFeatureClass targetClass)
{
txtMessages.Text += "要素类" + ((IDataset)sourceClass).Name + "拷贝开始..." + "\r\n";
txtMessages.Text += DateAndTime.Now.ToString() + "\r\n";
ScrollToBottom(txtMessages);
txtMessages.Update();
IFeatureCursor sourceFeatureCursor = sourceClass.Search(null, false);
IFeature sourceFeature = sourceFeatureCursor.NextFeature();
int j = 0;
while (sourceFeature != null)
{
IFields sourceFields = new FieldsClass();
sourceFields = sourceFeature.Fields;
IFeature targetFeature = targetClass.CreateFeature();
IFields targetFields = new FieldsClass();
targetFields = targetFeature.Fields;
IGeometry geometry = sourceFeature.ShapeCopy;
if (geometry == null) goto nextFeature;
if (geometry is ITopologicalOperator2)
{
ITopologicalOperator2 topo = geometry as ITopologicalOperator2;
topo.IsKnownSimple_2 = false;
topo.Simplify();
}
IZAware za = geometry as IZAware;
za.ZAware = bHasZ;
IMAware ma = geometry as IMAware;
ma.MAware = bHasM;
targetFeature.Shape = geometry;
ISubtypes subtypes = (ISubtypes)targetClass;
IRowSubtypes rowSubtypes = (IRowSubtypes)targetFeature;
rowSubtypes.InitDefaultValues();
for (int i = 0; i < targetFields.FieldCount; i++)
{
IField field = new FieldClass();
field = targetFields.get_Field(i);
if (field.Type == esriFieldType.esriFieldTypeOID)
continue;
if (field.Type == esriFieldType.esriFieldTypeGeometry)
continue;
if (field.Name == "Shape_Length")
continue;
if (field.Name == "Shape_Area")
continue;
int fieldIdx = sourceFields.FindField(field.Name);
object fieldValue = sourceFeature.get_Value(fieldIdx);
targetFeature.set_Value(i, fieldValue);
}
try
{
targetFeature.Store();
j++;
}
catch (Exception e)
{
txtMessages.Text += "要素类 " + ((IDataset)sourceClass).Name + " 拷贝有问题" + "\r\n";
txtMessages.Text += e.Message + "\r\n";
txtMessages.Text += DateAndTime.Now.ToString() + "\r\n";
ScrollToBottom(txtMessages);
txtMessages.Update();
}
nextFeature: sourceFeature = sourceFeatureCursor.NextFeature();
}
System.Runtime.InteropServices.Marshal.ReleaseComObject(sourceFeatureCursor);
txtMessages.Text += "要素类 " + ((IDataset)sourceClass).Name + " 拷贝结束" + "\r\n";
txtMessages.Text += "总要素个数: " + sourceClass.FeatureCount(null).ToString() + "\r\n";
txtMessages.Text += "拷贝要素个数: " + j.ToString() + "\r\n";
txtMessages.Text += DateAndTime.Now.ToString() + "\r\n\r\n\r\n";
ScrollToBottom(txtMessages);
txtMessages.Update();
}
private IEnumGxObject AddDatasetWithGxDialog()
{
try
{
IGxDialog gxDlg = new GxDialogClass();
IGxObjectFilter gxObjFilter = new GxFilterFileGeodatabasesClass();
IGxObjectFilterCollection filters = gxDlg as IGxObjectFilterCollection;
filters.AddFilter(gxObjFilter, true);
gxObjFilter = new GxFilterPersonalGeodatabasesClass();
filters.AddFilter(gxObjFilter, false);
//gxDlg.Title = "Open A Geodatabase";
////gxDlg.ObjectFilter = gxObjFilter;
//gxDlg.AllowMultiSelect = false;
//gxDlg.RememberLocation = true;
IEnumGxObject gxObjects;
bool open = gxDlg.DoModalOpen(0, out gxObjects);
return gxObjects;
}
catch (Exception e)
{
txtMessages.Text += "打开数据库出错! \r\n";
txtMessages.Text += e.Message + "\r\n";
txtMessages.Text += DateAndTime.Now.ToString() + "\r\n";
ScrollToBottom(txtMessages);
txtMessages.Update();
}
return null;
}
private void TreeViewAddNodes(IEnumDataset datasets, TreeNode currentNode)
{
if (datasets == null)
return;
datasets.Reset();
IDataset dataset = datasets.Next() as IDataset;
while (dataset != null)
{
if (dataset is IFeatureClass)
{
TreeNode datasetNode = new TreeNode(dataset.Name);
currentNode.Nodes.Add(datasetNode);
}
else if (dataset is IFeatureDataset)
{
TreeNode datasetName = new TreeNode(dataset.Name);
IFeatureDataset featureDataset = dataset as IFeatureDataset;
IEnumDataset pEnumDataset = featureDataset.Subsets;
pEnumDataset.Reset();
IDataset pDataSet = pEnumDataset.Next();
while (pDataSet != null)
{
if ((pDataSet is ITopology) || (pDataSet is IGeometricNetwork) || (pDataSet is INetworkDataset))
{
pDataSet = pEnumDataset.Next();
}
else
{
TreeNode featureClassName = new TreeNode(pDataSet.Name);
datasetName.Nodes.Add(featureClassName);
pDataSet = pEnumDataset.Next();
}
}
currentNode.Nodes.Add(datasetName);
}
dataset = datasets.Next() as IDataset;
}
}
private void CopyFeatureDatasetByGeoTransfer()
{
if (sourceWorkspace == null || targetWorkspace == null) return;
IEnumDataset sourceFeatureDatasets = sourceWorkspace.get_Datasets(esriDatasetType.esriDTFeatureDataset);
sourceFeatureDatasets.Reset();
IDataset dataset = sourceFeatureDatasets.Next();
while (dataset != null)
{
IGeoDataset geoDataset = dataset as IGeoDataset;
IFeatureDataset sourceFeatureDataset = dataset as IFeatureDataset;
CopyDataByGeoTransfer(sourceWorkspace, targetWorkspace, sourceFeatureDataset.Name, esriDatasetType.esriDTFeatureDataset);
dataset = sourceFeatureDatasets.Next();
}
}
public void CopyDataByGeoTransfer(IWorkspace sourceWorkspace, IWorkspace targetWorkspace, String objectName, esriDatasetType esriDataType)
{
if ((sourceWorkspace.Type == esriWorkspaceType.esriFileSystemWorkspace) || (targetWorkspace.Type == esriWorkspaceType.esriFileSystemWorkspace))
{
return;
}
IDataset sourceWorkspaceDataset = (IDataset)sourceWorkspace;
IWorkspaceName sourceWorkspaceName = (IWorkspaceName)sourceWorkspaceDataset.FullName;
//create target workspace name
IDataset targetWorkspaceDataset = (IDataset)targetWorkspace;
IWorkspaceName targetWorkspaceName = (IWorkspaceName)targetWorkspaceDataset.FullName;
//Create Name Object Based on data type
IDatasetName datasetName;
switch (esriDataType)
{
case esriDatasetType.esriDTFeatureDataset:
IFeatureDatasetName inFeatureDatasetName = new FeatureDatasetNameClass();
datasetName = (IDatasetName)inFeatureDatasetName;
break;
case esriDatasetType.esriDTFeatureClass:
IFeatureClassName inFeatureClassName = new FeatureClassNameClass();
datasetName = (IDatasetName)inFeatureClassName;
break;
case esriDatasetType.esriDTTable:
ITableName inTableName = new TableNameClass();
datasetName = (IDatasetName)inTableName;
break;
case esriDatasetType.esriDTGeometricNetwork:
IGeometricNetworkName inGeometricNetworkName = new GeometricNetworkNameClass();
datasetName = (IDatasetName)inGeometricNetworkName;
break;
case esriDatasetType.esriDTRelationshipClass:
IRelationshipClassName inRelationshipClassName = new RelationshipClassNameClass();
datasetName = (IDatasetName)inRelationshipClassName;
break;
case esriDatasetType.esriDTNetworkDataset:
INetworkDatasetName inNetworkDatasetName = new NetworkDatasetNameClass();
datasetName = (IDatasetName)inNetworkDatasetName;
break;
case esriDatasetType.esriDTTopology:
ITopologyName inTopologyName = new TopologyNameClass();
datasetName = (IDatasetName)inTopologyName;
break;
default:
return;
}
// Set the name of the object to be copied
datasetName.WorkspaceName = (IWorkspaceName)sourceWorkspaceName;
datasetName.Name = objectName;
//Setup mapping for copy/paste
IEnumNameMapping fromNameMapping;
ESRI.ArcGIS.esriSystem.IEnumNameEdit editFromName;
ESRI.ArcGIS.esriSystem.IEnumName fromName = new NamesEnumerator();
editFromName = (ESRI.ArcGIS.esriSystem.IEnumNameEdit)fromName;
editFromName.Add((ESRI.ArcGIS.esriSystem.IName)datasetName);
ESRI.ArcGIS.esriSystem.IName toName = (ESRI.ArcGIS.esriSystem.IName)targetWorkspaceName;
// Generate name mapping
ESRI.ArcGIS.Geodatabase.IGeoDBDataTransfer geoDBDataTransfer = new GeoDBDataTransferClass();
IEnumNameMapping enumChildMapping;
INameMapping childMapping;