Expand (or Buffer) Polygons

2667
9
Jump to solution
01-19-2012 07:08 AM
JustinConklin
New Contributor
I am trying to increase the size of polygons by 100 ft.  The polygons are squares 1mile x 1 mile and I essentially want to buffer them by 100 ft but keep the square edges.  The buffer tool has this option for lines but not polygons. 

I tried this
pEnv.XMax = pFeature.Shape.Envelope.XMax + 100
pEnv.XMin = pFeature.Shape.Envelope.XMin - 100
pEnv.YMax = pFeature.Shape.Envelope.YMax + 100
pEnv.YMin = pFeature.Shape.Envelope.YMin - 100

pFeature.shape = pEnv
pFeature.store

with no success

Also tried to simply expand pfeature.shape.envelope, but it doesn't update the shape itself only the envelope
Also tried to create a new feature and apply the desired shape through an envelope
Also tried to create a new polygon and add pPoly.addpoint with the 4 points but it won't create a new feature

Any help would be appreciated.  Thanks.
0 Kudos
1 Solution

Accepted Solutions
TimWhiteaker
Occasional Contributor II
Thanks for the help, Tim.  I implemented your suggestions.  The routine is still creating features with empty geometries.  Any idea why it would add a row to the table but not add take the geometry?  The shape field does show 'polygon' but when I select or try to zoom to one onf the new features - nothing.


Maybe you need to close the polygon by adding the first point to the end of the point collection.  This code worked for me.
            Dim pMap As IMap = My.ArcMap.Document.FocusMap             Dim pFLayer As IFeatureLayer = pMap.Layer(0)             Dim pFC As IFeatureClass = pFLayer.FeatureClass              Dim pCursor As IFeatureCursor = pFC.Search(Nothing, False)             Dim pFeature As IFeature = pCursor.NextFeature              Dim pEnv As IEnvelope             Dim pPoly As IPointCollection              Dim uid As ESRI.ArcGIS.esriSystem.UID             uid = New ESRI.ArcGIS.esriSystem.UIDClass()             uid.Value = "esriEditor.Editor"             Dim editor As IEditor             editor = CType(My.ArcMap.Application.FindExtensionByCLSID(uid), IEditor)              Dim pDS As IDataset = pFLayer.FeatureClass             Dim pWS As IWorkspace = pDS.Workspace              'Check to see if a workspace is already being edited.             If editor.EditState = esriEditState.esriStateNotEditing Then                 editor.StartEditing(pWS)             End If              Do Until pFeature Is Nothing                 ' Expand the envelope                 pEnv = pFeature.Shape.Envelope                 pEnv.Expand(100, 100, False)                  ' Create the vertices                 Dim LL As New Point                 LL.PutCoords(pEnv.XMin, pEnv.YMin)                 Dim LR As New Point                 LR.PutCoords(pEnv.XMax, pEnv.YMin)                 Dim UR As New Point                 UR.PutCoords(pEnv.XMax, pEnv.YMax)                 Dim UL As New Point                 UL.PutCoords(pEnv.XMin, pEnv.YMax)                  ' Add vertices to the polygon and close the loop                 pPoly = New Polygon                 pPoly.AddPoint(LL)                 pPoly.AddPoint(LR)                 pPoly.AddPoint(UR)                 pPoly.AddPoint(UL)                 pPoly.AddPoint(LL)                  ' Assign properties to the new feature                 Dim pNewFeat As IFeature = pFC.CreateFeature                 pNewFeat.Shape = pPoly                 pNewFeat.Value(3) = pFeature.Value(3)                 pNewFeat.Store()                  pFeature = pCursor.NextFeature             Loop              editor.StopEditing(True)             My.ArcMap.Document.ActiveView.Refresh()


By the way, consider using a ComReleaser to manage the lifetime of your cursor. 
http://blogs.esri.com/dev/blogs/geodatabase/archive/2008/12/18/using-the-comreleaser-to-manage-the-l...

If you are processing many features, consider using an insert cursor to improve performance.
http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#/d/00010000049v000000.ht...

