- 获取要素类的路径
public static string GetPath(IFeatureClass ifc) { string fullpath = string.Empty; FeatureDataset ifdt = ifc as FeatureDataset; if (ifdt != null) { string path = ifdt.Workspace.PathName; if (ifc.FeatureDataset != null) { path += "\\" + ifc.FeatureDataset.Name; } fullpath = path; } return fullpath; }
- 清空要素类
string fcname = ifc.AliasName;//ifc为目标要素类 IQueryFilter pQueryFilter = new QueryFilterClass(); pQueryFilter.WhereClause = "1>0"; ITable pTable = ifc as ITable; DialogResult result = MessageBox.Show("是否清空[" + fcname + "]图层", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information); if (result == DialogResult.OK) { IDataset pDataset = ifc as IDataset; IWorkspace wk = pDataset.Workspace; if (wk.IsDirectory()) { pTable.DeleteSearchedRows(pQueryFilter);//pTable.DeleteSearchedRows(pQueryFilter); System.Runtime.InteropServices.Marshal.ReleaseComObject(pQueryFilter);//释放内存 } else { wk.ExecuteSQL("delete from " + pDataset.Name + " where 1>0"); } MessageBox.Show("清除成功"); }
- 删除要素类
IDataset set = tmpfeatureclass as IDataset; set.CanDelete(); set.Delete();
- 创建要素类
public static IFeatureClass CreateFeatureClass(IWorkspace wk, IFeatureDataset fdt, ISpatialReference sr, string featureclassname, esriGeometryType geometryType) { IFeatureWorkspace fwk = wk as IFeatureWorkspace; string featureClassName = featureclassname; ESRI.ArcGIS.esriSystem.UID CLSID = null; ESRI.ArcGIS.esriSystem.UID CLSEXT = null; System.String strConfigKeyword = null; esriGeometryType geometrytype = esriGeometryType.esriGeometryPolygon; CLSID = new ESRI.ArcGIS.esriSystem.UIDClass(); if (geometryType == esriGeometryType.esriGeometryPolygon) { CLSID.Value = "esriGeoDatabase.Feature"; geometrytype = esriGeometryType.esriGeometryPolygon; } else if (geometryType == esriGeometryType.esriGeometryPolyline) { CLSID.Value = "{52353152-891A-11D0-BEC6-00805F7C4268}"; geometrytype = esriGeometryType.esriGeometryPolyline; } else if (geometryType == esriGeometryType.esriGeometryPoint) { //TODO 设置点类型的CLSID //CLSID.Value = "点的值"; geometrytype = esriGeometryType.esriGeometryPoint; } ISpatialReference reference = null; if (fdt == null) { if (sr == null) { ISpatialReferenceFactory iSpatialRefFac = new SpatialReferenceEnvironment(); reference = CreateProjectedCoordinate(esriSRProjCS4Type.esriSRProjCS_Xian1980_3_Degree_GK_Zone_38); } else { reference = sr; } } ESRI.ArcGIS.Geodatabase.IObjectClassDescription objectClassDescription = new ESRI.ArcGIS.Geodatabase.FeatureClassDescription(); ESRI.ArcGIS.Geodatabase.IFields fields = null; if (fields == null) { fields = objectClassDescription.RequiredFields; } //找到图形字段名称 System.String strShapeField = ""; for (int j = 0; j < fields.FieldCount; j++) { if (fields.get_Field(j).Type == ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeGeometry) { IGeometryDefEdit geoedit = (IGeometryDefEdit)fields.get_Field(j).GeometryDef; geoedit.GeometryType_2 = geometrytype; strShapeField = fields.get_Field(j).Name; if (fdt == null) { geoedit.SpatialReference_2 = reference; } break; } } ESRI.ArcGIS.Geodatabase.IFieldChecker fieldChecker = new ESRI.ArcGIS.Geodatabase.FieldChecker(); ESRI.ArcGIS.Geodatabase.IEnumFieldError enumFieldError = null; ESRI.ArcGIS.Geodatabase.IFields validatedFields = null;//可以是具体的IFields;如某个要素类的字段cfeatureclass.Fields fieldChecker.ValidateWorkspace = (ESRI.ArcGIS.Geodatabase.IWorkspace)wk; fieldChecker.Validate(fields, out enumFieldError, out validatedFields); ESRI.ArcGIS.Geodatabase.IFeatureClass featureClass; if (fdt != null) featureClass = fdt.CreateFeatureClass(featureClassName, validatedFields, CLSID, CLSEXT, esriFeatureType.esriFTSimple, strShapeField, strConfigKeyword); else { featureClass = fwk.CreateFeatureClass(featureClassName, validatedFields, CLSID, CLSEXT, ESRI.ArcGIS.Geodatabase.esriFeatureType.esriFTSimple, strShapeField, strConfigKeyword); } return featureClass; }
-
导出要素类为shp
//打开目标shapefile文件的路径 IWorkspaceFactory pFactory = new ShapefileWorkspaceFactoryClass(); IWorkspace pOutWorkspace = null; string exportFileShortName = System.IO.Path.GetFileNameWithoutExtension(destiShpPath); string exportFilePath = System.IO.Path.GetDirectoryName(destiShpPath); //存在,删除 if (File.Exists(destiShpPath)) { DialogResult r2 = MessageBox.Show("文件名已存在,要覆盖吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (System.Windows.Forms.DialogResult.Yes == r2) { DirectoryInfo fold; fold = new DirectoryInfo(exportFilePath); FileInfo[] files = fold.GetFiles(exportFileShortName + ".*"); foreach (FileInfo file in files) { file.Delete(); } } } pOutWorkspace = pFactory.OpenFromFile(exportFilePath, 0) ExportToShapefile(ifc, pOutWorkspace, exportFileShortName)//ifc为目标要素类
private bool ExportToShapefile(IFeatureClass pFeatureClass, IWorkspace outWorkspace, string fileName) { try { //输入的要素类空间 IDataset inDataSet = pFeatureClass as IDataset; IFeatureClassName inFCName = inDataSet.FullName as IFeatureClassName; IWorkspace inWorkspace = inDataSet.Workspace; //输出的shapfile文件的工作空间 IDataset outDataSet = outWorkspace as IDataset; IWorkspaceName outWorkspaceName = outDataSet.FullName as IWorkspaceName; IFeatureClassName outFCName = null; IDatasetName dataSetName = null; outFCName = new FeatureClassNameClass(); dataSetName = outFCName as IDatasetName; dataSetName.WorkspaceName = outWorkspaceName; dataSetName.Name = fileName; //检查字段的有效性 IFieldChecker fieldChecker = new FieldCheckerClass(); fieldChecker.InputWorkspace = inWorkspace; fieldChecker.ValidateWorkspace = outWorkspace; IFields fields = pFeatureClass.Fields; IFields outFields = null; IEnumFieldError enumFieldError = null; fieldChecker.Validate(fields, out enumFieldError, out outFields); //调用IFeatureDataConverter接口进行数据转换 IFeatureDataConverter featureDataConverter = new FeatureDataConverterClass(); featureDataConverter.ConvertFeatureClass(inFCName, null, null, outFCName, null, outFields, "", 100, 0); } catch (Exception ex) { return false; } return true; }
- 导出为dBASE表
//打开目标shapefile文件的路径 IWorkspaceFactory pFactory = new ShapefileWorkspaceFactoryClass(); IWorkspace pOutWorkspace = null; string exportFileShortName = System.IO.Path.GetFileNameWithoutExtension(destiShpPath); string exportFilePath = System.IO.Path.GetDirectoryName(destiShpPath); pOutWorkspace = pFactory.OpenFromFile(exportFilePath, 0); ExportToDbf(ifc, destiShpPath, pOutWorkspace);//ifc为目标要素类
private void ExportToDbf(IFeatureClass ifc, string dbfFileName, IWorkspace wk) { string shapeFileName = dbfFileName; string pnlfilename = shapeFileName.Substring(0, shapeFileName.Length - 4) + ".dbf"; if (File.Exists(pnlfilename) == true) File.Delete(pnlfilename); IFeatureWorkspace pFWS; IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactoryClass(); string name = null; FileInfo file = new FileInfo(pnlfilename); name = file.Name; string path = file.Directory.FullName + "\\"; pFWS = pWorkspaceFactory.OpenFromFile(path, 0) as IFeatureWorkspace; IField fd = null; IField pfield = null; IFields fields = new FieldsClass(); for (int i = 0; i < ifc.Fields.FieldCount; i++) { IFieldsEdit fieldsEdit = fields as IFieldsEdit; IField field = new FieldClass(); IFieldEdit fieldEdit = field as IFieldEdit; fd = ifc.Fields.Field[i]; if (fd.Name == "OBJECTID" || fd.Name == "SHAPE" || fd.Name == "SHAPE_Length" || fd.Name == "SHAPE_Area") { continue; } fieldEdit.Length_2 = fd.Length; fieldEdit.Name_2 = fd.Name; fieldEdit.Type_2 = fd.Type; fieldEdit.AliasName_2 = fd.AliasName; //fieldEdit.DefaultValue_2 = fd.DefaultValue; fieldsEdit.AddField(field); } ESRI.ArcGIS.Geodatabase.IFieldChecker fieldChecker = new ESRI.ArcGIS.Geodatabase.FieldChecker(); ESRI.ArcGIS.Geodatabase.IEnumFieldError enumFieldError = null; ESRI.ArcGIS.Geodatabase.IFields validatedFields = null; fieldChecker.ValidateWorkspace = (ESRI.ArcGIS.Geodatabase.IWorkspace)wk; fieldChecker.Validate(fields, out enumFieldError, out validatedFields); ITable table = pFWS.CreateTable(name, validatedFields, (ESRI.ArcGIS.esriSystem.UID)null, (ESRI.ArcGIS.esriSystem.UID)null, ""); IWorkspaceEdit jWorkspaceEdit = (IWorkspaceEdit)wk; jWorkspaceEdit.StartEditing(true); jWorkspaceEdit.StartEditOperation(); ITable pnlTable = ifc as ITable; IFeatureCursor mFeatureSearchCursor = ifc.Search(null, false); IFeature mFeature = mFeatureSearchCursor.NextFeature(); while (mFeature != null) { IRowBuffer row = table.CreateRowBuffer(); for (int j = 0; j < table.Fields.FieldCount; j++) { pfield = table.Fields.Field[j]; int t = ifc.Fields.FindField(pfield.Name); if (t > 0) { object obj = mFeature.get_Value(t); if (obj == null || obj.ToString() == "") { continue; } row.set_Value(j, obj); } } try { table.Insert(true).InsertRow(row); } catch (Exception EX) { MessageBox.Show(EX.Message); } System.Runtime.InteropServices.Marshal.ReleaseComObject(mFeature);//释放要素 mFeature = mFeatureSearchCursor.NextFeature(); } System.Runtime.InteropServices.Marshal.ReleaseComObject(mFeatureSearchCursor); jWorkspaceEdit.StopEditOperation(); jWorkspaceEdit.StopEditing(true); MessageBox.Show("OK"); }
- 查看要素类信息
- 属性信息
FeatureDataset fdt = m_feacls as FeatureDataset;//m_feacls 为目标要素类 if (fdt != null) { String name = fdt.Name; } String aliasName = m_feacls.AliasName; String featureCount = m_feacls.FeatureCount(null).ToString(); String shapeType = m_feacls.ShapeType.ToString(); IGeoDataset igt = m_feacls as IGeoDataset; if (igt != null) { try { IEnvelope env = igt.Extent; if (!env.IsEmpty) { String xmin = env.XMin.ToString(); String xmax = env.XMax.ToString(); String ymin = env.YMin.ToString(); String ymax = env.YMax.ToString(); } } catch (Exception ex) { } }
- 空间信息
IGeoDataset pGeoDataset = m_feacls as IGeoDataset;m_feacls 为目标要素类 if (pGeoDataset != null) { try { ISpatialReference sr = pGeoDataset.SpatialReference; IFeatureDataset idt = pGeoDataset as IFeatureDataset; IGeographicCoordinateSystem igpc = sr as IGeographicCoordinateSystem; IProjectedCoordinateSystem ipjc = sr as IProjectedCoordinateSystem; string name1 = ("空间参考名称:" + sr.Name); string name2 = ("WKID:" + sr.FactoryCode.ToString() + " 权限: EPSG"); if (ipjc != null) { IProjection ip = ipjc.Projection; string name3 = (" 投影方式(Projection):" + ip.Name); string name4 = (" X偏移量(False_Easting):" + ipjc.FalseEasting.ToString()); string name5 = (" Y偏移量(False_Northing):" + ipjc.FalseNorthing.ToString()); string name6 = (" 中央经线(Central_Meridian):" + ipjc.CentralMeridian[true].ToString()); //string name7 = (" 缩放比例(Scale_Factor):" + ipjc.ScaleFactor.ToString()); //string name8 = ("起始经度(Latitude_Of_Origin):" + ipjc..ToString()); string name9= (" 距离单位(Linear Unit):" + ipjc.CoordinateUnit.Name + "(" + ipjc.CoordinateUnit.ConversionFactor + ")"); IGeographicCoordinateSystem ig = ipjc.GeographicCoordinateSystem; string name10 = ("地理坐标系:"); string name11 = (" 名称:" + ig.Name); string name12 = (" 弧度单位(Angular Unit):" + ig.CoordinateUnit.Name + "(" + ig.CoordinateUnit.ConversionFactor + ")"); string name13 = (" 本初子午线(Prime Meridian):" + ig.PrimeMeridian.Name + "(" + ig.PrimeMeridian.Longitude + ")"); string name14 = (" 大地基准面(Datum):" + ig.Datum.Name); string name15 = (" 椭球体(Spheroid):" + ig.Datum.Spheroid.Name); string name16 = (" 半长轴(Semimajor Axis):" + ig.Datum.Spheroid.SemiMajorAxis); string name17 = (" 半短轴(Semiminor Axis):" + ig.Datum.Spheroid.SemiMinorAxis); string name18 = (" 扁率:" + ig.Datum.Spheroid.Flattening); } else if (igpc != null) { string name19 = ("地理坐标系:"); string name20 = (" 名称:" + igpc.Name); string name21 = (" 弧度单位(Angular Unit):" + igpc.CoordinateUnit.Name + "(" + igpc.CoordinateUnit.ConversionFactor + ")"); string name22 = (" 本初子午线(Prime Meridian):" + igpc.PrimeMeridian.Name + "(" + igpc.PrimeMeridian.Longitude + ")"); string name23 = (" 大地基准面(Datum):" + igpc.Datum.Name); string name24 = (" 椭球体(Spheroid):" + igpc.Datum.Spheroid.Name); string name25 = (" 半长轴(Semimajor Axis):" + igpc.Datum.Spheroid.SemiMajorAxis); string name26 = (" 半短轴(Semiminor Axis):" + igpc.Datum.Spheroid.SemiMinorAxis); string name27 = (" 扁率:" + igpc.Datum.Spheroid.Flattening); } } catch { } }
- 属性信息
- 要素类转换为DataTable
ITable itable = ifc as ITable; DataTable pAttDT = null; string pFieldName; string pFieldValue; DataRow pDataRow; if (this.itable != null) { try { IRow tempRow = null; //根据IFeatureClass字段结构初始化一个表结构 pAttDT = InitTableByFeaCls(); ITable pFeatTable = this.itable; int pFieldCout = pFeatTable.Fields.FieldCount; ICursor pCursor = pFeatTable.Search(m_queryfilter, false);//m_queryfilter为过滤条件,可为空可为具体 tempRow = pCursor.NextRow(); while (tempRow != null) { pDataRow = pAttDT.NewRow(); for (int j = 0; j < pFieldCout; j++) { pFieldName = pFeatTable.Fields.get_Field(j).Name; esriFieldType fildType = pFeatTable.Fields.get_Field(j).Type; if ("SHAPE" == pFieldName || "Shape" == pFieldName) { continue; } pFieldValue = tempRow.get_Value(j).ToString(); if (fildType == esriFieldType.esriFieldTypeSingle || fildType == esriFieldType.esriFieldTypeDouble) { if ("" != pFieldValue) { double tmpd = double.Parse(pFieldValue); pFieldValue = tmpd.ToString(); } else { pFieldValue = "0"; } } pDataRow[pFieldName] = pFieldValue; } pAttDT.Rows.Add(pDataRow); tempRow = pCursor.NextRow(); } } catch (Exception ex) { Console.Write(ex.StackTrace); MessageBox.Show(ex.Message); return null; } }
//根据FeatureClass获取表格头 private DataTable InitTableByFeaCls() {
DataTable tb = new DataTable(); IFields fields = itable.Fields; IField field; for (int i = 0; i < fields.FieldCount; i++) { field = fields.get_Field(i); DataColumn column = new DataColumn(); if ("SHAPE" == field.Name || "Shape" == field.Name ) { //过滤掉shape数据; continue; } column.ColumnName = field.Name; column.Caption = field.AliasName; column.DataType = typeof(string); tb.Columns.Add(column); } return tb; }ITable itable = ifc as ITable;
- DataTable转为ITable
public ITable ToITable(DataTable mTable) { try { #region 新建表字段 IField pField = null; IFields fields = new FieldsClass(); IFieldsEdit fieldsEdit = (IFieldsEdit)fields; int num = comboBox3.Items.Count; fieldsEdit.FieldCount_2 = num + 1; pField = new FieldClass(); IFieldEdit fieldEdit = (IFieldEdit)pField; fieldEdit.Name_2 = "OID"; fieldEdit.Type_2 = esriFieldType.esriFieldTypeOID; fieldEdit.Editable_2 = true; fieldsEdit.set_Field(0, pField); //添加字段 for (int i = 0; i < num; i++) { IField pField1 = new FieldClass(); IFieldEdit fieldEdit1 = (IFieldEdit)pField1; fieldEdit1.Name_2 = comboBox3.Items[i].ToString(); //DataRow pRrow = mTable.Rows[0]; //double doubleVal = 0; //if (Double.TryParse(pRrow.ItemArray.GetValue(i).ToString(), NumberStyles.Number, null, out doubleVal) == true) //{ // fieldEdit1.Type_2 = esriFieldType.esriFieldTypeString; //} fieldEdit1.Type_2 = esriFieldType.esriFieldTypeString; fieldEdit1.Editable_2 = true; fieldsEdit.set_Field(i + 1, pField1); } #endregion ShapefileWorkspaceFactoryClass class2 = new ShapefileWorkspaceFactoryClass(); IWorkspace iWorkspace; IFeatureDataset feaDataset; feaDataset = ifc.FeatureDataset;//ifc为要素类,通过ifc获取ITable的工作空间 iWorkspace = feaDataset.Workspace; string path = iWorkspace.PathName; IFeatureWorkspace pFWS = iWorkspace as IFeatureWorkspace; //删除已有的 try { OleDbConnection con = new OleDbConnection("Data Source=" + path + ";Provider=Microsoft.Jet.OLEDB.4.0;"); con.Open(); string sql = "drop table Sheet"; OleDbCommand command; command = new OleDbCommand(sql, con); command.ExecuteNonQuery(); con.Close(); } catch { con.Close(); } //创建空表 ESRI.ArcGIS.Geodatabase.ITable pTable; pTable = pFWS.CreateTable("Sheet", fieldsEdit, null, null, ""); //获取表中记录数 int count = mTable.Rows.Count; //转换为ITable中的数据 for (int k = 0; k < count; k++) { //ITable 的记录 IRow row = pTable.CreateRow(); DataRow pRrow = mTable.Rows[k]; //列元素 int rowNum = pRrow.ItemArray.Length; // 添加记录 for (int n = 1; n < rowNum + 1; n++) { row.set_Value(n, pRrow.ItemArray.GetValue(n - 1)); row.Store(); } } return pTable; } catch (Exception ex) { MessageBox.Show("Table转换失败" + ex.Message); return null; } }
ArcEngine操作要素类
最新推荐文章于 2020-12-19 13:48:21 发布