Subscribe Now: Feed Icon

Tuesday, November 15, 2011

ArcObjects: Workspace

This post is the forth post in my ArcObjects series.

As I have written in ArcObjects: Introduction, using ArcObjects is one way of performing CRUD operations against a Spatial DB (with SDE layer on top) and the way to do that is by using the intefaces of WorkspaceClass.

ESRI definition for the Workspace is this:

A Workspace is a container of spatial and non-spatial datasets such as feature classes, raster datasets and tables. It provides methods to instantiate existing datasets and to create new datasets.

Mine is a bit more simple, the WorkspaceClass is the class that does all the “work” within the “space” it is defined to work at. The Space section of the definition can be a DB Sde, File Geodatabase, Cad files and anything else ArcDesktop applications can open. The Work section refers to any basic action that can be done with ArcDesktop applications.

For example: IFeatureWorkspace allows you to create a new Feature Table, open an existing Feature Table and with the help of IFeatureClass perform CRUD operations on that table.

One important note however is that you cannot create a new instance of it, it must be returned for you – the next post will be on getting the workspace.

WorkspaceClass-Object

As you can see the WorkspaceClass implements a lot of interfaces, the good news is you don’t need most of them (I have only found usage for two of them).

What do you need then?

  1. IFeatureWorkspace – probably the most important interface, will allow you to edit features in the Space section
  2. IWorkspaceDomains – allows the creation, deletion of domains (important only if you want to automate the deployment process and using domains)

So how do we use it?

  1. protected internal readonly IFeatureWorkspace _workspace;

  1. protected IFeatureClass GetFeatureClass(string layerName)
  2. {
  3.     return _workspace.OpenFeatureClass(layerName);
  4. }

This actually the only thing you can do with just the IFeatureWorkspace, but if you actually want to do something like  return all the feature in a layer you need to use the IFeatureClass:

  1. private const bool IsRecyclingCursorInGetFeatures = false;

  1. private IQueryFilter GetQueryFilter(string whereClause)
  2. {
  3.     return new QueryFilter {WhereClause = whereClause};
  4. }
  1. protected List<IFeature> GetLayerFeatures(string layerName)
  2. {
  3.     var result = new List<IFeature>();
  4.     var filter = GetQueryFilter(EmptyWhereClause);
  5.     var featureClass = GetFeatureClass(layerName);
  6.     using (var comReleaser = new ComReleaser())
  7.     {
  8.         var cursor = featureClass.Search(filter, IsRecyclingCursorInGetFeatures);
  9.         comReleaser.ManageLifetime(cursor);
  10.  
  11.         var feature = cursor.NextFeature();
  12.         while (feature != null)
  13.         {
  14.             result.Add(feature);
  15.             feature = cursor.NextFeature();
  16.         }
  17.     }
  18.     return result;
  19. }

So what do we have here?

line 4: creates the IQueryFilter – this goes into the where clause of the SQL we will send to DB.

line 5: we get the IFeatureClass of the layer

line 6 and 9: this is important ArcObjects use Com which is a resource, every resource we use must be disposed. At kine 6 we create the disposing class and in line 9 we attach the cursor to that disposing class, when line 17 arrives the using statement will call the Dispose method for ComReleaser  and automatically dispose our cursor. More on cursor and disposing of the resources will be written in a later post.

line 8: creates the cursor that we will use to iterate the rows of the DB, IsRecyclingCursorInGetFeatures defines the way the cursor works (more on that on a later post)

lines 12-16: iterating the data

 

And that’s it.

IceRocket Tags: ,