This code demonstrates how to create a new line event table by locating polygon features along routes. The new event table can be added to ArcMap as a layer using dynamic segmentation.
How to use
- Paste the code into VBA.
- Change the code to point to your data
- Run the code.
Public Sub LocatePolygonsAlongRoutes(sAccessWS As String, sRouteFC As String, sPolyFC As String, sRouteIDField As String, sOutEvents As String)
'+++ sAccessWS - access workspace
'+++ sRouteFC - the route feature class
'+++ sPolyFC - the polygon feature class
'+++ sRouteIDField - the route identifier field for sRouteFC
'+++ sOutEvents - the output event table
On Error GoTo eh
'+++ Get the polygon feature class and the route feature class. We'll assume that they come from the
'+++ same workspace.
Dim pWS As IWorkspace
Dim pWSF As IWorkspaceFactory
Dim pFWS As IFeatureWorkspace
Dim pRouteFC As IFeatureClass
Dim pPolyFC As IFeatureClass
Set pWSF = New ShapefileWorkspaceFactory
Set pWS = pWSF.OpenFromFile(sAccessWS, 0)
Set pFWS = pWS
Set pRouteFC = pFWS.OpenFeatureClass(sRouteFC)
Set pPolyFC = pFWS.OpenFeatureClass(sPolyFC)
'+++Set up a RouteMeasureLocator object.
Dim pTempName As IName
Dim pTempDs As IDataset
Dim pRMLocName As IRouteLocatorName
Dim pRtLoc As IRouteLocator
Set pTempDs = pRouteFC
Set pTempName = pTempDs.FullName
Set pRMLocName = New RouteMeasureLocatorName
With pRMLocName
Set .RouteFeatureClassName = pTempName
.RouteIDFieldName = sRouteIDField
.RouteIDIsUnique = True
End With
Set pTempName = pRMLocName
Set pRtLoc = pTempName.Open
'+++ Create an output table name object. We'll write to the same
'+++ workspace as the input routes and polygons
Dim pOutDSN As IDatasetName
Dim pOutWSN As IWorkspaceName
Set pTempDs = pWS
Set pOutWSN = pTempDs.FullName
Set pOutDSN = New TableName
Set pOutDSN.WorkspaceName = pOutWSN
pOutDSN.Name = sOutEvents 'this table should not exist already
'+++ Create RouteLocatorOperations object. Note that you can use a selection set of polygons. If you want to do this,
'+++ set the InputFeatureSelection property instead of the InputFeatureClass property.
Dim pRouteLocOps As IRouteLocatorOperations
Set pRouteLocOps = New RouteLocatorOperations
With pRouteLocOps
Set .RouteLocator = pRtLoc
Set .InputFeatureClass = pPolyFC
End With
'+++ Set event properties for the output line event table. The field names specified will be written to the
'+++ output table.
Dim pEventProps As IRouteEventProperties
Dim pRMLineProps As IRouteMeasureLineProperties
Set pEventProps = New RouteMeasureLineProperties
pEventProps.EventRouteIDFieldName = "RKEY"
Set pRMLineProps = pEventProps
pRMLineProps.FromMeasureFieldName = "FROM_M"
pRMLineProps.ToMeasureFieldName = "TO_M"
'+++ Locate the polygons along the routes
Dim pOutTable As ITable
Dim bKeepZero As Boolean
Dim bKeepAllFields As Boolean
bKeepZero = False 'do not keep events where FROM_M = TO_M
bKeepAllFields = True 'keep all of the input polygon feature class's attributes
Set pOutTable = pRouteLocOps.LocatePolygonFeatures(pEventProps, bKeepAllFields, bKeepZero, pOutDSN, "", Nothing)
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