Polyline intersect on polygon edge

2979
6
08-16-2012 08:05 AM
DanielLaidlaw
New Contributor
I have a polyline that passes through a polygon, I???m trying to get the two intersect points on the polygon edge. If I try to get the fist and last of what is returned for the intersect point collection I get inconsistent results.

Does anyone have some input as what I might be doing wrong?  I have my code block below.

Appreciate the help
vb
 
 
  Polyline outGeometry = new Polyline();

            IGeometryCollection geometryCollection = null;
  if(geometryCollection == null) {
   IGeometryBag geometryBag = new GeometryBag();
   geometryCollection = (IGeometryCollection) geometryBag;
   geometryBag.setSpatialReferenceByRef(mySpatialRef));
  }


            // union all your polylines
  for(int i=0; i<featureList.size(); i++) {
   geometryCollection.addGeometry(featureList.get(i).getShape(), null, null);
  }
  outGeometry.constructUnion((IEnumGeometry) geometryCollection);   
  ((ITopologicalOperator) outGeometry).simplify(); 
  
  ITopologicalOperator topo ;
  geometry = ((ITopologicalOperator)geometry).getBoundary();
  topo = (ITopologicalOperator)geometry  ; 
  
  
IGeometry intersects = topo.intersect((IGeometry) outGeometry,    esriGeometryDimension.esriGeometry0Dimension);
  
  IPointCollection Pts = (IPointCollection)intersects;
  for (int j=0;j<Pts.getPointCount();j++){
   tempIntersectPointList.addPoint(Pts.getPoint(j), null, null);
  }


firstPointOnEdge = tempIntersectPointList.getPoint(0)
secondPointOnEdge = tempIntersectPointList.getPoint(tempIntersectPointList.getPointCount() - 1),

0 Kudos
6 Replies
KenBuja
MVP Esteemed Contributor
The way I worked with multiple points from an intersection in one of my applications was to use IGeometryCollection, since the intersection returns a multipoint geometry instead of a point geometry. When I tested for overlapping polygons or lines sharing segments, I used IGeometry instead. This shows how I write the intersections to a point feature class.

Dim pOverlapGeometryCollection As ESRI.ArcGIS.Geometry.IGeometryCollection = pTopoOp.Intersect(pInGeometry, ESRI.ArcGIS.Geometry.esriGeometryDimension.esriGeometry0Dimension)

If pOverlapGeometryCollection IsNot Nothing Then
    For j As Integer = 0 To pOverlapGeometryCollection.GeometryCount - 1
        pOverlapGeometry = pOverlapGeometryCollection.Geometry(j)
        pOverlapFeature = pOverlapBuffer
        pOverlapFeature.Shape = pOverlapGeometry
        pOverlapFeature.Value(pOverlapFeature.Fields.FindField(Field1)) = pFeature.OID
        pOverlapFeature.Value(pOverlapFeature.Fields.FindField(Field2)) = pSelFeature.OID
        pOverlapFCursor.InsertFeature(pOverlapBuffer)
    Next
End If
0 Kudos
DanielLaidlaw
New Contributor
I seem to get the intersecting points fine, my only issue is I cant seem to identify the point on the polygon edge. Getting the first and last point on the intersect point collection is not always the point on the edge of the polygon.  That's what I'm still tryingto figure out.
0 Kudos
DanielLaidlaw
New Contributor
Another question if someone knows the answer is why do I get so many points when I only use the polygon boundary to intersect the polyline. Shouldn�??t it just be two?
0 Kudos
KenBuja
MVP Esteemed Contributor
I seem to get the intersecting points fine, my only issue is I cant seem  to identify the point on the polygon edge. Getting the first and last  point on the intersect point collection is not always the point on the  edge of the polygon.  That's what I'm still tryingto figure out.        


It looks like it returns the points from the top to the bottom. I ran a test using the code below on a polygon and line, flashing each point in the geometry collection. The image below shows the direction the line was digitized by the arrows. The upper right intersection point was flashed first.