View solution in original post

0 Kudos
9 Replies
JustinConklin
New Contributor
Here is a code sample that is getting close.  It will create the new features in the table but there is no feature associated with it.  Seems like it isn't taking the geometry. 






            Dim pMap As IMap = My.ArcMap.Document.FocusMap
            Dim pFLayer As IFeatureLayer = pMap.Layer(0)
            Dim pFC As IFeatureClass = pFLayer.FeatureClass

            Dim pCursor As IFeatureCursor = pFC.Search(Nothing, False)
            Dim pFeature As IFeature = pCursor.NextFeature

            Dim pEnv As New Envelope
            Dim pPoly As New Polygon


            Dim uid As ESRI.ArcGIS.esriSystem.UID
            uid = New ESRI.ArcGIS.esriSystem.UIDClass()
            uid.Value = "esriEditor.Editor"
            Dim editor As IEditor
            editor = CType(My.ArcMap.Application.FindExtensionByCLSID(uid), IEditor)

            Dim pDS As IDataset = pFLayer.FeatureClass
            Dim pWS As IWorkspace = pDS.Workspace

            'Check to see if a workspace is already being edited.
            If editor.EditState = esriEditState.esriStateNotEditing Then
                editor.StartEditing(pWS)
            End If



            Do Until pFeature Is Nothing
                Dim pNewFeat As IFeature = pFC.CreateFeature


                pEnv.XMax = pFeature.Shape.Envelope.XMax + 100
                pEnv.XMin = pFeature.Shape.Envelope.XMin - 100
                pEnv.YMax = pFeature.Shape.Envelope.YMax + 100
                pEnv.YMin = pFeature.Shape.Envelope.YMin - 100

                Dim LL As New Point
                LL.PutCoords(pEnv.XMin, pEnv.YMin)
                Dim LR As New Point
                LR.PutCoords(pEnv.XMax, pEnv.YMin)
                Dim UR As New Point
                UR.PutCoords(pEnv.XMax, pEnv.YMax)
                Dim UL As New Point
                UL.PutCoords(pEnv.XMin, pEnv.YMax)


                pNewFeat.Shape = pPoly
                pNewFeat.Value(3) = pFeature.Value(3)

                pNewFeat.Store()

                pFeature = pCursor.NextFeature

            Loop
            editor.StopEditing(True)
            My.ArcMap.Document.ActiveView.Refresh()
0 Kudos
sapnas
by
Occasional Contributor III
You could use ITopologicalOperator. Below is the link to sample code
http://resources.esri.com/help/9.3/ArcGISEngine/ArcObjects/esrigeometry/Buffer_Example.htm
0 Kudos
TimWhiteaker
Occasional Contributor II
A few changes to the code might do the trick.  Try dimming pPoly as IPointCollection instead of New Polygon.
Dim pPoly As IPointCollection


Then, in your loop, create a new polygon and add each corner point to it.

pPoly = New Polygon

Dim LL As New Point
LL.PutCoords(pEnv.XMin, pEnv.YMin)
pPoly.AddPoint(LL)
Dim LR As New Point
LR.PutCoords(pEnv.XMax, pEnv.YMin)
pPoly.AddPoint(LR)


...and so on.
0 Kudos
TimWhiteaker
Occasional Contributor II
Also, I recommend creating a new envelope for each iteration of the loop.  Otherwise, the envelope coordinates might do funny things if the new XMax+100 is less than the previous XMin. 

A more concise way to write it might be (inside the loop):

pEnv = pFeature.Shape.Envelope
pEnv.Expand(100, 100, False)


This copies the current feature's envelope and expands it in the x and y directions by 100.  Then you don't need the four lines of code like pEnv.XMax = pFeature.Shape.Envelope.XMax + 100.
0 Kudos
JustinConklin
New Contributor
Thanks for the help, Tim.  I implemented your suggestions.  The routine is still creating features with empty geometries.  Any idea why it would add a row to the table but not add take the geometry?  The shape field does show 'polygon' but when I select or try to zoom to one onf the new features - nothing.
0 Kudos
sapnas
by
Occasional Contributor III
If you are just adding a buffer to the existing polygon then you can use the below code. You may have to convert it to VB though.

