Select to view content in your preferred language

How to set time property of layer programmatically? (C#)

1430
1
10-23-2020 01:30 PM
JohnWileczek
New Contributor

How do you set the 'Time' properties of a layer programmatically using the .NET ArcGIS Pro SDK? Attempting to set the 'Layer Time' and 'Time Field' properties of a layer when loaded automatically through code, but not seeing how these properties are linked to the underlying FeatureLayer or FeatureClass. Has anyone figured out how to do this, or if it is supported through the SDK?

Thanks!

1 Reply
by Anonymous User
Not applicable

Hello John, currently setting the time property is accomplished through the CIM.  Sample code below.

For more information on using the CIM, see this Dev Summit session - slides and video.

 

        CIMBaseLayer layerDefinition = layer.GetDefinition();

        CIMTimeDataDefinition timeDefinition = null;
        CIMTimeDisplayDefinition timeDisplayDefinition = null;
        CIMTimeTableDefinition timeFields = null;
        CIMTimeDimensionDefinition timeDimensionFields = null;

        if (layerDefinition is ICIMBasicFeatureLayer)
        {
          ICIMBasicFeatureLayer cimFeatureLayer = (ICIMBasicFeatureLayer)layerDefinition;
          CIMDisplayTable cimDisplayTable = cimFeatureLayer.FeatureTable as CIMDisplayTable;
          if (cimDisplayTable != null)
          {
            timeDefinition = cimDisplayTable.TimeDefinition;
            timeDisplayDefinition = cimDisplayTable.TimeDisplayDefinition;
            timeFields = cimDisplayTable.TimeFields;
            timeDimensionFields = cimDisplayTable.TimeDimensionFields;
          }
        }
        else if (layerDefinition is CIMRasterLayer)
        {
          CIMRasterLayer cimRasterLayer = (CIMRasterLayer)layerDefinition;
          timeDefinition = cimRasterLayer.TimeDefinition;
          timeDisplayDefinition = cimRasterLayer.TimeDisplayDefinition;
          timeDimensionFields = cimRasterLayer.TimeDimensionFields;
        }
        else if (layerDefinition is CIMMosaicLayer)
        {
          CIMMosaicLayer cimMosaicLayer = (CIMMosaicLayer)layerDefinition;
          timeDefinition = cimMosaicLayer.TimeDefinition;
          timeDisplayDefinition = cimMosaicLayer.TimeDisplayDefinition;
          timeFields = cimMosaicLayer.TimeFields;
        }

        if (timeDefinition == null)
          timeDefinition = new CIMTimeDataDefinition();
        if (timeDisplayDefinition == null)
          timeDisplayDefinition = new CIMTimeDisplayDefinition();
        if (timeFields == null)
          timeFields = new CIMTimeTableDefinition();
        if (timeDimensionFields == null)
          timeDimensionFields = new CIMTimeDimensionDefinition();

        // Set layer values

        TimeReference timeReference = null;
        TimeExtent timeExtent = new TimeExtent();
        timeExtent.StartTime = earliestLayerDateTime;
        timeExtent.StartTimeSpecified = true;
        timeExtent.EndTime = latestLayerDateTime;
        timeExtent.EndTimeSpecified = true;
        timeExtent.TimeReference = timeReference;

        timeDefinition.TimeReference = timeReference;
        timeDefinition.CustomTimeExtent = timeExtent;
        timeDefinition.UseTime = true;
        timeDefinition.HasLiveData = false;
        timeDefinition.TimeExtentCanChange = false;

        if (singleTimeField)
          timeFields.StartTimeField = TimeField.Value.ToString();
        else if (twoTimeFields)
        {
          timeFields.StartTimeField = startField
          timeFields.EndTimeField = endField;
        }
        else if (isTimeDimension)
          timeDimensionFields.TimeDimensionName = timeDimension;


        // optional time properties

        //timeDisplayDefinition.TimeOffset
        //timeDisplayDefinition.TimeOffsetUnits
        //timeDisplayDefinition.UniqueTimes
        //timeDisplayDefinition.TimeInterval
        //timeDisplayDefinition.TimeIntervalUnits

        if (layerDefinition is ICIMBasicFeatureLayer)
        {
          ICIMBasicFeatureLayer cimFeatureLayer = (ICIMBasicFeatureLayer)layerDefinition;
          CIMDisplayTable cimDisplayTable = cimFeatureLayer.FeatureTable as CIMDisplayTable;
          if (cimDisplayTable != null)
          {
            cimDisplayTable.TimeDefinition = timeDefinition;
            cimDisplayTable.TimeDisplayDefinition = timeDisplayDefinition;
            cimDisplayTable.TimeFields = timeFields;
            cimDisplayTable.TimeDimensionFields = timeDimensionFields;
          }
        }
        else if (layerDefinition is CIMRasterLayer)
        {
          CIMRasterLayer cimRasterLayer = (CIMRasterLayer)layerDefinition;
          cimRasterLayer.TimeDefinition = timeDefinition;
          cimRasterLayer.TimeDisplayDefinition = timeDisplayDefinition;
          cimRasterLayer.TimeDimensionFields = timeDimensionFields;
        }
        else if (layerDefinition is CIMMosaicLayer)
        {
          CIMMosaicLayer cimMosaicLayer = (CIMMosaicLayer)layerDefinition;
          cimMosaicLayer.TimeDefinition = timeDefinition;
          cimMosaicLayer.TimeDisplayDefinition = timeDisplayDefinition;
          cimMosaicLayer.TimeFields = timeFields;
       }

        layer.SetDefinition(layerDefinition);
0 Kudos