/// <summary> /// 创建栅格字段 /// </summary> /// <param name="pRasterFielsName">栅格字段名</param> /// <param name="pSpatialRes">栅格空间参考</param> /// <param name="eError"></param> /// <returns>返回字段</returns> private IField2 CreateRasterField(string pRasterFielsName, ISpatialReference pSpatialRes, bool isManaged) { IField2 pField = new FieldClass(); IFieldEdit2 pFieldEdit = pField as IFieldEdit2; pFieldEdit.Name_2 = pRasterFielsName; pFieldEdit.AliasName_2 = pRasterFielsName; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeRaster; IRasterDef pRasterDef = new RasterDefClass(); pRasterDef.Description = "this is Raster catalog"; if (pSpatialRes == null) { //如果空间参考为空,则设置为UnknownCoordinateSystemClass pSpatialRes = new UnknownCoordinateSystemClass(); } //only for PGDB pRasterDef.IsManaged = isManaged; pRasterDef.SpatialReference = pSpatialRes; pFieldEdit.RasterDef = pRasterDef; pField = pFieldEdit as IField2; return(pField); }
public static IField AlterRasterFieldSR(IField pField, ISpatialReference sr) { IFieldEdit2 pEdit = pField as IFieldEdit2; IRasterDef pRDef = pEdit.RasterDef; pRDef.SpatialReference = sr; return(pField); }
private bool Analyze(string filePath) { IFeatureClass featureClass = filePath.GetShpFeatureClass(); if (featureClass == null) { return(false); } var a = featureClass.Fields.FindField("TCMC"); if (a == -1) { try { IField field = new FieldClass(); IFieldEdit2 fieldEdit = field as IFieldEdit2; fieldEdit.Type_2 = esriFieldType.esriFieldTypeString; fieldEdit.Name_2 = "TCMC"; fieldEdit.AliasName_2 = "图层名称"; featureClass.AddField(field); } catch { return(false); } } var index = featureClass.Fields.FindField("TCMC"); if (index != -1) { var tcmc = System.IO.Path.GetFileNameWithoutExtension(filePath); IFeatureCursor featureCursor = featureClass.Search(null, false); IFeature feature = featureCursor.NextFeature(); while (feature != null) { feature.set_Value(index, tcmc); feature.Store(); feature = featureCursor.NextFeature(); } System.Runtime.InteropServices.Marshal.ReleaseComObject(featureCursor); //switch (featureClass.ShapeType) //{ // case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint: // PointFiles.Add(filePath); // break; // case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon: // PolygonFiles.Add(filePath); // break; // case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline: // PolylineFiles.Add(filePath); // break; //} } return(true); }
public bool CadastralTableAddField(ICadastralFabric pCadaFab, esriCadastralFabricTable eTable, esriFieldType FieldType, string FieldName, string FieldAlias, int FieldLength) { ITable pTable = pCadaFab.get_CadastralTable(eTable); // First check to see if a field with this name already exists if (pTable.FindField(FieldName) > -1) { if (pTable != null) { Marshal.ReleaseComObject(pTable); } return(false); } IField2 pField = null; try { //Create a new Field pField = new FieldClass(); //QI for IFieldEdit IFieldEdit2 pFieldEdit = (IFieldEdit2)pField; pFieldEdit.Type_2 = FieldType; pFieldEdit.Editable_2 = true; pFieldEdit.IsNullable_2 = true; pFieldEdit.Name_2 = FieldName; pFieldEdit.AliasName_2 = FieldAlias; pFieldEdit.Length_2 = FieldLength; //'.RasterDef_2 = pRasterDef pTable.AddField(pField); if (pField != null) { Marshal.ReleaseComObject(pField); } } catch (COMException ex) { if (pField != null) { Marshal.ReleaseComObject(pField); } MessageBox.Show(ex.Message); return(false); } return(true); }
/// <summary> /// 作用:向要素类中批量增加字段 /// 作者:汪建龙 /// 编写时间:2016年12月27日10:49:45 /// </summary> /// <param name="featureClass"></param> /// <param name="list"></param> public static void AddFields(this IFeatureClass featureClass, List <TangField> list) { if (featureClass == null || list == null || list.Count == 0) { return; } try { foreach (var item in list) { IField field = new FieldClass(); IFieldEdit2 fieldEdit = field as IFieldEdit2; fieldEdit.Type_2 = item.Type; fieldEdit.Name_2 = item.Name; fieldEdit.AliasName_2 = item.Alias; AddField(featureClass, field); } } catch (Exception ex) { System.Diagnostics.Trace.WriteLine(ex); } }
public bool CadastralTableAddFieldV1(ICadastralFabric pCadaFab, esriCadastralFabricTable eTable, esriFieldType FieldType, string FieldName, string FieldAlias, int FieldLength) { ITable pTable = pCadaFab.get_CadastralTable(eTable); // First check to see if a field with this name already exists if (pTable.FindField(FieldName) > -1) { if (pTable != null) { Marshal.ReleaseComObject(pTable); } return(false); } IDatasetComponent pDSComponent = (IDatasetComponent)pCadaFab; IDEDataset pDEDS = pDSComponent.DataElement; IDECadastralFabric pDECadaFab = (IDECadastralFabric)pDEDS; IField2 pField = null; try { //Create a new Field pField = new FieldClass(); //QI for IFieldEdit IFieldEdit2 pFieldEdit = (IFieldEdit2)pField; pFieldEdit.Type_2 = FieldType; pFieldEdit.Editable_2 = true; pFieldEdit.IsNullable_2 = true; pFieldEdit.Name_2 = FieldName; pFieldEdit.AliasName_2 = FieldAlias; pFieldEdit.Length_2 = FieldLength; //'.RasterDef_2 = pRasterDef } catch (COMException ex) { if (pField != null) { Marshal.ReleaseComObject(pField); } MessageBox.Show(ex.Message); return(false); } IArray pArr = pDECadaFab.CadastralTableFieldEdits; bool found = false; int cnt = pArr.Count; ICadastralTableFieldEdits pCadaTableFldEdits = null; IFields pFields = null; for (int i = 0; i <= (cnt - 1); i++) { pCadaTableFldEdits = (ICadastralTableFieldEdits)pArr.get_Element(i); IFieldsEdit pNewFields = new FieldsClass(); int fldCnt = 0; if (pCadaTableFldEdits.CadastralTable == eTable) { pFields = pCadaTableFldEdits.ExtendedAttributeFields; //Copy existing fields if (pFields != null) { fldCnt = pFields.FieldCount; pNewFields.FieldCount_2 = fldCnt + 1; for (int j = 0; j <= (fldCnt - 1); j++) { pNewFields.Field_2[j] = pFields.get_Field(j); } } else { pNewFields.FieldCount_2 = 1; } //Add the new field pNewFields.Field_2[fldCnt] = pField; //reset extended attribute fields pCadaTableFldEdits.ExtendedAttributeFields = pNewFields; found = true; break; } } if (!found) { pCadaTableFldEdits = new CadastralTableFieldEditsClass(); pCadaTableFldEdits.CadastralTable = eTable; //add the field to the table pFields = new FieldsClass(); int fldCnt = pFields.FieldCount; IFieldsEdit pFieldsEdit = (IFieldsEdit)pFields; pFieldsEdit.FieldCount_2 = fldCnt + 1; pFieldsEdit.Field_2[fldCnt] = pField; pCadaTableFldEdits.ExtendedAttributeFields = pFields; pArr.Add(pCadaTableFldEdits); Marshal.ReleaseComObject(pFields); } //Set the CadastralTableFieldEdits property on the DE to the array pDECadaFab.CadastralTableFieldEdits = pArr; // Update the schema ICadastralFabricSchemaEdit pSchemaEd = (ICadastralFabricSchemaEdit)pCadaFab; pSchemaEd.UpdateSchema(pDECadaFab); Marshal.ReleaseComObject(pField); return(true); }
public static bool IsHluLayer(IFeatureLayer layer, IFields origFields, IFieldChecker fieldChecker, object[] validWorkspaces, Dictionary <Type, int> typeMapSystemToSQL, ref HluGISLayer.incid_mm_polygonsDataTable hluLayerStructure, out int[] hluFieldMap, out string[] hluFieldNames) { hluFieldMap = null; hluFieldNames = null; if ((layer == null) || !layer.Valid || (((IFeatureLayerDefinition)layer).DefinitionSelectionSet != null)) { return(false); } bool isHlu = true; try { IFeatureClass testFeatureClass = layer.FeatureClass; if (hluLayerStructure == null) { hluLayerStructure = new HluGISLayer.incid_mm_polygonsDataTable(); } IFeatureWorkspace testWorkspace = ((IDataset)testFeatureClass).Workspace as IFeatureWorkspace; if (testWorkspace == null) { throw (new Exception("Invalid feature workspace.")); } if (System.Array.IndexOf(validWorkspaces, ((IWorkspace)testWorkspace).WorkspaceFactory.GetClassID().Value) == -1) { throw (new Exception("Invalid workspace type.")); } if (testFeatureClass.ShapeType != esriGeometryType.esriGeometryPolygon) { throw (new Exception("Invalid geometry type.")); } IFieldsEdit fieldsEdit = (IFieldsEdit)origFields; foreach (DataColumn c in hluLayerStructure.Columns) { IField newField = new FieldClass(); IFieldEdit2 newFieldEdit = (IFieldEdit2)newField; newFieldEdit.Name_2 = c.ColumnName; int fieldType; if (!typeMapSystemToSQL.TryGetValue(c.DataType, out fieldType)) { throw (new Exception("Invalid field type.")); } newFieldEdit.Type_2 = (esriFieldType)fieldType; if ((c.MaxLength != -1) && (newField.Type == esriFieldType.esriFieldTypeString)) { newFieldEdit.Length_2 = c.MaxLength; } fieldsEdit.AddField(newField); } fieldChecker.ValidateWorkspace = (IWorkspace)testWorkspace; IEnumFieldError error; IFields fixedFields; fieldChecker.Validate(origFields, out error, out fixedFields); hluFieldMap = new int[origFields.FieldCount]; hluFieldNames = new string[hluFieldMap.Length]; for (int i = 0; i < fixedFields.FieldCount; i++) { IField fixedField = fixedFields.get_Field(i); int ordinal = testFeatureClass.Fields.FindField(fixedField.Name); if (ordinal == -1) { throw (new Exception("Field name does not match the HLU GIS layer structure.")); } IField fcField = testFeatureClass.Fields.get_Field(ordinal); if (fcField.Type != fixedField.Type) { throw (new Exception("Field type does not match the HLU GIS layer structure.")); } if ((fcField.Type == esriFieldType.esriFieldTypeString) && (fcField.Length > fixedField.Length)) { throw (new Exception("Field length does not match the HLU GIS layer structure.")); } hluFieldMap[i] = ordinal; hluFieldNames[i] = fixedField.Name; } } catch { return(false); } return(isHlu); }
/// <summary> /// 显示每日疫情 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { //查询每日疫情 ArrayList arr = new ArrayList(); //获取日期 if (this.selectedDate.EditValue == null) { MessageBox.Show("请选择日期"); return; } arr.Add("YMD:'" + this.selectedDate.EditValue + "'"); DataTable dt = OperateDatabase.select("data", arr); if (dt.Rows.Count == 0) { MessageBox.Show("当日无数据"); return; } //为当前图层添加字段 IFeatureLayer layer = null; //遍历,寻找市域图层 for (int i = 0; i < this.axMapControl1.Map.LayerCount; i++) { ILayer layer1 = this.axMapControl1.Map.get_Layer(i); if (layer1.Name == "市域") { layer = layer1 as IFeatureLayer; break; } } if (layer == null) { MessageBox.Show("请打开市域图层"); return; } //获取图层字段,没有则添加一个num字段 IFeatureClass featureClass = layer.FeatureClass; int isExist = featureClass.FindField("num"); if (isExist == -1) { //添加一个字段 IFields pFields = featureClass.Fields; IFieldsEdit pFieldsEdit = pFields as IFieldsEdit; IField fld = new FieldClass(); IFieldEdit2 fldE = fld as IFieldEdit2; fldE.Name_2 = "num"; fldE.AliasName_2 = "数量"; fldE.Type_2 = esriFieldType.esriFieldTypeSingle; featureClass.AddField(fld); } //给字段赋值 IFeatureCursor pFtCursor = featureClass.Search(null, false); IFeature pFt = pFtCursor.NextFeature(); int index1 = pFt.Fields.FindField("num"); IDataset dataset = (IDataset)featureClass; IWorkspace workspace = dataset.Workspace; IWorkspaceEdit workspaceEdit = (IWorkspaceEdit)workspace; workspaceEdit.StartEditing(true); workspaceEdit.StartEditOperation(); while (pFt != null) { int index = pFt.Fields.FindField("code"); String code = pFt.get_Value(index).ToString(); DataRow[] drs = dt.Select("CODE=" + code); DataTable dtNew = dt.Clone(); for (int i = 0; i < drs.Length; i++) { dtNew.ImportRow(drs[i]); } String num = dtNew.Rows[0]["AllConfiemed"].ToString(); if (num == "") { num = "0"; } pFt.set_Value(index1, Convert.ToInt32(num)); pFt.Store(); pFt = pFtCursor.NextFeature(); } GisClass.ClassRender(this.axMapControl1.ActiveView, layer, 6, "num"); //GisClass.UniqueValueRender(this.axMapControl1.ActiveView,layer,10,"num"); }
private void btnChkField_Click(object sender, EventArgs e) { ICadastralEditor pCadEd = (ICadastralEditor)ArcMap.Application.FindExtensionByName("esriCadastralUI.CadastralEditorExtension"); bool bIsEditing = (ArcMap.Editor.EditState == ESRI.ArcGIS.Editor.esriEditState.esriStateEditing); ICadastralFabric pCadFab = pCadEd.CadastralFabric; IFeatureClass ParcelLinesFC = null; if (bIsEditing) { ParcelLinesFC = pCadFab.get_CadastralTable(esriCadastralFabricTable.esriCFTLines) as IFeatureClass; } else { UTIL.GetFabricFromMap(ParcelEditHelper.ArcMap.Document.ActiveView.FocusMap, out pCadFab); ParcelLinesFC = pCadFab.get_CadastralTable(esriCadastralFabricTable.esriCFTLines) as IFeatureClass; } if (pCadFab == null) { MessageBox.Show("Parcel fabric not found in the map.", "Check"); return; } int iFldIdx = ParcelLinesFC.FindField(this.txtFldName.Text); if (iFldIdx == -1) { if (bIsEditing) { MessageBox.Show("Field is not present. Stop editing, and click 'Check' again to be prompted to create the field, " + "or else manually create the string field using the Catalog window or ArcToolbox.", "Check"); } else { DialogResult dialogRes = MessageBox.Show("Field is not present. Create string field called " + this.txtFldName.Text + " ?", "Create field", MessageBoxButtons.YesNo); if (dialogRes == DialogResult.No) { return; } if (this.txtFldName.Text.Trim() == "") { return; } IField2 pFld = new FieldClass(); IFieldEdit2 pFldEd = pFld as IFieldEdit2; pFldEd.Editable_2 = true; pFldEd.Name_2 = this.txtFldName.Text; pFldEd.Type_2 = esriFieldType.esriFieldTypeString; pFldEd.Length_2 = 50; ParcelLinesFC.AddField(pFld); } } else { IField pField = ParcelLinesFC.Fields.Field[iFldIdx]; if (pField.Type != esriFieldType.esriFieldTypeString) { MessageBox.Show("A field called " + this.txtFldName.Text + " was found." + Environment.NewLine + "However, the field is not a Text field." + Environment.NewLine + "Please create or use a different Text field.", "Check field", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { MessageBox.Show("A field called " + this.txtFldName.Text + " was found.", "Check field"); } } }