Skip to content

ProSnippets LinearReferencing

UmaHarano edited this page Nov 12, 2025 · 1 revision
Language:              C#  
Subject:               LinearReferencing  
Contributor:           ArcGIS Pro SDK Team <[email protected]>  
Organization:          Esri, http://www.esri.com  
Date:                  11/7/2025  
ArcGIS Pro:            3.6  
Visual Studio:         2022  

Create a routes feature class using the DDL

static void CreateRoutes(SchemaBuilder schemaBuilder)
  {
    FieldDescription routeIdDescription = FieldDescription.CreateIntegerField("RouteID");
    FieldDescription routeNameDescription = FieldDescription.CreateStringField("RouteName", 100);
    ShapeDescription shapeDescription = new ShapeDescription(GeometryType.Polyline, SpatialReferences.WGS84)
    { HasM = true, HasZ = false };

    FeatureClassDescription routeFeatureClassDescription = new FeatureClassDescription("Routes",
      new List<FieldDescription>() { routeIdDescription, routeNameDescription }, shapeDescription);

    //Create an M-enabled poly-line feature class 
    schemaBuilder.Create(routeFeatureClassDescription);
    schemaBuilder.Build();
  }

Create an events table using the DDL

static void CreateEvents(SchemaBuilder schemaBuilder)
  {
    FieldDescription routeIdDescription = FieldDescription.CreateIntegerField("RID");
    FieldDescription measureFieldDescription = new FieldDescription("Measure", FieldType.Double);
    FieldDescription offsetFieldDescription = FieldDescription.CreateIntegerField("OffsetValue");

    TableDescription eventTableDescription = new TableDescription("LR_EventTable",
      new List<FieldDescription>() { routeIdDescription, offsetFieldDescription, measureFieldDescription });

    schemaBuilder.Create(eventTableDescription);
    schemaBuilder.Build();
  }

Get route information from a polyline feature class with M-values

static void CreateRouteInfo(Geodatabase geodatabase, string routeFeatureClassName = "Roads", string routeIdFieldName = "RouteID")
  {
    using (FeatureClass routeFeatureClass = geodatabase.OpenDataset<FeatureClass>(routeFeatureClassName))
    using (FeatureClassDefinition routeFeatureClassDefinition = routeFeatureClass.GetDefinition())
    {
      if (routeFeatureClassDefinition.HasM())
      {
        RouteInfo routeInfo = new RouteInfo(routeFeatureClass, routeIdFieldName);

        // Process the routeInfo as needed ...
      }
    }
  }

Get event information

static void CreateEventInfo(Geodatabase geodatabase, string eventTableName = "Accidents", string routeIdFieldName = "RID", string measureFieldName = "Measure", string offsetFieldName = "Offset")
  {
    using (Table eventTable = geodatabase.OpenDataset<Table>(eventTableName))
    {
      PointEventInfo eventInfo = new PointEventInfo(eventTable, routeIdFieldName, measureFieldName, offsetFieldName);

      // Get event type: Point or Line type
      EventType eventTableType = eventInfo.EventType;
    }
  }

Create a RouteEventSource via dynamic segmentation process for point events

static void CreatePointEventSource(Geodatabase geodatabase, string routeFeatureClassName = "Roads", string eventTableName = "Accidents", string routeIdFieldName = "RID", string measureFieldName = "Measure")
  {
    using (FeatureClass routesFeatureClass = geodatabase.OpenDataset<FeatureClass>(routeFeatureClassName))
    using (Table eventsTable = geodatabase.OpenDataset<Table>(eventTableName))
    {
      RouteInfo routeInfo = new RouteInfo(routesFeatureClass, routeIdFieldName);
      EventInfo eventInfo = new PointEventInfo(eventsTable, routeIdFieldName, measureFieldName);
      RouteEventSourceOptions routeEventSourceOptions = new PointEventSourceOptions(AngleType.Tangent)
      { ComplementAngle = true };

      using (RouteEventSource routeEventSource = new RouteEventSource(routeInfo, eventInfo, routeEventSourceOptions))
      using (RouteEventSourceDefinition routeEventSourceDefinition = routeEventSource.GetDefinition())
      {
        // Locating errors 
        IReadOnlyList<RouteEventSourceError> errors = routeEventSource.GetErrors();

        // Route event source fields 
        IReadOnlyList<Field> routeEventSourceFields = routeEventSourceDefinition.GetFields();

        // Add RouteEventSource to the ArcGIS Pro map
        FeatureLayerCreationParams layerParams = new FeatureLayerCreationParams(routeEventSource)
        {
          Name = "RoadAccidents"
        };

        LayerFactory.Instance.CreateLayer<FeatureLayer>(layerParams, MapView.Active.Map);
      }
    }
  }

Create a RouteEventSource via dynamic segmentation process for line events

static void CreateLineEventSource(Geodatabase geodatabase, string routeFeatureClassName = "Roads", string eventTableName = "Accidents", string routeIdFieldName = "RID", string toMeasureFieldName = "toMeasure", string fromMeasureFieldName = "fromMeasure", string offsetFieldName = "Offset")
  {
    using (FeatureClass routesFeatureClass = geodatabase.OpenDataset<FeatureClass>(routeFeatureClassName))
    using (Table eventsTable = geodatabase.OpenDataset<Table>(eventTableName))
    {
      RouteInfo routeInfo = new RouteInfo(routesFeatureClass, routeIdFieldName);
      EventInfo eventInfo = new LineEventInfo(eventsTable, routeIdFieldName, fromMeasureFieldName, toMeasureFieldName, offsetFieldName);
      RouteEventSourceOptions routeEventSourceOptions = new LineEventSourceOptions() { IsPositiveOffsetOnRight = true };

      using (RouteEventSource routeEventSource = new RouteEventSource(routeInfo, eventInfo, routeEventSourceOptions))
      using (RouteEventSourceDefinition routeEventSourceDefinition = routeEventSource.GetDefinition())
      {
        // Locating errors 
        IReadOnlyList<RouteEventSourceError> errors = routeEventSource.GetErrors();

        // Route event source fields 
        IReadOnlyList<Field> routeEventSourceFields = routeEventSourceDefinition.GetFields();

        // Add RouteEventSource to the ArcGIS Pro map
        FeatureLayerCreationParams layerParams = new FeatureLayerCreationParams(routeEventSource)
        {
          Name = "HighCrashAreas"
        };

        LayerFactory.Instance.CreateLayer<FeatureLayer>(layerParams, MapView.Active.Map);
      }
    }
  }

Locate features along routes

static void LocateLineFeaturesAlongRoutes(Geodatabase geodatabase, string routeFeatureClassName = "Roads", string eventTableName = "Accidents", string routeIdFieldName = "RID", string toMeasureFieldName = "toMeasure", string fromMeasureFieldName = "fromMeasure")
  {
    // Configure events table
    EventTableConfiguration lineEventTableConfiguration = new LineEventTableConfiguration(eventTableName, routeIdFieldName, fromMeasureFieldName, toMeasureFieldName) { KeepAllFields = true, MDirectionOffset = true };

    using (FeatureClass routeFeatureClass = geodatabase.OpenDataset<FeatureClass>(routeFeatureClassName))
    using (FeatureClass highCrashAreaFeatureClass = geodatabase.OpenDataset<FeatureClass>("HighCrashRegion"))
    {
      RouteInfo routeInfo = new RouteInfo(routeFeatureClass, routeIdFieldName);

      // Creates an event table inside the geodatabase
      routeInfo.LocateFeatures(highCrashAreaFeatureClass, 0.5, lineEventTableConfiguration);

      // Open newly created event table
      using (Table eventTable = geodatabase.OpenDataset<Table>(eventTableName))
      {
        EventInfo eventInfo = new LineEventInfo(eventTable, routeIdFieldName, fromMeasureFieldName, toMeasureFieldName);

        // Create RouteEventSource using new event table
        using (RouteEventSource routeEventSource = new RouteEventSource(routeInfo, eventInfo, new LineEventSourceOptions()))
        {
          // Process the routeEventSource as needed ...
        }
      }
    }
  }

Clone this wiki locally