Showing posts with label ArcMap. Show all posts
Showing posts with label ArcMap. Show all posts

25.8.10

Site Selection with ArcObjects

In continuing my application development class through Penn State, I wrote a small site selection tool using C# and ArcObjects. The interesting part of that the application was stand-alone in contrast to the ArcMap tools we wrote in weeks past. I made it through the practice exercises no problem, but ran into some issues when writing the deliverable for the week. Here are some of those problems and solutions:

Referencing Map Layers
One of the bugs in my app which gave me the most trouble was related the order in which layers are loaded into the application. The problem was caused by the following code:

this.axMapControl1.AddLayerFromFile(dataPath + "counties.lyr");
pCntyLayer = this.axMapControl1.get_Layer(0) as IFeatureLayer;
pCntyFC = pCntyLayer.FeatureClass;

this.axMapControl1.AddLayerFromFile(dataPath + "recareas.lyr");
pRecLayer = this.axMapControl1.get_Layer(1) as IFeatureLayer;
pRecFC = pRecLayer.FeatureClass;

this.axMapControl1.AddLayerFromFile(dataPath + "interstates.lyr");
pInterLayer = this.axMapControl1.get_Layer(2) as IFeatureLayer;
pInterFC = pInterLayer.FeatureClass;

this.axMapControl1.AddLayerFromFile(dataPath + "cities.lyr");
pCityLayer = this.axMapControl1.get_Layer(3) as IFeatureLayer;
pCityFC = pCityLayer.FeatureClass;


Notice the index values in the get_Layer function. I initially expected the layer indexes to be the same as the order in which they were added to the map. This turned out to be wrong. Actually each layer was added to the beginning of the array, meaning that for the code above, each of the indexes should be 0.

This bug through me on a wild goose chance because it turned out I got the first layer index correct, so I didn't get a error message until I was further into writing application logic. In the code above, each of the layers will be the county layer. By coincidence this was the first layer I was doing operations one which cause additional confusion when operations on the cities layer didn't work.

Creating the Geometry Bag
When creating a geometry bag of features from Query Filter, I first attempted writting my own functions but quick found that the professors solution was more elegant. I translated the VBA code into the following C# function:


private IGeometryBag getGeomBag(IQueryFilter pQueryFilter, IFeatureClass pFClass)
{
IEnumGeometry pEnumGeom = new EnumFeatureGeometryClass();
IEnumGeometryBind pEnumGeomBind = pEnumGeom as IEnumGeometryBind;
pEnumGeomBind.BindGeometrySource(pQueryFilter, pFClass);
IGeometryFactory pGeomFactory = new GeometryEnvironmentClass();
return pGeomFactory.CreateGeometryFromEnumerator(pEnumGeom) as IGeometryBag;
}


This function applies the filter and return a nice geometry for additional site selection steps.

Conclusion
While I got my stand alone application working, I was unable to integrate it into excel and powerpoint. I am currently experimenting with VSTO (Visual Studio Tools for Office) to see if I find a solution.

31.7.10

Developing COM Components to Plug Into ArcMap - My Noobie Problems

After my initial experience writing the War card game in VB and C#, I've decided to try and stick with C# for the remainder of GEOG 489. The problem with this is that while the IDE is the same, I've quickly found differences in working with ArcObjects in C# vs. VB.

No COM Class Template

The first major difference I found was that C# does not have a built-in COM Class template. Duh! Luckily within about 10 minutes I found one at Oren Ellenbogen's blog which was written for C# 2005, but it seems to be working just fine for 2008. I place the zip file in 'Visual Studio 2008\Templates\ItemTemplates\Visual C#' and it worked like a charm.

No Stub

The next issue that I ran into was that for some reason C# was not generating the stub code for implementing the ICommand interface. I had noticed this on the last project, but had simply brute forced it because there were relatively few methods to implement. This time I learned that if you right-click on the window, you can tell the IDE to stub out the methods:


In my opinion, if you have to write out all the stub code then you might as well be working in notepad.

ArcMap Don't See My Component

After writing the code to create the custom zoom button, I had a more frightening problem of not seeing the command category appear in ArcMap. I was worried that this might take a while to troubleshoot, but I quickly discovered a couple of issues that may have been the source of my problem. The key to solving them was finding a long tread on the bytes.com forums discussing a similar problem. I believe that the key to solving my problem was:
  • Adding a blank constructor to the class (seemed pretty logical)
  • Going to Project -> Properties "Build" tab and selecting the "Register for COM Interop" option
These steps resolved my problem. I was then able to see the "Geog 489" category in ArcMap and the zoom tool worked with no problem.

No Modules in C#

When starting section III of the project, I was surprised that there was no option to add a "module" in the add item dialog. I soon realized that the idea of Modules is not part of C#, instead everything must be in its own class. In figuring out the best achieve a similar solution for re-using code (DRY...don't repeat yourself...or even better DIE...duplication is evil), it stumbled upon a forum thread discussing this exact issue. The post recommends creating a public sealed class. I found this worked well and I made the Zoom method static so I could call the function without actually instantiating the class. Here's what the class looks like: