private static IFeatureClass CreatStnShp(string shp) { //打开工作空间 IWorkspaceFactory wsfactory = new ShapefileWorkspaceFactoryClass(); string ssss = System.IO.Path.GetDirectoryName(shp); IWorkspace workspace = wsfactory.OpenFromFile(ssss, 0); IFeatureWorkspace pFeatWsp = workspace as IFeatureWorkspace; if (File.Exists(shp)) { DialogResult dr = MessageBox.Show("文件已经存在,是否使用该文件?", "提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); if ( dr== DialogResult.Yes) { return pFeatWsp.OpenFeatureClass(System.IO.Path.GetFileNameWithoutExtension(shp)); } else if(dr==DialogResult.No) { //删除已有 DialogResult ddr = MessageBox.Show("是否删除并替换已有文件", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (ddr == DialogResult.Yes) { string dbffile = System.IO.Path.ChangeExtension(shp, ".dbf"); string shxfile = System.IO.Path.ChangeExtension(shp, ".shx"); string prjfile = System.IO.Path.ChangeExtension(shp, ".prj"); File.Delete(shp); if (File.Exists(dbffile)) File.Delete(dbffile); if (File.Exists(shxfile)) File.Delete(shxfile); if (File.Exists(prjfile)) File.Delete(prjfile); } else { MessageBox.Show("请重新选择shapfile文件的路径"); } } else { return null; } } IGeometryDefEdit pGeoDef = new GeometryDefClass(); pGeoDef.GeometryType_2 = esriGeometryType.esriGeometryPoint; //设置空间参考 ISpatialReferenceFactory3 spatialReferenceFactory = new SpatialReferenceEnvironmentClass(); ISpatialReference pSr = spatialReferenceFactory.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_WGS1984); //设置字段 IFieldEdit pField; IFieldsEdit pFieldsEdit = new FieldsClass(); //设置几何字段 pField = new FieldClass(); pField.Type_2 = esriFieldType.esriFieldTypeGeometry; pField.GeometryDef_2 = pGeoDef; pField.Name_2 = "Shape"; pFieldsEdit.AddField(pField); //产生唯一索引字段 pField = new FieldClass(); pField.Name_2 = "OBJECTID"; pField.Type_2 = esriFieldType.esriFieldTypeOID; pFieldsEdit.AddField(pField); //添加station相关字段 //string[] str = new string[]{"NCDCID","WBAN","NAME","COOPID","COUNTRY","STNTYPE"}; string[] str = new string[] { "WBAN", "NAME", "LOCATION", "LAT", "LON"}; foreach (string stt in str) { pField = new FieldClass(); pField.Name_2 = stt; pField.AliasName_2 = stt; pField.Type_2 = esriFieldType.esriFieldTypeString; if (stt == "LOCATION") pField.Length_2 = 60; else pField.Length_2 = 30; pFieldsEdit.AddField(pField); } pField = new FieldClass(); pField.Name_2 = "StnHeight"; pField.Type_2 = esriFieldType.esriFieldTypeDouble; pFieldsEdit.AddField(pField); pField = new FieldClass(); pField.Name_2 = "GndHeight"; pField.Type_2 = esriFieldType.esriFieldTypeDouble; pFieldsEdit.AddField(pField); IFeatureClass pfeatcls =pFeatWsp.CreateFeatureClass(System.IO.Path.GetFileNameWithoutExtension(shp), pFieldsEdit as IFields, null, null, esriFeatureType.esriFTSimple, "Shape", ""); IGeoDataset pGeoDs = pfeatcls as IGeoDataset; IGeoDatasetSchemaEdit pGeoDSe = pGeoDs as IGeoDatasetSchemaEdit; if (pGeoDSe.CanAlterSpatialReference) { pGeoDSe.AlterSpatialReference(pSr); } return pfeatcls; }
上面是可正常运行的代码。自己写的时候凭着自己的理解做了部分修改,在字段声明的时候略有不同,如下
IFields pfs = new FieldsClass(); IFieldsEdit pFieldsEdit = pfs as IFieldsEdit; IField pFieldd = new FieldClass(); IFieldEdit pField = pFieldd as IFieldEdit;
结果在运行的时候出现COM异常,仔细核查了CreateFeatureClass的每个参数,多次检查都没有查到结果。后来修改为文中第一段代码声明方式,再运行成功了。
为了找出错误,我又把声明换回第二段代码,令人郁闷的是,又没异常了。
本文只描述现象,不解释原因