ArcEngine开发代码

1、打开Shapefile

View Code
 1 private IFeatureLayer OpenShapeFile(string pPath)
 2         {
 3             IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactory(); // 1           
 4             string pFolder = System.IO.Path.GetDirectoryName(pPath);
 5             string pFileName = System.IO.Path.GetFileName(pPath);
 6             IWorkspace pWorkspace = pWorkspaceFactory.OpenFromFile(pFolder, 0); // 2
 7             IFeatureWorkspace pFeatureWorkspace = pWorkspace as IFeatureWorkspace;
 8             IFeatureClass pFC = pFeatureWorkspace.OpenFeatureClass(pFileName); //3
 9             IFeatureLayer pFLayer = new FeatureLayerClass(); // 4
10             pFLayer.FeatureClass = pFC;
11             pFLayer.Name = pFC.AliasName; // 5
12             return pFLayer;
13         }

2、进行空间查询

View Code
 1  private IFeatureCursor GetSpatialFeatureCursor(IFeatureLayer pFlayer, IGeometry pGeo)
 2         {
 3             IGeometry pointGemetry = pGeo;
 4             ISpatialFilter pointSpatialFilter = new SpatialFilterClass();
 5             pointSpatialFilter.Geometry = pointGemetry;
 6             pointSpatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
 7             pointSpatialFilter.GeometryField = "Shape";
 8             IFeatureLayer pointFeatureLayer = pFlayer;
 9             IFeatureClass pointFeatureClass = pointFeatureLayer.FeatureClass;
10             IFeatureCursor pointFeatureCursor = pointFeatureClass.Search(pointSpatialFilter, false);
11             return pointFeatureCursor;
12         }

3、进行属性查询

View Code
1  private IFeatureCursor GetQueryFeatureCursor(IFeatureLayer pFlayer, string whereclause)
2         {
3             IQueryFilter2 pQueryFilter = new QueryFilterClass();
4             pQueryFilter.WhereClause = whereclause;
5             IFeatureCursor pFeatureCursor;
6             pFeatureCursor = pFlayer.FeatureClass.Search(pQueryFilter, false);
7             return pFeatureCursor;           
8         }

4、绑定以及权限初始化类

