Tuesday, January 20, 2009

Converting geometry from 2D to 3D

One of the common issues with arises as most GISs move from the 2D world to 3D is how to update their databases. While you can do this from ArcMap or ArcScene as shown in the help, sometimes you need a more fine tuned and automated way to accomplish this. In this entry I'll show you how to accomplish this using some ArcObjects code. The first thing you need to do is open a featureclass. This can be accomplished in many ways and it depends on whether you're doing it from Desktop, from some console app or any number of other ways. Therefore, I'll leave that part out. Once you have the featureclass and you've open an edit session, you can run the following code segment on a polyline or polygon because each of these kinds of geometries contains points (vertices):

IFeatureCursor someFeatureCursor = someFeatureclass.Search(null, false);

IFeature someFeature = null;

while ((someFeature = someFeatureCursor.NextFeature()) != null)


IPointCollection pointCollection = (IPointCollection)someFeature.Shape;

int i;

for (i = 0; i < pointCollection.PointCount; i++)


IPoint point = pointCollection.get_Point(i);

((IZAware)point).ZAware = true;

point.Z = 500;

pointCollection.UpdatePoint(i, point);

someFeature.Shape = (IGeometry)pointCollection;




In this example code segment notice that each feature is set to a constant but you can change this to a value above the surface by determining the surface elevation at that point’s location. Or, you could access an attribute that has values stored in meters and you need feet so you could convert them. There are any number of ways this level of control can help.

The key method used is UpdatePoint which updates the ith point using the newly altered 3D point. A couple of other notes: 1) Don’t forget to release the cursor using Marshall.FinalReleaseComObject and 2) Don’t forget to close the edit session and close the workspace. There are ample examples in the help to aid you in accomplishing this.

No comments:

Post a Comment