Another question if someone knows the answer is why do I get so many  points when I only use the polygon boundary to intersect the polyline.  Shouldn�??t it just be two?        


Is it a polygon with no internal holes? In the sample I ran, I used a donut polygon, which resulted in four intersections. If you look at the help for Boundary, you'll see that it will return all the rings of a polygon, not just the outside boundary.

            Dim pLayer As ESRI.ArcGIS.Carto.ILayer2
            Dim pLayer1 As ESRI.ArcGIS.Carto.ILayer2
            Dim pFLayer As ESRI.ArcGIS.Carto.IFeatureLayer
            Dim pFClass As ESRI.ArcGIS.Geodatabase.IFeatureClass
            Dim pFCursor As ESRI.ArcGIS.Geodatabase.IFeatureCursor
            Dim pFeature As ESRI.ArcGIS.Geodatabase.IFeature
            Dim pPolygonGeometry As ESRI.ArcGIS.Geometry.IGeometry
            Dim pLineGeometry As ESRI.ArcGIS.Geometry.IGeometry
            Dim pTopoOp As ESRI.ArcGIS.Geometry.ITopologicalOperator
            Dim pOverlapGeometryCollection As ESRI.ArcGIS.Geometry.IGeometryCollection
            Dim pOverlapGeometry As ESRI.ArcGIS.Geometry.IGeometry
            Dim pRgbColor As New ESRI.ArcGIS.Display.RgbColor

            Using releaser As New ESRI.ArcGIS.ADF.ComReleaser
                releaser.ManageLifetime(pFCursor)

                pLayer = My.ArcMap.Document.FocusMap.Layer(0)
                pFLayer = New ESRI.ArcGIS.Carto.FeatureLayer
                pFLayer = pLayer
                pFClass = pFLayer.FeatureClass
                pFCursor = pFClass.Search(Nothing, False)
                pFeature = pFCursor.NextFeature
                pLineGeometry = pFeature.Shape

                pLayer1 = My.ArcMap.Document.FocusMap.Layer(1)
                pFLayer = New ESRI.ArcGIS.Carto.FeatureLayer
                pFLayer = pLayer1
                pFClass = pFLayer.FeatureClass
                pFCursor = pFClass.Search(Nothing, False)
                pFeature = pFCursor.NextFeature
                pPolygonGeometry = pFeature.Shape

                pTopoOp = pPolygonGeometry
                pOverlapGeometryCollection = pTopoOp.Intersect(pLineGeometry, ESRI.ArcGIS.Geometry.esriGeometryDimension.esriGeometry0Dimension)

                MsgBox(pOverlapGeometryCollection.GeometryCount)
                pRgbColor.Red = 255

                For j As Integer = 0 To pOverlapGeometryCollection.GeometryCount - 1
                    pOverlapGeometry = pOverlapGeometryCollection.Geometry(j)
                    FlashGeometry(pOverlapGeometry, pRgbColor, My.ArcMap.Document.ActiveView.ScreenDisplay, 500) ' this is an ArcGIS snippet
                Next
           End Using


[ATTACH=CONFIG]17019[/ATTACH]
0 Kudos
DanielLaidlaw
New Contributor
Its all comming together , yes I have a polygon with no internal holes. I though the getBoundary() returns a polygon with the donut hole...  So my question would be how do I get just the external ring of the polygon?

vb
0 Kudos
KenBuja
MVP Esteemed Contributor
Try the IPolygon4::ExteriorRingBag

Dim pPolygon As ESRI.ArcGIS.Geometry.IPolygon4 = pPolygonGeometry
Dim pGeoBag As ESRI.ArcGIS.Geometry.IGeometryBag = pPolygon.ExteriorRingBag
Dim pGeoCollection As ESRI.ArcGIS.Geometry.IGeometryCollection = pGeoBag

MsgBox(pGeoCollection.GeometryCount)
0 Kudos