View Code
  1  internal sealed class LicenseInitializer
  2     {
  3         private IAoInitialize m_AoInit = new AoInitializeClass();
  4 
  5         #region Private members
  6         private const string MessageNoLicensesRequested = "Product: No licenses were requested";
  7         private const string MessageProductAvailable = "Product: {0}: Available";
  8         private const string MessageProductNotLicensed = "Product: {0}: Not Licensed";
  9         private const string MessageExtensionAvailable = " Extension: {0}: Available";
 10         private const string MessageExtensionNotLicensed = " Extension: {0}: Not Licensed";
 11         private const string MessageExtensionFailed = " Extension: {0}: Failed";
 12         private const string MessageExtensionUnavailable = " Extension: {0}: Unavailable";
 13 
 14         private bool m_hasShutDown = false;
 15         private bool m_hasInitializeProduct = false;
 16 
 17         private List<int> m_requestedProducts;
 18         private List m_requestedExtensions;
 19         private Dictionary m_productStatus = new Dictionary();
 20         private Dictionary m_extensionStatus = new Dictionary();
 21 
 22         private bool m_productCheckOrdering = true; //default from low to high
 23         #endregion
 24 
 25         public bool InitializeApplication(esriLicenseProductCode[] productCodes, esriLicenseExtensionCode[] extensionLics)
 26         {
 27             //Cache product codes by enum int so can be sorted without custom sorter
 28             m_requestedProducts = new List<int>();
 29             foreach (esriLicenseProductCode code in productCodes)
 30             {
 31                 int requestCodeNum = Convert.ToInt32(code);
 32                 if (!m_requestedProducts.Contains(requestCodeNum))
 33                 {
 34                     m_requestedProducts.Add(requestCodeNum);
 35                 }
 36             }
 37 
 38             AddExtensions(extensionLics);
 39             return Initialize();
 40         }
 41 
 42         /// 
 43         /// A summary of the status of product and extensions initialization.
 44         /// 
 45         public string LicenseMessage()
 46         {
 47             string prodStatus = string.Empty;
 48             if (m_productStatus == null || m_productStatus.Count == 0)
 49             {
 50                 prodStatus = MessageNoLicensesRequested + Environment.NewLine;
 51             }
 52             else if (m_productStatus.ContainsValue(esriLicenseStatus.esriLicenseAlreadyInitialized)
 53                 || m_productStatus.ContainsValue(esriLicenseStatus.esriLicenseCheckedOut))
 54             {
 55                 prodStatus = ReportInformation(m_AoInit as ILicenseInformation,
 56                     m_AoInit.InitializedProduct(),
 57                     esriLicenseStatus.esriLicenseCheckedOut) + Environment.NewLine;
 58             }
 59             else
 60             {
 61                 //Failed...
 62                 foreach (KeyValuePair item in m_productStatus)
 63                 {
 64                     prodStatus += ReportInformation(m_AoInit as ILicenseInformation,
 65                         item.Key, item.Value) + Environment.NewLine;
 66                 }
 67             }
 68 
 69             string extStatus = string.Empty;
 70             foreach (KeyValuePair item in m_extensionStatus)
 71             {
 72                 string info = ReportInformation(m_AoInit as ILicenseInformation, item.Key, item.Value);
 73                 if (!string.IsNullOrEmpty(info))
 74                     extStatus += info + Environment.NewLine;
 75             }
 76 
 77             string status = prodStatus + extStatus;
 78             return status.Trim();
 79         }
 80 
 81         /// 
 82         /// Shuts down AoInitialize object and check back in extensions to ensure
 83         /// any ESRI libraries that have been used are unloaded in the correct order.
 84         /// 
 85         /// Once Shutdown has been called, you cannot re-initialize the product license
 86         /// and should not make any ArcObjects call.
 87         public void ShutdownApplication()
 88         {
 89             if (m_hasShutDown)
 90                 return;
 91 
 92             //Check back in extensions
 93             foreach (KeyValuePair item in m_extensionStatus)
 94             {
 95                 if (item.Value == esriLicenseStatus.esriLicenseCheckedOut)
 96                     m_AoInit.CheckInExtension(item.Key);
 97             }
 98 
 99             m_requestedProducts.Clear();
100             m_requestedExtensions.Clear();
101             m_extensionStatus.Clear();
102             m_productStatus.Clear();
103             m_AoInit.Shutdown();
104             m_hasShutDown = true;
105             //m_hasInitializeProduct = false;
106         }
107 
108         /// 
109         /// Indicates if the extension is currently checked out.
110         /// 
111         public bool IsExtensionCheckedOut(esriLicenseExtensionCode code)
112         {
113             return m_AoInit.IsExtensionCheckedOut(code);
114         }
115 
116         /// 
117         /// Set the extension(s) to be checked out for your ArcObjects code. 
118         /// 
119         public bool AddExtensions(params esriLicenseExtensionCode[] requestCodes)
120         {
121             if (m_requestedExtensions == null)
122                 m_requestedExtensions = new List();
123             foreach (esriLicenseExtensionCode code in requestCodes)
124             {
125                 if (!m_requestedExtensions.Contains(code))
126                     m_requestedExtensions.Add(code);
127             }
128 
129             if (m_hasInitializeProduct)
130                 return CheckOutLicenses(this.InitializedProduct);
131 
132             return false;
133         }
134 
135         /// 
136         /// Check in extension(s) when it is no longer needed.
137         /// 
138         public void RemoveExtensions(params esriLicenseExtensionCode[] requestCodes)
139         {
140             if (m_extensionStatus == null || m_extensionStatus.Count == 0)
141                 return;
142 
143             foreach (esriLicenseExtensionCode code in requestCodes)
144             {
145                 if (m_extensionStatus.ContainsKey(code))
146                 {
147                     if (m_AoInit.CheckInExtension(code) == esriLicenseStatus.esriLicenseCheckedIn)
148                     {
149                         m_extensionStatus[code] = esriLicenseStatus.esriLicenseCheckedIn;
150                     }
151                 }
152             }
153         }
154 
155         /// 
156         /// Get/Set the ordering of product code checking. If true, check from lowest to 
157         /// highest license. True by default.
158         /// 
159         public bool InitializeLowerProductFirst
160         {
161             get
162             {
163                 return m_productCheckOrdering;
164             }
165             set
166             {
167                 m_productCheckOrdering = value;
168             }
169         }
170 
171         /// 
172         /// Retrieves the product code initialized in the ArcObjects application
173         /// 
174         public esriLicenseProductCode InitializedProduct
175         {
176             get
177             {
178                 try
179                 {
180                     return m_AoInit.InitializedProduct();
181                 }
182                 catch
183                 {
184                     return 0;
185                 }
186             }
187         }
188 
189         #region Helper methods
190         private bool Initialize()
191         {
192             if (m_requestedProducts == null || m_requestedProducts.Count == 0)
193                 return false;
194 
195             esriLicenseProductCode currentProduct = new esriLicenseProductCode();
196             bool productInitialized = false;
197 
198             //Try to initialize a product
199             ILicenseInformation licInfo = (ILicenseInformation)m_AoInit;
200 
201             m_requestedProducts.Sort();
202             if (!InitializeLowerProductFirst) //Request license from highest to lowest
203                 m_requestedProducts.Reverse();
204 
205             foreach (int prodNumber in m_requestedProducts)
206             {
207                 esriLicenseProductCode prod = (esriLicenseProductCode)Enum.ToObject(typeof(esriLicenseProductCode), prodNumber);
208                 esriLicenseStatus status = m_AoInit.IsProductCodeAvailable(prod);
209                 if (status == esriLicenseStatus.esriLicenseAvailable)
210                 {
211                     status = m_AoInit.Initialize(prod);
212                     if (status == esriLicenseStatus.esriLicenseAlreadyInitialized ||
213                         status == esriLicenseStatus.esriLicenseCheckedOut)
214                     {
215                         productInitialized = true;
216                         currentProduct = m_AoInit.InitializedProduct();
217                     }
218                 }
219 
220                 m_productStatus.Add(prod, status);
221 
222                 if (productInitialized)
223                     break;
224             }
225 
226             m_hasInitializeProduct = productInitialized;
227             m_requestedProducts.Clear();
228 
229             //No product is initialized after trying all requested licenses, quit
230             if (!productInitialized)
231             {
232                 return false;
233             }
234 
235             //Check out extension licenses
236             return CheckOutLicenses(currentProduct);
237         }
238 
239         private bool CheckOutLicenses(esriLicenseProductCode currentProduct)
240         {
241             bool allSuccessful = true;
242             //Request extensions
243             if (m_requestedExtensions != null && currentProduct != 0)
244             {
245                 foreach (esriLicenseExtensionCode ext in m_requestedExtensions)
246                 {
247                     esriLicenseStatus licenseStatus = m_AoInit.IsExtensionCodeAvailable(currentProduct, ext);
248                     if (licenseStatus == esriLicenseStatus.esriLicenseAvailable)//skip unavailable extensions
249                     {
250                         licenseStatus = m_AoInit.CheckOutExtension(ext);
251                     }
252                     allSuccessful = (allSuccessful && licenseStatus == esriLicenseStatus.esriLicenseCheckedOut);
253                     if (m_extensionStatus.ContainsKey(ext))
254                         m_extensionStatus[ext] = licenseStatus;
255                     else
256                         m_extensionStatus.Add(ext, licenseStatus);
257                 }
258 
259                 m_requestedExtensions.Clear();
260             }
261 
262             return allSuccessful;
263         }
264 
265 
266         private string ReportInformation(ILicenseInformation licInfo,
267            esriLicenseProductCode code, esriLicenseStatus status)
268         {
269             string prodName = string.Empty;
270             try
271             {
272                 prodName = licInfo.GetLicenseProductName(code);
273             }
274             catch
275             {
276                 prodName = code.ToString();
277             }
278 
279             string statusInfo = string.Empty;
280 
281             switch (status)
282             {
283                 case esriLicenseStatus.esriLicenseAlreadyInitialized:
284                 case esriLicenseStatus.esriLicenseCheckedOut:
285                     statusInfo = string.Format(MessageProductAvailable, prodName);
286                     break;
287                 default:
288                     statusInfo = string.Format(MessageProductNotLicensed, prodName);
289                     break;
290             }
291 
292             return statusInfo;
293         }
294         private string ReportInformation(ILicenseInformation licInfo,
295             esriLicenseExtensionCode code, esriLicenseStatus status)
296         {
297             string extensionName = string.Empty;
298             try
299             {
300                 extensionName = licInfo.GetLicenseExtensionName(code);
301             }
302             catch
303             {
304                 extensionName = code.ToString();
305             }
306 
307             string statusInfo = string.Empty;
308 
309             switch (status)
310             {
311                 case esriLicenseStatus.esriLicenseAlreadyInitialized:
312                 case esriLicenseStatus.esriLicenseCheckedOut:
313                     statusInfo = string.Format(MessageExtensionAvailable, extensionName);
314                     break;
315                 case esriLicenseStatus.esriLicenseCheckedIn:
316                     break;
317                 case esriLicenseStatus.esriLicenseUnavailable:
318                     statusInfo = string.Format(MessageExtensionUnavailable, extensionName);
319                     break;
320                 case esriLicenseStatus.esriLicenseFailure:
321                     statusInfo = string.Format(MessageExtensionFailed, extensionName);
322                     break;
323                 default:
324                     statusInfo = string.Format(MessageExtensionNotLicensed, extensionName);
325                     break;
326             }
327 
328             return statusInfo;
329         }
330         #endregion
331 
332     }

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/xjzhangdc/archive/2013/03/29/2988358.html

你可能感兴趣的:(ArcEngine开发代码)