ITopologicalOperator topologicalOperator = pFeature.Shape as  ITopologicalOperator;
IPolygon pPoly  = topologicalOperator.Buffer(100) as IPolygon;
pNewFeat.Shape = pPoly;
0 Kudos
TimWhiteaker
Occasional Contributor II
If you are just adding a buffer to the existing polygon then you can use the below code. You may have to convert it to VB though.

ITopologicalOperator topologicalOperator = pFeature.Shape as  ITopologicalOperator;
IPolygon pPoly  = topologicalOperator.Buffer(100) as IPolygon;
pNewFeat.Shape = pPoly;


Is there a way to force the topo op to preserve the square corners of the rectangles?
0 Kudos
TimWhiteaker
Occasional Contributor II
Thanks for the help, Tim.  I implemented your suggestions.  The routine is still creating features with empty geometries.  Any idea why it would add a row to the table but not add take the geometry?  The shape field does show 'polygon' but when I select or try to zoom to one onf the new features - nothing.


Maybe you need to close the polygon by adding the first point to the end of the point collection.  This code worked for me.
            Dim pMap As IMap = My.ArcMap.Document.FocusMap             Dim pFLayer As IFeatureLayer = pMap.Layer(0)             Dim pFC As IFeatureClass = pFLayer.FeatureClass              Dim pCursor As IFeatureCursor = pFC.Search(Nothing, False)             Dim pFeature As IFeature = pCursor.NextFeature              Dim pEnv As IEnvelope             Dim pPoly As IPointCollection              Dim uid As ESRI.ArcGIS.esriSystem.UID             uid = New ESRI.ArcGIS.esriSystem.UIDClass()             uid.Value = "esriEditor.Editor"             Dim editor As IEditor             editor = CType(My.ArcMap.Application.FindExtensionByCLSID(uid), IEditor)              Dim pDS As IDataset = pFLayer.FeatureClass             Dim pWS As IWorkspace = pDS.Workspace              'Check to see if a workspace is already being edited.             If editor.EditState = esriEditState.esriStateNotEditing Then                 editor.StartEditing(pWS)             End If              Do Until pFeature Is Nothing                 ' Expand the envelope                 pEnv = pFeature.Shape.Envelope                 pEnv.Expand(100, 100, False)                  ' Create the vertices                 Dim LL As New Point                 LL.PutCoords(pEnv.XMin, pEnv.YMin)                 Dim LR As New Point                 LR.PutCoords(pEnv.XMax, pEnv.YMin)                 Dim UR As New Point                 UR.PutCoords(pEnv.XMax, pEnv.YMax)                 Dim UL As New Point                 UL.PutCoords(pEnv.XMin, pEnv.YMax)                  ' Add vertices to the polygon and close the loop                 pPoly = New Polygon                 pPoly.AddPoint(LL)                 pPoly.AddPoint(LR)                 pPoly.AddPoint(UR)                 pPoly.AddPoint(UL)                 pPoly.AddPoint(LL)                  ' Assign properties to the new feature                 Dim pNewFeat As IFeature = pFC.CreateFeature                 pNewFeat.Shape = pPoly                 pNewFeat.Value(3) = pFeature.Value(3)                 pNewFeat.Store()                  pFeature = pCursor.NextFeature             Loop              editor.StopEditing(True)             My.ArcMap.Document.ActiveView.Refresh()


By the way, consider using a ComReleaser to manage the lifetime of your cursor. 
http://blogs.esri.com/dev/blogs/geodatabase/archive/2008/12/18/using-the-comreleaser-to-manage-the-l...

If you are processing many features, consider using an insert cursor to improve performance.
http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#/d/00010000049v000000.ht...
0 Kudos
JustinConklin
New Contributor
Maybe you need to close the polygon by adding the first point to the end of the point collection.  This code worked for me.


Yup, that did it.  Thanks a lot Tim.

Justin
0 Kudos