Intersect with ItopologicalOperator

2110
3
08-09-2010 02:02 PM
New Contributor II
Hello all.  I'm needing some help with intersecting polygons using itopologicaloperator.  I've given it a good shot, but cannot get it to work.  The idea is for a button where the user selects a polygon, then hits a button, which intersects the selected poly with an underlying soils layer, eventually providing a subset and various statistics therein.  Here' the code so far:

Public Overrides Sub OnClick()

        Dim pMxDoc As IMxDocument
        pMxDoc = m_application.Document

        Dim pActView As IActiveView = pMxDoc.ActiveView
        Dim screenDisplay As IScreenDisplay = pActView.ScreenDisplay

        Dim pEditor As IEditor
        Dim editorUID As ESRI.ArcGIS.esriSystem.UID = New ESRI.ArcGIS.esriSystem.UIDClass()
        editorUID.Value = "esriEditor.Editor"
        pEditor = m_application.FindExtensionByCLSID(editorUID)

        If pEditor.EditState <> esriEditState.esriStateEditing Then
            MsgBox("You must be in <EDIT> mode to use this tool!")
            Exit Sub
        End If

        Dim pMap As IMap = pMxDoc.FocusMap
        Dim pParPolyLayer As IFeatureLayer2 = FindLayer(pMap, "parcel_polygons")
        Dim pParFC As IFeatureClass = pParPolyLayer.FeatureClass

        Dim pParPolyFSel As IFeatureSelection = pParPolyLayer
        Dim pSelSet As ISelectionSet2 = pParPolyFSel.SelectionSet

        Dim pSoilsLayer As IFeatureLayer2 = FindLayer(pMap, "Soils")
        Dim pSoilsFC As IFeatureClass = pSoilsLayer.FeatureClass

        Dim pFCur As IFeatureCursor = Nothing
        'Dim pQF As IQueryFilter2
        'pQF = New QueryFilter

        Dim pSoilsCur As IFeatureCursor = Nothing

        Dim aColor As IRgbColor
        aColor = New RgbColorClass()

        aColor.Red = 133
        aColor.Green = 135
        aColor.Blue = 43

        Dim parFeat As IFeature, soilsFeat As IFeature
        Dim pGeom As IGeometry5
        Dim soilsPoly As IPolygon5
        Dim pTopoOp As ITopologicalOperator5 = New PolygonClass()
        Dim outPoly As IPolygon

        pSelSet.Search(Nothing, False, pFCur)
        parFeat = pFCur.NextFeature

        Try
            Do Until parFeat Is Nothing
                pGeom = parFeat.ShapeCopy
                pSoilsCur = PerformSpatialQuery(pSoilsFC, pGeom, esriSpatialRelEnum.esriSpatialRelIntersects, Nothing)
                soilsFeat = pSoilsCur.NextFeature

                Do Until soilsFeat Is Nothing

                    soilsPoly = soilsFeat.ShapeCopy
                    pTopoOp = soilsPoly

                    pTopoOp.Intersect(pGeom, esriGeometryDimension.esriGeometry2Dimension)
                    outPoly = pTopoOp

                    'FlashGeometry(pTopoOp, aColor, screenDisplay, 1500)
                    soilsFeat = pSoilsCur.NextFeature

                Loop

                parFeat = pFCur.NextFeature
            Loop
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

At this point, it doesn't seem to be doing anything.  I figured if I could get it to flash the geometries of the 'intersected' polys, I would be on my way.

Please help!!!
Reply
0 Kudos
3 Replies
New Contributor
Hi
I suppose the first question is whether your cursors are pointing to any features or not.  Are you actually getting to the line pTopoOp.Intersect(pGeom, esriGeometryDimension.esriGeometry2Dimension) with a valid geometry?  Or is this not being reached?
Reply
0 Kudos
New Contributor II
Thanks for your response.

Here's the latest version of the code:

Public Overrides Sub OnClick()

        Dim pMxDoc As IMxDocument
        pMxDoc = m_application.Document

        Dim pActView As IActiveView = pMxDoc.ActiveView
        Dim screenDisplay As IScreenDisplay = pActView.ScreenDisplay

        Dim pEditor As IEditor
        Dim editorUID As ESRI.ArcGIS.esriSystem.UID = New ESRI.ArcGIS.esriSystem.UIDClass()
        editorUID.Value = "esriEditor.Editor"
        pEditor = m_application.FindExtensionByCLSID(editorUID)

        If pEditor.EditState <> esriEditState.esriStateEditing Then
            MsgBox("You must be in <EDIT> mode to use this tool!")
            Exit Sub
        End If

        Dim pMap As IMap = pMxDoc.FocusMap
        Dim pParPolyLayer As IFeatureLayer2 = FindLayer(pMap, "parcel_polygons")
        Dim pParFC As IFeatureClass = pParPolyLayer.FeatureClass

        Dim pParPolyFSel As IFeatureSelection = pParPolyLayer
        Dim pSelSet As ISelectionSet2 = pParPolyFSel.SelectionSet

        Dim pSoilsLayer As IFeatureLayer2 = FindLayer(pMap, "Soils")
        Dim pSoilsFC As IFeatureClass = pSoilsLayer.FeatureClass

        Dim pFCur As IFeatureCursor = Nothing
        'Dim pQF As IQueryFilter2
        'pQF = New QueryFilter

        Dim pSoilsCur As IFeatureCursor = Nothing

        Dim aColor As IRgbColor
        aColor = New RgbColorClass()

        aColor.Red = 133
        aColor.Green = 135
        aColor.Blue = 43

        Dim parFeat As IFeature, soilsFeat As IFeature
        Dim pGeom As IGeometry5
        Dim soilsPoly As IPolygon5
        Dim pTopoOp As ITopologicalOperator5
        Dim outPoly As IPolygon

        pSelSet.Search(Nothing, False, pFCur)
        parFeat = pFCur.NextFeature

        Try
            Do Until parFeat Is Nothing
                pGeom = parFeat.ShapeCopy
                pSoilsCur = PerformSpatialQuery(pSoilsFC, pGeom, esriSpatialRelEnum.esriSpatialRelIntersects, Nothing)
                soilsFeat = pSoilsCur.NextFeature

                Do Until soilsFeat Is Nothing

                    soilsPoly = soilsFeat.ShapeCopy
                    pTopoOp = TryCast(soilsPoly, IPolygon)

                    outPoly = TryCast(pTopoOp.Intersect(pGeom, esriGeometryDimension.esriGeometry2Dimension), IPolygon)

                    'FlashGeometry(pTopoOp, aColor, screenDisplay, 1500)
                    soilsFeat = pSoilsCur.NextFeature

                Loop

                parFeat = pFCur.NextFeature
            Loop
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

I'm now getting a 'COM' error (please see attachment).  I know I'm getting a feature; I think my problem is with the QI from itopoOperator to iPolygon...

Any ideas?
Reply
0 Kudos
New Contributor
It seems that the ITopologicalOperator5 interface is implemented on the Polyline class and not the Polygon class, whereas ITopologicalOperator4 is implemented on the Polygon class.  I'm not sure whether the documentation is correct as it seems a bit odd, and it does recommend using ITopologicalOperator5 instead of ITopologicalOperator4.  Might be worth casting to ITopologicalOperator4 instead though and see if that makes any difference.

See
http://resources.esri.com/help/9.3/arcgisengine/ArcObjects/esriGeometry/ITopologicalOperator4.htm
and
http://resources.esri.com/help/9.3/arcgisengine/ArcObjects/esriGeometry/ITopologicalOperator5.htm
Reply
0 Kudos