代码:
1 public class WorkspaceHelper 2 { 3 public static string GISConnectionString; 4 public static IWorkspace GetAccessWorkspace(string sFilePath) 5 { 6 if (!File.Exists(sFilePath)) 7 { 8 return null; 9 } 10 try 11 { 12 IWorkspaceFactory factory = new AccessWorkspaceFactoryClass(); 13 return factory.OpenFromFile(sFilePath, 0); 14 } 15 catch 16 { 17 return null; 18 } 19 } 20 public static IWorkspace GetSDEWorkspace(string sServerName, string sInstancePort, string sUserName, string sPassword, string sVersionName) 21 { 22 IPropertySet set = new PropertySetClass(); 23 set.SetProperty("Server", sServerName); 24 set.SetProperty("Instance", sInstancePort); 25 set.SetProperty("User", sUserName); 26 set.SetProperty("password", sPassword); 27 set.SetProperty("version", sVersionName); 28 SdeWorkspaceFactoryClass class2 = new SdeWorkspaceFactoryClass(); 29 try 30 { 31 return class2.Open(set, 0); 32 } 33 catch (Exception ex) 34 { 35 return null; 36 } 37 } 38 public static IWorkspace GetFGDBWorkspace(string sFilePath) 39 { 40 if (!System.IO.Directory.Exists(sFilePath)) 41 { 42 return null; 43 } 44 try 45 { 46 IWorkspaceFactory factory = new FileGDBWorkspaceFactoryClass(); 47 return factory.OpenFromFile(sFilePath,0); 48 } 49 catch 50 { 51 return null; 52 } 53 } 54 public static IWorkspace GetShapefileWorkspace(string sFilePath) 55 { 56 if (!File.Exists(sFilePath)) 57 { 58 return null; 59 } 60 try 61 { 62 IWorkspaceFactory factory = new ShapefileWorkspaceFactoryClass(); 63 sFilePath = System.IO.Path.GetDirectoryName(sFilePath); 64 return factory.OpenFromFile(sFilePath, 0); 65 } 66 catch 67 { 68 return null; 69 } 70 } 71 public static string PGDBDataConnectionString(string sPath) 72 { 73 return ("Provider=ESRI.GeoDB.OLEDB.1;Data Source=" + sPath + ";Extended Properties=workspacetype=esriDataSourcesGDB.AccessWorkspaceFactory.1;Geometry=WKB"); 74 } 75 public static string SDEDataConnectionString(string sServerName, string sDataSource, string sUserName, string sPW) 76 { 77 return ("Provider=ESRI.GeoDB.OLEDB.1;Location=" + sServerName + ";Data Source=" + sDataSource + "; User Id=" + sUserName + ";Password=" + sPW + "; Extended Properties=WorkspaceType= esriDataSourcesGDB.SDEWorkspaceFactory.1;Geometry=WKB|OBJECT;Instance=5151;Version=SDE.DEFAULT"); 78 } 79 public static string ShapefileDataConnectionString(string sPath) 80 { 81 sPath = System.IO.Path.GetDirectoryName(sPath); 82 return ("Provider=ESRI.GeoDB.OLEDB.1;Data Source=" + sPath + ";Extended Properties=WorkspaceType=esriDataSourcesFile.ShapefileWorkspaceFactory.1;Geometry=WKB|OBJECT"); 83 } 84 public static bool HighPrecision(IWorkspace pWorkspace) 85 { 86 IGeodatabaseRelease geoVersion = pWorkspace as IGeodatabaseRelease; 87 if (geoVersion == null) return false; 88 if (geoVersion.MajorVersion == 2 89 && geoVersion.MinorVersion == 2) 90 { 91 return true; 92 } 93 return false; 94 } 95 public static List<String> QueryFeatureClassName(IWorkspace pWorkspace) 96 { 97 return QueryFeatureClassName(pWorkspace, false, false); 98 } 99 public static List<String> QueryFeatureClassName(IWorkspace pWorkspace, bool pUpperCase) 100 { 101 return QueryFeatureClassName(pWorkspace, pUpperCase, false); 102 } 103 public static List<String> QueryFeatureClassName(IWorkspace pWorkspace, bool pUpperCase, bool pEscapeMetaTable) 104 { 105 try 106 { 107 String ownerName = ""; 108 if (pWorkspace.Type == esriWorkspaceType.esriRemoteDatabaseWorkspace) 109 { 110 ownerName = pWorkspace.ConnectionProperties.GetProperty("user").ToString(); 111 ownerName = ownerName.ToUpper(); 112 } 113 List<String> sc = new List<String>(); 114 IEnumDatasetName edn = pWorkspace.get_DatasetNames(esriDatasetType.esriDTFeatureDataset); 115 IDatasetName dn = edn.Next(); 116 while (dn != null) 117 { 118 string dsName = dn.Name.ToUpper(); 119 if (ownerName.Equals(LayerHelper.GetClassOwnerName(dsName))) 120 { 121 #region 添加数据集下面的FeatureClass 122 IEnumDatasetName fdn = dn.SubsetNames; 123 124 dn = fdn.Next(); 125 while (dn != null) 126 { 127 dsName = dn.Name.ToUpper(); 128 bool isTopology = dn is ITopologyName; 129 if (!isTopology) 130 { 131 string shortName = LayerHelper.GetClassShortName(dsName); 132 if (pUpperCase) 133 { 134 shortName = shortName.ToUpper(); 135 } 136 if (pEscapeMetaTable) 137 { 138 139 } 140 else 141 { 142 sc.Add(shortName); 143 } 144 } 145 dn = fdn.Next(); 146 } 147 #endregion 148 } 149 dn = edn.Next(); 150 } 151 #region 获取直接的FeatureClass 152 edn = pWorkspace.get_DatasetNames(esriDatasetType.esriDTFeatureClass); 153 dn = edn.Next(); 154 while (dn != null) 155 { 156 string dsName = dn.Name.ToUpper(); 157 if (ownerName.Equals(LayerHelper.GetClassOwnerName(dsName))) 158 { 159 string shortName = LayerHelper.GetClassShortName(dsName); 160 if (pUpperCase) 161 { 162 shortName = shortName.ToUpper(); 163 } 164 if (pEscapeMetaTable) 165 { 166 167 } 168 else 169 { 170 sc.Add(shortName); 171 } 172 } 173 dn = edn.Next(); 174 } 175 #endregion 176 return sc; 177 } 178 catch (Exception ex) { return null; } 179 } 180 public static List<IConfigurationKeyword> GetConfigurationKeywordList(IWorkspace pWS) 181 { 182 List<IConfigurationKeyword> pList = new List<IConfigurationKeyword>(); 183 IWorkspaceConfiguration pWConfig = pWS as IWorkspaceConfiguration; 184 IEnumConfigurationKeyword pEnumConfig = pWConfig.ConfigurationKeywords; 185 IConfigurationKeyword pConfig = pEnumConfig.Next(); 186 while (pConfig != null) 187 { 188 pList.Add(pConfig); 189 pConfig = pEnumConfig.Next(); 190 } 191 return pList; 192 } 193 public static List<IConfigurationParameter> GetConfigurationParameterList(IConfigurationKeyword pConfig) 194 { 195 List<IConfigurationParameter> pList = new List<IConfigurationParameter>(); 196 IEnumConfigurationParameter pEnumCP = pConfig.ConfigurationParameters; 197 IConfigurationParameter pCP = pEnumCP.Next(); 198 while (pCP != null) 199 { 200 pList.Add(pCP); 201 pCP = pEnumCP.Next(); 202 } 203 return pList; 204 } 205 }
时间: 2024-11-08 21:27:17