C# ArcGIS 创建shp文件,然后将SHP文件转成GDB

创建点shapefile
1 /// 创建点shapefile
2 /// 
3 /// target point shapefile path
4 /// target point shapefile name
5 public static void createPointShapefile(IMap map, string filePath, string fileName)
6 {
7 //建立shape字段
8 IFields pFields = new FieldsClass();
9 IFieldsEdit pFieldsEdit = pFields as IFieldsEdit;
10 IField pField = new FieldClass();
11 IFieldEdit pFieldEdit = pField as IFieldEdit;
12 pFieldEdit.Name_2 = "Shape";
13 pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
14 
15 //设置geometry definition
16 IGeometryDef pGeometryDef = new GeometryDefClass();
17 IGeometryDefEdit pGeometryDefEdit = pGeometryDef as IGeometryDefEdit;
18 pGeometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint;//点、线、面
19 pGeometryDefEdit.SpatialReference_2 = map.SpatialReference;
20 pFieldEdit.GeometryDef_2 = pGeometryDef;
21 pFieldsEdit.AddField(pField);
22 
23 //新建字段
24 pField = new FieldClass();
25 pFieldEdit = pField as IFieldEdit;
26 pFieldEdit.Length_2 = 10;
27 pFieldEdit.Name_2 = "id";
28 pFieldEdit.AliasName_2 = "id";
29 pFieldEdit.Type_2 = esriFieldType.esriFieldTypeSmallInteger;
30 pFieldsEdit.AddField(pField);
31 //继续增加其它字段
32 
33 IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactory();
34 IFeatureWorkspace pFeatureWorkspace = pWorkspaceFactory.OpenFromFile(filePath, 0) as IFeatureWorkspace;
35 
36 //IWorkspaceFactory pWorkspaceFactory = new FileGDBWorkspaceFactoryClass();
37 //IFeatureWorkspace pFeatureWorkspace = pWorkspaceFactory.OpenFromFile(filePath, 0) as IFeatureWorkspace;
38 
39 int i = fileName.IndexOf(".shp");
40 if (i == -1)
41 pFeatureWorkspace.CreateFeatureClass(fileName + ".shp", pFields, null, null, esriFeatureType.esriFTSimple, "Shape", "");
42 else
43 pFeatureWorkspace.CreateFeatureClass(fileName, pFields, null, null, esriFeatureType.esriFTSimple, "Shape", "");
44 
45 //MessageBox.Show("OK");
46 
47 }
2 将选中要素另存至GDB
  另存为shapefile类似,修改workspacefactory为ShapefileWorkspaceFactoryClass,修改对应路径即可。
1 /// 将选中的line feature作为线文件存至gdb
2 /// 
3 /// select layer
4 /// gdb路径
5 /// 文件名称
6 public static void saveSelectLineFeatureToGDB(IMap map,IFeatureLayer featureLayer,string gdbPath,string name)
7 {
8 IFeatureClass inputFeatureClass = featureLayer.FeatureClass;
9 IDataset inputDataset = (IDataset)inputFeatureClass;
10 IDatasetName inputDatasetName = (IDatasetName)inputDataset.FullName;
11 
12 // Get the layer's selection set. 
13 IFeatureSelection featureSelection = (IFeatureSelection)featureLayer;
14 ISelectionSet selectionSet = featureSelection.SelectionSet;
15 
16 IPropertySet ps = new PropertySetClass();
17 ps.SetProperty("DATABASE", gdbPath);
18 
19 IWorkspaceFactory wsf = new FileGDBWorkspaceFactoryClass();
20 IWorkspace ws = null;
21 try
22 {
23 ws = wsf.Open(ps, 0);
24 }
25 catch (Exception e)
26 {
27 Console.WriteLine(e.Message);
28 }
29 IDataset ds = (IDataset)ws;
30 IWorkspaceName wsName = (IWorkspaceName)ds.FullName;
31 IFeatureClassName featClsName = new FeatureClassNameClass();
32 IDatasetName dsName = (IDatasetName)featClsName;
33 dsName.WorkspaceName = wsName;
34 dsName.Name = name;
35 
36  Use the IFieldChecker interface to make sure all of the field names are valid for a shapefile. 
37 IFieldChecker fieldChecker = new FieldCheckerClass();
38 IFields shapefileFields = null;
39 IEnumFieldError enumFieldError = null;
40 fieldChecker.InputWorkspace = inputDataset.Workspace;
41 fieldChecker.ValidateWorkspace = ws;
42 fieldChecker.Validate(inputFeatureClass.Fields, out enumFieldError, out shapefileFields);
43 
44 // At this point, reporting/inspecting invalid fields would be useful, but for this example it's omitted.
45 
46 // We also need to retrieve the GeometryDef from the input feature class. 
47 int shapeFieldPosition = inputFeatureClass.FindField(inputFeatureClass.ShapeFieldName);
48 IFields inputFields = inputFeatureClass.Fields;
49 
50 IField shapeField = inputFields.get_Field(shapeFieldPosition);
51 IGeometryDef geometryDef = shapeField.GeometryDef;
52 
53 IGeometryDef pGeometryDef = new GeometryDef();
54 IGeometryDefEdit pGeometryDefEdit = pGeometryDef as IGeometryDefEdit;
55 pGeometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolyline;
56 pGeometryDefEdit.SpatialReference_2 = map.SpatialReference;
57 
58 // Now we can create a feature data converter. 
59 IFeatureDataConverter2 featureDataConverter2 = new FeatureDataConverterClass();
60 IEnumInvalidObject enumInvalidObject = featureDataConverter2.ConvertFeatureClass(inputDatasetName, null,
61 selectionSet, null, featClsName, pGeometryDef, shapefileFields, "", 1000, 0);
62 
63 // Again, checking for invalid objects would be useful at this point...
64 
65 inputFeatureClass = null;
66 ds = null;
67 ws = null;
68 wsf = null;
69 }

你可能感兴趣的:(ArcGIS,C#)