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; }
上面是可正常運行的代碼。本身寫的時候憑着本身的理解作了部分修改,在字段聲明的時候略有不一樣,以下spa
IFields pfs = new FieldsClass(); IFieldsEdit pFieldsEdit = pfs as IFieldsEdit; IField pFieldd = new FieldClass(); IFieldEdit pField = pFieldd as IFieldEdit;
結果在運行的時候出現COM異常,仔細覈查了CreateFeatureClass的每一個參數,屢次檢查都沒有查到結果。後來修改成文中第一段代碼聲明方式,再運行成功了。blog
爲了找出錯誤,我又把聲明換回第二段代碼,使人鬱悶的是,又沒異常了。索引
本文只描述現象,不解釋緣由string