private void 添加点型图层ToolStripMenuItem_Click(object sender, EventArgs e) { //创建要素类 #region 创建新的内存工作空间 IWorkspaceFactory pWSF = new InMemoryWorkspaceFactoryClass(); IWorkspaceName pWSName = pWSF.Create("", "Temp", null, 0); IName pName = (IName)pWSName; IWorkspace pMemoryWS = (IWorkspace)pName.Open(); #endregion IField oField = new FieldClass(); IFields oFields = new FieldsClass(); IFieldsEdit oFieldsEdit = null; IFieldEdit oFieldEdit = null; IFeatureClass oFeatureClass = null; IFeatureLayer oFeatureLayer = null; oFieldsEdit = oFields as IFieldsEdit; oFieldEdit = oField as IFieldEdit; oFieldEdit.Name_2 = "OBJECTID"; oFieldEdit.Type_2 = esriFieldType.esriFieldTypeOID; oFieldEdit.IsNullable_2 = false; oFieldEdit.Required_2 = false; oFieldsEdit.AddField(oField); oField = new FieldClass(); oFieldEdit = oField as IFieldEdit; IGeometryDef pGeoDef = new GeometryDefClass(); IGeometryDefEdit pGeoDefEdit = (IGeometryDefEdit)pGeoDef; pGeoDefEdit.AvgNumPoints_2 = 5; pGeoDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint; pGeoDefEdit.GridCount_2 = 1; pGeoDefEdit.HasM_2 = false; pGeoDefEdit.HasZ_2 = false; pGeoDefEdit.SpatialReference_2 = axMapControl1.SpatialReference; oFieldEdit.Name_2 = "SHAPE"; oFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; oFieldEdit.GeometryDef_2 = pGeoDef; oFieldEdit.IsNullable_2 = true; oFieldEdit.Required_2 = true; oFieldsEdit.AddField(oField); oField = new FieldClass(); oFieldEdit = oField as IFieldEdit; oFieldEdit.Name_2 = "Code"; oFieldEdit.Type_2 = esriFieldType.esriFieldTypeSmallInteger; //oFieldEdit.Length = 10; oFieldEdit.IsNullable_2 = true; oFieldsEdit.AddField(oField); //创建要素类 oFeatureClass = (pMemoryWS as IFeatureWorkspace).CreateFeatureClass("Temp", oFields, null, null, esriFeatureType.esriFTSimple, "SHAPE", ""); oFeatureLayer = new FeatureLayerClass(); oFeatureLayer.Name = "PointLayer"; oFeatureLayer.FeatureClass = oFeatureClass; //创建唯一值符号化对象 IUniqueValueRenderer pURender = new UniqueValueRendererClass(); pURender.FieldCount = 1; pURender.set_Field(0, "Code"); pURender.UseDefaultSymbol = false; //创建SimpleMarkerSymbolClass对象 ISimpleMarkerSymbol pSimpleMarkerSymbol = new SimpleMarkerSymbolClass(); //创建RgbColorClass对象为pSimpleMarkerSymbol设置颜色 IRgbColor pRgbColor = new RgbColorClass(); pRgbColor.Red = 255; pSimpleMarkerSymbol.Color = pRgbColor as IColor; //设置pSimpleMarkerSymbol对象的符号类型,选择钻石 pSimpleMarkerSymbol.Style = esriSimpleMarkerStyle.esriSMSDiamond; //设置pSimpleMarkerSymbol对象大小,设置为5 pSimpleMarkerSymbol.Size = 5; //显示外框线 pSimpleMarkerSymbol.Outline = true; //为外框线设置颜色 IRgbColor pLineRgbColor = new RgbColorClass(); pLineRgbColor.Green = 255; pSimpleMarkerSymbol.OutlineColor = pLineRgbColor as IColor; //设置外框线的宽度 pSimpleMarkerSymbol.OutlineSize = 1; //半透明颜色 pURender.AddValue("1", "", pSimpleMarkerSymbol as ISymbol); //唯一值符号化内存图层 (oFeatureLayer as IGeoFeatureLayer).Renderer = pURender as IFeatureRenderer; ILayerEffects pLyrEffect = oFeatureLayer as ILayerEffects; //透明度 pLyrEffect.Transparency = 0; oFeatureLayer.Visible = true; this.axMapControl1.AddLayer(oFeatureLayer,axMapControl1.LayerCount); insertpoint = true; }
static IFeatureClass CreateFeatureClass(IFeatureWorkspace workspace, string name, ISpatialReference outSR) { IFieldsEdit fields = new FieldsClass(); IFieldEdit field = new FieldClass(); field.Type_2 = esriFieldType.esriFieldTypeOID; field.Name_2 = "OBJECTID"; field.AliasName_2 = "OBJECTID"; fields.AddField(field); IGeometryDefEdit geom = new GeometryDefClass(); geom.GeometryType_2 = esriGeometryType.esriGeometryPolygon; geom.SpatialReference_2 = outSR; geom.HasM_2 = true; geom.HasZ_2 = false; field = new FieldClass(); field.Name_2 = "SHAPE"; field.AliasName_2 = "SHAPE"; field.Type_2 = esriFieldType.esriFieldTypeGeometry; field.GeometryDef_2 = geom; fields.AddField(field); return workspace.CreateFeatureClass(name, fields, null, null, esriFeatureType.esriFTSimple, "SHAPE", ""); }
private void addScoreToolStripMenuItem_Click(object sender, EventArgs e) { IFeatureLayer fealyr = axMapControl1.get_Layer(0) as IFeatureLayer; IFeatureClass feacls = fealyr.FeatureClass; IField field = new FieldClass(); IFieldEdit2 fe = field as IFieldEdit2; fe.Name_2 = "FinalScore"; fe.Type_2 = esriFieldType.esriFieldTypeDouble; feacls.AddField(field); int index = feacls.FindField("FinalScore"); object obj = null; Random rand = new Random(); using (ComReleaser com = new ComReleaser()) { IFeatureCursor cursor = feacls.Search(null, true); com.ManageLifetime(cursor); IFeature fea = cursor.NextFeature(); while (fea != null) { obj = rand.NextDouble() * 100; fea.set_Value(index, obj); fea.Store(); fea = cursor.NextFeature(); } } }
//http://resources.esri.com/help/9.3/arcgisengine/arcobjects/esriGeoDatabase/IFeatureWorkspace.CreateFeatureClass_Example.htm static IFeatureClass CreateFeatureClass(string name, IFeatureWorkspace ftrSpc, esriGeometryType type, int epsg, List<dynamic> extraFields) { IFeatureClass ftrc = null; if(null != ftrSpc && null != name) { IFieldsEdit flds = new FieldsClass(); flds.FieldCount_2 = 2 + (extraFields == null ? 0 : extraFields.Count); IFieldEdit fld = new FieldClass(); fld.Name_2 = OBJECT_ID; fld.Type_2 = esriFieldType.esriFieldTypeOID; flds.Field_2[0] = fld; fld = new FieldClass(); fld.Name_2 = SHP_NAME; fld.Type_2 = esriFieldType.esriFieldTypeGeometry; fld.GeometryDef_2 = CreateGeometryDef(type, epsg); flds.Field_2[1] = fld; int eidx = 2; foreach(var efld in extraFields) { fld = new FieldClass(); fld.Name_2 = efld.Name; fld.Type_2 = efld.Type; flds.Field_2[eidx++] = fld; } ftrc = ftrSpc.CreateFeatureClass(name, flds, null, null, esriFeatureType.esriFTSimple, SHP_NAME, null); } return ftrc; }
/// <summary> /// �½���ͼ�� /// </summary> public void CreatePointLayer() { SaveFileDialog sfdPoint = new SaveFileDialog(); sfdPoint.Title = "��ѡ���ͼ��Ĵ洢λ��"; sfdPoint.Filter = "Shapefile(*.shp)|*.shp|All files(*.*)|*.*"; sfdPoint.RestoreDirectory = true; if (sfdPoint.ShowDialog() == DialogResult.OK) { LocalFilePath = sfdPoint.FileName; FilePath = System.IO.Path.GetDirectoryName(LocalFilePath); FileName = System.IO.Path.GetFileName(LocalFilePath); IFields pFields = new FieldsClass(); IFieldsEdit pFieldsEdit = pFields as IFieldsEdit; IField pField = new FieldClass(); IFieldEdit pFieldEdit = pField as IFieldEdit; pFieldEdit.Name_2 = "SHAPE"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; IGeometryDef pGeometryDef = new GeometryDefClass(); IGeometryDefEdit pGeometryDefEdit = pGeometryDef as IGeometryDefEdit; pGeometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint; pFieldEdit.GeometryDef_2 = pGeometryDef; pFieldsEdit.AddField(pField); IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactoryClass(); IFeatureWorkspace pFeatureWorkspace = pWorkspaceFactory.OpenFromFile(FilePath, 0) as IFeatureWorkspace; pFeatureWorkspace.CreateFeatureClass(FileName, pFields, null, null, esriFeatureType.esriFTSimple, "SHAPE", ""); m_mapControl.AddShapeFile(FilePath, FileName); m_mapControl.ActiveView.Refresh(); } }
public static void AddField(this IFields fields, string name, esriFieldType type) { IFieldEdit fe = new FieldClass(); fe.Name_2 = name; fe.Type_2 = type; ((IFieldsEdit)fields).AddField((IField)fe); }
public static IFeatureClass CreateFClassInPDB(IWorkspace accessworkspace, string feaDSname, string FCname, esriGeometryType esriGeometryType,ISpatialReference sprf) { try { IFeatureDataset featureDataset = ((IFeatureWorkspace)accessworkspace).OpenFeatureDataset(feaDSname); //IGeoDataset geoDataset = featureDataset as IGeoDataset; IFields pFields = new FieldsClass(); IFieldsEdit pFieldsEdit = pFields as IFieldsEdit; IField pField = new FieldClass(); IFieldEdit pFieldEdit = pField as IFieldEdit; pFieldEdit.Name_2 = "SHAPE"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; IGeometryDef pGeometryDef = new GeometryDefClass(); IGeometryDefEdit pGeometryDefEdit = pGeometryDef as IGeometryDefEdit; pGeometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint; pFieldEdit.GeometryDef_2 = pGeometryDef; pFieldsEdit.AddField(pField); IFeatureClass fc = featureDataset.CreateFeatureClass(FCname, pFields, null, null, esriFeatureType.esriFTSimple, "Shape", ""); return fc; } catch (Exception ee) { MessageBox.Show(ee.Message.ToString()); return null; } }
private static void RunGPFieldMapping() { // Initialize the Geoprocessor ESRI.ArcGIS.Geoprocessor.Geoprocessor GP = new ESRI.ArcGIS.Geoprocessor.Geoprocessor(); GP.OverwriteOutput = true; // Create the GPUtilites object IGPUtilities gputilities = new GPUtilitiesClass(); // Create a DETable data element object IDETable inputTableA = (IDETable)gputilities.MakeDataElement(@"C:\data\citiblocks.gdb\census", null, null); // Create an array of input tables IArray inputtables = new ArrayClass(); inputtables.Add(inputTableA); // Initialize the GPFieldMapping IGPFieldMapping fieldmapping = new GPFieldMappingClass(); fieldmapping.Initialize(inputtables, null); // Create a new output field IFieldEdit trackidfield = new FieldClass(); trackidfield.Name_2 = "TRACTID"; trackidfield.Type_2 = esriFieldType.esriFieldTypeString; trackidfield.Length_2 = 50; // Create a new FieldMap IGPFieldMap trackid = new GPFieldMapClass(); trackid.OutputField = trackidfield; // Find field map "STFID" containing the input field "STFID". Add input field to the new field map. int fieldmap_index = fieldmapping.FindFieldMap("STFID"); IGPFieldMap stfid_fieldmap = fieldmapping.GetFieldMap(fieldmap_index); int field_index = stfid_fieldmap.FindInputField(inputTableA, "STFID"); IField inputField = stfid_fieldmap.GetField(field_index); trackid.AddInputField(inputTableA, inputField, 5, 10); // Add the new field map to the field mapping fieldmapping.AddFieldMap(trackid); // Execute Table to Table tool using the FieldMapping TableToTable tblTotbl = new TableToTable(); tblTotbl.in_rows = inputTableA; tblTotbl.out_path = @"C:\data\citiblocks.gdb"; tblTotbl.out_name = "census_out"; tblTotbl.field_mapping = fieldmapping; object sev = null; try { GP.Execute(tblTotbl, null); System.Windows.Forms.MessageBox.Show(GP.GetMessages(ref sev)); } catch (Exception ex) { Console.WriteLine(ex.Message); System.Windows.Forms.MessageBox.Show(GP.GetMessages(ref sev)); } }
protected override void OnClick() { string tablePath = Path.Combine(DataPath, @"Geodatabase\ManhattanKS.gdb\ParcelIDs"); string tableName = Path.GetFileName(tablePath); const string searchField = "PID"; Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory"); IWorkspaceFactory workspaceFactory = Activator.CreateInstance(factoryType) as IWorkspaceFactory; IWorkspace workspace = workspaceFactory.OpenFromFile(Path.GetDirectoryName(tablePath), 0); IObjectClassDescription objClassDesc = new ObjectClassDescriptionClass(); IFields fields = objClassDesc.RequiredFields; IFieldsEdit fieldsEdit = (IFieldsEdit) fields; IField field = new FieldClass(); IFieldEdit fieldEdit = (IFieldEdit) field; fieldEdit.Name_2 = searchField; fieldEdit.Type_2 = esriFieldType.esriFieldTypeString; fieldEdit.IsNullable_2 = true; fieldEdit.AliasName_2 = searchField; fieldEdit.Editable_2 = true; fieldEdit.Length_2 = 250; fieldsEdit.AddField(field); fields = fieldsEdit; ITable table = CreateTable((IWorkspace2) workspace, tableName, fields); using (ComReleaser releaser = new ComReleaser()) { ICursor cursor = table.Insert(true); releaser.ManageLifetime(cursor); string txtPath = Path.Combine(DataPath, "UniqueValues.txt"); int searchFieldIndex = table.FindField(searchField); IRowBuffer buffer = table.CreateRowBuffer(); using (StreamReader reader = new StreamReader(txtPath)) { string line; while ((line = reader.ReadLine()) != null) { string id = line.Split(new[] {'.', ' '}, StringSplitOptions.RemoveEmptyEntries)[1]; buffer.Value[searchFieldIndex] = id; cursor.InsertRow(buffer); } cursor.Flush(); } } ((ITableCollection) ArcMap.Document.FocusMap).AddTable(table); ArcMap.Document.UpdateContents(); Marshal.FinalReleaseComObject(table); }
public IFeatureClass CreateShapefile( string sParentDirectory, string sWorkspaceName, string sFileName) { if (System.IO.Directory.Exists(sParentDirectory + sWorkspaceName)) { System.IO.Directory.Delete(sParentDirectory + sWorkspaceName, true); } IWorkspaceFactory pWf = new ShapefileWorkspaceFactoryClass(); IWorkspaceName pWn = pWf.Create(sParentDirectory, sWorkspaceName, null, 0); ESRI.ArcGIS.esriSystem.IName name = pWn as ESRI.ArcGIS.esriSystem.IName; IWorkspace pW = (IWorkspace)name.Open(); IFeatureWorkspace pFw = pW as IFeatureWorkspace; IFields pFs = new FieldsClass(); IFieldsEdit pFse = pFs as IFieldsEdit; IFieldEdit pFe = new FieldClass(); pFe.Name_2 = "OID"; pFe.AliasName_2 = "序号"; pFe.Type_2 = esriFieldType.esriFieldTypeOID; pFse.AddField((IField)pFe);//IFieldEdit.AddField是AO隐藏属性 pFe = new FieldClass(); pFe.Name_2 = "Name"; pFe.AliasName_2 = "名称"; pFe.Type_2 = esriFieldType.esriFieldTypeString; pFse.AddField((IField)pFe); IGeometryDefEdit pGde = new GeometryDefClass(); ISpatialReference pSr = m_map.SpatialReference; pGde.SpatialReference_2 = pSr; pGde.GeometryType_2 = esriGeometryType.esriGeometryPoint; pFe = new FieldClass(); string sShapeFieldName = "Shape"; pFe.Name_2 = sShapeFieldName; pFe.AliasName_2 = "形状"; pFe.Type_2 = esriFieldType.esriFieldTypeGeometry; pFe.GeometryDef_2 = pGde; pFse.AddField((IField)pFe); IFeatureClass pFc = pFw.CreateFeatureClass(sFileName, pFs, null, null, esriFeatureType.esriFTSimple, "Shape", ""); if (pFc == null) { return(null); } return(pFc); }
public static IField CreateIntField(string pFieldName, int pFieldLen) { FieldClass class2 = new FieldClass(); IFieldEdit edit = class2; edit.Name_2 = pFieldName; edit.Precision_2 = pFieldLen; edit.Type_2 = esriFieldType.esriFieldTypeInteger; return class2; }
public IFeatureClass CreateShapefile(String sParentDirectory, String sWorkspaceName, String sFileName) { IWorkspaceFactory workspaceFactory; IWorkspace workspace; IFeatureWorkspace featureWorkspace; IFeatureClass featureClass; if (System.IO.Directory.Exists(sParentDirectory + sWorkspaceName)) { workspaceFactory = new ShapefileWorkspaceFactoryClass(); workspace = workspaceFactory.OpenFromFile(sParentDirectory + sWorkspaceName, 0); featureWorkspace = workspace as IFeatureWorkspace; featureClass = featureWorkspace.OpenFeatureClass(sFileName); } else { workspaceFactory = new ShapefileWorkspaceFactoryClass(); IWorkspaceName workspaceName = workspaceFactory.Create(sParentDirectory, sWorkspaceName, null, 0); ESRI.ArcGIS.esriSystem.IName name = workspaceName as ESRI.ArcGIS.esriSystem.IName; workspace = (IWorkspace)name.Open(); featureWorkspace = workspace as IFeatureWorkspace; IFields fields = new FieldsClass(); IFieldsEdit fieldsEdit = fields as IFieldsEdit; IFieldEdit fieldEdit = new FieldClass(); fieldEdit.Name_2 = "OID"; fieldEdit.AliasName_2 = "序号"; fieldEdit.Type_2 = esriFieldType.esriFieldTypeOID; fieldsEdit.AddField((IField)fieldEdit); fieldEdit = new FieldClass(); fieldEdit.Name_2 = "Name"; fieldEdit.AliasName_2 = "名称"; fieldEdit.Type_2 = esriFieldType.esriFieldTypeString; fieldsEdit.AddField((IField)fieldEdit); IGeometryDefEdit geoDefEdit = new GeometryDefClass(); ISpatialReference spatialReference = m_map.SpatialReference; geoDefEdit.SpatialReference_2 = spatialReference; geoDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint; fieldEdit = new FieldClass(); fieldEdit.Name_2 = "Shape"; fieldEdit.AliasName_2 = "形状"; fieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; fieldEdit.GeometryDef_2 = geoDefEdit; fieldsEdit.AddField((IField)fieldEdit); featureClass = featureWorkspace.CreateFeatureClass(sFileName, fields, null, null, esriFeatureType.esriFTSimple, "Shape", ""); if (featureClass == null) { return(null); } } return(featureClass); }
/// <summary> /// Creates a new small integer field with the given name. Does not check that the name is /// valid. /// </summary> /// <param name="name">name of field</param> /// <returns>new field</returns> public static IField MakeFieldSmallInt(string name) { IField field = new FieldClass(); IFieldEdit fieldEdit = (IFieldEdit)field; fieldEdit.Type_2 = esriFieldType.esriFieldTypeSmallInteger; fieldEdit.Name_2 = name; return(field); }
/// <summary> /// create feature class of output /// </summary> /// <param name="workspace">object workspace</param> /// <param name="spatialReference">spatial reference of feature class of output</param> /// <param name="nameFeatureClass">name of feature class</param> /// <returns>object feature class</returns> private static IFeatureClass CreateFeatureClassOutput(IWorkspace workspace, ISpatialReference spatialReference, string nameFeatureClass) { IFeatureClassDescription featureClassDescription = new FeatureClassDescriptionClass(); IObjectClassDescription objectClassDescription = (IObjectClassDescription)featureClassDescription; IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspace; // Create the fields collection. IFields fields = new FieldsClass(); IFieldsEdit fieldsEdit = (IFieldsEdit)fields; IField oidField = new FieldClass(); IFieldEdit oidFieldEdit = (IFieldEdit)oidField; oidFieldEdit.Name_2 = "OBJECTID"; oidFieldEdit.Type_2 = esriFieldType.esriFieldTypeOID; fieldsEdit.AddField(oidField); // Create the Shape field. IField shapeField = new Field(); IFieldEdit shapeFieldEdit = (IFieldEdit)shapeField; // Set up the geometry definition for the Shape field. IGeometryDef geometryDef = new GeometryDefClass(); IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit)geometryDef; geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolygon; // 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.HasM_2 = false; geometryDefEdit.HasZ_2 = false; geometryDefEdit.SpatialReference_2 = spatialReference; // Set standard field properties. shapeFieldEdit.Name_2 = featureClassDescription.ShapeFieldName; shapeFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; shapeFieldEdit.GeometryDef_2 = geometryDef; shapeFieldEdit.IsNullable_2 = true; shapeFieldEdit.Required_2 = true; fieldsEdit.AddField(shapeField); IField idField = new FieldClass(); IFieldEdit idIsolaFieldEdit = (IFieldEdit)idField; idIsolaFieldEdit.Name_2 = Program.nameFieldIdOutput; idIsolaFieldEdit.Type_2 = esriFieldType.esriFieldTypeInteger; fieldsEdit.AddField(idField); // Use IFieldChecker to create a validated fields collection. IFieldChecker fieldChecker = new FieldCheckerClass(); IEnumFieldError enumFieldError = null; IFields validatedFields = null; fieldChecker.ValidateWorkspace = workspace; fieldChecker.Validate(fields, out enumFieldError, out validatedFields); return featureWorkspace.CreateFeatureClass(nameFeatureClass, fields, objectClassDescription.InstanceCLSID, objectClassDescription.ClassExtensionCLSID, esriFeatureType.esriFTSimple, featureClassDescription.ShapeFieldName, string.Empty); }
public void AddField(string name, esriFieldType type) { IFieldEdit fieldEdit = new FieldClass(); fieldEdit.Name_2 = name; fieldEdit.Type_2 = type; AddField(fieldEdit); }
public static IFields GetFields(IFeatureClass featureClass) { IFields pNewFields = new FieldsClass(); IFields pFields = featureClass.Fields; IFieldsEdit pNewFieldsEdit = pNewFields as IFieldsEdit; int fieldCount = pFields.FieldCount; for (int i = 0; i < fieldCount; i++) { IField pField = pFields.Field[i]; if (pField.Name == "OBJECTID_1" || pField.Name == "OBJECTID") { continue; } if (pField.Editable == false) { continue; } IField pNewField = new FieldClass(); IFieldEdit pNewFieldEdit = pNewField as IFieldEdit; pNewFieldEdit.Name_2 = pField.Name; pNewFieldEdit.AliasName_2 = pField.AliasName; if (pField.Type == esriFieldType.esriFieldTypeGeometry) { pNewFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; IGeometryDef pGeometryDef = new GeometryDefClass(); IGeometryDefEdit pGeometryDefEdit = pGeometryDef as IGeometryDefEdit; pGeometryDefEdit.GeometryType_2 = pField.GeometryDef.GeometryType; if (pField.GeometryDef.SpatialReference.Name == "Unknown") { ISpatialReference pSpatialReference = new UnknownCoordinateSystemClass(); pSpatialReference.SetDomain(-1000000, 1000000, -1000000, 1000000); pSpatialReference.SetZDomain(0, 0); pSpatialReference.SetMDomain(0, 0); pGeometryDefEdit.SpatialReference_2 = pSpatialReference; } else { pGeometryDefEdit.SpatialReference_2 = pField.GeometryDef.SpatialReference; } pNewFieldEdit.GeometryDef_2 = pGeometryDefEdit; } else { pNewFieldEdit.DefaultValue_2 = pField.DefaultValue; pNewFieldEdit.DomainFixed_2 = pField.DomainFixed; pNewFieldEdit.Domain_2 = pField.Domain; pNewFieldEdit.Precision_2 = pField.Precision; pNewFieldEdit.Required_2 = pField.Required; pNewFieldEdit.Scale_2 = pField.Scale; pNewFieldEdit.Type_2 = pField.Type; } pNewFieldsEdit.AddField(pNewField); } return(pNewFieldsEdit); }
internal static void AddField(IFeatureClass featureClass, string name, esriFieldType type) { IFieldEdit field = new FieldClass(); field.Name_2 = name; field.Type_2 = type; featureClass.AddField(field); }
public static IFeatureClass CreateFeatureClassByAxfFeatureLayer(IWorkspace workspace, IFeatureLayer axfFeatureLayer, bool isConvertToPoint = false, string className = "") { IFeatureWorkspace pFeatureWorkspace = workspace as IFeatureWorkspace; if (pFeatureWorkspace == null) { return(null); } IFields pFields = GetFields(axfFeatureLayer.FeatureClass); if (pFields == null) { return(null); } IFieldsEdit pFieldsEdit = pFields as IFieldsEdit; if (pFieldsEdit == null) { return(null); } IField pField = new FieldClass(); IFieldEdit pFieldEdit = pField as IFieldEdit; pFieldEdit.Name_2 = "AXF_STATUS"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeInteger; pFieldsEdit.AddField(pField); if (isConvertToPoint && axfFeatureLayer.FeatureClass.ShapeType == esriGeometryType.esriGeometryMultipoint) { for (int i = 0; i < pFieldsEdit.FieldCount; i++) { if (pFieldsEdit.Field[i].Type == esriFieldType.esriFieldTypeGeometry) { IFieldEdit geoFieldEdit = pFieldsEdit.Field[i] as IFieldEdit; IGeometryDefEdit pGeometryDefEdit = geoFieldEdit.GeometryDef as IGeometryDefEdit; pGeometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint; geoFieldEdit.GeometryDef_2 = pGeometryDefEdit; } } } IFieldChecker pFieldChecker = new FieldCheckerClass(); IEnumFieldError pEnumFieldError = null; IFields validateFields = null; pFieldChecker.ValidateWorkspace = workspace; pFieldChecker.Validate(pFieldsEdit, out pEnumFieldError, out validateFields); if (string.IsNullOrEmpty(className)) { return(pFeatureWorkspace.CreateFeatureClass(className, validateFields, axfFeatureLayer.FeatureClass.CLSID, axfFeatureLayer.FeatureClass.EXTCLSID, axfFeatureLayer.FeatureClass.FeatureType, "SHAPE", null)); } else { return(pFeatureWorkspace.CreateFeatureClass(axfFeatureLayer.Name, validateFields, axfFeatureLayer.FeatureClass.CLSID, axfFeatureLayer.FeatureClass.EXTCLSID, axfFeatureLayer.FeatureClass.FeatureType, "SHAPE", null)); } }
/// <summary> /// 把DataTable转为ITable ,tempPath 不含文件名的w文件夹路径 /// </summary> /// <param name="mTable"></param> /// <returns></returns> private ITable DataTableToITable(DataTable mTable, string xFieldName, string yFieldName, string tempPath) { try { #region 新建表字段 IField pField = null; IFields fields = new FieldsClass(); IFieldsEdit fieldsEdit = (IFieldsEdit)fields; fieldsEdit.FieldCount_2 = 2; //添加X字段 pField = new FieldClass(); IFieldEdit fieldEdit = (IFieldEdit)pField; fieldEdit.Name_2 = "X"; fieldEdit.AliasName_2 = "X"; //设置字段类型 fieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble; fieldEdit.Editable_2 = true; fieldsEdit.set_Field(0, pField); //添加Y字段 IField pField1 = new FieldClass(); IFieldEdit fieldEdit1 = (IFieldEdit)pField1; fieldEdit1.Name_2 = "Y"; fieldEdit1.AliasName_2 = "Y"; fieldEdit1.Type_2 = esriFieldType.esriFieldTypeDouble; fieldEdit1.Editable_2 = true; fieldsEdit.set_Field(1, pField1); #endregion ShapefileWorkspaceFactoryClass class2 = new ShapefileWorkspaceFactoryClass(); ESRI.ArcGIS.Geodatabase.IWorkspace pWorkspace = class2.OpenFromFile(tempPath, 0); IFeatureWorkspace pFWS = pWorkspace as IFeatureWorkspace; //删除已有的 if (System.IO.File.Exists(tempPath + "点数据.dbf")) { System.IO.File.Delete(tempPath + "点数据.dbf"); } //创建空表 ITable pTable = pFWS.CreateTable("点数据", fieldsEdit, null, null, ""); //遍历DataTable中数据,然后转换为ITable中的数据 int xRowIndex = pTable.Fields.FindField("X"); int yRowIndex = pTable.Fields.FindField("Y"); for (int k = 0; k < mTable.Rows.Count; k++) { //ITable 的记录 IRow row = pTable.CreateRow(); DataRow pRrow = mTable.Rows[k]; row.set_Value(xRowIndex, pRrow[xFieldName]); row.set_Value(yRowIndex, pRrow[yFieldName]); row.Store(); } return(pTable); } catch { return(null); } }
/// <summary> /// create a feature class in workspace with type geometry /// </summary> /// <param name="geometry">geometry of feature class</param> /// <param name="featureWorkspace">workspace for store feature class</param> /// <returns>feature class created</returns> private IFeatureClass CreateFeatureClass(IGeometry geometry, IFeatureWorkspace featureWorkspace) { // Create a fields collection for the feature class. IFields fields = new FieldsClass(); IFieldsEdit fieldsEdit = (IFieldsEdit)fields; // Add an object ID field to the fields collection. This is mandatory for feature classes. IField oidField = new FieldClass(); IFieldEdit oidFieldEdit = (IFieldEdit)oidField; oidFieldEdit.Name_2 = "OID"; oidFieldEdit.Type_2 = esriFieldType.esriFieldTypeOID; fieldsEdit.AddField(oidField); IField idField = new FieldClass(); IFieldEdit idFieldEdit = (IFieldEdit)idField; idFieldEdit.Name_2 = SAUtility.FieldNameIdWatershed; idFieldEdit.Type_2 = esriFieldType.esriFieldTypeInteger; fieldsEdit.AddField(idField); // Create a geometry definition (and spatial reference) for the feature class. IGeometryDef geometryDef = new GeometryDefClass(); IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit)geometryDef; geometryDefEdit.GeometryType_2 = geometry.GeometryType; geometryDefEdit.SpatialReference_2 = geometry.SpatialReference; IFeatureClassDescription featureClassDescription = new FeatureClassDescriptionClass(); IObjectClassDescription objectClassDescription = (IObjectClassDescription)featureClassDescription; // Add a geometry field to the fields collection. This is where the geometry definition is applied. IField geometryField = new FieldClass(); IFieldEdit geometryFieldEdit = (IFieldEdit)geometryField; geometryFieldEdit.Name_2 = featureClassDescription.ShapeFieldName; geometryFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; geometryFieldEdit.GeometryDef_2 = geometryDef; fieldsEdit.AddField(geometryField); // Use IFieldChecker to create a validated fields collection. IFieldChecker fieldChecker = new FieldCheckerClass(); IEnumFieldError enumFieldError = null; IFields validatedFields = null; fieldChecker.ValidateWorkspace = (IWorkspace)featureWorkspace; fieldChecker.Validate(fields, out enumFieldError, out validatedFields); // The enumFieldError enumerator can be inspected at this point to determine // which fields were modified during validation. // Create the feature class. Note that the CLSID parameter is null - this indicates to use the // default CLSID, esriGeodatabase.Feature (acceptable in most cases for feature classes). IFeatureClass featureClass = featureWorkspace.CreateFeatureClass("pourPoint", validatedFields, objectClassDescription.InstanceCLSID, objectClassDescription.ClassExtensionCLSID, esriFeatureType.esriFTSimple, featureClassDescription.ShapeFieldName, string.Empty); return(featureClass); }
public void Test() { //定义一个几何字段,类型为点类型 ISpatialReferenceFactory2 originalSpatialReferenceFactory = new SpatialReferenceEnvironmentClass(); ISpatialReference originalSpatialReference = originalSpatialReferenceFactory.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_WGS1984); IGeometryDefEdit pGeoDef = new GeometryDefClass(); IGeometryDefEdit pGeoDefEdit = pGeoDef as IGeometryDefEdit; pGeoDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint; pGeoDefEdit.SpatialReference_2 = originalSpatialReference; //定义一个字段集合对象 IFields pFields = new FieldsClass(); IFieldsEdit pFieldsEdit = (IFieldsEdit)pFields; //定义shape字段 IField pField = new FieldClass(); IFieldEdit pFieldEdit = (IFieldEdit)pField; pFieldEdit.Name_2 = "SHAPE"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; pFieldsEdit.AddField(pField); pFieldEdit.GeometryDef_2 = pGeoDef; //定义单个的字段,并添加到字段集合中 pField = new FieldClass(); pFieldEdit = (IFieldEdit)pField; pFieldEdit.Name_2 = "STCD"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString; pFieldsEdit.AddField(pField); //定义单个的字段,并添加到字段集合中 pField = new FieldClass(); pFieldEdit = (IFieldEdit)pField; pFieldEdit.Name_2 = "SLM10"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString; pFieldsEdit.AddField(pField); //定义单个的字段,并添加到字段集合中 pField = new FieldClass(); pFieldEdit = (IFieldEdit)pField; pFieldEdit.Name_2 = "SLM20"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString; pFieldsEdit.AddField(pField); //定义单个的字段,并添加到字段集合中 pField = new FieldClass(); pFieldEdit = (IFieldEdit)pField; pFieldEdit.Name_2 = "SLM40"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString; pFieldsEdit.AddField(pField); IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactory(); IFeatureWorkspace pFWS = pWorkspaceFactory.OpenFromFile(workspaceDirectory, 0) as IFeatureWorkspace; IFeatureClass pFtClass = pFWS.CreateFeatureClass("Test", pFields, null, null, esriFeatureType.esriFTSimple, "SHAPE", null); }
/// <summary> /// 创建字段 /// </summary> /// <param name="name">字段名</param> /// <param name="aliasName">字段别名</param> /// <param name="fieldType">字段类型</param> /// <returns></returns> public static IField CreateField(string name, string aliasName, esriFieldType fieldType) { IField field = new FieldClass(); IFieldEdit fieldEdit = field as IFieldEdit; fieldEdit.Name_2 = name; fieldEdit.AliasName_2 = aliasName; fieldEdit.Type_2 = fieldType; return(field); }
public static IField CreateIntField(string pFieldName, int pFieldLen) { FieldClass class2 = new FieldClass(); IFieldEdit edit = class2; edit.Name_2 = pFieldName; edit.Precision_2 = pFieldLen; edit.Type_2 = esriFieldType.esriFieldTypeInteger; return(class2); }
private static IFieldEdit GenerateFieldEdit(esriFieldType pesriFieldType, string strFieldName) { IFieldEdit pFieldEdit; pFieldEdit = new FieldClass(); pFieldEdit.Type_2 = pesriFieldType; pFieldEdit.Name_2 = strFieldName; return(pFieldEdit); }
public void addFiled(string fieldName, esriFieldType esriFieldType, ref IFieldsEdit pFieldsEdit) { IField pField = new FieldClass(); IFieldEdit pFieldEdit = (IFieldEdit)pField; pFieldEdit.Name_2 = fieldName; pFieldEdit.Type_2 = esriFieldType; pFieldEdit.Length_2 = 50; pFieldsEdit.AddField(pField); }
private void AddSimpleField(IFieldsEdit fieldsEdit, esriFieldType type, string name, string aliasName) { IField field = new FieldClass(); IFieldEdit fieldEdit = field as IFieldEdit; fieldEdit.Type_2 = type; fieldEdit.Name_2 = name; fieldEdit.AliasName_2 = aliasName; fieldsEdit.AddField(field); }
/// <summary> /// Creates a new string field with the given name and length. Does not check that either are /// valid values. /// </summary> /// <param name="name">name of field</param> /// <param name="length">length of field</param> /// <returns>new field</returns> public static IField MakeFieldString(string name, int length) { IField field = new FieldClass(); IFieldEdit fieldEdit = (IFieldEdit)field; fieldEdit.Type_2 = esriFieldType.esriFieldTypeString; fieldEdit.Name_2 = name; fieldEdit.Length_2 = length; return(field); }
/// <summary> /// Creates a new double field with the given name and precision. Does not check that either /// are valid values. /// </summary> /// <param name="name">name of field</param> /// <param name="precision">precision of field</param> /// <returns>new field</returns> public static IField MakeFieldDouble(string name, int precision) { IField field = new FieldClass(); IFieldEdit fieldEdit = (IFieldEdit)field; fieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble; fieldEdit.Name_2 = name; //fieldEdit.Precision_2 = precision; return(field); }
public static IField CreateOIDField() { FieldClass class2 = new FieldClass(); IFieldEdit edit = class2; edit.Name_2 = "OBJECTID"; edit.AliasName_2 = "表内唯一编号"; edit.Type_2 = esriFieldType.esriFieldTypeOID; return(class2); }
private void AddFld(IFeatureClass pFClass, string strName, esriFieldType fieldType) { IField pField = new FieldClass(); IFieldEdit pFieldEdit = (IFieldEdit)pField; pFieldEdit.Name_2 = strName; pFieldEdit.Type_2 = fieldType; pFClass.AddField(pField); }
public FieldService(int fieldWidth, int fieldHeight, int digX, int digY) { _width = fieldWidth; _height = fieldHeight; _initX = digX; _initY = digY; _x = digX; _y = digY; Field = new FieldClass[fieldWidth, fieldHeight]; }
//Temp private void AddField(IFeatureClass fClass, string name) { IField newField = new FieldClass(); IFieldEdit fieldEdit = (IFieldEdit)newField; fieldEdit.Name_2 = name; fieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble; fClass.AddField(newField); }
// Methods public static IField CreateDoubleField(string pFieldName, int pFieldLen, int pFieldScale) { FieldClass class2 = new FieldClass(); IFieldEdit edit = class2; edit.Name_2 = pFieldName; edit.Type_2 = esriFieldType.esriFieldTypeDouble; edit.Precision_2 = pFieldLen; edit.Scale_2 = pFieldScale; return class2; }
public void AddField(IFeatureClass pFeatureClass,string fieldName,esriFieldType fieldType) { //如果存在不必添加字段,直接返回 if (pFeatureClass.FieldExistCheck(fieldName)) return; var pField = new FieldClass(); var pFieldEdit = (IFieldEdit)pField; pFieldEdit.Name_2 = fieldName; pFieldEdit.Type_2 = fieldType; pFeatureClass.AddField(pFieldEdit); }
//添加字段并赋值 private void AddField() { //添加温度字段 string HighTemp = "HighTemperature"; string LowTemp = "LowTemperature"; IFeatureLayer lyrProVince = Utilities.GetLayerByName("省市", axMapControl1.Map) as IFeatureLayer; IFeatureClass ProClass = lyrProVince.FeatureClass;; if (ProClass.Fields.FindField(HighTemp) < 0) { IField pField = new FieldClass(); IFieldEdit pFieldEdit = pField as IFieldEdit; pFieldEdit.Name_2 = HighTemp; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble; IClass pClass = ProClass as IClass; pClass.AddField(pFieldEdit); } if (ProClass.Fields.FindField(LowTemp) < 0) { IField pField = new FieldClass(); IFieldEdit pFieldEdit = pField as IFieldEdit; pFieldEdit.Name_2 = LowTemp; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble; IClass pClass = ProClass as IClass; pClass.AddField(pFieldEdit); } //为字段赋值 string[] provinces = new string[] { "黑龙江省", "内蒙古自治区", "新疆维吾尔自治区", "吉林省", "甘肃省", "河北省", "北京市", "山西省", "天津市", "陕西省", "宁夏回族自治区", "青海省", "辽宁省", "山东省", "西藏自治区", "河南省", "江苏省", "安徽省", "四川省", "湖北省", "重庆市", "上海市", "浙江省", "湖南省", "江西省", "云南省", "贵州省", "广西壮族自治区", "福建省", "台湾省", "海南省", "广东省", "香港特别行政区", "澳门" }; TomorrowWeatherInfo weath; IFeatureCursor featCursor = null; IQueryFilter queryFilter = new QueryFilterClass(); for (int i = 0; i < provinces.Length; i++) { string selCity = provinces[i]; string whereClause = "[NAME] = '" + selCity + "'"; queryFilter.WhereClause = whereClause; featCursor = ProClass.Search(queryFilter, false); IFeature pFeature = featCursor.NextFeature(); string pcity = Utilities.getCity(selCity); weath = new TomorrowWeatherInfo(pcity); Random random = new Random();//测试用随机数产生器 if (pFeature != null) { pFeature.set_Value(pFeature.Fields.FindField("HighTemperature"), random.Next(20, 40)); pFeature.set_Value(pFeature.Fields.FindField("LowTemperature"), random.Next(1, 20)); pFeature.Store(); } } IGeoFeatureLayer geoProVince = Utilities.GetLayerByName("省市", axMapControl1.Map) as IGeoFeatureLayer; setColor(geoProVince); }
public IFeatureClass CreatePolygonFile(String strFolder, String filename, IEnvelope pEnvBorder, ISpatialReference pSR) { // Open the folder to contain the shapefile as a workspace IWorkspaceFactory pWF = new ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactoryClass(); IFeatureWorkspace pfws = (IFeatureWorkspace)pWF.OpenFromFile(strFolder, 0); //Set up a simple fields collection IField pField = new FieldClass(); IFieldEdit pFieldEdit = (IFieldEdit)pField; IFields pFields = new FieldsClass(); IFieldsEdit pFieldsEdit = (IFieldsEdit)pFields; // Make the shape field //it will need a geometry definition, with a spatial reference pFieldEdit.Name_2 = "Shape"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; IGeometryDef pGeomDef = new GeometryDef(); IGeometryDefEdit pGeomDefEdit = (IGeometryDefEdit)pGeomDef; pGeomDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolygon; pGeomDefEdit.SpatialReference_2 = pSR; pFieldEdit.GeometryDef_2 = pGeomDef; pFieldsEdit.AddField(pField); // Add ID field IField pFieldOID = new Field(); pFieldEdit = (IFieldEdit)pFieldOID; pFieldEdit.Name_2 = "OBJECTID"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeOID; pFieldEdit.IsNullable_2 = false; pFieldsEdit.AddField(pFieldOID); //EulerPoleReFrmgwn eulerfrm=new EulerPoleReFrmgwn(); if (File.Exists(strFolder + filename + ".shp") == true) { DialogResult result = MessageBox.Show("There is a shapefile have the same name in this foler, do you want to overwrite it?", "", MessageBoxButtons.OKCancel); if (result == DialogResult.OK) { File.Delete(strFolder + filename); } else { return null; } } IFeatureClass pFeatclass = pfws.CreateFeatureClass(filename, pFields, null, null, esriFeatureType.esriFTSimple, "Shape", ""); return pFeatclass; }
void ParseClass(string name, ScriptMap table) { var classes = new PackageClass(); foreach (var pair in table) { var fieldName = pair.Key as string; if (string.IsNullOrEmpty(fieldName)) { throw new Exception($"Class:{name} Field:{fieldName} 参数出错 参数模版 \"[索引],[类型],[是否数组=false],[注释]\""); } var value = pair.Value.ToString(); var infos = value.Split(','); if (infos.Length < 2) { throw new Exception($"Class:{name} Field:{fieldName} 参数出错 参数模版 \"[索引],[类型],[是否数组=false],[注释]\""); } var packageField = new FieldClass(this) { Name = fieldName, Index = infos[0].ToInt32(), Type = infos[1], IsArray = infos.Length > 2 && infos[2].ToBoolean(), Comment = infos.Length > 3 ? infos[3] : "", }; if (!packageField.IsBasic) { if (!Script.HasGlobal(packageField.Type) && //判断网络协议自定义类 !Script.HasGlobal(ENUM_KEYWORD + packageField.Type) && //判断是否是枚举 !Script.HasGlobal(MESSAGE_KEYWORD + packageField.Type) && //判断网络协议自定义类 !Script.HasGlobal(TABLE_KEYWORD + packageField.Type) //判断Table内嵌类 ) { throw new Exception($"Class:{name} Field:{fieldName} 未知类型:{packageField.Type}"); } } classes.Fields.Add(packageField); } classes.Fields.Sort((m1, m2) => { return(m1.Index.CompareTo(m2.Index)); }); if (name.StartsWith(MESSAGE_KEYWORD)) //协议结构 { name = name.Substring(MESSAGE_KEYWORD.Length); Messages[name] = classes; } else if (name.StartsWith(TABLE_KEYWORD)) //table结构 { name = name.Substring(TABLE_KEYWORD.Length); Tables[name] = classes; } else { Classes[name] = classes; } classes.Name = name; }
private IFields CreateChainFields() { IFields flds = new FieldsClass(); IFieldsEdit eflds = flds as IFieldsEdit; eflds.FieldCount_2 = 7; IField oid_field = new FieldClass(); IFieldEdit eOid = (IFieldEdit)oid_field; eOid.Name_2 = "ObjectID"; eOid.AliasName_2 = "ObjectID"; eOid.Type_2 = esriFieldType.esriFieldTypeOID; eflds.set_Field(0, oid_field); //Create A geometry Definition for the layer IGeometryDef geomDef = new GeometryDefClass(); IGeometryDefEdit egeomDef = (IGeometryDefEdit)geomDef; egeomDef.GeometryType_2 = esriGeometryType.esriGeometryPolyline; egeomDef.SpatialReference_2 = ArcMap.Document.FocusMap.SpatialReference; IField shap = new FieldClass(); IFieldEdit eshape = (IFieldEdit)shap; eshape.Type_2 = esriFieldType.esriFieldTypeGeometry; eshape.Name_2 = "Shape"; eshape.AliasName_2 = "Shape"; eshape.GeometryDef_2 = geomDef; eflds.set_Field(1, shap); IField FromLeft = CreateIntegerField("FromLeft"); eflds.set_Field(2, FromLeft); IField ToLeft = CreateIntegerField("ToLeft"); eflds.set_Field(3, ToLeft); IField FromRight = CreateIntegerField("FromRight"); eflds.set_Field(4, FromRight); IField ToRight = CreateIntegerField("ToRight"); eflds.set_Field(5, ToRight); IField CLID = CreateIntegerField("CLID"); eflds.set_Field(6, CLID); return(flds); }
public static IField CreateTextField(string pName, string pAliasName, int aWidth) { FieldClass class2 = new FieldClass(); IFieldEdit edit = class2; edit.Name_2 = pName.ToUpper(); edit.AliasName_2 = pAliasName; edit.Type_2 = esriFieldType.esriFieldTypeString; edit.Length_2 = aWidth; return(class2); }
private IField CreateIntegerField(string Name) { IField fkey = new FieldClass(); IFieldEdit efkey = (IFieldEdit)fkey; efkey.Name_2 = Name; efkey.Type_2 = esriFieldType.esriFieldTypeInteger; efkey.AliasName_2 = Name; return(fkey); }
/// <summary> /// 创建ObjectID字段 /// (ObjectID会在不同数据源中自动转换为OID/FID/OJBECTID,关于OID、FID、OBJECTID参考 /// http://support.esrichina.com.cn/2009/1229/595.html 或 /// http://blog.csdn.net/yh0503/article/details/27862401) /// </summary> /// <returns></returns> public static IField CreateOidField() { //创建OBJECTID字段 IField fieldOid = new FieldClass(); IFieldEdit fieldEditOID = (IFieldEdit)fieldOid; fieldEditOID.Name_2 = "OBJECTID"; //OBJECTID字段名在不同数据源会自动转换为FID或OID fieldEditOID.AliasName_2 = "OBJECTID"; fieldEditOID.Type_2 = esriFieldType.esriFieldTypeOID; return(fieldOid); }
// Methods public static IField CreateDoubleField(string pFieldName, int pFieldLen, int pFieldScale) { FieldClass class2 = new FieldClass(); IFieldEdit edit = class2; edit.Name_2 = pFieldName; edit.Type_2 = esriFieldType.esriFieldTypeDouble; edit.Precision_2 = pFieldLen; edit.Scale_2 = pFieldScale; return(class2); }
//字段集 private IFields CreateFieldsCollectionForFeatueClass(ISpatialReference spatialReference, esriGeometryType geometryType, string className) { //Use the feature class description to return the required fields in a fields collection. IFeatureClassDescription fcDesc = new FeatureClassDescriptionClass(); IObjectClassDescription ocDesc = (IObjectClassDescription)fcDesc; //Create the fields using the required fields method. IFields fields = ocDesc.RequiredFields; //Locate the shape field with the name from the feature class description. int shapeFieldIndex = fields.FindField(fcDesc.ShapeFieldName); IField shapeField = fields.get_Field(shapeFieldIndex); //Modify the GeometryDef object before using the fields collection to create a //feature class. IGeometryDef geometryDef = shapeField.GeometryDef; IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit)geometryDef; //Alter the feature class geometry type to the type we need. geometryDefEdit.GeometryType_2 = geometryType; geometryDefEdit.HasZ_2 = true; geometryDefEdit.HasM_2 = true; geometryDefEdit.GridCount_2 = 1; //Set the first grid size to zero and allow ArcGIS to determine a valid grid size. geometryDefEdit.set_GridSize(0, 0); geometryDefEdit.SpatialReference_2 = spatialReference; //添加地质属性概率字段 if (geometryType == esriGeometryType.esriGeometryPoint) { //Create a user-defined double field for "probability" IFieldsEdit fieldsEdit = (IFieldsEdit)fields; IField incomeField = new FieldClass(); IFieldEdit incomeFieldEdit = (IFieldEdit)incomeField; incomeFieldEdit.AliasName_2 = "probability"; incomeFieldEdit.Editable_2 = true; incomeFieldEdit.IsNullable_2 = false; incomeFieldEdit.Name_2 = "probability"; incomeFieldEdit.Precision_2 = 2; incomeFieldEdit.Scale_2 = 5; incomeFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble; fieldsEdit.AddField(incomeField); //Create a user-defined double field for "RegardP" IFieldsEdit fieldsEdit1 = (IFieldsEdit)fields; IField incomeField1 = new FieldClass(); IFieldEdit incomeFieldEdit1 = (IFieldEdit)incomeField1; incomeFieldEdit1.AliasName_2 = "RegardP"; incomeFieldEdit1.Editable_2 = true; incomeFieldEdit1.IsNullable_2 = false; incomeFieldEdit1.Name_2 = "RegardP"; incomeFieldEdit1.Precision_2 = 2; incomeFieldEdit1.Scale_2 = 5; incomeFieldEdit1.Type_2 = esriFieldType.esriFieldTypeDouble; fieldsEdit1.AddField(incomeField1); } return(fields); }
/// <summary> /// 创建OID字段 /// </summary> /// <param name="pOIDFieldName">OID字段名</param> /// <returns></returns> private IField CreateOIDField(string pOIDFieldName) { IField pField = new FieldClass(); IFieldEdit pFieldEdit = pField as IFieldEdit; pFieldEdit.Name_2 = pOIDFieldName; pFieldEdit.AliasName_2 = pOIDFieldName; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeOID; pField = pFieldEdit as IField; return(pField); }
//添加字段并赋值 private void AddField() { //添加温度字段 string HighTemp = "HighTemperature"; string LowTemp = "LowTemperature"; IFeatureLayer lyrProVince = Utilities.GetLayerByName("省市", axMapControl1.Map) as IFeatureLayer; IFeatureClass ProClass = lyrProVince.FeatureClass; ; if (ProClass.Fields.FindField(HighTemp) < 0) { IField pField = new FieldClass(); IFieldEdit pFieldEdit = pField as IFieldEdit; pFieldEdit.Name_2 = HighTemp; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble; IClass pClass = ProClass as IClass; pClass.AddField(pFieldEdit); } if (ProClass.Fields.FindField(LowTemp) < 0) { IField pField = new FieldClass(); IFieldEdit pFieldEdit = pField as IFieldEdit; pFieldEdit.Name_2 = LowTemp; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble; IClass pClass = ProClass as IClass; pClass.AddField(pFieldEdit); } //为字段赋值 string[] provinces = new string[] { "黑龙江省", "内蒙古自治区", "新疆维吾尔自治区", "吉林省", "甘肃省", "河北省", "北京市", "山西省", "天津市", "陕西省", "宁夏回族自治区", "青海省", "辽宁省", "山东省", "西藏自治区", "河南省", "江苏省", "安徽省", "四川省", "湖北省", "重庆市", "上海市", "浙江省", "湖南省", "江西省", "云南省", "贵州省", "广西壮族自治区", "福建省", "台湾省", "海南省", "广东省", "香港特别行政区", "澳门" }; TomorrowWeatherInfo weath; IFeatureCursor featCursor = null; IQueryFilter queryFilter = new QueryFilterClass(); for (int i = 0; i < provinces.Length; i++) { string selCity = provinces[i]; string whereClause = "[NAME] = '" + selCity + "'"; queryFilter.WhereClause = whereClause; featCursor = ProClass.Search(queryFilter, false); IFeature pFeature = featCursor.NextFeature(); string pcity = Utilities.getCity(selCity); weath = new TomorrowWeatherInfo(pcity); Random random=new Random();//测试用随机数产生器 if (pFeature != null) { pFeature.set_Value(pFeature.Fields.FindField("HighTemperature"), random.Next(20,40)); pFeature.set_Value(pFeature.Fields.FindField("LowTemperature"), random.Next(1,20)); pFeature.Store(); } } IGeoFeatureLayer geoProVince = Utilities.GetLayerByName("省市", axMapControl1.Map) as IGeoFeatureLayer; setColor(geoProVince); }
public static IField CreateField(FieldInfo fInfo) { IFieldEdit normalField = new FieldClass(); normalField.Name_2 = fInfo.Name; normalField.AliasName_2 = fInfo.AliasName; normalField.Type_2 = (esriFieldType)fInfo.Type; normalField.IsNullable_2 = fInfo.NullAble; normalField.Length_2 = fInfo.Length; normalField.Precision_2 = fInfo.Precision; return normalField; }
/// <summary> /// ��ӵ�ͼ�����ֶ� /// </summary> /// <param name="name">����</param> /// <param name="aliasName">����</param> /// <param name="type">����</param> /// <param name="length">����</param> /// <param name="defaultValue">Ĭ��ֵ</param> /// <param name="editAble">�Ƿ���Ա༭</param> public static void addAttributeField(string name, string aliasName,string type, int length, string defaultValue, bool editAble,IMap pMap) { try { //���� IFieldEdit pMyField; pMyField = new FieldClass(); pMyField.Name_2 = name; pMyField.AliasName_2 = aliasName; pMyField.Length_2 =length; if (type == "Date") pMyField.Type_2 = esriFieldType.esriFieldTypeDate; else if (type == "Integer") pMyField.Type_2 = esriFieldType.esriFieldTypeInteger; else if (type == "Single") pMyField.Type_2 = esriFieldType.esriFieldTypeSingle; else if (type == "Double") pMyField.Type_2 = esriFieldType.esriFieldTypeDouble; else if (type == "String") pMyField.Type_2 = esriFieldType.esriFieldTypeString; if (defaultValue.Equals("")) { } else { pMyField.IsNullable_2 = false; if (type == "Date") pMyField.DefaultValue_2 =DateTime.Parse(defaultValue); else if (type == "Integer") pMyField.DefaultValue_2 =int.Parse(defaultValue); else if (type == "Single") pMyField.DefaultValue_2 =float.Parse(defaultValue); else if (type == "Double") pMyField.DefaultValue_2 =double.Parse(defaultValue); else if (type == "String") pMyField.DefaultValue_2 = defaultValue; } pMyField.Editable_2 = editAble; ITable pTable = (ITable)pMap.get_Layer(GetLayerIdByName(globalInfo.currentLayerName,pMap)); pTable.AddField(pMyField); MessageBox.Show("����ֶ�" + name+ "�ɹ�"); } catch (Exception ex) { MessageBox.Show("���ʧ�ܣ�" + ex.Message); } }
public static IField CreateShapeField(FeatureClassInfo fcInfo) { IFieldEdit shpField = new FieldClass(); shpField.Name_2=string.IsNullOrWhiteSpace(fcInfo.ShapeFieldName)?"Shape":fcInfo.ShapeFieldName; shpField.Type_2 = esriFieldType.esriFieldTypeGeometry; IGeometryDefEdit geoDef=new GeometryDefClass(); geoDef.AvgNumPoints_2 = fcInfo.AvgNumPoints; geoDef.GeometryType_2=fcInfo.ShapeType; geoDef.GridCount_2=fcInfo.GridCount; geoDef.HasM_2 = fcInfo.HasM; geoDef.HasZ_2 = fcInfo.HasZ; geoDef.SpatialReference_2 = fcInfo.SpatialReference; shpField.GeometryDef_2=geoDef; return shpField; }
/// <summary> /// �½���ͼ�� /// </summary> public IFeatureClass CreatePolygonLayer(IPolygon polygon) { //LocalFilePath = sfdPoint.FileName; FilePath = Application.StartupPath + "\\tempSHP"; FileName = "test"; IFields pFields = new FieldsClass(); IFieldsEdit pFieldsEdit = pFields as IFieldsEdit; IField pField = new FieldClass(); IFieldEdit pFieldEdit = pField as IFieldEdit; pFieldEdit.Name_2 = "SHAPE"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; IGeometryDef pGeometryDef = new GeometryDefClass(); IGeometryDefEdit pGeometryDefEdit = pGeometryDef as IGeometryDefEdit; pGeometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolygon; pFieldEdit.GeometryDef_2 = pGeometryDef; pFieldsEdit.AddField(pField); pField = new FieldClass(); pFieldEdit = pField as IFieldEdit; pFieldEdit.Name_2 = "HEIGHT"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble; pFieldsEdit.AddField(pField); IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactoryClass(); IFeatureWorkspace pFeatureWorkspace = pWorkspaceFactory.OpenFromFile(FilePath, 0) as IFeatureWorkspace; IFeatureClass feaC = pFeatureWorkspace.CreateFeatureClass(FileName, pFields, null, null, esriFeatureType.esriFTSimple, "SHAPE", ""); IFeature pFeature = feaC.CreateFeature(); pFeature.Shape = polygon as IGeometry; pFeature.Store(); IFeatureCursor feaCur = feaC.Search(null, true); pFeature = feaCur.NextFeature(); while (pFeature != null) { //pFeature.set_Value(2, "181"); pFeature.Store(); pFeature = feaCur.NextFeature(); } //m_mapControl.AddShapeFile(FilePath, FileName); //m_mapControl.ActiveView.Refresh(); return feaC; }
public static IField CreateGeometryField(esriGeometryType pGT, ISpatialReference pSR, bool pHasZ) { FieldClass class2 = new FieldClass(); IFieldEdit edit = class2; edit.Name_2 = "SHAPE"; edit.AliasName_2 = "图形对象"; edit.Type_2 = esriFieldType.esriFieldTypeGeometry; IGeometryDef pGeoDef = new GeometryDefClass(); IGeometryDefEdit class3 = (IGeometryDefEdit)pGeoDef; class3.GeometryType_2 = pGT; class3.HasM_2 = false; class3.HasZ_2 = pHasZ; class3.GridCount_2 = 1; class3.set_GridSize(0, 1000.0); class3.SpatialReference_2 = pSR; edit.GeometryDef_2 = class3; return class2; }
private static void AddField(IFeatureClass featureClass, string fieldName, esriFieldType fieldType) { IField newField = new FieldClass(); ((IFieldEdit)newField).Name_2 = fieldName; ((IFieldEdit)newField).Type_2 = fieldType; var schemaLock = (ISchemaLock)featureClass; try { // Get an exclusive schema lock to change the schema. schemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock); featureClass.AddField(newField); } finally { //release the exclusive schema lock schemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock); } }
/// <summary> /// 给图层添加字段 /// </summary> /// <param name="strFieldCode"></param> /// <param name="strFieldName"></param> /// <param name="pFtCls"></param> public static void AddField(string strFieldCode, string strFieldName, Type pType, int nLength, IFeatureClass pFtCls) { try { ///创建一新字段 esriFieldType fieldType = GetFieldType(pType); IField pField = new FieldClass(); IFieldEdit pFieldEdit = (IFieldEdit)pField; pFieldEdit.Length_2 = nLength; pFieldEdit.Name_2 = strFieldName; pFieldEdit.AliasName_2 = strFieldName; pFieldEdit.IsNullable_2 = true; pFieldEdit.Required_2 = true; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString; pFtCls.AddField(pField); } catch (Exception exp) { Hy.Common.Utility.Log.OperationalLogManager.AppendMessage(exp.ToString()); } }
private void 添加面形地物ToolStripMenuItem_Click(object sender, EventArgs e) { //创建要素类 #region 创建新的内存工作空间 IWorkspaceFactory pWSF = new InMemoryWorkspaceFactoryClass(); IWorkspaceName pWSName = pWSF.Create("", "Temp", null, 0); IName pName = (IName)pWSName; IWorkspace pMemoryWS = (IWorkspace)pName.Open(); #endregion IField oField = new FieldClass(); IFields oFields = new FieldsClass(); IFieldsEdit oFieldsEdit = null; IFieldEdit oFieldEdit = null; IFeatureClass oFeatureClass = null; IFeatureLayer oFeatureLayer = null; oFieldsEdit = oFields as IFieldsEdit; oFieldEdit = oField as IFieldEdit; oFieldEdit.Name_2 = "OBJECTID"; oFieldEdit.Type_2 = esriFieldType.esriFieldTypeOID; oFieldEdit.IsNullable_2 = false; oFieldEdit.Required_2 = false; oFieldsEdit.AddField(oField); oField = new FieldClass(); oFieldEdit = oField as IFieldEdit; IGeometryDef pGeoDef = new GeometryDefClass(); IGeometryDefEdit pGeoDefEdit = (IGeometryDefEdit)pGeoDef; pGeoDefEdit.AvgNumPoints_2 = 5; pGeoDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolygon; pGeoDefEdit.GridCount_2 = 1; pGeoDefEdit.HasM_2 = false; pGeoDefEdit.HasZ_2 = false; pGeoDefEdit.SpatialReference_2 = axMapControl1.SpatialReference; oFieldEdit.Name_2 = "SHAPE"; oFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; oFieldEdit.GeometryDef_2 = pGeoDef; oFieldEdit.IsNullable_2 = true; oFieldEdit.Required_2 = true; oFieldsEdit.AddField(oField); oField = new FieldClass(); oFieldEdit = oField as IFieldEdit; oFieldEdit.Name_2 = "Code"; oFieldEdit.Type_2 = esriFieldType.esriFieldTypeSmallInteger; oFieldEdit.IsNullable_2 = true; oFieldsEdit.AddField(oField); //创建要素类 oFeatureClass = (pMemoryWS as IFeatureWorkspace).CreateFeatureClass("Temp", oFields, null, null, esriFeatureType.esriFTSimple, "SHAPE", ""); oFeatureLayer = new FeatureLayerClass(); oFeatureLayer.Name = "PolygonLayer"; oFeatureLayer.FeatureClass = oFeatureClass; //创建唯一值符号化对象 IUniqueValueRenderer pURender = new UniqueValueRendererClass(); pURender.FieldCount = 1; pURender.set_Field(0, "Code"); pURender.UseDefaultSymbol = false; ISimpleFillSymbol pFillSym = new SimpleFillSymbolClass(); pFillSym.Style = esriSimpleFillStyle.esriSFSSolid; //半透明颜色 IRgbColor pColor = new RgbColorClass(); pColor.Red = 255; pColor.Green = 255; pFillSym.Color = pColor; pURender.AddValue("1", "", pFillSym as ISymbol); pFillSym = new SimpleFillSymbolClass(); pFillSym.Style = esriSimpleFillStyle.esriSFSSolid; //唯一值符号化内存图层 (oFeatureLayer as IGeoFeatureLayer).Renderer = pURender as IFeatureRenderer; ILayerEffects pLyrEffect = oFeatureLayer as ILayerEffects; //透明度 pLyrEffect.Transparency = 0; this.axMapControl1.AddLayer(oFeatureLayer, axMapControl1.LayerCount); insertPolygon = true; }
private void fillFields() { if (rd != null) rd.addMessage("Output table name = " + wks.PathName + "\\" + tblName); //bool weCreate = true; //if (!geoUtil.ftrExists(wks, tblName)) //{ IFields nflds = new FieldsClass(); IFieldsEdit nfldsE = (IFieldsEdit)nflds; IField nfld = new FieldClass(); IFieldEdit nfldE = (IFieldEdit)nfld; nfldE.Name_2 = "Band"; nfldE.Type_2 = esriFieldType.esriFieldTypeDouble; nfldsE.AddField(nfldE); IField nfld2 = new FieldClass(); IFieldEdit nfld2E = (IFieldEdit)nfld2; nfld2E.Name_2 = "Zone"; nfld2E.Type_2 = esriFieldType.esriFieldTypeDouble; nfldsE.AddField(nfld2E); IField nfld3 = new FieldClass(); IFieldEdit nfld3E = (IFieldEdit)nfld3; nfld3E.Name_2 = "Count"; nfld3E.Type_2 = esriFieldType.esriFieldTypeDouble; nfldsE.AddField(nfld3E); oTbl = geoUtil.createTable(wks, tblName, nflds); int[] fldIndex = new int[ZoneTypes.Length]; for (int zIndex=0; zIndex<ZoneTypes.Length;zIndex++) { string fldNm = ZoneTypes[zIndex].ToString(); fldNm = geoUtil.createField(oTbl, fldNm, esriFieldType.esriFieldTypeDouble); fldIndex[zIndex] = oTbl.FindField(fldNm); } IWorkspaceEdit wksE = (IWorkspaceEdit)wks; if (wksE.IsBeingEdited()) { wksE.StopEditing(true); } try { int bdIndex = oTbl.FindField("Band"); int vlIndex = oTbl.FindField("Zone"); int cntIndex = oTbl.FindField("Count"); int bndCnt = 1; ICursor cur = oTbl.Insert(true); IRowBuffer rw = oTbl.CreateRowBuffer(); foreach (Dictionary<double, object[]> zoneValueDicOut in zoneValueDicArr) { foreach (KeyValuePair<double, object[]> kVp in zoneValueDicOut) { double key = kVp.Key; object[] vl = kVp.Value; Dictionary<rasterUtil.zoneType, double> vDic = getValueDic(vl); //IRow rw = null; //if (!weCreate) //{ // string qry = "Band = " + bndCnt.ToString() + " and Zone = " + key; // IQueryFilter qf = new QueryFilterClass(); // qf.WhereClause = qry; // ISelectionSet tblSelectionSet = oTbl.Select(qf, esriSelectionType.esriSelectionTypeIDSet, esriSelectionOption.esriSelectionOptionOnlyOne, wks); // if (tblSelectionSet.Count > 0) // { // int id = tblSelectionSet.IDs.Next(); // rw = oTbl.GetRow(id); // } // else // { // rw = oTbl.CreateRow(); // } //} //else //{ //rw = oTbl.CreateRow(); //} //Console.WriteLine(key.ToString()); rw.set_Value(bdIndex, bndCnt); rw.set_Value(vlIndex, key); rw.set_Value(cntIndex, vl[0]); for(int zIndex=0;zIndex<ZoneTypes.Length;zIndex++)// (rasterUtil.zoneType zT in ZoneTypes) { rasterUtil.zoneType zT = ZoneTypes[zIndex]; double zVl = vDic[zT]; //Console.WriteLine("\t"+fldNm+ ": " + zVl.ToString()); rw.set_Value(fldIndex[zIndex], zVl); } //rw.Store(); cur.InsertRow(rw); } bndCnt += 1; } //trs.CommitTransaction(); cur.Flush(); System.Runtime.InteropServices.Marshal.ReleaseComObject(cur); System.Runtime.InteropServices.Marshal.ReleaseComObject(rw); } catch (Exception e) { Console.WriteLine(e.ToString()); //trs.AbortTransaction(); } finally { } }
private void buildZoneClassCount() { string cTblName = geoUtil.getSafeOutputNameNonRaster(wks,tblName.Replace("_VAT","_CLASS")); if (rd != null) rd.addMessage("Output table name = " + wks.PathName + "\\" + cTblName); IFields nflds = new FieldsClass(); IFieldsEdit nfldsE = (IFieldsEdit)nflds; IField nfld = new FieldClass(); IFieldEdit nfldE = (IFieldEdit)nfld; nfldE.Name_2 = "Band"; nfldE.Type_2 = esriFieldType.esriFieldTypeDouble; nfldsE.AddField(nfldE); IField nfld2 = new FieldClass(); IFieldEdit nfld2E = (IFieldEdit)nfld2; nfld2E.Name_2 = "Zone"; nfld2E.Type_2 = esriFieldType.esriFieldTypeDouble; nfldsE.AddField(nfld2E); IField nfld4 = new FieldClass(); IFieldEdit nfld4E = (IFieldEdit)nfld4; nfld4E.Name_2 = "Class"; nfld4E.Type_2 = esriFieldType.esriFieldTypeDouble; nfldsE.AddField(nfld4E); IField nfld3 = new FieldClass(); IFieldEdit nfld3E = (IFieldEdit)nfld3; nfld3E.Name_2 = "Count"; nfld3E.Type_2 = esriFieldType.esriFieldTypeDouble; nfldsE.AddField(nfld3E); oTbl2 = geoUtil.createTable(wks,cTblName,nflds); IWorkspaceEdit wksE = (IWorkspaceEdit)wks; if (wksE.IsBeingEdited()) { wksE.StopEditing(true); } //ITransactions trs = (ITransactions)wks; //trs.StartTransaction(); try { int bdIndex = oTbl2.FindField("Band"); int vlIndex = oTbl2.FindField("Zone"); int cntIndex = oTbl2.FindField("Count"); int clsIndex = oTbl2.FindField("Class"); int bndCnt = 1; ICursor cur = oTbl2.Insert(true); IRowBuffer rw = oTbl2.CreateRowBuffer(); foreach (Dictionary<double, object[]> zoneValueDicOut in zoneValueDicArr) { foreach (KeyValuePair<double, object[]> kVp in zoneValueDicOut) { double key = kVp.Key; object[] vl = kVp.Value; Dictionary<double, int> uDic = (Dictionary<double, int>)vl[5]; foreach(KeyValuePair<double,int> uKvp in uDic) { double uDicKey = uKvp.Key; int uDicVl = uKvp.Value; rw.set_Value(bdIndex, bndCnt); rw.set_Value(vlIndex, key); rw.set_Value(cntIndex, uDicVl); rw.set_Value(clsIndex, uDicKey); cur.InsertRow(rw); } } bndCnt += 1; } cur.Flush(); System.Runtime.InteropServices.Marshal.ReleaseComObject(cur); System.Runtime.InteropServices.Marshal.ReleaseComObject(rw); //trs.CommitTransaction(); } catch(Exception e) { Console.WriteLine(e.ToString()); //trs.AbortTransaction(); } finally { } }
private static IFields CreateGdbFields() { IObjectClassDescription objectClassDescription = new FeatureClassDescriptionClass(); // create the required fields IFields fields = objectClassDescription.RequiredFields; var fieldsEdit = (IFieldsEdit)fields; IFieldEdit fieldEdit = new FieldClass(); fieldEdit.Name_2 = "Col"; fieldEdit.Type_2 = esriFieldType.esriFieldTypeInteger; fieldEdit.AliasName_2 = "Column"; fieldsEdit.AddField(fieldEdit); // ReSharper disable UseObjectOrCollectionInitializer // The field class COM object does not support object initializers fieldEdit = new FieldClass(); fieldEdit.Name_2 = "Row"; fieldEdit.Type_2 = esriFieldType.esriFieldTypeInteger; fieldsEdit.AddField(fieldEdit); fieldEdit = new FieldClass(); fieldEdit.Name_2 = "Col_Label"; fieldEdit.Type_2 = esriFieldType.esriFieldTypeString; fieldEdit.AliasName_2 = "Column Label"; fieldEdit.Length_2 = 20; fieldsEdit.AddField(fieldEdit); fieldEdit = new FieldClass(); fieldEdit.Name_2 = "Row_Label"; fieldEdit.Type_2 = esriFieldType.esriFieldTypeString; fieldEdit.AliasName_2 = "Row Label"; fieldEdit.Length_2 = 20; fieldsEdit.AddField(fieldEdit); fieldEdit = new FieldClass(); fieldEdit.Name_2 = "Cell_Label"; fieldEdit.Type_2 = esriFieldType.esriFieldTypeString; fieldEdit.AliasName_2 = "Cell Label"; fieldEdit.Length_2 = 20; fieldsEdit.AddField(fieldEdit); fieldEdit = new FieldClass(); fieldEdit.Name_2 = "Page"; fieldEdit.Type_2 = esriFieldType.esriFieldTypeInteger; fieldEdit.AliasName_2 = "Page Number"; fieldsEdit.AddField(fieldEdit); // ReSharper restore UseObjectOrCollectionInitializer return fields; }
///<summary>Simple helper to create a featureclass in a geodatabase.</summary> /// ///<param name="workspace">An IWorkspace2 interface</param> ///<param name="featureDataset">An IFeatureDataset interface or Nothing</param> ///<param name="featureClassName">A System.String that contains the name of the feature class to open or create. Example: "states"</param> ///<param name="fields">An IFields interface</param> ///<param name="clsid">A UID value or Nothing. Example "esriGeoDatabase.Feature" or Nothing</param> ///<param name="clsext">A UID value or Nothing (this is the class extension if you want to reference a class extension when creating the feature class).</param> ///<param name="strConfigKeyword">An empty System.String or RDBMS table string for ArcSDE. Example: "myTable" or ""</param> /// ///<returns>An IFeatureClass interface or a Nothing</returns> /// ///<remarks> /// (1) If a 'featureClassName' already exists in the workspace a reference to that feature class /// object will be returned. /// (2) If an IFeatureDataset is passed in for the 'featureDataset' argument the feature class /// will be created in the dataset. If a Nothing is passed in for the 'featureDataset' /// argument the feature class will be created in the workspace. /// (3) When creating a feature class in a dataset the spatial reference is inherited /// from the dataset object. /// (4) If an IFields interface is supplied for the 'fields' collection it will be used to create the /// table. If a Nothing value is supplied for the 'fields' collection, a table will be created using /// default values in the method. /// (5) The 'strConfigurationKeyword' parameter allows the application to control the physical layout /// for this table in the underlying RDBMSfor example, in the case of an Oracle database, the /// configuration keyword controls the tablespace in which the table is created, the initial and /// next extents, and other properties. The 'strConfigurationKeywords' for an ArcSDE instance are /// set up by the ArcSDE data administrator, the list of available keywords supported by a workspace /// may be obtained using the IWorkspaceConfiguration interface. For more information on configuration /// keywords, refer to the ArcSDE documentation. When not using an ArcSDE table use an empty /// string (ex: ""). ///</remarks> internal IFeatureClass CreateFeatureClass(IWorkspace2 workspace, IFeatureDataset featureDataset, string featureClassName, IFields fields, ESRI.ArcGIS.esriSystem.UID clsid, ESRI.ArcGIS.esriSystem.UID clsext, string strConfigKeyword) { if (string.IsNullOrEmpty(featureClassName)) return null; IFeatureClass featureClass; var featureWorkspace = (IFeatureWorkspace)workspace; // Cast may throw exception if (workspace.NameExists[esriDatasetType.esriDTFeatureClass, featureClassName]) //feature class with that name already exists { featureClass = featureWorkspace.OpenFeatureClass(featureClassName); return featureClass; } // assign the class id value if not assigned if (clsid == null) { clsid = new ESRI.ArcGIS.esriSystem.UIDClass { Value = "esriGeoDatabase.Feature" }; //Works for shapefiles as well } IObjectClassDescription objectClassDescription = new FeatureClassDescriptionClass(); // if a fields collection is not passed in then supply our own if (fields == null) { // create the fields using the required fields method fields = objectClassDescription.RequiredFields; var fieldsEdit = (IFieldsEdit)fields; // Explicit Cast IField field = new FieldClass(); // create a user defined text field var fieldEdit = (IFieldEdit)field; // Explicit Cast // setup field properties fieldEdit.Name_2 = "SampleField"; fieldEdit.Type_2 = esriFieldType.esriFieldTypeString; fieldEdit.IsNullable_2 = true; fieldEdit.AliasName_2 = "Sample Field Column"; fieldEdit.DefaultValue_2 = "test"; fieldEdit.Editable_2 = true; fieldEdit.Length_2 = 100; // add field to field collection fieldsEdit.AddField(field); fields = fieldsEdit; } string strShapeField = ""; // locate the shape field for (int j = 0; j < fields.FieldCount; j++) { if (fields.Field[j].Type != esriFieldType.esriFieldTypeGeometry) continue; strShapeField = fields.Field[j].Name; break; } // Use IFieldChecker to create a validated fields collection. IFieldChecker fieldChecker = new FieldCheckerClass(); IEnumFieldError enumFieldError; IFields validatedFields; fieldChecker.ValidateWorkspace = (IWorkspace)workspace; fieldChecker.Validate(fields, out enumFieldError, out validatedFields); // The enumFieldError enumerator can be inspected at this point to determine // which fields were modified during validation. // finally create and return the feature class featureClass = featureDataset == null ? featureWorkspace.CreateFeatureClass(featureClassName, validatedFields, clsid, clsext, esriFeatureType.esriFTSimple, strShapeField, strConfigKeyword) : featureDataset.CreateFeatureClass(featureClassName, validatedFields, clsid, clsext, esriFeatureType.esriFTSimple, strShapeField, strConfigKeyword); return featureClass; }
/// <summary> /// 储存点shp,聚类号在"index"字段中标注 /// </summary> public void CreatePointsShapefile() { string filePath = m_dataInfo.GetOutputFilePath(); string fileName = m_dataInfo.GetOutputFileName(); ISpatialReference spatialReference = m_dataInfo.GetSpatialReference(); int index = fileName.LastIndexOf("."); fileName = fileName.Substring(0, index); fileName = fileName + "_pts.shp"; //打开工作空间 const string strShapeFieldName = "shape"; IWorkspaceFactory pWSF = new ShapefileWorkspaceFactoryClass(); IFeatureWorkspace pWS = (IFeatureWorkspace)pWSF.OpenFromFile(filePath, 0); //设置字段集 IFields pFields = new FieldsClass(); IFieldsEdit pFieldsEdit = (IFieldsEdit)pFields; //设置字段 IField pField = new FieldClass(); IFieldEdit pFieldEdit = (IFieldEdit)pField; //创建类型为几何类型的字段 pFieldEdit.Name_2 = strShapeFieldName; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; //为esriFieldTypeGeometry类型的字段创建几何定义,包括类型和空间参照 IGeometryDef pGeoDef = new GeometryDefClass(); //The geometry definition for the field if IsGeometry is TRUE. IGeometryDefEdit pGeoDefEdit = (IGeometryDefEdit)pGeoDef; pGeoDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint; pGeoDefEdit.SpatialReference_2 = spatialReference; pFieldEdit.GeometryDef_2 = pGeoDef; pFieldsEdit.AddField(pField); pField = new FieldClass(); pFieldEdit = (IFieldEdit)pField; pFieldEdit.Name_2 = "index"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeInteger; pFieldEdit.Precision_2 = 7;//数值精度 // pFieldEdit.Scale_2 = 3;//小数点位数 pFieldsEdit.AddField(pField); //创建shapefile pWS.CreateFeatureClass(fileName, pFields, null, null, esriFeatureType.esriFTSimple, strShapeFieldName, ""); //在featureclass中创建feature IWorkspaceEdit workspaceEdit = pWS as IWorkspaceEdit; workspaceEdit.StartEditing(true); workspaceEdit.StartEditOperation(); IFeatureClass featureClass = pWS.OpenFeatureClass(fileName); IFeatureBuffer featureBuffer = featureClass.CreateFeatureBuffer(); IFeatureCursor featureCursor = featureClass.Search(null, true); IFeature feature = featureCursor.NextFeature(); while (feature != null) { feature.Delete(); feature = featureCursor.NextFeature(); } featureCursor = featureClass.Insert(true); int typeFieldIndex = featureClass.FindField("index"); //插入点,并写入聚类号 for (int i = 0; i < m_clusters.GetClusterCount(); i++) { Cluster currentCluster = m_clusters.GetCluster(i); List<IPoint> currentPoints = currentCluster.GetPointsList(); for (int j = 0; j < currentPoints.Count; j++) { featureBuffer.set_Value(typeFieldIndex, currentCluster.GetClusterIndex()); featureBuffer.Shape = currentPoints[j] as IGeometry; object featureOID = featureCursor.InsertFeature(featureBuffer); } } featureCursor.Flush(); workspaceEdit.StopEditOperation(); workspaceEdit.StopEditing(true); System.Runtime.InteropServices.Marshal.ReleaseComObject(featureCursor); //将新创建的shapfile作为图层添加到map里 IFeatureLayer featureLayer = new FeatureLayerClass(); featureLayer.FeatureClass = featureClass; featureLayer.Name = featureClass.AliasName; m_pointLayer = featureLayer; ////设置渲染 //DefinePointUniqueValueRenderer(featureLayer as IGeoFeatureLayer, "Class"); //m_mapControl.AddLayer(featureLayer as ILayer); //this.m_mapControl.Refresh(); }
private void setFeatureClassFieldValue(IFeatureClass feacls, string strfield, int val) { if(feacls.FindField(strfield)<0) { IField field = new FieldClass(); IFieldEdit fe = field as IFieldEdit; fe.Name_2 = strfield; fe.Type_2 = esriFieldType.esriFieldTypeInteger; feacls.AddField(field); } int index = feacls.FindField(strfield); IFeatureCursor cursor = feacls.Search(null, false); IFeature fea = cursor.NextFeature(); while (fea != null) { fea.set_Value(index, val); fea.Store(); fea = cursor.NextFeature(); } }
public IFeatureClass reprojectInFeatureClass(IFeatureClass InFeatureClass, ISpatialReference SpatialReference) { IWorkspace tempWorkspace = geoUtil.OpenWorkSpace(tempWksStr); string outNm = ((IDataset)InFeatureClass).BrowseName+"_PR"; outNm = geoUtil.getSafeOutputNameNonRaster(wks,outNm); IFields outFlds = new FieldsClass(); IFieldsEdit outFldsE = (IFieldsEdit)outFlds; IField inFld = InFeatureClass.Fields.get_Field(InFeatureClass.FindField(ftrField)); IField outFld = new FieldClass(); if (inFld.Type == esriFieldType.esriFieldTypeOID) { IFieldEdit outFldE = (IFieldEdit)outFld; outFldE.Type_2 = esriFieldType.esriFieldTypeInteger; outFldE.Name_2 = inFld.Name; } else { IClone cl = (IClone)inFld; outFld = (IField)cl.Clone(); } outFldsE.AddField(outFld); IFeatureClass outFtrCls = geoUtil.createFeatureClass((IWorkspace2)tempWorkspace,outNm,outFldsE,InFeatureClass.ShapeType,SpatialReference); string ozName = ftrField; int ozIndex = outFtrCls.FindField(ozName); if (ozIndex == -1) { ozName = ftrField+"_1"; ozIndex = outFtrCls.FindField(ozName); } int izIndex = InFeatureClass.FindField(ftrField); IQueryFilter qf = new QueryFilterClass(); qf.SubFields = InFeatureClass.ShapeFieldName + "," + ftrField; IFeatureCursor fCur = InFeatureClass.Search(qf, false); IFeature ftr = fCur.NextFeature(); IWorkspaceEdit wksE = (IWorkspaceEdit)tempWorkspace; bool weStart = true; if(wksE.IsBeingEdited()) { weStart=false; } else { wksE.StartEditing(false); } wksE.StartEditOperation(); try { while (ftr != null) { object vl = ftr.get_Value(izIndex); IFeatureProject ftrP = (IFeatureProject)ftr; ftrP.Project(SpatialReference); IFeature oFtr = outFtrCls.CreateFeature(); oFtr.Shape = ftr.Shape; if(ozIndex>-1) oFtr.set_Value(ozIndex, vl); oFtr.Store(); ftr = fCur.NextFeature(); } ftrField = ozName; } catch (Exception e) { Console.WriteLine(e.ToString()); } finally { wksE.StopEditOperation(); if (weStart) wksE.StopEditing(true); } return outFtrCls; }