This code demonstrates how to create a route feature class using an existing line feature class. The input lines will be merged based on a common route identifier field.
The CreateUsing2Fields method is used in this sample. The CreateUsingCoordinatePriority option is also available.
When using CreateUsing2Fields, you must specify both a from- and a to-measure field. These fields are used to set the output route measures. It is important that all input lines are digitized in the direction of increasing measure. This is not the case with CreateUsingCoordinatePriority, where orientation of the ouput route is determined using a starting coordinate priority.
How to use
- Paste the code into your project.
- Modify the code to match your data paths, etc.
- Run the code.
Public Sub CreateRoutesUsing2Fields(sAccessWS As String, sLineFC As String, sOutRouteFC As String, sWhereClause As String, _
sRouteIDField As String, sFromMeasureField As String, sToMeasureField As String)
'+++ VARIABLES
'+++ sAccessWS - access workspace (i.e. "\\burt\data\dyndata\pitt.mdb")
'+++ sLineFC - the input line feature class ("base_roads")
'+++ sOutRouteFC - the output route feature class name ("routes")
'+++ sWhereClause - query filter for line feature class if needed (i.e. "[RKey] <> 0")
'+++ sRouteIDField - route ID field (i.e "Rkey")
'+++ sFromMeasureField - the from-measure field (i.e. "BegMP")
'+++ sToMeasureField - the to-measure field (i.e "EndMP")
On Error GoTo eh
'+++ Get the line feature class
Dim pWS As IWorkspace
Dim pWSF As IWorkspaceFactory
Dim pFWS As IFeatureWorkspace
Dim pLineFC As IFeatureClass
Set pWSF = New AccessWorkspaceFactory
Set pWS = pWSF.OpenFromFile(sAccessWS, 0)
Set pFWS = pWS
Set pLineFC = pFWS.OpenFeatureClass(sLineFC)
'+++ Create an output feature class name object. We'll write to a stand alone feature class in the
'+++ the same workspace as the inputs
Dim pOutFCN As IFeatureClassName
Dim pOutDSN As IDatasetName
Dim pOutWSN As IWorkspaceName
Dim sName As String
Dim pDS As IDataset
Set pDS = pWS
Set pOutWSN = pDS.FullName
Set pOutFCN = New FeatureClassname
Set pOutDSN = pOutFCN
Set pOutDSN.WorkspaceName = pOutWSN
pOutDSN.Name = sOutRouteFC 'This name should not already exist
'+++ Create a geometry definition for the new feature class. For the most part, we will copy the geometry
'+++ definition from the input lines. We'll explicitly set the M Domain, however. You should always set an
'+++ M Domain that is appropriate to your data. What is below is just a sample.
Dim pClone As IClone
Dim pGDef As IGeometryDef
Dim pGDefEdit As IGeometryDefEdit
Dim pSRef As ISpatialReference2
Dim pFlds As IFields
Dim pFld As IField
Dim i As Long
Set pFlds = pLineFC.Fields
i = pFlds.FindField(pLineFC.ShapeFieldName)
Set pFld = pFlds.Field(i)
Set pClone = pFld.GeometryDef
Set pGDef = pClone.Clone
Set pSRef = pGDef.SpatialReference
pSRef.SetMFalseOriginAndUnits -1000, 1000
'+++ Create a selection set to limit the number of lines that will be used to create routes
Dim pQFilt As IQueryFilter
Dim pSelSet As ISelectionSet2
Set pQFilt = New QueryFilter
pQFilt.WhereClause = sWhereClause
Set pSelSet = pLineFC.Select(pQFilt, esriSelectionTypeIDSet, esriSelectionOptionNormal, pWS)
'+++ Create a new RouteMeasureCreator object. Note that below, we use the selection set and not the
'+++ InputFeatureClass property
Dim pRouteCreator As IRouteMeasureCreator
Dim pErrors As IEnumBSTR
Set pRouteCreator = New RouteMeasureCreator
With pRouteCreator
Set .InputFeatureSelection = pSelSet
.InputRouteIDFieldName = sRouteIDField
End With
Set pErrors = pRouteCreator.CreateUsing2Fields(sFromMeasureField, sToMeasureField, pOutFCN, pGDef, "", Nothing)
'+++ The results of running CreatingUsing2Fields returns IEnumBSTR, which is a container
'+++ for a list of error strings indicating why certain lines could not be used to create routes.
Dim sError As String
sError = pErrors.Next
While Not Len(sError) = 0
Debug.Print sError
sError = pErrors.Next
Wend
Exit Sub
eh:
Dim lNum As Long, sSrc As String, sDesc As String
lNum = Err.Number
sSrc = Err.Source
sDesc = Err.Description
Err.Raise lNum, sSrc, sDesc
End Sub