Get segment of polyline between 2 points

Discussion created by gbushek on May 3, 2012
Hi. I have 2 points that were obtained from intersecting street 2 with street 1 and then street 3 with street 1.  Now I want to get the segment of street 1 that falls between those 2 points.  Is there an easy way to do this in the silverlight API?  I'm looking for some good ideas on which direction to go; whether there are geometry service utilities in the API to handle this or if there are toolbox tools I should be utilizing within a custom geoprocessing service. Any guidance is greatly appreciated.  The old code for this process was written in 9.1 ADF/ArcObjects using IPath.QueryPointAndDistance() and IPath.GetSubCurve() and somehow I need to accomplish the same thing in the Silverlight API.  I included the old code below simply to show the complexity that was being handled in the old code written several years ago.

Thanks, Gary

The old code looks like this:

            ...Pts is an array that typically will hold 2 points found by interesting street 2 with street 1 and street 3 with street 1.
            ...pTopo is the street 1 geometry
            ...pT is the final polyline segment returned

            Dim pLine As IGeometryCollection = CType(glb.SC.CreateObject("esriGeometry.Polyline"), IGeometryCollection)
            'Now we have all the points. Find the points of the segment lines.
            Dim PathNum(Pts.Count) As Integer
            Dim Dist(Pts.Count) As Double
            For I = 0 To Pts.Count - 1
                PathNum(I) = -1

            Dim pPoint As IPoint = CType(glb.SC.CreateObject("esriGeometry.Point"), IPoint)
            Dim GeoCol As IGeometryCollection = CType(pTopo, IGeometryCollection)
            Dim path As IPath
            Dim DistAlongCurve As Double
            Dim DistanceFromCurve As Double
            Dim RightSide As Boolean

            For I = 0 To GeoCol.GeometryCount - 1
                path = CType(GeoCol.Geometry(I), IPath)

                For J = 0 To Pts.Count - 1
                    If PathNum(J) = -1 Then
                        path.QueryPointAndDistance(esriSegmentExtension.esriNoExtension, CType(Pts(J), IPoint), False, pPoint, DistAlongCurve, DistanceFromCurve, RightSide)
                        If Math.Abs(DistanceFromCurve) < 10 Then
                            PathNum(J) = I
                            Dist(J) = DistAlongCurve
                        End If
                    End If

                'Find start and end point on this segment
                Dim StartPt As IPoint = Nothing
                Dim EndPt As IPoint = Nothing
                Dim MinDist As Double = 99999999
                Dim MaxDist As Double = -1
                For J = 0 To Pts.Count - 1
                    If PathNum(J) = I Then
                        If Dist(J) < MinDist Then
                            StartPt = CType(Pts(J), IPoint)
                            MinDist = Dist(J)
                        End If
                        If Dist(J) > MaxDist Then
                            EndPt = CType(Pts(J), IPoint)
                            MaxDist = Dist(J)
                        End If
                    End If

                If MinDist < MaxDist Then
                    'A peice of segment found
                    Dim pL3 As ICurve = CType(glb.SC.CreateObject("esriGeometry.Polyline"), ICurve)

                    path.GetSubcurve(MinDist, MaxDist, False, pL3)
                End If

            'When we exit this method, we do not need the complete shape, so dispose it

            'If pLine has something return it, otherwise return nothing
            Dim pT As ITopologicalOperator = CType(pLine, ITopologicalOperator)
            Dim pL4 As IPolyline = CType(pLine, IPolyline)
            If pL4.IsEmpty Then
                Return Nothing
            End If
            pL4.SpatialReference = glb.MapInfo.SpatialReference

            Return pT