using System; using System.Collections.Generic; using System.Linq; using System.Text; using ESRI.ArcGIS.Geodatabase; using ESRI.ArcGIS.DataSourcesRaster; using ESRI.ArcGIS.Geometry; namespace Subsidence_Forecast_System.Class { class WriteToSDE { #region 全局变量,构造函数 //protected SDEConnectParas paras; private FileWorkSpace pFileWorkSpace; public WriteToSDE(SDEConnectParas para) { //paras = para; pFileWorkSpace = new FileWorkSpace(); } #endregion #region 初始化SDE连接参数 private SDEConnectPara InitPara() { //paras.ReadFromFile(); string Server = "ORCLSDE_192.168.71.162"; string Instance = "5151"; string User = "sde"; string Pass = "sde"; string Database = "ORCLSDE"; string Version = "SDE_DEFAULT"; //paras.GetSDEParameters(out Server, out Instance, out User, out Pass, out Database, out Version); //从paras中获取SDE连接参数 SDEConnectPara para = new SDEConnectPara(); para.Server = Server; para.Instance = Instance; para.User = User; para.Password = Pass; para.Database = Database; para.Version = Version; return para; } #endregion #region 连接到工作空间 public ConnectRasterWorkSpaceDef ConnToSdeRasterDef() { SDEConnectPara para = InitPara(); ConnectRasterWorkSpaceDef ConnRasterDef = new ConnectRasterWorkSpaceDef(para); return ConnRasterDef; } public ConnectFeatureWorkSpaceDef ConnToSdeFeatureDef() { SDEConnectPara para = InitPara(); ConnectFeatureWorkSpaceDef ConnFeatureDef = new ConnectFeatureWorkSpaceDef(para); return ConnFeatureDef; } #endregion #region 读取空间矢量和栅格数据 public IRasterDataset GetRasterDataset(ConnectRasterWorkSpaceDef ConnDef, string RasterName) { ConnectRasterWorkSpaceGet pWorkSpaceGet = new ConnectRasterWorkSpaceGet(ConnDef); IRasterWorkspaceEx pWorkSpace = pWorkSpaceGet.GetSDEWorkspace(); if (pWorkSpace == null) return null; try { return pWorkSpace.OpenRasterDataset(RasterName); } catch { return null; } } public IFeatureClass GetFeatureClass(ConnectFeatureWorkSpaceDef ConnDef, string FeatureName) { ConnectFeatureWorkSpaceGet pWorkSpaceGet = new ConnectFeatureWorkSpaceGet(ConnDef); IFeatureWorkspace pWorkSpace = pWorkSpaceGet.GetSDEWorkspace(); if (pWorkSpace == null) return null; try { return pWorkSpace.OpenFeatureClass(FeatureName); } catch { return null; } } #endregion #region sde处理栅格 public bool WriteRasterToSDE(Guid MapGuid, ConnectRasterWorkSpaceDef ConnDef, string filePath) { ConnectRasterWorkSpaceGet pWorkSpaceGet = new ConnectRasterWorkSpaceGet(ConnDef); IRasterWorkspaceEx pWorkSpace = pWorkSpaceGet.GetSDEWorkspace(); //pWorkSpace.SaveAsRasterDataset("fasdf",pDataSet.CreateDefaultRaster(),null,"",null,null); string LayerStoreName = CopyRasterToSSDE(filePath, pWorkSpace); //#region 写入数据库图层信息 //WriteToDB wtd = new WriteToDB(); //FilePath file = new FilePath(filePath); //wtd.WriteLayerInfo(file.GetFileName(), MapGuid, 1, LayerStoreName); //#endregion return true; } public string CopyRasterToSSDE(string filePath, IRasterWorkspaceEx pWorkSpace) { FilePath file = new FilePath(filePath); IRasterWorkspace2 pRW = pFileWorkSpace.OpenRasterWorkspace(file.GetDir()); IRasterDataset pRds = pRW.OpenRasterDataset(file.GetFileName()); IRasterProps pRasterProps = (IRasterProps)pRds.CreateDefaultRaster(); IRasterStorageDef pRasterStorageDef = new RasterStorageDefClass(); IRasterDef pRasterDef = new RasterDefClass(); pRasterDef.SpatialReference = pRasterProps.SpatialReference; IGeometryDef pGeoDef = new GeometryDefClass(); IGeometryDefEdit pGeoDefEdit = pGeoDef as IGeometryDefEdit; pGeoDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolygon; pGeoDefEdit.AvgNumPoints_2 = 4; pGeoDefEdit.GridCount_2 = 1; pGeoDefEdit.set_GridSize(0, 1000); pGeoDefEdit.SpatialReference_2 = pRasterProps.SpatialReference; DateTime dt = DateTime.Now; string timestr = "sde" + "_" + dt.Year.ToString() + dt.Month.ToString() + dt.Day.ToString() + "_" + dt.Hour + dt.Minute + dt.Second; IRasterDataset pRasterDataset = pWorkSpace.SaveAsRasterDataset(timestr, pRds.CreateDefaultRaster(), pRasterStorageDef, "", pRasterDef, pGeoDef); return timestr; } public bool CreateRasterToSSDE(IRasterDataset pRDs, IRasterWorkspaceEx pWorkSpace) { IRasterProps pRasterProps = (IRasterProps)pRDs.CreateDefaultRaster(); IRasterStorageDef pRasterStorageDef = new RasterStorageDefClass(); pRasterStorageDef.CompressionType = esriRasterCompressionType.esriRasterCompressionJPEG2000; pRasterStorageDef.CompressionQuality = 50; pRasterStorageDef.PyramidLevel = 2; pRasterStorageDef.PyramidResampleType = rstResamplingTypes.RSP_BilinearInterpolation; pRasterStorageDef.TileHeight = 128; pRasterStorageDef.TileWidth = 128; IRasterDef pRasterDef = new RasterDefClass(); pRasterDef.Description = "rasterdataset"; pRasterDef.SpatialReference = pRasterProps.SpatialReference; IGeometryDef pGeoDef = new GeometryDefClass(); IGeometryDefEdit pGeoDefEdit = pGeoDef as IGeometryDefEdit; pGeoDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolygon; pGeoDefEdit.AvgNumPoints_2 = 4; pGeoDefEdit.GridCount_2 = 1; pGeoDefEdit.set_GridSize(0, 1000); pGeoDefEdit.SpatialReference_2 = pRasterProps.SpatialReference; //使用CreateRasterDataset创建一个新层可以 IRasterDataset pRasterDataset = pWorkSpace.CreateRasterDataset("zzy", 3, rstPixelType.PT_UCHAR, pRasterStorageDef, "", pRasterDef, pGeoDef); pRasterDataset = pRDs; return true; } #endregion #region sde处理feature public bool AddFeatureToSDE(Guid MapGuid, ConnectFeatureWorkSpaceDef ConnDef, IFeatureClass pFC, string LayerName) { ConnectFeatureWorkSpaceGet pWorkSpaceGet = new ConnectFeatureWorkSpaceGet(ConnDef); IFeatureWorkspace pWorkSpace = pWorkSpaceGet.GetSDEWorkspace(); //pWorkSpace.SaveAsRasterDataset("fasdf",pDataSet.CreateDefaultRaster(),null,"",null,null); string LayerStoreName = CopyFeatureToSSDE(pFC, pWorkSpace, LayerName); //#region 写入数据库图层信息 //WriteToDB wtd = new WriteToDB(); //wtd.WriteLayerInfo(LayerName, MapGuid, 0, LayerStoreName); //#endregion return true; } public bool AddFeatureToSDE(ConnectFeatureWorkSpaceDef ConnDef, string filePath) { try { FilePath file = new FilePath(filePath); IFeatureWorkspace pFW = pFileWorkSpace.OpenFeatureWorkspace(file.GetDir()); IFeatureClass pFC = pFW.OpenFeatureClass(file.GetFileName()); ConnectFeatureWorkSpaceGet pWorkSpaceGet = new ConnectFeatureWorkSpaceGet(ConnDef); IFeatureWorkspace pWorkSpace = pWorkSpaceGet.GetSDEWorkspace(); CopyFeatureToSSDE(pFC, pWorkSpace, file.GetFileName()); } catch { return false; } return true; } public string CopyFeatureToSSDE(IFeatureClass pFC, IFeatureWorkspace pWorkSpace, string LayerName) { IDataset pDataset = pFC as IDataset; //IFeatureDataset pFD = pWorkSpace.OpenFeatureDataset("test.DBO.ContourDataSet"); DateTime dt = DateTime.Now; string timestr = "contour" + dt.Year.ToString() + dt.Month.ToString() + dt.Day.ToString() + "_" + dt.Hour + dt.Minute + dt.Second; IFeatureDataConverter_ConvertFeatureClass(pDataset.Workspace as IWorkspace, pWorkSpace as IWorkspace, LayerName, timestr); return timestr; } public void IFeatureDataConverter_ConvertFeatureClass(IWorkspace sourceWorkspace, IWorkspace targetWorkspace, string nameOfSourceFeatureClass, string nameOfTargetFeatureClass) { //create source workspace name IDataset sourceWorkspaceDataset = (IDataset)sourceWorkspace; IWorkspaceName sourceWorkspaceName = (IWorkspaceName)sourceWorkspaceDataset.FullName; //create source dataset name IFeatureClassName sourceFeatureClassName = new FeatureClassNameClass(); IDatasetName sourceDatasetName = (IDatasetName)sourceFeatureClassName; sourceDatasetName.WorkspaceName = sourceWorkspaceName; sourceDatasetName.Name = nameOfSourceFeatureClass; //create target workspace name IDataset targetWorkspaceDataset = (IDataset)targetWorkspace; IWorkspaceName targetWorkspaceName = (IWorkspaceName)targetWorkspaceDataset.FullName; //create target dataset name IFeatureClassName targetFeatureClassName = new FeatureClassNameClass(); IDatasetName targetDatasetName = (IDatasetName)targetFeatureClassName; targetDatasetName.WorkspaceName = targetWorkspaceName; targetDatasetName.Name = nameOfTargetFeatureClass; //Open input Featureclass to get field definitions. ESRI.ArcGIS.esriSystem.IName sourceName = (ESRI.ArcGIS.esriSystem.IName)sourceFeatureClassName; IFeatureClass sourceFeatureClass = (IFeatureClass)sourceName.Open(); //Validate the field names because you are converting between different workspace types. IFieldChecker fieldChecker = new FieldCheckerClass(); IFields targetFeatureClassFields; IFields sourceFeatureClassFields = sourceFeatureClass.Fields; IEnumFieldError enumFieldError; // Most importantly set the input and validate workspaces! fieldChecker.InputWorkspace = sourceWorkspace; fieldChecker.ValidateWorkspace = targetWorkspace; fieldChecker.Validate(sourceFeatureClassFields, out enumFieldError, out targetFeatureClassFields); // Loop through the output fields to find the geomerty field IField geometryField; for (int i = 0; i < targetFeatureClassFields.FieldCount; i++) { if (targetFeatureClassFields.get_Field(i).Type == esriFieldType.esriFieldTypeGeometry) { geometryField = targetFeatureClassFields.get_Field(i); // Get the geometry field's geometry defenition IGeometryDef geometryDef = geometryField.GeometryDef; //Give the geometry definition a spatial index grid count and grid size IGeometryDefEdit targetFCGeoDefEdit = (IGeometryDefEdit)geometryDef; targetFCGeoDefEdit.GridCount_2 = 1; targetFCGeoDefEdit.set_GridSize(0, 0); //Allow ArcGIS to determine a valid grid size for the data loaded targetFCGeoDefEdit.SpatialReference_2 = geometryField.GeometryDef.SpatialReference; // we want to convert all of the features IQueryFilter queryFilter = new QueryFilterClass(); queryFilter.WhereClause = ""; // Load the feature class IFeatureDataConverter fctofc = new FeatureDataConverterClass(); IEnumInvalidObject enumErrors = fctofc.ConvertFeatureClass(sourceFeatureClassName, queryFilter, null, targetFeatureClassName, geometryDef, targetFeatureClassFields, "", 1000, 0); break; } } } #endregion } } using System; using System.Collections.Generic; using System.Linq; using System.Text; using ESRI.ArcGIS.Geodatabase; using ESRI.ArcGIS.DataSourcesFile; using ESRI.ArcGIS.DataSourcesRaster; using System.Windows.Forms; namespace Subsidence_Forecast_System.Class { class FileWorkSpace { /// <summary> /// 打开一个Feature工作空间 /// </summary> /// <param name="PathName">工作空间路径</param> /// <returns></returns> public IFeatureWorkspace OpenFeatureWorkspace(string PathName) { try { IWorkspaceFactory workspaceFact = new ShapefileWorkspaceFactoryClass(); IWorkspace pwork = workspaceFact.OpenFromFile(PathName, 0); IFeatureWorkspace pFeatWork = (IFeatureWorkspace)pwork; return pFeatWork; } catch (Exception ex) { return null; } } /// <summary> /// 打开一个Raster工作空间 /// </summary> /// <param name="PathName">工作空间路径</param> /// <returns></returns> public IRasterWorkspace2 OpenRasterWorkspace(string PathName) { try { IWorkspaceFactory workspaceFact = new RasterWorkspaceFactoryClass(); IWorkspace pWork = workspaceFact.OpenFromFile(PathName, 0); IRasterWorkspace2 pRasterWork = (IRasterWorkspace2)pWork; return pRasterWork; } catch (Exception ex) { return null; } } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; using ESRI.ArcGIS.Geodatabase; using ESRI.ArcGIS.DataSourcesGDB; using ESRI.ArcGIS.esriSystem; using System.IO; namespace Subsidence_Forecast_System.Class { /// <summary> /// 连接类型 /// </summary> public enum ConnectWorkSpaceType { FeatureClassWorkSpace = 0, RasterWorkSpace = 1 }; public class ConnectWorkSpaceDef { protected ConnectWorkSpaceType CurrentWorkSpaceType = ConnectWorkSpaceType.FeatureClassWorkSpace; public ConnectWorkSpaceDef() { } public ConnectWorkSpaceType GetWorkSpaceType() { return CurrentWorkSpaceType; } } public class SDEConnectPara //SDE的连接参数 { public string Server; public string Instance; public string User; public string Password; public string Database; public string Version; public SDEConnectPara() { Version = "SDE.DEFAULT"; } } #region 定义连接参数 public class ConnectFeatureWorkSpaceDef : ConnectWorkSpaceDef { public readonly SDEConnectPara ConnectPara; public ConnectFeatureWorkSpaceDef(SDEConnectPara Connparas) { CurrentWorkSpaceType = ConnectWorkSpaceType.FeatureClassWorkSpace; ConnectPara = Connparas; } } public class ConnectRasterWorkSpaceDef : ConnectWorkSpaceDef { public readonly SDEConnectPara ConnectPara; public ConnectRasterWorkSpaceDef(SDEConnectPara Connparas) { CurrentWorkSpaceType = ConnectWorkSpaceType.RasterWorkSpace; ConnectPara = Connparas; } } #endregion #region 获取工作空间 /// <summary> /// 连接到SDE feature数据工作空间 /// </summary> public class ConnectFeatureWorkSpaceGet { private ConnectFeatureWorkSpaceDef pDef; public ConnectFeatureWorkSpaceGet(ConnectFeatureWorkSpaceDef ConnDef) { pDef = ConnDef; } public IFeatureWorkspace GetSDEWorkspace() //依据pDef得到SDE的工作空间 { IWorkspaceFactory pWorkspaceFactory = new SdeWorkspaceFactoryClass(); IFeatureWorkspace pWorkspace; SDEConnectPara para = pDef.ConnectPara; IPropertySet pPropSet = new PropertySetClass(); pPropSet.SetProperty("SERVER", para.Server); pPropSet.SetProperty("INSTANCE", para.Instance); pPropSet.SetProperty("USER", para.User); pPropSet.SetProperty("PASSWORD", para.Password); pPropSet.SetProperty("VERSION", para.Version);//可选,缺省为SDE.DEFAULT版本 pPropSet.SetProperty("Database", para.Database); try { pWorkspace = pWorkspaceFactory.Open(pPropSet, 0) as IFeatureWorkspace; } catch (Exception ex) { return null; } return pWorkspace; } } /// <summary> /// 连接到SDE栅格数据工作空间 /// </summary> public class ConnectRasterWorkSpaceGet { private ConnectRasterWorkSpaceDef pDef; public ConnectRasterWorkSpaceGet(ConnectRasterWorkSpaceDef ConnDef) { pDef = ConnDef; } public IRasterWorkspaceEx GetSDEWorkspace() //依据pDef得到SDE的工作空间 { IWorkspaceFactory pWorkspaceFactory = new SdeWorkspaceFactoryClass(); IRasterWorkspaceEx pWorkspace; SDEConnectPara para = pDef.ConnectPara; IPropertySet pPropSet = new PropertySetClass(); pPropSet.SetProperty("SERVER", para.Server); pPropSet.SetProperty("INSTANCE", para.Instance); pPropSet.SetProperty("USER", para.User); pPropSet.SetProperty("PASSWORD", para.Password); pPropSet.SetProperty("VERSION", para.Version);//可选,缺省为SDE.DEFAULT版本 pPropSet.SetProperty("Database", para.Database); try { pWorkspace = pWorkspaceFactory.Open(pPropSet, 0) as IRasterWorkspaceEx; } catch (Exception ex) { return null; } return pWorkspace; } } #endregion #region 连接参数设置 public class SDEConnectParas { #region SDE连接参数 string Server = "gisserver"; string Instance = "5152/tcp"; string User = "sde"; string Pass = "sde"; string Database = "sde"; string Version = "SDE.DEFAULT"; #endregion public void GetSDEParameters(out string server, out string instance, out string user, out string password, out string database, out string version) { server = Server; instance = Instance; user = User; password = Pass; database = Database; version = Version; }//得到SDE连接参数 //public bool ReadFromFile() ////从连接参数txt文件读取属性库和SDE的连接关键参数,以及站点关键表和关键SDE要素 //{ // string lpszPathName = FilePath.GetApplicationDir(); // lpszPathName = lpszPathName + "//sdeconn.txt"; // StreamReader reader = null; // try // { // reader = new StreamReader(lpszPathName); // string line = reader.ReadLine(); // line = reader.ReadLine(); // string[] SubStrings = line.Split(':'); // Server = SubStrings[1]; // line = reader.ReadLine(); // SubStrings = line.Split(':'); // Instance = SubStrings[1]; // line = reader.ReadLine(); // SubStrings = line.Split(':'); // User = SubStrings[1]; // line = reader.ReadLine(); // SubStrings = line.Split(':'); // Pass = SubStrings[1]; // line = reader.ReadLine(); // SubStrings = line.Split(':'); // Database = SubStrings[1]; // line = reader.ReadLine(); // SubStrings = line.Split(':'); // Version = SubStrings[1]; // } // catch (IOException e) // { // Console.WriteLine(e.Message); // return false; // } // catch // { // return false; // } // finally // { // if (reader != null) reader.Close(); // } // return true; //} //public void WhiteToLogFile() ////写入连接参数txt文件-----属性库和SDE的连接关键参数,以及站点关键表和关键SDE要素 //{ // string lpszPathName = FilePath.GetCurrentDir(); // lpszPathName = lpszPathName + "//sdeconn.txt"; // StreamWriter writer = File.CreateText(lpszPathName); // writer.Write("SDE配置参数"); // writer.Write(writer.NewLine); // writer.Write("1、服务器:" + Server); // writer.Write(writer.NewLine); // writer.Write("2、SDE端口:" + Instance); // writer.Write(writer.NewLine); // writer.Write("3、用户:" + User); // writer.Write(writer.NewLine); // writer.Write("4、密码:" + Pass); // writer.Write(writer.NewLine); // writer.Write("5、数据库:" + Database); // writer.Write(writer.NewLine); // writer.Write("6、版本:" + Version); // writer.Write(writer.NewLine); // writer.Write(@"///////////////////////////////////////////"); // writer.Write(writer.NewLine); // writer.Close(); //} } #endregion } using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace Subsidence_Forecast_System.Class { class FilePath { string lpszPathName; /// <summary> /// 构造函数 /// </summary> /// <param name="PathName"></param> public FilePath(string PathName) { lpszPathName = PathName; } /// <summary> /// 设置文件全路径 /// </summary> /// <param name="PathName"></param> public void SetFilePath(string PathName) { lpszPathName = PathName; } /// <summary> /// 获取路径 /// </summary> /// <returns></returns> public string GetDir() { string Dir=Directory.GetParent(lpszPathName).ToString(); //if (Dir.Substring(Dir.Length - 1, 1) == ")//") Dir = Dir.Remove(Dir.Length - 1, 1); return Dir; } /// <summary> /// 获取文件名称 /// </summary> /// <returns></returns> public string GetFileName() { string fullpath = lpszPathName; string filename = Path.GetFileNameWithoutExtension(fullpath); return filename; //fullpath.Trim(); //int ipos = fullpath.LastIndexOf("//"); //if (ipos >!= 1) //{ // string fn = fullpath.Substring(ipos+1, fullpath.Length - ipos - 1); //} } } }