/// <summary> /// Create the polyline feature class /// </summary> /// <param name="featWorkspace">IFeatureWorkspace</param> /// <param name="name">Name of the featureclass</param> /// <returns>IFeatureClass</returns> private IFeatureClass CreatePolylineFeatureClass(IFeatureWorkspace featWorkspace, string name) { IFieldsEdit pFldsEdt = new FieldsClass(); IFieldEdit pFldEdt = new FieldClass(); pFldEdt = new FieldClass(); pFldEdt.Type_2 = esriFieldType.esriFieldTypeOID; pFldEdt.Name_2 = "OBJECTID"; pFldEdt.AliasName_2 = "OBJECTID"; pFldsEdt.AddField(pFldEdt); IGeometryDefEdit pGeoDef; pGeoDef = new GeometryDefClass(); pGeoDef.GeometryType_2 = esriGeometryType.esriGeometryPolyline; pGeoDef.SpatialReference_2 = ArcMap.Document.FocusMap.SpatialReference; pFldEdt = new FieldClass(); pFldEdt.Name_2 = "SHAPE"; pFldEdt.AliasName_2 = "SHAPE"; pFldEdt.Type_2 = esriFieldType.esriFieldTypeGeometry; pFldEdt.GeometryDef_2 = pGeoDef; pFldsEdt.AddField(pFldEdt); IFeatureClass pFClass = featWorkspace.CreateFeatureClass(name, pFldsEdt, null, null, esriFeatureType.esriFTSimple, "SHAPE", ""); return pFClass; }
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; }
/// <summary> /// 在内存中创建要素类 /// </summary> /// <returns></returns> public IFeatureClass MemoryFeatureClass() { AccessGeoData accData = new AccessGeoData(); IFeatureWorkspace pFeaWorkspace = accData.FeatureWorkspace(); IFields fields = new FieldsClass(); IFieldsEdit fedits = (IFieldsEdit)fields; IField objfield = new FieldClass(); IFieldEdit objEdit = (IFieldEdit)objfield; objEdit.Name_2 = "OBJECTID"; objEdit.Type_2 = esriFieldType.esriFieldTypeOID; fedits.AddField(objEdit); IField Spfield = new FieldClass(); IFieldEdit fEdit = (IFieldEdit)Spfield; IGeometryDef pGeoDef = new GeometryDefClass(); IGeometryDefEdit pGeoDefEdit = (IGeometryDefEdit)pGeoDef; pGeoDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint; pGeoDefEdit.SpatialReference_2 = spatialRef; fEdit.Name_2 = "Shape"; fEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; fEdit.GeometryDef_2 = pGeoDef; fedits.AddField(Spfield); IField fieldZ = new FieldClass(); IFieldEdit EditZ = (IFieldEdit)fieldZ; EditZ.Name_2 = "Z"; EditZ.Type_2 = esriFieldType.esriFieldTypeDouble; fedits.AddField(EditZ); IFeatureClass pFeaClass = pFeaWorkspace.CreateFeatureClass("FeaturePoints", fields, null, null, esriFeatureType.esriFTSimple, "Shape", ""); return pFeaClass; }
//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); }
private static void VerifyErrorHasZ(IFeatureWorkspace ws) { IFieldsEdit fields = new FieldsClass(); fields.AddField(FieldUtils.CreateOIDField()); fields.AddField(FieldUtils.CreateShapeField( "Shape", esriGeometryType.esriGeometryPolyline, SpatialReferenceUtils.CreateSpatialReference ((int)esriSRProjCS2Type.esriSRProjCS_CH1903Plus_LV95, true), 1000, true, false)); IFeatureClass featureClass = DatasetUtils.CreateSimpleFeatureClass(ws, "VerifyErrorHasZ", fields, null); // make sure the table is known by the workspace ((IWorkspaceEdit)ws).StartEditing(false); ((IWorkspaceEdit)ws).StopEditing(true); IPolycurve line1 = CurveConstruction.StartLine(0, 0, 5) .LineTo(2, 0, 5) .Curve; IFeature row1 = featureClass.CreateFeature(); row1.Shape = line1; row1.Store(); IPolycurve line2 = CurveConstruction.StartLine(-1, 0.02, 5) .LineTo(1, 0.02, 5) .Curve; IFeature row2 = featureClass.CreateFeature(); row2.Shape = line2; row2.Store(); var test = new QaNotNear(featureClass, 0.1, 0.5); var runners = new List <QaTestRunnerBase> { new QaTestRunner(test) { KeepGeometry = true }, new QaContainerTestRunner(1000, test) { KeepGeometry = true } }; foreach (QaTestRunnerBase runner in runners) { runner.Execute(); Assert.True(runner.ErrorGeometries.Count > 0); foreach (IGeometry errorGeometry in runner.ErrorGeometries) { Assert.AreEqual(5, errorGeometry.Envelope.ZMin); } } }
/// <summary> /// 在地理数据库中创建要素类 /// </summary> /// <param name="workspacePath">工作空间路径</param> /// <param name="feaclassName">所创建要素类名称</param> /// <param name="GeometeyType">枚举要素类类型: esriGeimetryType(</param> /// <param name="gcsType">地理空间坐标系类型,格式如: (int)esriSRGeoCS3Type.esriSRGeoCS_Xian1980</param> /// <returns></returns> public IFeatureClass CreateFeatureClass(string workspacePath, string feaclassName, esriGeometryType GeometeyType, int gcsType) { AccessGeoData accData = new AccessGeoData(); IFeatureWorkspace pFeaWorkspace = accData.OpenFeatureWorkspace(workspacePath); spatialRef = spatialRefFactory.CreateGeographicCoordinateSystem(gcsType); //字段编辑:包括ObjectID 与 Shape IFields fields = new FieldsClass(); IFieldsEdit fedits = (IFieldsEdit)fields; //构建唯一标识符字段 IField objfield = new FieldClass(); IFieldEdit objEdit = (IFieldEdit)objfield; objEdit.Name_2 = "OBJECTID"; objEdit.Type_2 = esriFieldType.esriFieldTypeOID; fedits.AddField(objEdit); //构建shape IField Spfield = new FieldClass(); IFieldEdit fEdit = (IFieldEdit)Spfield; IGeometryDef pGeoDef = new GeometryDefClass(); IGeometryDefEdit pGeoDefEdit = (IGeometryDefEdit)pGeoDef; pGeoDefEdit.GeometryType_2 = GeometeyType; pGeoDefEdit.SpatialReference_2 = spatialRef; fEdit.Name_2 = "Shape"; fEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; fEdit.GeometryDef_2 = pGeoDef; fedits.AddField(Spfield); //附加一个可编辑字段,存放属性值 IField fieldZ = new FieldClass(); IFieldEdit EditZ = (IFieldEdit)fieldZ; EditZ.Name_2 = "Value"; EditZ.Type_2 = esriFieldType.esriFieldTypeDouble; fedits.AddField(EditZ); //若有其他需求,在此处添加字段,参加Value字段添加方式 IFeatureClass pFeaClass = pFeaWorkspace.CreateFeatureClass(feaclassName, fields, null, null, esriFeatureType.esriFTSimple, "Shape", ""); return pFeaClass; }
// This method will update the output parameter value with the additional area field. public void UpdateParameters(IArray paramvalues, IGPEnvironmentManager pEnvMgr) { m_Parameters = paramvalues; // Retrieve the input parameter value IGPValue parameterValue = m_GPUtilities.UnpackGPValue(m_Parameters.get_Element(0)); // Get the derived output feature class schema and empty the additional fields. This will ensure you don't get duplicate entries. IGPParameter3 derivedFeatures = (IGPParameter3)paramvalues.get_Element(2); IGPFeatureSchema schema = (IGPFeatureSchema)derivedFeatures.Schema; schema.AdditionalFields = null; // If we have an input value, create a new field based on the field name the user entered. if (parameterValue.IsEmpty() == false) { IGPParameter3 fieldNameParameter = (IGPParameter3)paramvalues.get_Element(1); string fieldName = fieldNameParameter.Value.GetAsText(); // Check if the user's input field already exists IField areaField = m_GPUtilities.FindField(parameterValue, fieldName); if (areaField == null) { IFieldsEdit fieldsEdit = new FieldsClass(); IFieldEdit fieldEdit = new FieldClass(); fieldEdit.Name_2 = fieldName; fieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble; fieldsEdit.AddField(fieldEdit); // Add an additional field for the area values to the derived output. IFields fields = fieldsEdit as IFields; schema.AdditionalFields = fields; } } }
/// <summary> /// 创建内存IFeatureLayer,该方法为创建内存临时图层的便捷方法,创建的图层带有id,和name两个属性字段。 /// </summary> /// <param name="myAxMapControl">AxMapControl扩展对象</param> /// <param name="strIFeatureClassName">IFeatureClass名称</param> /// <param name="myEsriGeometryType">数据类型</param> /// <returns>IFeatureLayer对象</returns> public static IFeatureLayer CreateMemoryFeatureLayer(this AxMapControl myAxMapControl, String strIFeatureClassName, esriGeometryType myEsriGeometryType) { IWorkspaceFactory myIWorkspaceFactory = new InMemoryWorkspaceFactoryClass(); IWorkspaceName myIWorkspaceName = myIWorkspaceFactory.Create("", strIFeatureClassName, null, 0); IName myIName = (IName)myIWorkspaceName; IWorkspace myIWorkspace = (IWorkspace)myIName.Open(); IField myIField = null; IFields myIFields = new FieldsClass(); IFieldsEdit myIFieldsEdit = myIFields as IFieldsEdit; IFieldEdit myIFieldEdit = null; IFeatureClass myIFeatureClass = null; IFeatureLayer myIFeatureLayer = null; try { //主键id myIField = new FieldClass(); myIFieldEdit = myIField as IFieldEdit; myIFieldEdit.Name_2 = "id"; myIFieldEdit.IsNullable_2 = true; myIFieldEdit.Length_2 = 50; myIFieldEdit.Type_2 = esriFieldType.esriFieldTypeString; myIFieldsEdit.AddField(myIField); //名称name myIField = new FieldClass(); myIFieldEdit = myIField as IFieldEdit; myIFieldEdit.Name_2 = "name"; myIFieldEdit.IsNullable_2 = true; myIFieldEdit.Length_2 = 50; myIFieldEdit.Type_2 = esriFieldType.esriFieldTypeString; myIFieldsEdit.AddField(myIField); //IGeometryI字段 IGeometryDef myIGeometryDef = new GeometryDefClass(); IGeometryDefEdit myIGeometryDefEdit = (IGeometryDefEdit)myIGeometryDef; myIGeometryDefEdit.AvgNumPoints_2 = 5; myIGeometryDefEdit.GeometryType_2 = myEsriGeometryType; myIGeometryDefEdit.GridCount_2 = 1; myIGeometryDefEdit.HasM_2 = false; myIGeometryDefEdit.HasZ_2 = false; myIGeometryDefEdit.SpatialReference_2 = myAxMapControl.SpatialReference; myIField = new FieldClass(); myIFieldEdit = myIField as IFieldEdit; myIFieldEdit.Name_2 = "SHAPE"; myIFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; myIFieldEdit.GeometryDef_2 = myIGeometryDef; myIFieldEdit.IsNullable_2 = true; myIFieldEdit.Required_2 = true; myIFieldsEdit.AddField(myIField); // myIFeatureClass = (myIWorkspace as IFeatureWorkspace).CreateFeatureClass(strIFeatureClassName, myIFields, null, null, esriFeatureType.esriFTSimple, "SHAPE", ""); (myIFeatureClass as IDataset).BrowseName = strIFeatureClassName; myIFeatureLayer = new FeatureLayerClass(); myIFeatureLayer.Name = strIFeatureClassName; myIFeatureLayer.FeatureClass = myIFeatureClass; } catch { } return(myIFeatureLayer); }
public void TestUniqueIntegers() { IFieldsEdit fields = new FieldsClass(); fields.AddField(FieldUtils.CreateOIDField()); fields.AddField(FieldUtils.CreateField("Unique", esriFieldType.esriFieldTypeInteger)); ITable table = TestWorkspaceUtils.CreateSimpleTable(_pgdbWorkspace, "Unique1", fields); for (var i = 0; i < 10; i++) { IRow row = table.CreateRow(); row.set_Value(1, i); row.Store(); } foreach (bool forceInMemoryTableSort in new[] { false, true }) { var test = new QaUnique(table, "Unique") { ForceInMemoryTableSorting = forceInMemoryTableSort }; var runner = new QaTestRunner(test); runner.Execute(); AssertUtils.NoError(runner); } }
public void CanCheckGuids() { IFieldsEdit fields = new FieldsClass(); fields.AddField(FieldUtils.CreateOIDField()); fields.AddField(FieldUtils.CreateField("UUID", esriFieldType.esriFieldTypeGUID)); ITable table = TestWorkspaceUtils.CreateSimpleTable(_fgdbWorkspace, "CanCheckGuids", fields); Guid latest = Guid.NewGuid(); IRow row; for (var i = 0; i < 10; i++) { latest = Guid.NewGuid(); row = table.CreateRow(); row.set_Value(1, latest.ToString("B")); row.Store(); } row = table.CreateRow(); row.set_Value(1, latest.ToString("B")); row.Store(); //IWorkspace ws = TestDataUtils.OpenTopgisTlm(); //ITable table = ((IFeatureWorkspace) ws).OpenTable("TOPGIS_TLM.TLM_STRASSE"); var test = new QaUnique(table, "UUID"); var runner = new QaTestRunner(test); runner.Execute(); Assert.AreEqual(2, runner.Errors.Count); }
private DataTable CreateDataTable(ITable table, string[] fieldsName) { //创建一个DataTable表 DataTable pDataTable = new DataTable(); //取得ITable接口 IField pField = null; DataColumn pDataColumn; IFields fields = new FieldsClass(); if (fieldsName == null) { fields = table.Fields; } else { for (int i = 0; i < fieldsName.Length; i++) { int index = table.Fields.FindField(fieldsName[i]); if (index != -1) { IField field = table.Fields.get_Field(index); (fields as IFieldsEdit).AddField(field); } } } //根据每个字段的属性建立DataColumn对象 for (int i = 0; i < fields.FieldCount; i++) { pField = fields.get_Field(i); //新建一个DataColumn并设置其属性 pDataColumn = new DataColumn(pField.Name); //if (pField.Name == table.OIDFieldName) //{ // pDataColumn.Unique = true;//字段值是否唯一 //} //字段值是否允许为空 pDataColumn.AllowDBNull = pField.IsNullable; //字段别名 pDataColumn.Caption = pField.AliasName; //字段数据类型 pDataColumn.DataType = System.Type.GetType(CovertFieldType(pField.Type)); //字段默认值 pDataColumn.DefaultValue = pField.DefaultValue; //当字段为String类型是设置字段长度 //if (pField.VarType == 8 && pDataColumn.MaxLength < pField.Length) //{ // if (pField.Name == "FID_1") // pDataColumn.MaxLength = pField.Length; //} //字段添加到表中 pDataTable.Columns.Add(pDataColumn); pField = null; pDataColumn = null; } return(pDataTable); }
/// <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 IFeatureClass CreateShapeFile(DataSet ds, string strOutShpName, ISpatialReference pSRF) { try { DirectoryInfo di = new DirectoryInfo(strOutShpName); string filefolder = di.Parent.FullName; ClsGDBDataCommon cdc = new ClsGDBDataCommon(); //cdc.OpenFromShapefile(filefolder); IFields pFields = new FieldsClass(); IFieldsEdit pFieldsEdit = (IFieldsEdit)pFields; //设置字段 IField pField = new FieldClass(); IFieldEdit pFieldEdit = (IFieldEdit)pField; //创建类型为几何类型的字段 IGeometryDef pGeoDef = new GeometryDefClass(); IGeometryDefEdit pGeoDefEdit = (IGeometryDefEdit)pGeoDef; pGeoDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint; pGeoDefEdit.HasM_2 = false; pGeoDefEdit.HasZ_2 = false; pGeoDefEdit.SpatialReference_2 = pSRF; pFieldEdit.Name_2 = "SHAPE"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; pFieldEdit.GeometryDef_2 = pGeoDef; //pFieldEdit.IsNullable_2 = true; //pFieldEdit.Required_2 = true; pFieldsEdit.AddField(pField); for (int i = 0; i < ds.Tables[0].Columns.Count; i++) { pField = new FieldClass(); pFieldEdit = (IFieldEdit)pField; pFieldEdit.Name_2 = ds.Tables[0].Columns[i].ColumnName; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString; pFieldEdit.IsNullable_2 = true; pFieldsEdit.AddField(pField); } ClsGDBDataCommon comm = new ClsGDBDataCommon(); IWorkspace inmemWor = comm.OpenFromShapefile(filefolder); // ifeatureworkspacee IFeatureWorkspace pFeatureWorkspace = inmemWor as IFeatureWorkspace; IFeatureClass pFeatureClass = pFeatureWorkspace.CreateFeatureClass(di.Name, pFields, null, null, esriFeatureType.esriFTSimple, "SHAPE", ""); return(pFeatureClass); //IFeatureLayer pFeatureLayer = new FeatureLayerClass(); //pFeatureLayer.FeatureClass = pFeatureClass; //pFeatureLayer.Name = System.IO.Path.GetFileNameWithoutExtension(di.Name); //m_mapControl.AddLayer(pFeatureLayer as ILayer, 0); //m_mapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null); } catch (SystemException ee) { MessageBox.Show(ee.Message); return(null); } }
//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; }
private static IFeatureClass EnsureFeatureClass(IFeatureWorkspace ws, esriGeometryType geometryType, string name, ISpatialReference sr) { try { IFeatureClass existing = DatasetUtils.OpenFeatureClass(ws, name); DatasetUtils.DeleteFeatureClass(existing); } catch (Exception e) { Console.WriteLine(e); } IFieldsEdit fields = new FieldsClass(); fields.AddField(FieldUtils.CreateOIDField()); fields.AddField(FieldUtils.CreateShapeField( "Shape", geometryType, sr, 1000, true)); IFeatureClass fc = DatasetUtils.CreateSimpleFeatureClass(ws, name, fields); if (fc is IVersionedTable) { DatasetUtils.RegisterAsVersioned(fc); } return(fc); }
private void createFeatureClass() { string ftClsPath = rsUtil.TempMosaicDir + "\\catBnd.shp"; IFields flds = new FieldsClass(); IFieldsEdit fldsE = (IFieldsEdit)flds; IField fld = new FieldClass(); IFieldEdit fldE = (IFieldEdit)fld; fldE.Name_2 = "catIndex"; fldE.Type_2 = esriFieldType.esriFieldTypeSmallInteger; fldsE.AddField(fldE); ftCls = geoUtil.createFeatureClass(ftClsPath, fldsE, esriGeometryType.esriGeometryPolygon, sr); int catInd = ftCls.FindField("catIndex"); int cnt = 0; foreach (IRaster rs in inrs) { IFeature ftr = ftCls.CreateFeature(); ftr.set_Value(catInd, cnt); IEnvelope ext = ((IRasterProps)rs).Extent; IPolygon poly = new PolygonClass(); IPointCollection pColl = (IPointCollection)poly; pColl.AddPoint(ext.UpperLeft); pColl.AddPoint(ext.UpperRight); pColl.AddPoint(ext.LowerRight); pColl.AddPoint(ext.LowerLeft); poly.Close(); ftr.Shape = poly; ftr.Store(); cnt++; } }
/// <summary> /// Create a polygon feature class used to store buffer geometries for observer points /// </summary> /// <param name="featWorkspace">IFeatureWorkspace</param> /// <param name="spatialRef">ISpatialReference of selected surface</param> /// <param name="name">Name of the feature class</param> /// <returns>IFeatureClass</returns> private static IFeatureClass CreateMaskFeatureClass(IFeatureWorkspace featWorkspace, ISpatialReference spatialRef, string name) { IFieldsEdit pFldsEdt = new FieldsClass(); IFieldEdit pFldEdt = new FieldClass(); pFldEdt = new FieldClass(); pFldEdt.Type_2 = esriFieldType.esriFieldTypeOID; pFldEdt.Name_2 = "OBJECTID"; pFldEdt.AliasName_2 = "OBJECTID"; pFldsEdt.AddField(pFldEdt); IGeometryDefEdit pGeoDef; pGeoDef = new GeometryDefClass(); pGeoDef.GeometryType_2 = esriGeometryType.esriGeometryPolygon; pGeoDef.SpatialReference_2 = spatialRef; pFldEdt = new FieldClass(); pFldEdt.Name_2 = "SHAPE"; pFldEdt.AliasName_2 = "SHAPE"; pFldEdt.Type_2 = esriFieldType.esriFieldTypeGeometry; pFldEdt.GeometryDef_2 = pGeoDef; pFldsEdt.AddField(pFldEdt); IFeatureClass pFClass = featWorkspace.CreateFeatureClass(name, pFldsEdt, null, null, esriFeatureType.esriFTSimple, "SHAPE", ""); return(pFClass); }
private IFeatureClass CreateFeatureClass(IFeatureWorkspace FeatureWorkspace, string LayerName, esriFeatureType featureType, esriGeometryType GeometryType) { IFields fields = new FieldsClass(); IFieldsEdit edit = fields as IFieldsEdit; //创建OBJECTID字段 IField field3 = new FieldClass(); IFieldEdit edit2 = field3 as IFieldEdit; edit2.Name_2 = "OBJECTID"; edit2.AliasName_2 = "OBJECTID"; edit2.Type_2 = esriFieldType.esriFieldTypeOID; edit.AddField(field3); //创建Shape字段 IGeometryDef def = new GeometryDefClass(); IGeometryDefEdit edit4 = def as IGeometryDefEdit; edit4.GeometryType_2 = GeometryType; edit4.GridCount_2 = 1; edit4.set_GridSize(0, 1000); edit4.AvgNumPoints_2 = 2; edit4.HasM_2 = false; edit4.HasZ_2 = false; edit4.SpatialReference_2 = this.ExportSpatialReference; IField field4 = new FieldClass(); IFieldEdit edit3 = field4 as IFieldEdit; edit3.Name_2 = "SHAPE"; edit3.AliasName_2 = "SHAPE"; edit3.Type_2 = esriFieldType.esriFieldTypeGeometry; edit3.GeometryDef_2 = def; edit.AddField(field4); string ShapeFiledName = field4.Name; UID uid = null; UID uid2 = null; switch (featureType) { case esriFeatureType.esriFTSimple: //FeatureClass IObjectClassDescription description4 = new FeatureClassDescriptionClass(); uid = description4.InstanceCLSID; uid2 = description4.ClassExtensionCLSID; break; case esriFeatureType.esriFTAnnotation: //AnnotationFeatureClass IObjectClassDescription description = new AnnotationFeatureClassDescriptionClass(); uid = description.InstanceCLSID; uid2 = description.ClassExtensionCLSID; GeometryType = esriGeometryType.esriGeometryPolygon; break; } //创建要素对象 IFeatureClass fc = FeatureWorkspace.CreateFeatureClass(LayerName, fields, uid, uid2, featureType, ShapeFiledName, null); return(fc); }
internal static IFields CreateBaseStationFields(ISpatialReference spatialReference) { //Set up a simple fields collection IFields pFields = new FieldsClass(); IFieldsEdit pFieldsEdit = pFields as IFieldsEdit; //Make the shape field //it will need a geometry definition, with a spatial reference IField pField = new FieldClass(); IFieldEdit pFieldEdit = pField as IFieldEdit; pFieldEdit.Name_2 = "Shape"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; IGeometryDef pGeometryDef = new GeometryDef(); IGeometryDefEdit pGeometryDefEdit = pGeometryDef as IGeometryDefEdit; pGeometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint; pGeometryDefEdit.SpatialReference_2 = spatialReference; pFieldEdit.GeometryDef_2 = pGeometryDef; pFieldsEdit.AddField(pField); //Add OID field pField = new FieldClass(); pFieldEdit = pField as IFieldEdit; pFieldEdit.Length_2 = 30; pFieldEdit.Name_2 = "OID"; //pFieldEdit.AliasName_2 = "AliasName"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString; pFieldsEdit.AddField(pField); return(pFields); }
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; } }
/// <summary> /// Creates the fields for the AttributeUnits table. /// </summary> /// <param name="pWorkspace">FileGeodatabase workspace.</param> /// <returns>List of fields.</returns> private IFields CreateUnitsFields(IWorkspace pWorkspace) { try { IFields pFields = new FieldsClass(); IFieldsEdit pFieldsEdit = (IFieldsEdit)pFields; pFieldsEdit.AddField(createField("OID", esriFieldType.esriFieldTypeOID)); pFieldsEdit.AddField(createField("Attribute", esriFieldType.esriFieldTypeString)); pFieldsEdit.AddField(createField("Unit", esriFieldType.esriFieldTypeString)); // Use IFieldChecker to create a validated fields collection. IFieldChecker fieldChecker = new FieldCheckerClass(); IEnumFieldError enumFieldError = null; IFields validatedFields = null; fieldChecker.ValidateWorkspace = (IWorkspace)pWorkspace; fieldChecker.Validate(pFields, out enumFieldError, out validatedFields); return(pFields); } catch (Exception ex) { MessageBox.Show(ex.ToString()); return(null); } }
public void MultipartTest() { IFeatureWorkspace workspace = TestWorkspaceUtils.CreateInMemoryWorkspace("MultipartTest"); IFieldsEdit fields = new FieldsClass(); fields.AddField(FieldUtils.CreateOIDField()); fields.AddField(FieldUtils.CreateShapeField( "Shape", esriGeometryType.esriGeometryPolyline, CreateLV95(), 1000)); IFeatureClass featureClass = DatasetUtils.CreateSimpleFeatureClass( workspace, "Border", fields); AddFeature(featureClass, CurveConstruction.StartLine(0, 0).LineTo(4, 0).MoveTo(6, 0).LineTo(6, 10) .Curve); AddFeature(featureClass, CurveConstruction.StartLine(6, 10).LineTo(4, 10).Curve); AddFeature(featureClass, CurveConstruction.StartLine(4, 0).LineTo(6, 0).MoveTo(4, 10).LineTo(0, 0) .Curve); // expect counter-clockwise: 0 errors var runnerCounterClockwise = new QaContainerTestRunner( 1000, new QaBorderSense(featureClass, false)); Assert.AreEqual(0, runnerCounterClockwise.Execute()); // expect clockwise: 1 error var runnerClockwise = new QaContainerTestRunner( 1000, new QaBorderSense(featureClass, true)); Assert.AreEqual(1, runnerClockwise.Execute()); }
public IFields CreateShapeFields(esriGeometryType p_esriGeotype) { //创建字段编辑所需要的接口 IFields pFields = new FieldsClass(); IFieldsEdit pFieldsEdit = pFields as IFieldsEdit; //创建基本必要的属性字段 IField pFld = new FieldClass(); IFieldEdit pFldEdit = pFld as IFieldEdit; pFldEdit.Name_2 = "shape"; pFldEdit.IsNullable_2 = false; pFldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; //增加特定的属性字段 pFieldsEdit.AddField(pFld); IGeometryDef pGeoDef = new GeometryDefClass(); IGeometryDefEdit pGeoDefEdit = pGeoDef as IGeometryDefEdit; pGeoDefEdit.GeometryType_2 = p_esriGeotype; pGeoDefEdit.SpatialReference_2 = (ISpatialReference) new UnknownCoordinateSystem(); pFldEdit.GeometryDef_2 = pGeoDef; return(pFieldsEdit as IFields); }
public IFeatureClass CreateFeatureClass() { IFeatureClass result = null; IFieldsEdit fieldsEdit = new FieldsClass(); fieldsEdit = CreateField(fieldsEdit, "ObjectId", esriFieldType.esriFieldTypeOID); IMappedFeature mapFeature = CreateMappedFeature(null); esriGeometryType esriGeometryType = mapFeature.EsriGeometryType; Dictionary <string, esriFieldType> fields = mapFeature.Fields; if ((esriGeometryType != esriGeometryType.esriGeometryNull) && (fields.Count >= 1)) { string shapeFieldName = mapFeature.ShapeFieldName; ISpatialReference spatialReference = ArcUtils.SpatialReference; fieldsEdit = CreateGeometryField(fieldsEdit, shapeFieldName, spatialReference, esriGeometryType); fieldsEdit = fields.Aggregate(fieldsEdit, (current, field) => CreateField(current, field.Key, field.Value)); foreach (var fieldName in FieldNames) { if ((!string.IsNullOrEmpty(fieldName)) && (!fields.Aggregate(false, (current, field) => (field.Key == fieldName) || current))) { CreateField(fieldsEdit, fieldName, esriFieldType.esriFieldTypeString); } } IFeatureWorkspace featureWorkspace = _cycloMediaGroupLayer.FeatureWorkspace; result = featureWorkspace.CreateFeatureClass(FcName, fieldsEdit, null, null, esriFeatureType.esriFTSimple, shapeFieldName, string.Empty); } return(result); }
private IFeatureClass CreateShapeFile(string shapeFullname, string proj) { FileInfo finfo = new FileInfo(shapeFullname); string parFolder = System.IO.Path.GetDirectoryName(shapeFullname); if (!Directory.Exists(parFolder)) { Directory.CreateDirectory(parFolder); } IWorkspaceFactory shpwf = new ShapefileWorkspaceFactory(); IFeatureWorkspace featureWs = shpwf.OpenFromFile(parFolder, 0) as IFeatureWorkspace; 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 pGeomDef = new GeometryDefClass(); IGeometryDefEdit pGeomDefEdit = pGeomDef as IGeometryDefEdit; pGeomDefEdit.GeometryType_2 = ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint; //几何类型 pGeomDefEdit.HasZ_2 = true; //是否有Z值 pGeomDefEdit.SpatialReference_2 = CreateSpatialReference(proj); //设置空间参考 pFieldEdit.GeometryDef_2 = pGeomDef; pFieldsEdit.AddField(pField); return(featureWs.CreateFeatureClass(finfo.Name, pFields, null, null, esriFeatureType.esriFTSimple, "SHAPE", null)); }
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", ""); }
/* * Called each time the user changes a parameter in the tool dialog or Command Line. * This updates the output data of the tool, which extremely useful for building models. * After returning from UpdateParameters(), geoprocessing calls its internal validation routine checkng that a given set of parameter values * are of the appropriate number, DataType, and value. * This method will update the output parameter value with the unique field. */ public void UpdateParameters(IArray paramvalues, IGPEnvironmentManager pEnvMgr) { m_Parameters = paramvalues; // Retrieve the input parameter value IGPValue parameterValue = m_GPUtilities.UnpackGPValue(m_Parameters.get_Element(0)); // Retrieve the unique field parameter value IGPParameter3 fieldNameParameter = (IGPParameter3)paramvalues.get_Element(3); // Get the output feature class schema and empty the additional fields. This will ensure // you don't get dublicate entries. IGPParameter3 outputFeatures = (IGPParameter3)paramvalues.get_Element(2); IGPFeatureSchema schema = (IGPFeatureSchema)outputFeatures.Schema; schema.AdditionalFields = null; // If we have an unique field value, create a new field based on the unique field name the user entered. if (fieldNameParameter.Value.IsEmpty() == false) { string fieldName = fieldNameParameter.Value.GetAsText(); IField uniqueField = m_GPUtilities.FindField(parameterValue, fieldName); IFieldsEdit fieldsEdit = new FieldsClass(); fieldsEdit.AddField(uniqueField); IFields fields = fieldsEdit as IFields; schema.AdditionalFields = fields; } }
private void createFeatureClass() { if (outftr == null) { //mfldIndex = new int[bCnt]; segIndex = 0; IFields flds = new FieldsClass(); IFieldsEdit fldsE = (IFieldsEdit)flds; IField fld = new FieldClass(); IFieldEdit fldE = (IFieldEdit)fld; fldE.Name_2 = "SegID"; fldE.Type_2 = esriFieldType.esriFieldTypeInteger; fldsE.AddField(fldE); //IField fld2 = new FieldClass(); //IFieldEdit fldE2 = (IFieldEdit)fld2; //fldE2.Name_2 = "Cluster"; //fldE2.Type_2 = esriFieldType.esriFieldTypeInteger; //fldsE.AddField(fldE2); //for (int i = 0; i < bCnt; i++) //{ // IField mFld = new FieldClass(); // IFieldEdit mFldE = (IFieldEdit)mFld; // mFldE.Name_2 = "Band" + i.ToString(); // mFldE.Type_2 = esriFieldType.esriFieldTypeDouble; // fldsE.AddField(mFld); //} outftr = geoUtil.createFeatureClass((IWorkspace2)OutWorkSpace, OutName, flds, esriGeometryType.esriGeometryPolygon, ((IRasterProps)InputRaster).SpatialReference); segIndex = outftr.FindField("SegID"); //clustIndex = outftr.FindField("Cluster"); //for (int i = 0; i < bCnt; i++) //{ // mfldIndex[i] = outftr.FindField("Band" + i.ToString()); //} } }
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); } }
public static ITable SaveToWorkspace(this ICursor icursor, IFeatureWorkspace workspace, string layername) { UID uid = new UID(); uid.Value = "esriGeoDatabase.Object"; IFields newFields = new FieldsClass(); newFields = icursor.Fields; ITable newtable = workspace.CreateTable(layername, newFields, uid, null, ""); ICursor cursor = newtable.Insert(true); IRowBuffer prowbuffer = newtable.CreateRowBuffer(); IRow irow = null; while (null != (irow = icursor.NextRow())) { for (int i = 0; i < newtable.Fields.FieldCount; i++) { string name = irow.Value[i].ToString(); bool editfield = newtable.Fields.get_Field(i).Editable; if (editfield) { prowbuffer.Value[i] = irow.Value[i]; } } cursor.InsertRow(prowbuffer); } cursor.Flush(); return(newtable); }
public static void Execute(IFeatureClass featureClass, String indexName, String nameOfField) { // Ensure the feature class contains the specified field. int fieldIndex = featureClass.FindField(nameOfField); if (fieldIndex == -1) { throw new ArgumentException("The specified field does not exist in the feature class."); } // Get the specified field from the feature class. IFields featureClassFields = featureClass.Fields; IField field = featureClassFields.get_Field(fieldIndex); // Create a fields collection and add the specified field to it. IFields fields = new FieldsClass(); IFieldsEdit fieldsEdit = (IFieldsEdit)fields; fieldsEdit.FieldCount_2 = 1; fieldsEdit.set_Field(0, field); // Create an index and cast to the IIndexEdit interface. IIndex index = new IndexClass(); IIndexEdit indexEdit = (IIndexEdit)index; // Set the index's properties, including the associated fields. indexEdit.Fields_2 = fields; indexEdit.IsAscending_2 = false; indexEdit.IsUnique_2 = false; indexEdit.Name_2 = indexName; // Add the index to the feature class. featureClass.AddIndex(index); }
/// <summary> /// 新建线图层 /// </summary> public void CreatePolylineLayer() { 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.esriGeometryPolyline; 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 void MultipartTest() { IFeatureWorkspace workspace = TestWorkspaceUtils.CreateInMemoryWorkspace("MultipartTest"); IFieldsEdit fields = new FieldsClass(); fields.AddField(FieldUtils.CreateOIDField()); fields.AddField(FieldUtils.CreateShapeField( "Shape", esriGeometryType.esriGeometryPolyline, CreateLV95(), 1000)); IFeatureClass linesFc = DatasetUtils.CreateSimpleFeatureClass(workspace, "Flow", fields); AddFeature( linesFc, CurveConstruction.StartLine(0, 0).LineTo(4, 0).MoveTo(6, 0).LineTo(6, 10).Curve); AddFeature(linesFc, CurveConstruction.StartLine(4, 0).LineTo(6, 0).Curve); AddFeature(linesFc, CurveConstruction.StartLine(0, 20).LineTo(4, 20).Curve); AddFeature( linesFc, CurveConstruction.StartLine(0, 30).LineTo(4, 30).MoveTo(0, 32).LineTo(4, 30) .MoveTo(4, 30).LineTo(8, 30).Curve); // expect counter-clockwise: 0 errors var runner = new QaContainerTestRunner( 1000, new QaFlowLogic(linesFc)); Assert.AreEqual(3, runner.Execute()); }
/// <summary> /// Creates fields for BaseStation table. /// </summary> /// <param name="GDBWorkspace"></param> /// <returns></returns> public static IFields createBaseStationFields(IWorkspace GDBWorkspace) { try { IFields pFields = new FieldsClass(); IFieldsEdit pFieldsEdit = (IFieldsEdit)pFields; pFieldsEdit.AddField(createField("OID", esriFieldType.esriFieldTypeOID)); //pFieldsEdit.AddField(createIntegerField("VesselID")); pFieldsEdit.AddField(createField("MMSI", esriFieldType.esriFieldTypeInteger)); pFieldsEdit.AddField(createField("Longitude", esriFieldType.esriFieldTypeDouble)); pFieldsEdit.AddField(createField("Latitude", esriFieldType.esriFieldTypeDouble)); // Use IFieldChecker to create a validated fields collection. IFieldChecker fieldChecker = new FieldCheckerClass(); IEnumFieldError enumFieldError = null; IFields validatedFields = null; fieldChecker.ValidateWorkspace = (IWorkspace)GDBWorkspace; fieldChecker.Validate(pFields, out enumFieldError, out validatedFields); return(pFields); } catch (Exception ex) { MessageBox.Show(ex.Message + "\n\n" + ex.StackTrace, "AIS_Menu:Utilities:createVesselFields"); return(null); } }
/// <summary> /// Creates fields for Voyage table. /// </summary> /// <param name="GDBWorkspace"></param> /// <returns></returns> public static IFields createVoyageFields(IWorkspace GDBWorkspace) { try { IFields pFields = new FieldsClass(); IFieldsEdit pFieldsEdit = (IFieldsEdit)pFields; pFieldsEdit.AddField(createField("OID", esriFieldType.esriFieldTypeOID)); pFieldsEdit.AddField(createField("VoyageID", esriFieldType.esriFieldTypeInteger)); pFieldsEdit.AddField(createField("Destination", esriFieldType.esriFieldTypeString)); pFieldsEdit.AddField(createField("Cargo", esriFieldType.esriFieldTypeInteger)); pFieldsEdit.AddField(createField("Draught", esriFieldType.esriFieldTypeInteger)); pFieldsEdit.AddField(createField("ETA", esriFieldType.esriFieldTypeDate)); pFieldsEdit.AddField(createField("StartTime", esriFieldType.esriFieldTypeDate)); pFieldsEdit.AddField(createField("EndTime", esriFieldType.esriFieldTypeDate)); pFieldsEdit.AddField(createField("MMSI", esriFieldType.esriFieldTypeInteger)); // Use IFieldChecker to create a validated fields collection. IFieldChecker fieldChecker = new FieldCheckerClass(); IEnumFieldError enumFieldError = null; IFields validatedFields = null; fieldChecker.ValidateWorkspace = (IWorkspace)GDBWorkspace; fieldChecker.Validate(pFields, out enumFieldError, out validatedFields); return(pFields); } catch (Exception ex) { MessageBox.Show(ex.Message + "\n\n" + ex.StackTrace, "AIS_Menu:Utilities:createVoyageFields"); return(null); } }
/// <summary> /// Join Urban-rural Devide Table /// </summary> /// <param name="layer"></param> /// <param name="year"></param> /// <author>Shen Yongyuan</author> /// <date>20091111</date> public static void JoinDevideTable(ILayer layer, int year, string city) { //Add Fields IFieldsEdit allFields = new FieldsClass(); IFieldEdit field1 = new FieldClass(); field1.Name_2 = "ID"; field1.Type_2 = esriFieldType.esriFieldTypeString; allFields.AddField(field1); IFieldEdit field2 = new FieldClass(); field2.Name_2 = Town.Constant.Constant.TmpFieldName; field2.Type_2 = esriFieldType.esriFieldTypeString; allFields.AddField(field2); //Create Table IWorkspaceFactory workspaceFactory = new InMemoryWorkspaceFactoryClass(); IWorkspaceName workspaceName = workspaceFactory.Create("", "Temporary WorkSpace In Memory", null, 0); IFeatureWorkspace workspace = ((IName)workspaceName).Open() as IFeatureWorkspace; ITable table = workspace.CreateTable(Town.Constant.Constant.TmpTableDevide, allFields, null, null, ""); //Import Data IWorkspaceEdit workspaceEdit = workspace as IWorkspaceEdit; workspaceEdit.StartEditing(false); workspaceEdit.StartEditOperation(); ICursor cursor = table.Insert(true); int fldField1 = cursor.Fields.FindField("ID"); int fldField2 = cursor.Fields.FindField(Town.Constant.Constant.TmpFieldName); //Query and Import Dictionary <int, string> devideResult = DataLib.DA_Devide.GetDevideResult(year, city); foreach (KeyValuePair <int, string> d in devideResult) { IRowBuffer buffer = table.CreateRowBuffer(); buffer.set_Value(fldField1, d.Key.ToString()); buffer.set_Value(fldField2, d.Value); cursor.InsertRow(buffer); } cursor.Flush(); workspaceEdit.StartEditOperation(); workspaceEdit.StopEditing(true); System.Runtime.InteropServices.Marshal.ReleaseComObject(cursor); //Join IMemoryRelationshipClassFactory pMemRelFact = new MemoryRelationshipClassFactory(); IFeatureLayer feaLayer = layer as IFeatureLayer; ITable originTable = feaLayer.FeatureClass as ITable; IRelationshipClass pRelClass = pMemRelFact.Open("Join", originTable as IObjectClass, "ID", table as IObjectClass, "ID", "forward", "backward", esriRelCardinality.esriRelCardinalityOneToOne); IDisplayRelationshipClass pDispRC = feaLayer as IDisplayRelationshipClass; pDispRC.DisplayRelationshipClass(null, esriJoinType.esriLeftOuterJoin); pDispRC.DisplayRelationshipClass(pRelClass, esriJoinType.esriLeftOuterJoin); }
/// <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 IFeatureClass CreatePointFile(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.esriGeometryPoint; 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); 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; }
public static IFields CreateFields(TableInfo tInfo) { if (tInfo == null || tInfo.FieldsInfo == null || tInfo.FieldsInfo.Count == 0) return null; IFieldsEdit fields=new FieldsClass(); foreach (FieldInfo fInfo in tInfo.FieldsInfo) { fields.AddField(CreateField(fInfo)); } return fields; }
public static IFields CreateFields(FeatureClassInfo fcInfo) { if (fcInfo == null ) return null; IFieldsEdit fields = CreateFields(fcInfo as TableInfo) as IFieldsEdit; if (fields == null) fields = new FieldsClass(); fields.AddField(CreateShapeField(fcInfo)); return fields; }
public static IFeatureClass CreateFeatureClass(fulcrumform form, string pathToGeoDB) { try { ISpatialReferenceFactory spatialReferenceFactory = new SpatialReferenceEnvironmentClass(); IGeographicCoordinateSystem geographicCoordinateSystem = spatialReferenceFactory.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_WGS1984); ISpatialReference sr = geographicCoordinateSystem; IWorkspaceFactory2 wsf = new FileGDBWorkspaceFactoryClass(); IWorkspace2 workspace = (IWorkspace2)wsf.OpenFromFile(pathToGeoDB, 0); IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspace; string fcName = GetUniqueFeatureClassName(form.name, workspace); IFields fieldsCollection = new FieldsClass(); IFieldEdit newField = fieldsCollection as IFieldEdit; IFeatureClassDescription fcDesc = new ESRI.ArcGIS.Geodatabase.FeatureClassDescriptionClass(); IObjectClassDescription ocDesc = (IObjectClassDescription)fcDesc; // create required fields using the required fields method IFields fields = ocDesc.RequiredFields; //Grab the GeometryDef from the shape field, edit it and give it back int shapeFieldIndex = fields.FindField(fcDesc.ShapeFieldName); IField shapeField = fields.get_Field(shapeFieldIndex); IGeometryDef geomDef = shapeField.GeometryDef; IGeometryDefEdit geomDefEdit = (IGeometryDefEdit)geomDef; geomDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint; geomDefEdit.GridCount_2 = 1; geomDefEdit.set_GridSize(0, 0); geomDefEdit.SpatialReference_2 = sr; IFields newFields = CreateNewFields(form, fields, sr); IFeatureClass featureClass = featureWorkspace.CreateFeatureClass (fcName, newFields, ocDesc.InstanceCLSID, ocDesc.ClassExtensionCLSID, esriFeatureType.esriFTSimple, fcDesc.ShapeFieldName, ""); return featureClass; } catch (Exception e) { string errMsg = e.Message; return null; } }
/// <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 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; }
/// <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(); }
/// <summary> /// 储存凸包shp,聚类号在"index"字段中标注 /// </summary> public void CreatePolygonShapefile() { string filePath = m_dataInfo.GetOutputFilePath(); string fileName = m_dataInfo.GetOutputFileName(); ISpatialReference spatialReference = m_dataInfo.GetSpatialReference(); //打开工作空间 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.esriGeometryPolygon; 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;//数值精度 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); IPolygon currentPolygon = currentCluster.GetConvexHull(); //we know that there are IPoints only in the Geometrycollection. //But this is the safe and recommended way if (currentPolygon != null) { featureBuffer.set_Value(typeFieldIndex, currentCluster.GetClusterIndex()); featureBuffer.Shape = currentPolygon 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_polygonLayer = featureLayer; /* //提前设置渲染 DefinePolygonUniqueValueRenderer(featureLayer as IGeoFeatureLayer, "ClusterIndex"); m_mapControl.AddLayer(featureLayer as ILayer); this.m_mapControl.Refresh(); */ }
public void UpdateParameters(ESRI.ArcGIS.esriSystem.IArray paramvalues, ESRI.ArcGIS.Geoprocessing.IGPEnvironmentManager pEnvMgr) { IGPUtilities3 gpUtilities3 = new GPUtilitiesClass(); IGPParameter targetDatasetParameter = paramvalues.get_Element(out_targetDatasetNumber) as IGPParameter; IGPValue targetDatasetGPValue = gpUtilities3.UnpackGPValue(targetDatasetParameter); IDEFeatureDataset targetDEFeatureDataset = targetDatasetGPValue as IDEFeatureDataset; IGPParameter3 outPointsFeatureClassParameter = null; IGPValue outPointsFeatureClass = null; string outpointsPath = String.Empty; IGPParameter3 outLinesFeatureClassParameter = null; IGPValue outLinesFeatureClass = null; string outlinesPath = String.Empty; IGPParameter3 outPolygonFeatureClassParameter = null; IGPValue outPolygonFeatureClass = null; string outpolygonsPath = String.Empty; if (((IGPValue)targetDEFeatureDataset).GetAsText().Length != 0) { IDataElement dataElement = targetDEFeatureDataset as IDataElement; try { gpUtilities3.QualifyOutputDataElement(gpUtilities3.UnpackGPValue(targetDatasetParameter)); } catch { return; } string nameOfPointFeatureClass = dataElement.GetBaseName() + "_osm_pt"; string nameOfLineFeatureClass = dataElement.GetBaseName() + "_osm_ln"; string nameOfPolygonFeatureClass = dataElement.GetBaseName() + "_osm_ply"; try { outpointsPath = dataElement.CatalogPath + System.IO.Path.DirectorySeparatorChar + nameOfPointFeatureClass; } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); } outPointsFeatureClassParameter = paramvalues.get_Element(out_osmPointsNumber) as IGPParameter3; outPointsFeatureClass = gpUtilities3.UnpackGPValue(outPointsFeatureClassParameter); outlinesPath = dataElement.CatalogPath + System.IO.Path.DirectorySeparatorChar + nameOfLineFeatureClass; outLinesFeatureClassParameter = paramvalues.get_Element(out_osmLinesNumber) as IGPParameter3; outLinesFeatureClass = gpUtilities3.UnpackGPValue(outLinesFeatureClassParameter); outpolygonsPath = dataElement.CatalogPath + System.IO.Path.DirectorySeparatorChar + nameOfPolygonFeatureClass; outPolygonFeatureClassParameter = paramvalues.get_Element(out_osmPolygonsNumber) as IGPParameter3; outPolygonFeatureClass = gpUtilities3.UnpackGPValue(outPolygonFeatureClassParameter); } // TE - 10/20/2014 if (((IGPParameter)paramvalues.get_Element(in_attributeSelector)).Altered) { IGPParameter tagCollectionParameter = paramvalues.get_Element(in_attributeSelector) as IGPParameter; IGPMultiValue tagCollectionGPValue = gpUtilities3.UnpackGPValue(tagCollectionParameter) as IGPMultiValue; IGPCodedValueDomain codedTagDomain = tagCollectionParameter.Domain as IGPCodedValueDomain; for (int attributeValueIndex = 0; attributeValueIndex < tagCollectionGPValue.Count; attributeValueIndex++) { string valueString = tagCollectionGPValue.get_Value(attributeValueIndex).GetAsText(); IGPValue testFieldValue = codedTagDomain.FindValue(valueString); if (testFieldValue == null) { codedTagDomain.AddStringCode(valueString, valueString); } } string illegalCharacters = "`~@#$%^&()-+=,{}.![];"; IFieldsEdit fieldsEdit = new FieldsClass(); for (int valueIndex = 0; valueIndex < tagCollectionGPValue.Count; valueIndex++) { string tagString = tagCollectionGPValue.get_Value(valueIndex).GetAsText(); if (tagString != "ALL") { // Check if the input field already exists. string cleanedTagKey = OSMToolHelper.convert2AttributeFieldName(tagString, illegalCharacters); IFieldEdit fieldEdit = new FieldClass(); fieldEdit.Name_2 = cleanedTagKey; fieldEdit.AliasName_2 = tagCollectionGPValue.get_Value(valueIndex).GetAsText(); fieldEdit.Type_2 = esriFieldType.esriFieldTypeString; fieldEdit.Length_2 = 100; fieldsEdit.AddField(fieldEdit); } } IFields fields = fieldsEdit as IFields; // Get the derived output feature class schema and empty the additional fields. This ensures // that you don't get dublicate entries. if (outPointsFeatureClassParameter != null) { IGPFeatureSchema polySchema = outPolygonFeatureClassParameter.Schema as IGPFeatureSchema; if (polySchema != null) { // Add the additional field to the polygon output. polySchema.AdditionalFields = fields; } else { polySchema = new GPFeatureSchemaClass(); polySchema.AdditionalFields = fields; polySchema.GeometryType = esriGeometryType.esriGeometryPolygon; polySchema.FeatureType = esriFeatureType.esriFTSimple; } } if (outLinesFeatureClassParameter != null) { IGPFeatureSchema lineSchema = outLinesFeatureClassParameter.Schema as IGPFeatureSchema; if (lineSchema != null) { // Add the additional field to the line output. lineSchema.AdditionalFields = fields; } else { lineSchema = new GPFeatureSchemaClass(); lineSchema.AdditionalFields = fields; lineSchema.GeometryType = esriGeometryType.esriGeometryPolyline; lineSchema.FeatureType = esriFeatureType.esriFTSimple; } } if (outPointsFeatureClassParameter != null) { IGPFeatureSchema pointSchema = outPointsFeatureClassParameter.Schema as IGPFeatureSchema; if (pointSchema != null) { // Add the additional field to the point output. pointSchema.AdditionalFields = fields; } else { pointSchema = new GPFeatureSchemaClass(); pointSchema.AdditionalFields = fields; pointSchema.GeometryType = esriGeometryType.esriGeometryPoint; pointSchema.FeatureType = esriFeatureType.esriFTSimple; } } } if (outPointsFeatureClassParameter != null) { outPointsFeatureClass.SetAsText(outpointsPath); gpUtilities3.PackGPValue(outPointsFeatureClass, outPointsFeatureClassParameter); } if (outLinesFeatureClassParameter != null) { outLinesFeatureClass.SetAsText(outlinesPath); gpUtilities3.PackGPValue(outLinesFeatureClass, outLinesFeatureClassParameter); } if (outPolygonFeatureClassParameter != null) { outPolygonFeatureClass.SetAsText(outpolygonsPath); gpUtilities3.PackGPValue(outPolygonFeatureClass, outPolygonFeatureClassParameter); } gpUtilities3.ReleaseInternals(); if (gpUtilities3 != null) ComReleaser.ReleaseCOMObject(gpUtilities3); }
private static IFields createLineLabelFields() { // create fields IFields pFields; IFieldsEdit pFieldsEdit; IField pField; IFieldEdit pFieldEdit; pFields = new FieldsClass(); pFieldsEdit = (IFieldsEdit)pFields; pFieldsEdit.FieldCount_2 = 4; pField = new FieldClass(); pFieldEdit = (IFieldEdit)pField; pFieldEdit.Name_2 = "FACILITYID"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString; pFieldEdit.Precision_2 = 20; pFieldEdit.Scale_2 = 8; pFieldEdit.AliasName_2 = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsAlias_11"); pFieldEdit.IsNullable_2 = true; pFieldsEdit.set_Field(0, pField); pField = new FieldClass(); pFieldEdit = (IFieldEdit)pField; pFieldEdit.Name_2 = "MEASURE"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble; pFieldEdit.Precision_2 = 20; pFieldEdit.Scale_2 = 8; pFieldEdit.AliasName_2 = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsAlias_12"); pFieldEdit.IsNullable_2 = false; pFieldsEdit.set_Field(1, pField); pField = new FieldClass(); pFieldEdit = (IFieldEdit)pField; pFieldEdit.Name_2 = "ELEVATION"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble; pFieldEdit.Precision_2 = 20; pFieldEdit.Scale_2 = 8; pFieldEdit.AliasName_2 = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsAlias_13"); pFieldEdit.IsNullable_2 = false; pFieldsEdit.set_Field(2, pField); pField = new FieldClass(); pFieldEdit = (IFieldEdit)pField; pFieldEdit.Name_2 = "LABEL"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString; pFieldEdit.Length_2 = 150; pFieldEdit.AliasName_2 = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsAlias_14"); pFieldEdit.IsNullable_2 = true; pFieldsEdit.set_Field(3, pField); return pFields; }
public static void CreateFClassInPDB(string filePath) { string shapeFieldName = "shape"; IFeatureWorkspace pFWS; try { pFWS = openPDB(filePath); //IEnumDataset pDatasets; //pDatasets=pWS.get_Datasets(esriDatasetType.esriDTFeatureDataset); IFeatureDataset pFeatureDataset = pFWS.OpenFeatureDataset("Water"); //MessageBox.Show(pFeatureDataset.Name); // IDataset pDataset=pDatasets.Next(); // IFeatureDataset pFeatureDataset=pDataset as IFeatureDataset; IGeoDataset pGeoDataset = pFeatureDataset as IGeoDataset; IFields pFields = null; IFieldsEdit pFieldsEdit = null; pFields = new FieldsClass(); pFieldsEdit = pFields as IFieldsEdit; pFieldsEdit.FieldCount_2 = 2; IField pField = null; IFieldEdit pFieldEdit = null; //Make the shape field it will need a geometry definition, with a spatial reference pField = new FieldClass(); pFieldEdit = pField as IFieldEdit; pFieldEdit.Name_2 = shapeFieldName; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; IGeometryDef pGeomDef = null; IGeometryDefEdit pGeomDefEdit = null; pGeomDef = new GeometryDefClass(); pGeomDefEdit = pGeomDef as IGeometryDefEdit; pGeomDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolygon; pGeomDefEdit.SpatialReference_2 = pGeoDataset.SpatialReference;//get the spatial reference pFieldEdit.GeometryDef_2 = pGeomDefEdit; //pFieldsEdit.AddField(pField); pFieldsEdit.set_Field(0, pField); //Add another miscellaneous text field pField = new FieldClass(); pFieldEdit = pField as IFieldEdit; pFieldEdit.Length_2 = 30; pFieldEdit.Name_2 = "TextField"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString; //pFieldsEdit.AddField(pField); pFieldsEdit.set_Field(1, pField); //pFeatureDataset.CreateFeatureClass("test",pFields,null,null,esriFeatureType.esriFTSimple,"Shape",""); UID pUID; pUID = new UIDClass(); pUID.Value = "esriGeoDatabase.Feature"; //pFeatureDataset.CreateFeatureClass("test",pFields,pUID,null,esriFeatureType.esriFTSimple,"Shape",""); pFeatureDataset.CreateFeatureClass("test", pFields, pUID, null, esriFeatureType.esriFTSimple, "Shape", ""); MessageBox.Show("�����ɹ�"); } catch (Exception e) { MessageBox.Show(e.Message); } }
//����DBASE�ļ� public static ITable CreateDBFTable(string strName, string strFolder, bool overwrite, IFields pFields) { IFeatureWorkspace pFWS = null; IWorkspaceFactory pWSF = new ShapefileWorkspaceFactoryClass(); DirectoryInfo dir = new DirectoryInfo(strFolder); FileInfo fileInfo = new FileInfo(strFolder + @"\" + strName + ".dbf"); if (dir.Exists) pFWS = pWSF.OpenFromFile(strFolder, 0) as IFeatureWorkspace; if (fileInfo.Exists) { if (overwrite) fileInfo.Delete(); else { fileInfo.Delete(); } } IFieldsEdit pFieldsEdit; //����ֶμ��ϲ��ܴ��ڣ����� if (pFields == null) { pFields = new FieldsClass(); pFieldsEdit = pFields as IFieldsEdit; pFieldsEdit.FieldCount_2 = 1; IField pField = new FieldClass(); IFieldEdit pFieldEdit = pField as IFieldEdit; pFieldEdit.Length_2 = 30; pFieldEdit.Name_2 = "TextField"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString; pFieldEdit.AliasName_2 = "hi"; pFieldsEdit.set_Field(0, pField); } ITable pTable = pFWS.CreateTable(strName, pFields, null, null, ""); return pTable; }
//�����յ�����ͼ���Ʊ�� public static ITable ProfileCreateTable() { //�����ֶ� IFields pFields = new FieldsClass(); IFieldsEdit pFieldsEdit = pFields as IFieldsEdit; pFieldsEdit.FieldCount_2 = 5; IField pField = new FieldClass(); IFieldEdit pFieldEdit = pField as IFieldEdit; pFieldEdit.Name_2 = "X"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble; pFieldEdit.Precision_2 = 20; pFieldEdit.Scale_2 = 8; pFieldsEdit.set_Field(0, pField); pField = new FieldClass(); pFieldEdit = pField as IFieldEdit; pFieldEdit.Name_2 = "Y"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble; pFieldEdit.Precision_2 = 20; pFieldEdit.Scale_2 = 8; pFieldsEdit.set_Field(1, pField); pField = new FieldClass(); pFieldEdit = pField as IFieldEdit; pFieldEdit.Name_2 = "Z"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble; pFieldEdit.Precision_2 = 20; pFieldEdit.Scale_2 = 8; pFieldEdit.AliasName_2 = "Ground Elevation"; pFieldsEdit.set_Field(2, pField); pField = new FieldClass(); pFieldEdit = pField as IFieldEdit; pFieldEdit.Name_2 = "M"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeSingle; pFieldEdit.Precision_2 = 10; pFieldEdit.Scale_2 = 1; pFieldsEdit.set_Field(3, pField); pField = new FieldClass(); pFieldEdit = pField as IFieldEdit; pFieldEdit.Name_2 = "SewerElev"; pFieldEdit.IsNullable_2 = true; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble; pFieldEdit.Precision_2 = 20; pFieldEdit.Scale_2 = 8; pFieldEdit.AliasName_2 = "Sewer Line Elevation"; pFieldsEdit.set_Field(4, pField); ITable pTable = CreateDBFTable("xxprofiletable", "c:\\temp", true, pFields); return pTable; }
private static IFields createSurfaceFields() { // create fields IFields pFields; IFieldsEdit pFieldsEdit; IField pField; IFieldEdit pFieldEdit; pFields = new FieldsClass(); pFieldsEdit = (IFieldsEdit)pFields; pFieldsEdit.FieldCount_2 = 2; pField = new FieldClass(); pFieldEdit = (IFieldEdit)pField; pFieldEdit.Name_2 = "X"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble; pFieldEdit.Precision_2 = 20; pFieldEdit.Scale_2 = 8; pFieldEdit.IsNullable_2 = false; pFieldsEdit.set_Field(0, pField); pField = new FieldClass(); pFieldEdit = (IFieldEdit)pField; pFieldEdit.Name_2 = "Y"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble; pFieldEdit.Precision_2 = 20; pFieldEdit.Scale_2 = 8; pFieldEdit.AliasName_2 = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsAlias_1"); pFieldEdit.IsNullable_2 = false; pFieldsEdit.set_Field(1, pField); return pFields; }
private static IFields createProfilePointFields() { // create fields IFields pFields; IFieldsEdit pFieldsEdit; IField pField; IFieldEdit pFieldEdit; pFields = new FieldsClass(); pFieldsEdit = (IFieldsEdit)pFields; pFieldsEdit.FieldCount_2 = 4; pField = new FieldClass(); pFieldEdit = (IFieldEdit)pField; pFieldEdit.Name_2 = "X"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble; pFieldEdit.Precision_2 = 20; pFieldEdit.Scale_2 = 8; pFieldEdit.IsNullable_2 = false; pFieldsEdit.set_Field(0, pField); pField = new FieldClass(); pFieldEdit = (IFieldEdit)pField; pFieldEdit.Name_2 = "TOPELEV"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble; pFieldEdit.Precision_2 = 20; pFieldEdit.Scale_2 = 8; pFieldEdit.AliasName_2 = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsAlias_6"); pFieldEdit.IsNullable_2 = false; pFieldsEdit.set_Field(1, pField); //pField = new FieldClass(); //pFieldEdit = (IFieldEdit)pField; //pFieldEdit.Name_2 = "INVERT"; //pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble; //pFieldEdit.Precision_2 = 20; //pFieldEdit.Scale_2 = 10; //pFieldEdit.AliasName_2 = "Invert"; //pFieldEdit.IsNullable_2 = false; //pFieldsEdit.set_Field(2, pField); pField = new FieldClass(); pFieldEdit = (IFieldEdit)pField; pFieldEdit.Name_2 = "BOTELEV"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble; pFieldEdit.Precision_2 = 20; pFieldEdit.AliasName_2 = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsAlias_7"); pFieldEdit.Scale_2 = 1; pFieldEdit.IsNullable_2 = false; pFieldsEdit.set_Field(2, pField); pField = new FieldClass(); pFieldEdit = (IFieldEdit)pField; pFieldEdit.Name_2 = "ID"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString; pFieldEdit.AliasName_2 = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsAlias_8"); pFieldEdit.IsNullable_2 = false; pFieldsEdit.set_Field(3, pField); return pFields; }
private static IFields createProfileFields() { // create fields IFields pFields; IFieldsEdit pFieldsEdit; IField pField; IFieldEdit pFieldEdit; pFields = new FieldsClass(); pFieldsEdit = (IFieldsEdit)pFields; pFieldsEdit.FieldCount_2 = 8; pField = new FieldClass(); pFieldEdit = (IFieldEdit)pField; pFieldEdit.Name_2 = "X"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble; pFieldEdit.Precision_2 = 20; pFieldEdit.Scale_2 = 8; pFieldEdit.IsNullable_2 = false; pFieldsEdit.set_Field(0, pField); pField = new FieldClass(); pFieldEdit = (IFieldEdit)pField; pFieldEdit.Name_2 = "Y"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble; pFieldEdit.Precision_2 = 20; pFieldEdit.Scale_2 = 8; pFieldEdit.IsNullable_2 = false; pFieldsEdit.set_Field(1, pField); pField = new FieldClass(); pFieldEdit = (IFieldEdit)pField; pFieldEdit.Name_2 = "Z"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble; pFieldEdit.Precision_2 = 20; pFieldEdit.Scale_2 = 8; pFieldEdit.AliasName_2 = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsAlias_1"); pFieldEdit.IsNullable_2 = false; pFieldsEdit.set_Field(2, pField); pField = new FieldClass(); pFieldEdit = (IFieldEdit)pField; pFieldEdit.Name_2 = "M"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble; pFieldEdit.Precision_2 = 10; pFieldEdit.Scale_2 = 1; pFieldEdit.IsNullable_2 = false; pFieldsEdit.set_Field(3, pField); pField = new FieldClass(); pFieldEdit = (IFieldEdit)pField; pFieldEdit.Name_2 = "DOWNELEV"; pFieldEdit.IsNullable_2 = true; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble; pFieldEdit.Precision_2 = 20; pFieldEdit.Scale_2 = 8; pFieldEdit.AliasName_2 = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsAlias_2"); pFieldEdit.IsNullable_2 = false; pFieldsEdit.set_Field(4, pField); pField = new FieldClass(); pFieldEdit = (IFieldEdit)pField; pFieldEdit.Name_2 = "UPELEV"; pFieldEdit.IsNullable_2 = true; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble; pFieldEdit.Precision_2 = 20; pFieldEdit.Scale_2 = 8; pFieldEdit.AliasName_2 = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsAlias_3"); pFieldEdit.IsNullable_2 = false; pFieldsEdit.set_Field(5, pField); pField = new FieldClass(); pFieldEdit = (IFieldEdit)pField; pFieldEdit.Name_2 = "TOPELEV"; pFieldEdit.IsNullable_2 = true; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble; pFieldEdit.Precision_2 = 20; pFieldEdit.Scale_2 = 8; pFieldEdit.AliasName_2 = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsAlias_4"); pFieldEdit.IsNullable_2 = false; pFieldsEdit.set_Field(6, pField); pField = new FieldClass(); pFieldEdit = (IFieldEdit)pField; pFieldEdit.Name_2 = "BOTELEV"; pFieldEdit.IsNullable_2 = true; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble; pFieldEdit.Precision_2 = 20; pFieldEdit.Scale_2 = 8; pFieldEdit.AliasName_2 = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsAlias_5"); pFieldEdit.IsNullable_2 = false; pFieldsEdit.set_Field(7, pField); return pFields; }
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 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 { } }
// // // public static void createShapeFile(String folderName, String shapeName) { if (folderName == "" || shapeName == "") return; string shapeFieldName = "shape"; try { IFeatureWorkspace pFWS = null; IWorkspaceFactory pWorkspaceFactory = null; pWorkspaceFactory = new ShapefileWorkspaceFactoryClass(); //if(pWorkspaceFactory.IsWorkspace(folderName)==false) return; pFWS = pWorkspaceFactory.OpenFromFile(folderName, 0) as IFeatureWorkspace; IFields pFields = null; IFieldsEdit pFieldsEdit = null; pFields = new FieldsClass(); pFieldsEdit = pFields as IFieldsEdit; IField pField = null; IFieldEdit pFieldEdit = null; //Make the shape field it will need a geometry definition, with a spatial reference pField = new FieldClass(); pFieldEdit = pField as IFieldEdit; pFieldEdit.Name_2 = shapeFieldName; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; IGeometryDef pGeomDef = null; IGeometryDefEdit pGeomDefEdit = null; pGeomDef = new GeometryDefClass(); pGeomDefEdit = pGeomDef as IGeometryDefEdit; pGeomDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolygon; pGeomDefEdit.SpatialReference_2 = new UnknownCoordinateSystemClass(); pFieldEdit.GeometryDef_2 = pGeomDefEdit; pFieldsEdit.AddField(pField); //Add another miscellaneous text field pField = new FieldClass(); pFieldEdit = pField as IFieldEdit; pFieldEdit.Length_2 = 30; pFieldEdit.Name_2 = "TextField"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString; pFieldsEdit.AddField(pField); IFeatureClass pFeatClass = null; pFeatClass = pFWS.CreateFeatureClass(shapeName, pFields, null, null, esriFeatureType.esriFTSimple, shapeFieldName, ""); MessageBox.Show("��Ϊ" + shapeName + "��shape�ļ������ɹ�"); } catch (Exception e) { MessageBox.Show(e.Message); } }
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; }
//--------------------------------------------------------------------- // CHANGED: CR13 (Export features performance) // Add a new attribute index to a feature class. /// <summary> /// Adds an attribute index to a field in a feature class. /// </summary> /// <param name="featureClass">The feature class.</param> /// <param name="indexName">Name of the index.</param> /// <param name="fieldName">Name of the field.</param> public void AddFieldIndex(IFeatureClass featureClass, String indexName, String fieldName) { // Ensure the feature class contains the specified field. int fieldIndex = featureClass.FindField(fieldName); if (fieldIndex == -1) return; // Get the specified field from the feature class. IFields featureClassFields = featureClass.Fields; IField field = featureClassFields.get_Field(fieldIndex); // Create a fields collection and add the specified field to it. IFields fields = new FieldsClass(); IFieldsEdit fieldsEdit = (IFieldsEdit)fields; fieldsEdit.FieldCount_2 = 1; fieldsEdit.set_Field(0, field); //Create an index and cast to the IIndexEdit interface. IIndex index = new IndexClass(); IIndexEdit indexEdit = (IIndexEdit)index; // Set the index's properties, including the associated fields. indexEdit.Fields_2 = fields; indexEdit.IsAscending_2 = false; indexEdit.IsUnique_2 = false; indexEdit.Name_2 = indexName; // Attempt to acquire an exclusive schema lock on the feature class. ISchemaLock schemaLock = (ISchemaLock)featureClass; try { schemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock); featureClass.AddIndex(index); } catch (COMException comExc) { // Handle this in a way appropriate to your application. Console.WriteLine("A COM Exception was thrown: {0}", comExc.Message); } finally { schemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock); } }
private static IFields createLine2Fields() { // create fields IFields pFields; IFieldsEdit pFieldsEdit; IField pField; IFieldEdit pFieldEdit; pFields = new FieldsClass(); pFieldsEdit = (IFieldsEdit)pFields; pFieldsEdit.FieldCount_2 = 5; pField = new FieldClass(); pFieldEdit = (IFieldEdit)pField; pFieldEdit.Name_2 = "FACILITYID"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString; pFieldEdit.Precision_2 = 20; pFieldEdit.Scale_2 = 8; pFieldEdit.AliasName_2 = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsAlias_11"); pFieldEdit.IsNullable_2 = true; pFieldsEdit.set_Field(0, pField); pField = new FieldClass(); pFieldEdit = (IFieldEdit)pField; pFieldEdit.Name_2 = "FROMM"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble; pFieldEdit.Precision_2 = 20; pFieldEdit.Scale_2 = 8; pFieldEdit.AliasName_2 = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsAlias_15"); pFieldEdit.IsNullable_2 = false; pFieldsEdit.set_Field(1, pField); pField = new FieldClass(); pFieldEdit = (IFieldEdit)pField; pFieldEdit.Name_2 = "TOM"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble; pFieldEdit.Precision_2 = 10; pFieldEdit.Scale_2 = 1; pFieldEdit.IsNullable_2 = false; pFieldEdit.AliasName_2 = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsAlias_16"); pFieldsEdit.set_Field(2, pField); pField = new FieldClass(); pFieldEdit = (IFieldEdit)pField; pFieldEdit.Name_2 = "FROMELEV"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble; pFieldEdit.Precision_2 = 20; pFieldEdit.Scale_2 = 8; pFieldEdit.AliasName_2 = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsAlias_17"); pFieldEdit.IsNullable_2 = false; pFieldsEdit.set_Field(3, pField); pField = new FieldClass(); pFieldEdit = (IFieldEdit)pField; pFieldEdit.Name_2 = "TOELEV"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble; pFieldEdit.Precision_2 = 20; pFieldEdit.Scale_2 = 8; pFieldEdit.AliasName_2 = A4LGSharedFunctions.Localizer.GetString("GeoNetToolsAlias_18"); pFieldEdit.IsNullable_2 = false; pFieldsEdit.set_Field(4, pField); return pFields; }
/// <summary> /// Creates the field collection for a new feature class or standalone table. /// </summary> /// <param name="bSpatial">True if the new fields collection is for a feature class, false if a table.</param> /// <param name="bHasZ">True if the geometry field of the fields collection to be created is to be Z aware.</param> /// <param name="bHasM">True if the geometry field of the fields collection to be created is to be M aware.</param> /// <param name="oWorkspace">The workspace in which a new feature class will be created from the fields collection.</param> /// <param name="geomType">The geometry type of the feature class to be created from the new fields collection.</param> /// <param name="aFldLengths">An array of field lengths of the fields collection to be created.</param> /// <param name="aFldNames">An array of field names of the fields collection to be created.</param> /// <param name="aFldAliases">An array of field aliases of the fields collection to be created.</param> /// <param name="aFldTypes">An array of field types of the fields collection to be created.</param> /// <param name="spatialReference">The coordinate system to be assigned to the new feature class.</param> /// <returns>Returns the new field collection. Throws an error if no field collection can be created.</returns> private IFields CreateFieldsCollection(bool hasGeom, bool hasZ, bool hasM, object outWorkspace, esriGeometryType geometryType, int[] fieldLengths, string[] fieldNames, string[] fieldAliases, esriFieldType[] fieldTypes, bool[] fieldNullable, ISpatialReference spatialReference) { if ((hasGeom) && (outWorkspace is IWorkspace) && (spatialReference == null)) throw new Exception("Spatial reference must be defined for standalone feature classes"); IWorkspace outputWorkspace = null; if (outWorkspace is IWorkspace) { outputWorkspace = outWorkspace as IWorkspace; } else if (outWorkspace is IFeatureDataset) { outputWorkspace = ((IDataset)((IFeatureDataset)outWorkspace)).Workspace; } IFields fields = new FieldsClass(); IFieldsEdit fieldsEdit = (IFieldsEdit)fields; if (hasGeom) { IGeometryDef geometryDef = new GeometryDefClass(); IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit)geometryDef; geometryDefEdit.GeometryType_2 = geometryType; geometryDefEdit.GridCount_2 = 1; geometryDefEdit.set_GridSize(0, 0); // (0, 0.5); geometryDefEdit.AvgNumPoints_2 = 2; geometryDefEdit.HasM_2 = hasM; geometryDefEdit.HasZ_2 = hasZ; if (outWorkspace is IWorkspace) geometryDefEdit.SpatialReference_2 = spatialReference; IField fieldOID = new FieldClass(); IFieldEdit fieldEditOID = (IFieldEdit)fieldOID; fieldEditOID.Name_2 = "OBJECTID"; fieldEditOID.AliasName_2 = "OBJECTID"; fieldEditOID.Type_2 = esriFieldType.esriFieldTypeOID; fieldsEdit.AddField(fieldOID); IField fieldShape = new FieldClass(); IFieldEdit fieldEditShape = (IFieldEdit)fieldShape; fieldEditShape.Name_2 = "SHAPE"; fieldEditShape.AliasName_2 = "SHAPE"; fieldEditShape.Type_2 = esriFieldType.esriFieldTypeGeometry; fieldEditShape.GeometryDef_2 = geometryDef; fieldsEdit.AddField(fieldShape); } else { IField fieldOID = new FieldClass(); IFieldEdit fieldEditOID = (IFieldEdit)fieldOID; fieldEditOID.Name_2 = "OBJECTID"; fieldEditOID.AliasName_2 = "OBJECTID"; fieldEditOID.Type_2 = esriFieldType.esriFieldTypeOID; fieldsEdit.AddField(fieldOID); } if (fieldAliases == null) fieldAliases = fieldNames; IField fieldAtt; IFieldEdit fieldEditAtt; for (int i = 0; i < fieldTypes.Length; i++) { if (String.IsNullOrEmpty(fieldNames[i])) throw new Exception(String.Format("Error creating field {0}", i)); if ((fieldTypes[i] == esriFieldType.esriFieldTypeOID) || (fieldTypes[i] == esriFieldType.esriFieldTypeGeometry)) continue; fieldAtt = new FieldClass(); fieldEditAtt = (IFieldEdit)fieldAtt; fieldEditAtt.Name_2 = fieldNames[i]; if (!String.IsNullOrEmpty(fieldAliases[i])) fieldEditAtt.AliasName_2 = fieldAliases[i]; fieldEditAtt.Type_2 = fieldTypes[i]; fieldEditAtt.Editable_2 = true; fieldEditAtt.Required_2 = false; fieldEditAtt.IsNullable_2 = fieldNullable[i];// true; if (fieldTypes[i] == esriFieldType.esriFieldTypeString) { //if ((fieldLengths[i] == -1) || ((fieldLengths[i] > 0) && (fieldLengths[i] < 256))) fieldEditAtt.Length_2 = fieldLengths[i]; //else // fieldEditAtt.Length_2 = 255; } fieldsEdit.AddField(fieldAtt); } IFieldChecker fldChk = new FieldCheckerClass(); fldChk.ValidateWorkspace = outputWorkspace; IFields outFields; IEnumFieldError fieldErrors; fldChk.Validate(fields, out fieldErrors, out outFields); return outFields; }