ArcObjects Library Reference  (Geometry)    

Share_a_line_segment

[Visual Basic 6.0]

This sample uses ITopologicalOperator::Intersect to do the equivalent of a select by location with the share a line segment operator.

Public Sub ShareLineSegment()
   
  Dim pMxDoc As IMxDocument
  Set pMxDoc = ThisDocument
  Dim pMap As IMap
  Set pMap = pMxDoc.FocusMap
  Dim pFL1 As IFeatureLayer, pFL2 As IFeatureLayer
  Set pFL1 = pMap.Layer(0)  ' first layer of map
  Set pFL2 = pMap.Layer(1)  ' second layer
  Dim pF1 As IFeature, pF2 As IFeature
  Set pF1 = pFL1.FeatureClass.GetFeature(1) ' get feature with ID=1 in first layer's feature class
 
  Dim pfs As IFeatureSelection
  Set pfs = pFL2
  Dim id As Long
  id = pfs.SelectionSet.IDs.Next  ' get first id in selection set an actual function would probably loop over the selection
 
  Set pF2 = pFL2.FeatureClass.GetFeature(id) ' this is line 2
 
 
'is there an intersection bw the two features
  Dim pTopo As ITopologicalOperator
  Set pTopo = pF1.Shape ' store the line 1 shape in the topo op
  If Not pTopo.IsSimple Then pTopo.Simplify
  Dim pGeom As IGeometry
  Set pGeom = pTopo.Intersect(pF2.Shape, esriGeometry1Dimension) ' get intersection with line 2
  If pGeom.IsEmpty Then
    MsgBox "The intersection of the 2 geometries is empty or consists of points"
    Exit Sub
  End If
 
' pGeom now contains the intersection of pf1 and pf2
' because dimension = esriGeometry1Dimension
' it is a geometry of type polyline
' if the intersection of the two features consisted of a point or points pgeom would be empty
' we could use ITopologicalOperator::Intersect with 0 dimension to find about those points

'if pGeom contains only two vertices then the intersection is a simple line segment
'3 vertices means it's a polyline with two successive segments
'>3 means we have several segments or a polyline or a combination of these
  Dim ppc1 As IPointCollection
  Set ppc1 = pGeom
  Dim l As Long, g As Long
  l = ppc1.PointCount
  If l = 2 Then
    MsgBox "The two features share a single line segment"
  ElseIf l = 3 Then
    MsgBox "The intersection of the two features is a polyline consisting of 2 segments"
  ElseIf l > 3 Then
    Dim pGeomColl As IGeometryCollection
    Set pGeomColl = pGeom
    g = pGeomColl.GeometryCount
    MsgBox "The intersection of the two features is made of " & g & " polyline(s)and/or segment(s)"
  End If

End Sub


[C#]
//This sample uses ITopologicalOperator::Intersect
//to do the equivalent of a select by location with the share a line segment operator.
public void ShareLineSegment()
{
  IMxDocument mxDocument = m_application.Document as IMxDocument;
  IMap map = mxDocument.FocusMap;
  //first layer of map
  IFeatureLayer firstLayer = map.get_Layer(0) as IFeatureLayer;
  if (firstLayer == null)
  {
    return;
  }
  // second layer
  IFeatureLayer secondLayer = map.get_Layer(1) as IFeatureLayer;
  if (secondLayer == null)
  {
    return;
  }
  //get feature with ID=1 in first layer's feature class
  IFeature feature1 = firstLayer.FeatureClass.GetFeature(1);
  if (feature1 == null)
  {
    return;
  }
  IFeatureSelection featureSelection = secondLayer as IFeatureSelection;
  //get first id in selection set an actual function would probably loop over the selection
  int id = featureSelection.SelectionSet.IDs.Next();
  if (id == -1)
  {
   return;
  }
  //this is line 2
  IFeature feature2 = secondLayer.FeatureClass.GetFeature(id);
  if (feature2 == null)
  {
    return;
  }
  //is there an intersection between the two features
  //store the line 1 shape in the topo op
  ITopologicalOperator topologicalOperator = feature1.Shape as ITopologicalOperator;
  if (!topologicalOperator.IsSimple)
  {
    topologicalOperator.Simplify();
  }
  //get intersection with line 2
  IGeometry geometry = topologicalOperator.Intersect(feature2.Shape, esriGeometryDimension.esriGeometry1Dimension);
  if (geometry.IsEmpty)
  {
    System.Windows.Forms.MessageBox.Show("The intersection of the 2 geometries is empty or consists of points");
    return;
  }
  //geometry now contains the intersection of feature1 and feature2
  //because dimension = esriGeometry1Dimension
  //it is a geometry of type polyline
  //if the intersection of the two features consisted of a point or points pgeom would be empty
  //we could use ITopologicalOperator::Intersect with 0 dimension to find about those points
  //if geometry contains only two vertices then the intersection is a simple line segment
  //3 vertices means it's a polyline with two successive segments
  //>3 means we have several segments or a polyline or a combination of these
  IPointCollection pointCollection = geometry as IPointCollection;
  int pointCount = pointCollection.PointCount;
  if (pointCount == 2)
  {
    System.Windows.Forms.MessageBox.Show("The two features share a single line segment");
  }
  else
  {
    if (pointCount == 3)
    {
      System.Windows.Forms.MessageBox.Show("The intersection of the two features is a polyline consisting of 2 segments");
    }
    else
    {
      if (pointCount > 3)
      {
      IGeometryCollection geometryCollection = geometry as IGeometryCollection;
      int geometryCount = geometryCollection.GeometryCount;
      System.Windows.Forms.MessageBox.Show("The intersection of the two features is made of " + geometryCount + " polyline(s)and/or segment(s)");
      }
    }
  }
}

[Visual Basic .NET, C++]
No example is available for Visual Basic .NET or C++. To view a Visual Basic 6.0 or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.