How to change data source of Stereo Map

621
4
Jump to solution
09-13-2023 03:53 AM
BarbaraSchneider2
Occasional Contributor III

I'm using the following code to change the data source of the stereo map, but it doesn't work:

await QueuedTask.Run(async () =>
{
    Map stereoMap = await ProjectUtils.GetStereoMapAsync();
    CIMMap cimMap = stereoMap.GetDefinition();
    var stereoProps = cimMap.StereoProperties;

    WorkspaceFactory workspaceFactory = WorkspaceFactory.FileGDB;
    var fileGdbConnPath = new FileGeodatabaseConnectionPath(new Uri(@"D:\Data\StartProject.gdb", UriKind.Absolute));
    var workspaceConnectionString = new Geodatabase(fileGdbConnPath).GetConnectionString();

    CIMStandardDataConnection mosaicDataConnection = new  CIMStandardDataConnection()
    {
        WorkspaceConnectionString = workspaceConnectionString,
        WorkspaceFactory = workspaceFactory,
        Dataset = "MyMosaic",
        DatasetType = esriDatasetType.esriDTMosaicDataset
    };
    stereoProps.StereoModelCollection = mosaicDataConnection;
    cimMap.StereoProperties = stereoProps;

    MapView.Active.Redraw(true);
});

 Any help is appreciated, thank you!

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
NarelleChedzey
Esri Contributor

Hi Barbara, 

 

It looks like you have forgotten to save the updated CIM definition back to the map.   Remember that a GetDefinition call also needs a SetDefinition call after any updates. 

 

Try adding a  steroMap.SetDefinition call with your updated cimMap before you redraw. 

Narelle

 

View solution in original post

4 Replies
NarelleChedzey
Esri Contributor

Hi Barbara, 

 

It looks like you have forgotten to save the updated CIM definition back to the map.   Remember that a GetDefinition call also needs a SetDefinition call after any updates. 

 

Try adding a  steroMap.SetDefinition call with your updated cimMap before you redraw. 

Narelle

 

BarbaraSchneider2
Occasional Contributor III

Hi Narelle,

thank you for your reply! It works now. However, I can only set the data source if there already is a data source set. If I manually clear the data source of the Stereo Map and then add the data source again programmatically, nothing happens. Is this a bug?

Thank you,
Barbara

0 Kudos
NarelleChedzey
Esri Contributor

Barbara, 

If the data source has been cleared, then you probably need to also set other properties on the CIMMapStereoProperties instance.   

 

Try something similar to the following

protected override async void OnClick()
{
  Map stereoMap = MapView.Active.Map;

  await QueuedTask.Run(async () =>
  {
    CIMMap cimMap = stereoMap.GetDefinition();
    var stereoProps = cimMap.StereoProperties;

    string path = @"D:\Data\StartProject.gdb";
    var fileGdbConnPath = new FileGeodatabaseConnectionPath(new Uri(path, UriKind.Absolute));

    using (var gdb = new Geodatabase(fileGdbConnPath))
    {
      ArcGIS.Core.Data.Raster.MosaicDataset mDataset = gdb.OpenDataset<ArcGIS.Core.Data.Raster.MosaicDataset>("MyMoaid");
      var mosaicDataConnection = mDataset.GetDataConnection();

      var (leftImageID, rightImageID) = GetStereoImages(mDataset);
          
      stereoProps.SourceType = StereoSourceType.StereoModelCollection;
      stereoProps.StereoModelCollection = mosaicDataConnection;
      stereoProps.LeftImageID = leftImageID;
      stereoProps.RightImageID = rightImageID;

      cimMap.StereoProperties = stereoProps;

      stereoMap.SetDefinition(cimMap);
    }

  //        MapView.Active.Redraw(true);
  });
}

// Find the left and right image IDs from the mosaicDataset
// This is achieved by querying the stereo table and retrieving
// the ImageID1 and ImageID2 fields. 
(long image1ID, long image2ID) GetStereoImages(ArcGIS.Core.Data.Raster.MosaicDataset mDataset)
{
  var stereoTable = mDataset?.GetStereoTable();
  var stereoTableDef = stereoTable?.GetDefinition();
  if (stereoTableDef == null)
    return (-1, -1);

  var image1Fld = stereoTableDef.FindField("ImageID1");
  var image2Fld = stereoTableDef.FindField("ImageID2");
  if ((image1Fld == -1) || (image2Fld == -1))
    return (-1, -1);

  long image1 = -1;
  long image2 = -1;
  using (var cursor = stereoTable.Search(null))
  {
    if (cursor.MoveNext())
    {
      using (var row = cursor.Current)
      {
        var image = row[image1Fld];
        if (image != null)
          System.Int64.TryParse(image.ToString(), out image1);
        image = row[image2Fld].ToString();
        if (image != null)
          System.Int64.TryParse(image.ToString(), out image2);
      }
    }
  }

  return (image1, image2);
}

 

You may also need to set the LeftImageColorizer and RightImageColorizer properties, but I am not sure. 

 

Let me know if this still doesn't work for you.  It worked for my test scenario, but I am not a raster expert.

 

Narelle

0 Kudos
BarbaraSchneider2
Occasional Contributor III

Hello Narelle,

this works! I don't need to set the LeftImageColorizer and RightImageColorizer. Thank you very much.

Barbara

0 Kudos