Buffering using VBA

2553
8
10-30-2013 06:48 PM
RamaGangavall
New Contributor
I am using the following code to draw a 1 mile buffer around a polygon. When I measure the distance from the edge of the polygon to the buffer, its not accurate. North and South are fine, but East and West are off.

Dim pMxDoc As IMxDocument
  Dim pActiveView As IActiveView
  Dim pGraphicsContainer As IGraphicsContainer
  Dim pEnumFeature As IEnumFeature
  Dim pFeature As IFeature
  Dim pTopoOp As ITopologicalOperator
  Dim pElement As IElement
  Dim strBufferDistance As String
 
  Set pMxDoc = Application.Document
  Set pActiveView = pMxDoc.FocusMap
  Set pGraphicsContainer = pMxDoc.FocusMap
 
  'Verify there is a feature selection
  If pMxDoc.FocusMap.SelectionCount = 0 Then Exit Sub
 
  'Get a buffer distance from the user
  strBufferDistance = InputBox("Enter Distance:", "Buffer")
  If strBufferDistance = "" Or Not IsNumeric(strBufferDistance) Then Exit Sub
 
  'Buffer all the selected features by the BufferDistance
  'and create a new polygon element from each result
  Set pEnumFeature = pMxDoc.FocusMap.FeatureSelection
  pEnumFeature.Reset
  Set pFeature = pEnumFeature.Next
  Do While Not pFeature Is Nothing
    Set pTopoOp = pFeature.Shape
    Set pElement = New PolygonElement
    pElement.Geometry = pTopoOp.Buffer(CInt(strBufferDistance))
    pGraphicsContainer.AddElement pElement, 0
    Set pFeature = pEnumFeature.Next
  Loop
 
  'Redraw the graphics
0 Kudos
8 Replies
NeilClemmons
Regular Contributor III
I notice that you aren't giving your graphic element a spatial reference.  Without a spatial reference defined, it's quite possible that ArcMap will not properly project the element.  Have you determined that this is not a spatial reference issue?  I've been using ITopoOp.Buffer for over a decade and have never had any problems with it.
0 Kudos
MelitaKennedy
Esri Notable Contributor
If you're using the measure tool, check what type of distance it's returning. A problem with east-west distances implies to me that the geodesic distance is being returned.

Melita
0 Kudos
RamaGangavall
New Contributor
I notice that you aren't giving your graphic element a spatial reference.  Without a spatial reference defined, it's quite possible that ArcMap will not properly project the element.  Have you determined that this is not a spatial reference issue?  I've been using ITopoOp.Buffer for over a decade and have never had any problems with it.


Modified my code to the following, but still it doesn't work. When I measure the distance using the measuring tool, E is 0.75 miles and W is 0.78 miles, but N and S returns 1 mile.


Dim pMxDoc As IMxDocument
  Dim pxMap As IMap
  Dim pActiveView As IActiveView
  Dim pGraphicsContainer As IGraphicsContainer
  Dim pEnumFeature As IEnumFeature
  Dim pFeature As iFeature
  Dim pTopoOp As ITopologicalOperator
  Dim pElement As IElement
  Dim strBufferDistance As String
 
  Set pMxDoc = ThisDocument
  Set pxMap = pMxDoc.FocusMap
 
 
  Dim pSpatRefFact As ISpatialReferenceFactory
  Set pSpatRefFact = New SpatialReferenceEnvironment
  Dim pspatref As ISpatialReference
  Set pspatref = pSpatRefFact.CreateESRISpatialReferenceFromPRJFile("NAD83.prj")
   
  Set pxMap.SpatialReference = pspatref
 
  Set pActiveView = pxMap
  Set pGraphicsContainer = pMxDoc.FocusMap
 
  'Verify there is a feature selection
  If pMxDoc.FocusMap.SelectionCount = 0 Then Exit Sub
 
  Set pEnumFeature = pMxDoc.FocusMap.FeatureSelection
  pEnumFeature.Reset
  Set pFeature = pEnumFeature.Next
  Do While Not pFeature Is Nothing
    Set pTopoOp = pFeature.Shape
    Set pElement = New PolygonElement
    pElement.Geometry = pTopoOp.Buffer(ConvertUnits(1#, esriMiles, esriDecimalDegrees))
    pGraphicsContainer.AddElement pElement, 0
    Set pFeature = pEnumFeature.Next
  Loop
 
  'Redraw the graphics
  pActiveView.PartialRefresh esriViewGraphics, Nothing, Nothing
0 Kudos
RamaGangavall
New Contributor
If you're using the measure tool, check what type of distance it's returning. A problem with east-west distances implies to me that the geodesic distance is being returned.

Melita


It returns me in miles.
0 Kudos
MelitaKennedy
Esri Notable Contributor
The geodesic distance is the distance on the ellipsoid/spheroid. It will be in linear units like miles or meters.

The ConvertUnits is making an assumption to convert miles to degrees, probably using a mile at the equator to calculate the equivalent length in degrees. When you check that with the measure tool, the fact that the meridians (longitude lines) converge toward the pole means that the east-west distance is shorter than the north-south distance.  My guess, based on the difference in the lengths, is that the data is at approximately 41 North latitude.

Melita
0 Kudos
RamaGangavall
New Contributor
Attaching a couple of screenshots of the buffers. East is measured at 0.75 mile. and North at 1mile. This is using NAD83 projection. But when I change the projection to Mercator, East and West measure fine, but North and South are off.
0 Kudos
NeilClemmons
Regular Contributor III
You should reread Melita's last post.  Your screenshots clearly indicate you are looking at the geodesic distance.
0 Kudos
RamaGangavall
New Contributor
Once I switched to the right Projection (UTM) it worked. Thanks for your replies.
0 Kudos