Dynamic Segmentation/ Linear Referencing

540
3
Jump to solution
02-21-2012 05:27 AM
jamesborris
New Contributor III
Hi, Can anyone help me write a quick c# tool to query and display a segment of a route based on a TO and FROM measure? Please point me in the right direction or paste a quick code if you can. Thanks.
0 Kudos
1 Solution

Accepted Solutions
RichardFairhurst
MVP Honored Contributor
This code in VBA may do what you are wanting to do.

View solution in original post

0 Kudos
3 Replies
RichardFairhurst
MVP Honored Contributor
Hi, Can anyone help me write a quick c# tool to query and display a segment of a route based on a TO and FROM measure? Please point me in the right direction or paste a quick code if you can. Thanks.


There is nothing quick about programming LR interfaces. I don't write c# code, but have done LR programming in VBA.  To check out some of the interfaces you need to familiarize yourself with check out this thread.  You also have to test the To/From measure to find out if they are actually on your line and decide what to do if they are not.
0 Kudos
RichardFairhurst
MVP Honored Contributor
This code in VBA may do what you are wanting to do.
0 Kudos
jamesborris
New Contributor III
Thanks, This script worked perfectly. can anyone help with converting it to C# even the begining would help. Thanks.

Description:

This VBA code demonstrates how to find a line location along a given route feature. In this example a location between 22.35 and 1149.86 measured units on route 1 is found and the located line is drawn on the screen.

How to use:
Add a route (PolyLineM) layer to ArcMap called 'roads_route_hwy' (or change the code accordingly).
Paste the code into VBA.
Run the code.
Public Sub FindLineRouteLocation()
 
  '+++ VBA code that shows how to find a line location along a route
 
  On Error GoTo eh

  Dim pMxDoc As IMxDocument
  Dim pMap As IMap
  Set pMxDoc = ThisDocument
  Set pMap = pMxDoc.FocusMap

  '+++ Get the route feature class. It is called 'roads_route_hwy'.
  Dim pLayer As ILayer
  Dim pFLayer As IFeatureLayer
  Dim pRouteFc As IFeatureClass
  Dim i As Long
  For i = 0 To pMap.LayerCount - 1
    Set pLayer = pMap.Layer(i)
    If LCase(pLayer.Name) = "roads_route_hwy" Then
        If TypeOf pLayer Is IFeatureLayer Then
          Set pFLayer = pLayer
          Set pRouteFc = pFLayer.FeatureClass
          Exit For
        End If
    End If
  Next i
  If pRouteFc Is Nothing Then
    MsgBox "Could not find the route feature class", vbExclamation, "FindLineRouteLocation"
    Exit Sub
  End If
  
  '+++ Create a route locator. This is the object that knows how to find
  '+++ locations along a route.
  Dim pName As IName
  Dim pDS As IDataset
  Dim pRtLocName As IRouteLocatorName
  Dim pRtLoc As IRouteLocator
 
  Set pDS = pRouteFc '+++ A PolyLineM feature class
  Set pName = pDS.FullName
  Set pRtLocName = New RouteMeasureLocatorName
  With pRtLocName
    Set .RouteFeatureClassName = pName
    .RouteIDFieldName = "rkey"
    .RouteIDIsUnique = True
    .RouteMeasureUnit = esriMeters
    .RouteWhereClause = ""
  End With
  Set pName = pRtLocName
  Set pRtLoc = pName.Open
 
  '+++ Create a route location
  Dim pRouteLoc As IRouteLocation
  Dim pRMLineLoc As IRouteMeasureLineLocation
  Set pRouteLoc = New RouteMeasureLineLocation
  With pRouteLoc
    .MeasureUnit = esriMeters
    .RouteID = 1  '+++ the route key
    .LateralOffset = 0
  End With
  Set pRMLineLoc = pRouteLoc
  pRMLineLoc.FromMeasure = 22.35
  pRMLineLoc.ToMeasure = 1149.86
 
  '+++ Locate the line (it's actually a PolyLine)
  Dim pGeom As IGeometry
  Dim LocError As esriLocatingError
  pRtLoc.Locate pRMLineLoc, pGeom, LocError
 
  '+++ Draw the PolyLine as a graphic on the screen
  If Not pGeom Is Nothing And Not pGeom.IsEmpty Then
    Dim pGCont As IGraphicsContainer
    Dim pGraphicsLayer As IGraphicsLayer
    Dim pLElement As ILineElement
    Dim pElement As IElement
    Dim pActive As IActiveView
    Set pGraphicsLayer = pMap.BasicGraphicsLayer
    Set pGCont = pGraphicsLayer
    Set pActive = pMxDoc.ActiveView
    Set pLElement = New LineElement
    Set pElement = pLElement
    Dim pLineSymbol As ILineSymbol
    Set pLineSymbol = New SimpleLineSymbol
    pLineSymbol.Width = 2
    pLElement.Symbol = pLineSymbol
    pElement.Geometry = pGeom
    pGCont.AddElement pElement, 0
    pActive.Refresh
  Else
    MsgBox "Line not found", vbExclamation, "FindLineRouteLocation"
    Exit Sub
  End If
  Exit Sub
 
eh:
  Dim lNum As Long, sDesc As String, sSrc As String
  lNum = Err.Number
  sDesc = Err.Description
  sSrc = Err.source
  Err.Raise lNum, sSrc, sDesc
 
End Sub
0 Kudos