public static void AddDefaultField(IFieldsEdit ifieldsEdit_0, esriGeometryType esriGeometryType_0, ISpatialReference ispatialReference_0, bool bool_0, bool bool_1, int int_0) { IFieldEdit fieldClass = new ESRI.ArcGIS.Geodatabase.Field() as IFieldEdit; fieldClass.Name_2 = "OBJECTID"; fieldClass.AliasName_2 = "OBJECTID"; fieldClass.IsNullable_2 = false; fieldClass.Type_2 = esriFieldType.esriFieldTypeOID; ifieldsEdit_0.AddField(fieldClass); fieldClass = new ESRI.ArcGIS.Geodatabase.Field() as IFieldEdit; fieldClass.Name_2 = "SHAPE"; fieldClass.AliasName_2 = "SHAPE"; fieldClass.IsNullable_2 = true; fieldClass.Type_2 = esriFieldType.esriFieldTypeGeometry; IGeometryDefEdit geometryDefClass = new GeometryDef() as IGeometryDefEdit; geometryDefClass.SpatialReference_2 = ispatialReference_0; geometryDefClass.GridCount_2 = 1; geometryDefClass.GridSize_2[0] = (double)int_0; geometryDefClass.GeometryType_2 = esriGeometryType_0; geometryDefClass.HasZ_2 = bool_1; geometryDefClass.HasM_2 = bool_0; fieldClass.GeometryDef_2 = geometryDefClass; ifieldsEdit_0.AddField(fieldClass); }
/// <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; }
private IFields CreateFeatureField(ITable pTable) { IFields pFields = new Fields(); IFieldsEdit pFieldsEdit = (IFieldsEdit)pFields; IField pField = new Field(); IFieldEdit pFieldEdit = (IFieldEdit)pField; pFieldEdit.Name_2 = "SHAPE"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; IGeometryDefEdit pGeoDef = new GeometryDef() as IGeometryDefEdit; pGeoDef.GeometryType_2 = esriGeometryType.esriGeometryPolygon; //TODO 坐标系问题 //pGeoDef.SpatialReference_2 = pFeatureLayer.SpatialReference; pFieldEdit.GeometryDef_2 = pGeoDef; pFieldsEdit.AddField(pField); for (int i = 0; i < pTable.Fields.FieldCount; i++) { IField field = pTable.Fields.Field[i]; if (field.Type == esriFieldType.esriFieldTypeOID || field.Type == esriFieldType.esriFieldTypeGeometry) { continue; } pField = new Field(); pFieldEdit = (IFieldEdit)pField; pFieldEdit.Name_2 = field.Name; //"YSDM"; pFieldEdit.Type_2 = field.Type; //esriFieldType.esriFieldTypeString; pFieldsEdit.AddField(pField); } return(pFields); }
/// <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; }
/// <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); }
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); }
/// <summary> /// 向字段集添加或修改ObjectID和拥有指定几何类型与坐标系Shape字段,若字段存在且符合要求则不添加或修改 /// (ObjectID会在不同数据源中自动转换为OID/FID/OJBECTID) /// </summary> /// <param name="fields">字段集</param> /// <param name="geometryType">需要添加的Shape字段存储的几何类型</param> /// <param name="spatialRef">需要添加的Shape字段存储的坐标系</param> /// <returns></returns> public static IFields AddBaseFields(this IFields fields, esriGeometryType geometryType, ISpatialReference spatialRef) { IFieldsEdit fieldsEdit = (IFieldsEdit)fields; var oidField = fields.GetFirstFieldsByType(esriFieldType.esriFieldTypeOID); if (oidField == null) { fieldsEdit.AddField(CreateOidField()); } var shapeField = fields.GetFirstFieldsByType(esriFieldType.esriFieldTypeGeometry); if (shapeField == null) { fieldsEdit.AddField(CreateShapeField(geometryType, spatialRef)); } else if (shapeField.GeometryDef.GeometryType != geometryType || shapeField.GeometryDef.SpatialReference != spatialRef) { IFieldEdit shapeFieldEdit = (IFieldEdit)shapeField; shapeFieldEdit.GeometryDef_2 = CreateGeometryDef(geometryType, spatialRef); } return(fields); }
// //create fields needed in roi shp file // private static IFields CreateFields(string xzqmc, string layer) { IObjectClassDescription objectClassDescription = new FeatureClassDescriptionClass(); IFields requiredFields = objectClassDescription.RequiredFields; IFieldsEdit fieldsEdit = requiredFields as IFieldsEdit; IField field1 = new FieldClass(); IFieldEdit fieldEdit1 = field1 as IFieldEdit; fieldEdit1.Name_2 = _YFBH; fieldEdit1.Type_2 = esriFieldType.esriFieldTypeInteger; IField field2 = new FieldClass(); IFieldEdit fieldEdit2 = field2 as IFieldEdit; fieldEdit2.Name_2 = xzqmc; fieldEdit2.Type_2 = esriFieldType.esriFieldTypeString; fieldEdit2.Length_2 = 50; IField field3 = new FieldClass(); IFieldEdit fieldEdit3 = field3 as IFieldEdit; fieldEdit3.Name_2 = layer; fieldEdit3.Type_2 = esriFieldType.esriFieldTypeInteger; fieldsEdit.AddField(field1); fieldsEdit.AddField(field2); fieldsEdit.AddField(field3); return(requiredFields); }
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); } }
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); }
/// <summary> /// 添加注记类的附加字段 /// </summary> /// <param name="pFields"></param> /// <param name="strObjNum"></param> private void addAnnoField(ref IFields pFields, string strObjNum) { IFieldsEdit pFieldsEdit = (IFieldsEdit)pFields; IField newField = new FieldClass(); IFieldEdit pFieldEdit = (IFieldEdit)newField; pFieldEdit.Name_2 = strObjNum; pFieldEdit.AliasName_2 = strObjNum; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString; pFieldsEdit.AddField(newField); newField = new FieldClass(); pFieldEdit = (IFieldEdit)newField; pFieldEdit.Name_2 = "SMSCode"; pFieldEdit.AliasName_2 = "SMSCode"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString; pFieldsEdit.AddField(newField); newField = new FieldClass(); pFieldEdit = (IFieldEdit)newField; pFieldEdit.Name_2 = "SMSSymbol"; pFieldEdit.AliasName_2 = "SMSSymbol"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString; pFieldsEdit.AddField(newField); }
// //create fields needed in roi shp file // private IFields CreateFields() { IObjectClassDescription objectClassDescription = new FeatureClassDescriptionClass(); IFields requiredFields = objectClassDescription.RequiredFields; IFieldsEdit fieldsEdit = requiredFields as IFieldsEdit; IField field1 = new FieldClass(); IFieldEdit fieldEdit1 = field1 as IFieldEdit; fieldEdit1.Name_2 = "CLASS_NAME"; fieldEdit1.Type_2 = esriFieldType.esriFieldTypeString; fieldEdit1.Length_2 = 50; IField field2 = new FieldClass(); IFieldEdit fieldEdit2 = field2 as IFieldEdit; fieldEdit2.Name_2 = "CLASS_ID"; fieldEdit2.Type_2 = esriFieldType.esriFieldTypeInteger; IField field3 = new FieldClass(); IFieldEdit fieldEdit3 = field3 as IFieldEdit; fieldEdit3.Name_2 = "CLASS_CLRS"; fieldEdit3.Type_2 = esriFieldType.esriFieldTypeString; fieldEdit3.Length_2 = 50; fieldsEdit.AddField(field1); fieldsEdit.AddField(field2); fieldsEdit.AddField(field3); return(requiredFields); }
/// <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); } }
private void method_1(int int_0, IFieldsEdit ifieldsEdit_1) { IFieldEdit field = new FieldClass(); field.Name_2 = "OBJECTID"; field.AliasName_2 = "OBJECTID"; field.IsNullable_2 = false; field.Type_2 = esriFieldType.esriFieldTypeOID; ifieldsEdit_1.AddField(field); if (int_0 == 1) { field = new FieldClass(); field.Name_2 = "SHAPE"; field.AliasName_2 = "SHAPE"; field.IsNullable_2 = true; field.Type_2 = esriFieldType.esriFieldTypeGeometry; IGeometryDefEdit edit2 = new GeometryDefClass(); edit2.SpatialReference_2 = new UnknownCoordinateSystemClass(); edit2.GridCount_2 = 1; edit2.set_GridSize(0, 1000.0); edit2.GeometryType_2 = esriGeometryType.esriGeometryPolygon; edit2.HasZ_2 = false; edit2.HasM_2 = false; field.GeometryDef_2 = edit2; ifieldsEdit_1.AddField(field); } }
private void method_1(int int_0, IFieldsEdit ifieldsEdit_2) { IFieldEdit field = new FieldClass(); field.Name_2 = "OBJECTID"; field.AliasName_2 = "OBJECTID"; field.IsNullable_2 = false; field.Type_2 = esriFieldType.esriFieldTypeOID; ifieldsEdit_2.AddField(field); if (int_0 == 1) { field = new FieldClass(); field.Name_2 = "SHAPE"; field.AliasName_2 = "SHAPE"; field.IsNullable_2 = true; field.Type_2 = esriFieldType.esriFieldTypeGeometry; IGeometryDefEdit edit2 = new GeometryDefClass(); edit2.SpatialReference_2 = SpatialReferenctOperator.ConstructCoordinateSystem(this.iworkspace_0 as IGeodatabaseRelease); edit2.GridCount_2 = 1; edit2.set_GridSize(0, 1000.0); edit2.GeometryType_2 = esriGeometryType.esriGeometryPolygon; edit2.HasZ_2 = false; edit2.HasM_2 = false; field.GeometryDef_2 = edit2; ifieldsEdit_2.AddField(field); } }
/// <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); } }
public IFeatureClass CreateShapefile(String sParentDirectory, String sWorkspaceName, String sFileName) { IWorkspaceFactory workspaceFactory; IWorkspace workspace; IFeatureWorkspace featureWorkspace; IFeatureClass featureClass; if (System.IO.Directory.Exists(sParentDirectory + sWorkspaceName)) { workspaceFactory = new ShapefileWorkspaceFactoryClass(); workspace = workspaceFactory.OpenFromFile(sParentDirectory + sWorkspaceName, 0); featureWorkspace = workspace as IFeatureWorkspace; featureClass = featureWorkspace.OpenFeatureClass(sFileName); } else { workspaceFactory = new ShapefileWorkspaceFactoryClass(); IWorkspaceName workspaceName = workspaceFactory.Create(sParentDirectory, sWorkspaceName, null, 0); ESRI.ArcGIS.esriSystem.IName name = workspaceName as ESRI.ArcGIS.esriSystem.IName; workspace = (IWorkspace)name.Open(); featureWorkspace = workspace as IFeatureWorkspace; IFields fields = new FieldsClass(); IFieldsEdit fieldsEdit = fields as IFieldsEdit; IFieldEdit fieldEdit = new FieldClass(); fieldEdit.Name_2 = "OID"; fieldEdit.AliasName_2 = "序号"; fieldEdit.Type_2 = esriFieldType.esriFieldTypeOID; fieldsEdit.AddField((IField)fieldEdit); fieldEdit = new FieldClass(); fieldEdit.Name_2 = "Name"; fieldEdit.AliasName_2 = "名称"; fieldEdit.Type_2 = esriFieldType.esriFieldTypeString; fieldsEdit.AddField((IField)fieldEdit); IGeometryDefEdit geoDefEdit = new GeometryDefClass(); ISpatialReference spatialReference = m_map.SpatialReference; geoDefEdit.SpatialReference_2 = spatialReference; geoDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint; fieldEdit = new FieldClass(); fieldEdit.Name_2 = "Shape"; fieldEdit.AliasName_2 = "形状"; fieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; fieldEdit.GeometryDef_2 = geoDefEdit; fieldsEdit.AddField((IField)fieldEdit); featureClass = featureWorkspace.CreateFeatureClass(sFileName, fields, null, null, esriFeatureType.esriFTSimple, "Shape", ""); if (featureClass == null) { return(null); } } return(featureClass); }
/// <summary> /// 创建包含ObjectID和Shape字段的字段集(ObjectID会在不同数据源中自动转换为OID/FID/OJBECTID) /// </summary> /// <param name="geometryType">几何类型</param> /// <param name="spatialRef">坐标系(若Shape字段所在要素类位于工作空间中,则此值不可为null,若位于要素数据集则应设置为null</param> /// <returns></returns> public static IFields CreateBaseFields(esriGeometryType geometryType, ISpatialReference spatialRef) { IFields fields = new FieldsClass(); IFieldsEdit fieldsEdit = (IFieldsEdit)fields; fieldsEdit.AddField(CreateOidField()); fieldsEdit.AddField(CreateShapeField(geometryType, spatialRef)); return(fields); }
public IFeatureClass CreateShapefile( string sParentDirectory, string sWorkspaceName, string sFileName) { if (System.IO.Directory.Exists(sParentDirectory + sWorkspaceName)) { System.IO.Directory.Delete(sParentDirectory + sWorkspaceName, true); } IWorkspaceFactory pWf = new ShapefileWorkspaceFactoryClass(); IWorkspaceName pWn = pWf.Create(sParentDirectory, sWorkspaceName, null, 0); ESRI.ArcGIS.esriSystem.IName name = pWn as ESRI.ArcGIS.esriSystem.IName; IWorkspace pW = (IWorkspace)name.Open(); IFeatureWorkspace pFw = pW as IFeatureWorkspace; IFields pFs = new FieldsClass(); IFieldsEdit pFse = pFs as IFieldsEdit; IFieldEdit pFe = new FieldClass(); pFe.Name_2 = "OID"; pFe.AliasName_2 = "序号"; pFe.Type_2 = esriFieldType.esriFieldTypeOID; pFse.AddField((IField)pFe);//IFieldEdit.AddField是AO隐藏属性 pFe = new FieldClass(); pFe.Name_2 = "Name"; pFe.AliasName_2 = "名称"; pFe.Type_2 = esriFieldType.esriFieldTypeString; pFse.AddField((IField)pFe); IGeometryDefEdit pGde = new GeometryDefClass(); ISpatialReference pSr = m_map.SpatialReference; pGde.SpatialReference_2 = pSr; pGde.GeometryType_2 = esriGeometryType.esriGeometryPoint; pFe = new FieldClass(); string sShapeFieldName = "Shape"; pFe.Name_2 = sShapeFieldName; pFe.AliasName_2 = "形状"; pFe.Type_2 = esriFieldType.esriFieldTypeGeometry; pFe.GeometryDef_2 = pGde; pFse.AddField((IField)pFe); IFeatureClass pFc = pFw.CreateFeatureClass(sFileName, pFs, null, null, esriFeatureType.esriFTSimple, "Shape", ""); if (pFc == null) { return(null); } return(pFc); }
/// <summary> /// create a feature class in workspace with type geometry /// </summary> /// <param name="geometry">geometry of feature class</param> /// <param name="featureWorkspace">workspace for store feature class</param> /// <returns>feature class created</returns> private IFeatureClass CreateFeatureClass(IGeometry geometry, IFeatureWorkspace featureWorkspace) { // Create a fields collection for the feature class. IFields fields = new FieldsClass(); IFieldsEdit fieldsEdit = (IFieldsEdit)fields; // Add an object ID field to the fields collection. This is mandatory for feature classes. IField oidField = new FieldClass(); IFieldEdit oidFieldEdit = (IFieldEdit)oidField; oidFieldEdit.Name_2 = "OID"; oidFieldEdit.Type_2 = esriFieldType.esriFieldTypeOID; fieldsEdit.AddField(oidField); IField idField = new FieldClass(); IFieldEdit idFieldEdit = (IFieldEdit)idField; idFieldEdit.Name_2 = SAUtility.FieldNameIdWatershed; idFieldEdit.Type_2 = esriFieldType.esriFieldTypeInteger; fieldsEdit.AddField(idField); // Create a geometry definition (and spatial reference) for the feature class. IGeometryDef geometryDef = new GeometryDefClass(); IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit)geometryDef; geometryDefEdit.GeometryType_2 = geometry.GeometryType; geometryDefEdit.SpatialReference_2 = geometry.SpatialReference; IFeatureClassDescription featureClassDescription = new FeatureClassDescriptionClass(); IObjectClassDescription objectClassDescription = (IObjectClassDescription)featureClassDescription; // Add a geometry field to the fields collection. This is where the geometry definition is applied. IField geometryField = new FieldClass(); IFieldEdit geometryFieldEdit = (IFieldEdit)geometryField; geometryFieldEdit.Name_2 = featureClassDescription.ShapeFieldName; geometryFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; geometryFieldEdit.GeometryDef_2 = geometryDef; fieldsEdit.AddField(geometryField); // Use IFieldChecker to create a validated fields collection. IFieldChecker fieldChecker = new FieldCheckerClass(); IEnumFieldError enumFieldError = null; IFields validatedFields = null; fieldChecker.ValidateWorkspace = (IWorkspace)featureWorkspace; fieldChecker.Validate(fields, out enumFieldError, out validatedFields); // The enumFieldError enumerator can be inspected at this point to determine // which fields were modified during validation. // Create the feature class. Note that the CLSID parameter is null - this indicates to use the // default CLSID, esriGeodatabase.Feature (acceptable in most cases for feature classes). IFeatureClass featureClass = featureWorkspace.CreateFeatureClass("pourPoint", validatedFields, objectClassDescription.InstanceCLSID, objectClassDescription.ClassExtensionCLSID, esriFeatureType.esriFTSimple, featureClassDescription.ShapeFieldName, string.Empty); return(featureClass); }
public void Test() { //定义一个几何字段,类型为点类型 ISpatialReferenceFactory2 originalSpatialReferenceFactory = new SpatialReferenceEnvironmentClass(); ISpatialReference originalSpatialReference = originalSpatialReferenceFactory.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_WGS1984); IGeometryDefEdit pGeoDef = new GeometryDefClass(); IGeometryDefEdit pGeoDefEdit = pGeoDef as IGeometryDefEdit; pGeoDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint; pGeoDefEdit.SpatialReference_2 = originalSpatialReference; //定义一个字段集合对象 IFields pFields = new FieldsClass(); IFieldsEdit pFieldsEdit = (IFieldsEdit)pFields; //定义shape字段 IField pField = new FieldClass(); IFieldEdit pFieldEdit = (IFieldEdit)pField; pFieldEdit.Name_2 = "SHAPE"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; pFieldsEdit.AddField(pField); pFieldEdit.GeometryDef_2 = pGeoDef; //定义单个的字段,并添加到字段集合中 pField = new FieldClass(); pFieldEdit = (IFieldEdit)pField; pFieldEdit.Name_2 = "STCD"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString; pFieldsEdit.AddField(pField); //定义单个的字段,并添加到字段集合中 pField = new FieldClass(); pFieldEdit = (IFieldEdit)pField; pFieldEdit.Name_2 = "SLM10"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString; pFieldsEdit.AddField(pField); //定义单个的字段,并添加到字段集合中 pField = new FieldClass(); pFieldEdit = (IFieldEdit)pField; pFieldEdit.Name_2 = "SLM20"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString; pFieldsEdit.AddField(pField); //定义单个的字段,并添加到字段集合中 pField = new FieldClass(); pFieldEdit = (IFieldEdit)pField; pFieldEdit.Name_2 = "SLM40"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString; pFieldsEdit.AddField(pField); IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactory(); IFeatureWorkspace pFWS = pWorkspaceFactory.OpenFromFile(workspaceDirectory, 0) as IFeatureWorkspace; IFeatureClass pFtClass = pFWS.CreateFeatureClass("Test", pFields, null, null, esriFeatureType.esriFTSimple, "SHAPE", null); }
/// <summary> /// 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); } }
public void CreateLineShpFile(string fileName, ISpatialReference pSRF) { //判断需要生成的线文件是否存在,若不存在则创建文件,若存在则将新添加的线写入文件 if (File.Exists(fileName)) { MessageBox.Show("file already exist, please change file name!"); return; } else { string pLineFile = fileName; string pFilePath = System.IO.Path.GetDirectoryName(pLineFile); string pFileName = System.IO.Path.GetFileName(pLineFile); //打开工作空间 IWorkspaceFactory pWSF = new ShapefileWorkspaceFactoryClass(); IFeatureWorkspace pWS = (IFeatureWorkspace)pWSF.OpenFromFile(pFilePath, 0); //判断文件是否存在,若不存在则创建文件 if (!(File.Exists(fileName))) { const string strShapeFieldName = "shape"; //设置字段集 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.esriGeometryPolyline; pGeoDefEdit.SpatialReference_2 = pSRF; pFieldEdit.GeometryDef_2 = pGeoDef; pFieldsEdit.AddField(pField); //add length field pField = new FieldClass(); pFieldEdit = (IFieldEdit)pField; pFieldEdit.Name_2 = "length"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble; pFieldEdit.IsNullable_2 = true; pFieldsEdit.AddField(pField); //创建shapefile线图层 pWS.CreateFeatureClass(pFileName, pFields, null, null, esriFeatureType.esriFTSimple, strShapeFieldName, ""); } } }
//创建线图层 public IFeatureClass CreatLineFC(IFeatureDataset pFeatureDataset, IFeatureClass pPointFClass) { try { //建立shape字段 IFields pFields = new FieldsClass(); IFieldsEdit pFieldsEdit = (IFieldsEdit)pFields; IField pField = new FieldClass(); IFieldEdit pFieldEdit = (IFieldEdit)pField; pFieldEdit.Name_2 = "SHAPE"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; //设置Geometry definition IGeometryDef pGeometryDef = new GeometryDefClass(); IGeometryDefEdit pGeometryDefEdit = (IGeometryDefEdit)pGeometryDef; pGeometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolyline; //点、线、面 pGeometryDefEdit.SpatialReference_2 = ClsGDBDataCommon.CreateProjectedCoordinateSystem(); pFieldEdit.GeometryDef_2 = pGeometryDef; pFieldsEdit.AddField(pField); //新建字段 pField = new FieldClass(); pFieldEdit = (IFieldEdit)pField; pFieldEdit.Name_2 = "Length"; //点类型 pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString; pFieldEdit.IsNullable_2 = true; pFieldsEdit.AddField(pField); IFeatureClass pFeatureClass = pFeatureDataset.CreateFeatureClass(textBoxX3.Text + "_GmpLine", pFields, null, null, esriFeatureType.esriFTSimple, "SHAPE", ""); FrmPointToLine frmP2L = new FrmPointToLine(m_pMapCtl.Object as IMapControl3); IFeatureLayer PFLayer = new FeatureLayerClass(); PFLayer.FeatureClass = pPointFClass; IPointCollection pPointColl = frmP2L.ReadPoint(PFLayer); List <IPolyline> LineList = CreatePolyline(pPointColl); for (int i = 0; i < LineList.Count; i++) { if (LineList[i].Length > 0) { IFeature pFeature = pFeatureClass.CreateFeature(); pFeature.Shape = LineList[i]; string sLength = ((int)(LineList[i].Length * 100.0) / 100.0).ToString() + "m"; pFeature.set_Value(pFeature.Fields.FindField("Length"), sLength); pFeature.Store(); } } return(pFeatureClass); } catch (System.Exception ex) { return(null); } }
/// <summary> /// 创建指定名称的FeatureClass 适用于创建Stops和Barriers /// </summary> /// <param name="workspace"></param> /// <param name="name"></param> /// <param name="spatialReference">与主窗体上的axMapcontrol的连接吧...</param> public static IFeatureClass CreateFeatureClass(IFeatureWorkspace workspace, string name, ISpatialReference spatialReference = null) { if (spatialReference == null) { spatialReference = ApplicationV.GlobalMapControl.ActiveView.FocusMap.SpatialReference; } //创建字段集 IFeatureClassDescription fcDescription = new FeatureClassDescriptionClass(); IObjectClassDescription ocDescription = (IObjectClassDescription)fcDescription; //创建字段组 IFields pFields = new FieldsClass(); IFieldsEdit pFieldsEdit = (IFieldsEdit)pFields; IField Field_OID = new FieldClass(); IFieldEdit Field_OID_EDIT = Field_OID as IFieldEdit; Field_OID_EDIT.Name_2 = "OBJECTID"; Field_OID_EDIT.Type_2 = esriFieldType.esriFieldTypeOID; Field_OID_EDIT.IsNullable_2 = false; Field_OID_EDIT.Required_2 = false; pFieldsEdit.AddField(Field_OID); //创建一个必要的东西 IGeometryDef geoDef = new GeometryDefClass(); IGeometryDefEdit geoDefEdit = (IGeometryDefEdit)geoDef; geoDefEdit.AvgNumPoints_2 = 5; geoDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint; geoDefEdit.GridCount_2 = 1; geoDefEdit.HasM_2 = false; geoDefEdit.HasZ_2 = false; geoDefEdit.SpatialReference_2 = spatialReference; //创建SHAPE字段 IField Field_SHAPE = new FieldClass(); IFieldEdit pFieldEdit_SHAPE = (IFieldEdit)Field_SHAPE; pFieldEdit_SHAPE.Name_2 = "SHAPE"; pFieldEdit_SHAPE.Type_2 = esriFieldType.esriFieldTypeGeometry; pFieldEdit_SHAPE.GeometryDef_2 = geoDef; //放入组 pFieldsEdit.AddField(Field_SHAPE); //正式创建要素类 return(workspace.CreateFeatureClass(name, pFields, ocDescription.ClassExtensionCLSID, ocDescription.ClassExtensionCLSID, esriFeatureType.esriFTSimple, fcDescription.ShapeFieldName, "")); }
/// <summary> /// 新建面图层 /// </summary> public IFeatureClass CreatePolygonLayer(IPolygon polygon) { //LocalFilePath = sfdPoint.FileName; FilePath = Application.StartupPath + "\\tempSHP"; FileName = "test"; IFields pFields = new FieldsClass(); IFieldsEdit pFieldsEdit = pFields as IFieldsEdit; IField pField = new FieldClass(); IFieldEdit pFieldEdit = pField as IFieldEdit; pFieldEdit.Name_2 = "SHAPE"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; IGeometryDef pGeometryDef = new GeometryDefClass(); IGeometryDefEdit pGeometryDefEdit = pGeometryDef as IGeometryDefEdit; pGeometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolygon; pFieldEdit.GeometryDef_2 = pGeometryDef; pFieldsEdit.AddField(pField); pField = new FieldClass(); pFieldEdit = pField as IFieldEdit; pFieldEdit.Name_2 = "HEIGHT"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble; pFieldsEdit.AddField(pField); IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactoryClass(); IFeatureWorkspace pFeatureWorkspace = pWorkspaceFactory.OpenFromFile(FilePath, 0) as IFeatureWorkspace; IFeatureClass feaC = pFeatureWorkspace.CreateFeatureClass(FileName, pFields, null, null, esriFeatureType.esriFTSimple, "SHAPE", ""); IFeature pFeature = feaC.CreateFeature(); pFeature.Shape = polygon as IGeometry; pFeature.Store(); IFeatureCursor feaCur = feaC.Search(null, true); pFeature = feaCur.NextFeature(); while (pFeature != null) { //pFeature.set_Value(2, "181"); pFeature.Store(); pFeature = feaCur.NextFeature(); } //m_mapControl.AddShapeFile(FilePath, FileName); //m_mapControl.ActiveView.Refresh(); return(feaC); }
public static bool CreateLogTable() { if (m_sysTable == null) { return(false); } //InitStaticFields(); Exception err; ITable pTable = m_sysTable.OpenTable(m_LogNAME, out err); if (pTable != null) { return(true);//若日志表已存在,返回true } IFields pFields = new FieldsClass(); IFieldsEdit pFieldsEdit = pFields as IFieldsEdit; IField pField = new FieldClass(); IFieldEdit pEdit = pField as IFieldEdit; pEdit.Name_2 = "logTime"; pEdit.Type_2 = esriFieldType.esriFieldTypeString; pEdit.Length_2 = 30; pFieldsEdit.AddField(pField); pField = new FieldClass(); pEdit = pField as IFieldEdit; pEdit.Name_2 = "logUser"; pEdit.Type_2 = esriFieldType.esriFieldTypeString; pEdit.Length_2 = 50; pFieldsEdit.AddField(pField); pField = new FieldClass(); pEdit = pField as IFieldEdit; pEdit.Name_2 = "logIP"; pEdit.Type_2 = esriFieldType.esriFieldTypeString; pEdit.Length_2 = 30; pFieldsEdit.AddField(pField); pField = new FieldClass(); pEdit = pField as IFieldEdit; pEdit.Name_2 = "logEVENT"; pEdit.Type_2 = esriFieldType.esriFieldTypeString; pEdit.Length_2 = 255; pFieldsEdit.AddField(pField); IFieldChecker pFieldChecker = new FieldCheckerClass();//检查字段有效性 pFieldChecker.ValidateWorkspace = m_gisDb.WorkSpace; IFields pValidFields = null; IEnumFieldError pEFE = null; pFieldChecker.Validate(pFields, out pEFE, out pValidFields); return(m_sysTable.CreateTable(m_LogNAME, pValidFields, out err)); }
//创建shapefile public IFeatureClass CreateShapefile(string shpPath, string shpName, ISpatialReference shpRef, string shpType) ///////////模式 { IFeatureClass feaCls = null; ////////模式 IFields fields = new Fields(); IFieldsEdit fieldsEdit = fields as IFieldsEdit; IField field = new Field(); // IFieldEdit fieldEdit = field as IFieldEdit; // fieldEdit.Name_2 = "OID"; //遵守规定。。。但是都有??? fieldEdit.Type_2 = esriFieldType.esriFieldTypeOID; // fieldsEdit.AddField(field); // fieldEdit = new FieldClass(); fieldEdit.Name_2 = "NAME"; //遵守规定。。。但是都有??? fieldEdit.Type_2 = esriFieldType.esriFieldTypeString; fieldsEdit.AddField(fieldEdit as IField); //定义一个图形shape字段, //先给出几何图形的定义,包括设置图形类型和空间参考 IGeometryDefEdit geoDefEdit = new GeometryDefClass(); if (shpType == "point") { geoDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint; } if (shpType == "polyline") { geoDefEdit.GeometryType_2 = esriGeometryType.esriGeometryLine; } if (shpType == "polygon") { geoDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolygon; } geoDefEdit.SpatialReference_2 = shpRef; //几何类型有空间参考属性 fieldEdit = new FieldClass(); fieldEdit.Name_2 = "Shape"; //遵守规定。。。但是都有??? fieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; //图形参考在这里定义 ,要说明几何类型 fieldEdit.GeometryDef_2 = geoDefEdit as IGeometryDef; //////!重头戏!真有趣啊 因为是几何类型,所以在字段编辑的属性再加上几何ref fieldsEdit.AddField(fieldEdit as IField); IWorkspaceFactory workspaceFactory = new ShapefileWorkspaceFactory(); IWorkspace workspace = workspaceFactory.OpenFromFile(shpPath, 0); //调用工厂的方法; IFeatureWorkspace shpWorkspace = (IFeatureWorkspace)workspace; feaCls = shpWorkspace.CreateFeatureClass(shpName, fields, null, null, esriFeatureType.esriFTSimple, "Shape", ""); //此处发现更替路径创建会报错 return(feaCls); //////////////模式 }
/// <summary> /// 根据属性不同建立空的shp文件 /// </summary> /// <param name="CADfeatureClass">待转换的CAD要素类</param> /// <param name="ShpPath">存储新建空shp文件的路径</param> /// <returns>返回创建好的FeatureClass列表</returns> private List <IFeatureClass> ExportFeatureClassByAtt(IFeatureClass CADfeatureClass, string ShpPath, List <string> FieldName) { List <string> AttList = new List <string>(); List <IFeatureClass> feaClassList = new List <IFeatureClass>(); IFeatureClass target_FeatureClass; ISpatialReferenceFactory spatialReferenceFactory = new SpatialReferenceEnvironmentClass(); string s = CADfeatureClass.ShapeType.ToString(); //定义属性字段 DataManager DM = new DataManager(); IFields pFields = new FieldsClass(); IFieldsEdit pFieldsEdit = (IFieldsEdit)pFields; IField pField = new FieldClass(); IFieldEdit pFieldEdit = (IFieldEdit)pField; //设置地理属性字段,点线面,空间参考等 pFieldEdit.Name_2 = "shape"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; IGeometryDef pGeoDef = new GeometryDefClass(); IGeometryDefEdit pGeoDefEdit = pGeoDef as IGeometryDefEdit; pGeoDefEdit.GeometryType_2 = CADfeatureClass.ShapeType; pGeoDefEdit.HasM_2 = true; pGeoDefEdit.HasZ_2 = true; ISpatialReference spatialReference = DataManager.getSpatialReference(CADfeatureClass); pFieldEdit.GeometryDef_2 = pGeoDef; pFieldsEdit.AddField(pField); //遍历各字段设置其他属性字段 for (int i = 2; i < CADfeatureClass.Fields.FieldCount; i++) { pField = new FieldClass(); pFieldEdit = (IFieldEdit)pField; pFieldEdit.Name_2 = CADfeatureClass.Fields.Field[i].Name; pFieldEdit.AliasName_2 = CADfeatureClass.Fields.Field[i].AliasName; pFieldEdit.Type_2 = CADfeatureClass.Fields.Field[i].Type; pFieldsEdit.AddField(pField); } AttList = getUniqueValue(CADfeatureClass, "Layer"); int n = FieldName.Count; for (int i = 0; i < n; i++) { //建立空的shapefile target_FeatureClass = DM.CreateVoidShp(ShpPath, FieldName[i], pFields, spatialReference); feaClassList.Add(target_FeatureClass); } return(feaClassList); }
private IFeatureClass CreateShapeFile(string strOutFileSource, string strOutFileName, ISpatialReference spatialReference, esriGeometryType pGeometryType) { try { //Setting Output Data IWorkspaceFactory wsfOutput = new ShapefileWorkspaceFactoryClass(); IWorkspace wsOutput = wsfOutput.OpenFromFile(strOutFileSource, 0); IFeatureWorkspace fwOutput = wsOutput as IFeatureWorkspace; IFields pFields = new FieldsClass(); IFieldsEdit pFieldsEdit = pFields as IFieldsEdit; IField oidField = new FieldClass(); IFieldEdit oidFieldEdit = (IFieldEdit)oidField; oidFieldEdit.Name_2 = "OID"; oidFieldEdit.Type_2 = esriFieldType.esriFieldTypeOID; pFieldsEdit.AddField(oidField); IGeometryDef geometryDef = new GeometryDefClass(); IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit)geometryDef; geometryDefEdit.GeometryType_2 = pGeometryType; if (spatialReference != null) { geometryDefEdit.SpatialReference_2 = spatialReference; } IField geometryField = new FieldClass(); IFieldEdit geometryFieldEdit = (IFieldEdit)geometryField; geometryFieldEdit.Name_2 = "Shape"; geometryFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; geometryFieldEdit.GeometryDef_2 = geometryDef; pFieldsEdit.AddField(geometryField); IFieldChecker fieldChecker = new FieldCheckerClass(); IEnumFieldError enumFieldError = null; IFields validatedFields = null; fieldChecker.ValidateWorkspace = (IWorkspace)fwOutput; fieldChecker.Validate(pFields, out enumFieldError, out validatedFields); IFeatureClass fcOutput = fwOutput.CreateFeatureClass(strOutFileName, validatedFields, null, null, esriFeatureType.esriFTSimple, "Shape", ""); return(fcOutput); } catch (Exception ex) { MessageBox.Show("Fail to create a new shapefile."); return(null); } }
private void AddSimpleField(IFieldsEdit fieldsEdit,esriFieldType type,string name,string aliasName) { IField field = new FieldClass(); IFieldEdit fieldEdit = field as IFieldEdit; fieldEdit.Type_2 = type; fieldEdit.Name_2 = name; fieldEdit.AliasName_2 = aliasName; fieldsEdit.AddField(field); }
/// <summary> /// add feature feild of each feature /// </summary> /// <param name="fieldName"></param> /// <param name="aliasName"></param> /// <param name="type"></param> /// <param name="fieldsEdit"></param> private void addFeatureFeild(string fieldName, string aliasName, esriFieldType type, IFieldsEdit fieldsEdit) { IField field = new FieldClass(); IFieldEdit fieldEdit = (IFieldEdit)field; fieldEdit.Name_2 = fieldName; fieldEdit.Type_2 = type; fieldEdit.AliasName_2 = aliasName; fieldsEdit.AddField(field); }