ThinkGeo.com    |     Documentation    |     Premium Support

"The given key was not present in the dictionary" Error on Refresh Call

I'm getting this error on my refresh call and can't see where it is coming from:


"The given key was not present in the dictionary"


I've tried to snip out all relevant code here...


Initialization:



// INIT ThinkGeo MapSuite
try {
  winformsMap.MapUnit = GeographyUnit.DecimalDegree;
  winformsMap.BackgroundOverlay.BackgroundBrush = new GeoSolidBrush(GeoColor.GeographicColors.DeepOcean);
  winformsMap.CenterAt(new PointShape(-76.286, 36.827));
  winformsMap.ZoomToScale(winformsMap.ZoomLevelSet.ZoomLevel11.Scale);

  worldMapKitRenderLayer = new WorldMapKitRenderLayer(@"D:\data\ThinkGeo\ThinkGeoWorldMapKit");
  worldMapKitRenderLayer.LoadLayers();

  LayerOverlay worldMapKitRenderLayerOverlay = new LayerOverlay();          
  worldMapKitRenderLayerOverlay.Layers.Add(worldMapKitRenderLayer);
  winformsMap.Overlays.Add(worldMapKitRenderLayerOverlay);


  InMemoryFeatureLayer tracksFriendlyInMemoryFeatureLayer = new InMemoryFeatureLayer();
  tracksFriendlyInMemoryFeatureLayer.Open();
  tracksFriendlyInMemoryFeatureLayer.Columns.Add(new FeatureSourceColumn("Label"));
  tracksFriendlyInMemoryFeatureLayer.Close();
  tracksFriendlyInMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.DefaultTextStyle = new TextStyle("Label", new GeoFont("Verdana", 10, DrawingFontStyles.Bold), new GeoSolidBrush(GeoColor.StandardColors.White));
  tracksFriendlyInMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.DefaultTextStyle.BestPlacement = true;
  tracksFriendlyInMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.DefaultTextStyle.PointPlacement = PointPlacement.CenterRight;
  tracksFriendlyInMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.DefaultTextStyle.HaloPen = new GeoPen(GeoColor.StandardColors.Black, 1);   tracksFriendlyInMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.DefaultLineStyle.OuterPen = new GeoPen(GeoColor.StandardColors.DarkGreen, 2);
  tracksFriendlyInMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.DefaultPointStyle = new PointStyle(PointSymbolType.Square, new GeoSolidBrush(GeoColor.StandardColors.Green), new GeoPen(GeoColor.StandardColors.DarkGreen, 2), 10);
  tracksFriendlyInMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;

  LayerOverlay tracksFriendlyLayerOverlay = new LayerOverlay();
  tracksFriendlyLayerOverlay.Layers.Add(tracksFriendlyInMemoryFeatureLayer);

  winformsMap.Overlays.Add("Tracks|Friendly", tracksFriendlyLayerOverlay);


  winformsMap.Refresh();

} catch (Exception Exception) {
  log.Enqueue("Problem initializing mapping tools: " + Exception.Message);
}

Inside update map timer:



try {
  winformsMap.Overlays["Tracks|Friendly"].Lock.EnterWriteLock();
  LayerOverlay trackLayerOverlay = (LayerOverlay)winformsMap.Overlays["Tracks|Friendly"];
  InMemoryFeatureLayer trackInMemoryFeatureLayer = (InMemoryFeatureLayer)trackLayerOverlay.Layers[0];

  trackInMemoryFeatureLayer.InternalFeatures.Clear();

  for (int i = 0; i < MAX_TARGETS; i++) {
    if (!(hideUnknownTracks && targets<i>.trackClass == trackClassificationEnum.UNKNOWN)) {
      if ((targets<i>.timeLapseComms < TARGET_DROP_TIMEOUT && targets<i>.trackStatus == trackStatusEnum.TARGET_ACTIVE) || i == selectedIconTrackIndex) {

        Feature feature = new Feature(new PointShape(targets<i>.longitude, targets<i>.latitude));
        if (targets<i>.ais.isValid) {
          feature.ColumnValues.Add("Label", targets<i>.ais.name);
        } else {
          feature.ColumnValues.Add("Label", targets<i>.ID);
        }
        trackInMemoryFeatureLayer.InternalFeatures.Add("Tracks|Friendly|Icon|" + targets<i>.ID, feature);

        if (targets<i>.trackHistory.Count > 5) {
          feature = new Feature(new LineShape(targets<i>.trackHistory));
          trackInMemoryFeatureLayer.InternalFeatures.Add("Tracks|Friendly|Trail|" + targets<i>.ID, feature);
        }
      }
    }
  }

} catch (Exception ex) {
  log.Enqueue("ERROR: Problem updating track icons. - " + ex.Message);

} finally {
  winformsMap.Overlays["Tracks|Friendly"].Lock.ExitWriteLock();
}

winformsMap.Refresh((LayerOverlay)winformsMap.Overlays["Tracks|Friendly"]);

Where trackHistory is a Vertex Collection.


If I comment the "add lineshape feature" line, the code works fine showing tracks from our AIS receiver, otherwise I get the error after building up enough track history.


I know this is a pretty inefficient way to do this, but until I get a better understanding of MapSuite, this is how I can best understand what is going on.



 Side question:  Why are my array index brackets turning into < and > characters when using the code tags?



Nate, 



Thanks for your post, 



I cannot compile your code successfully because of some variables are not defined, so I cannot reproduce your problem exactly, I just made a simple sample and try to reproduce the exception what you occurred, the sample code is very easy, here is the code below: 



 
private void button1_Click(object sender, EventArgs e) 

Collection<Vertex> verteices1 = new Collection<Vertex>(); 
verteices1.Add(new Vertex(0.2, 0.5)); 
verteices1.Add(new Vertex(0.8, 60.2)); 
verteices1.Add(new Vertex(1.8, 70.1)); 
LineShape lineShape2 = new LineShape(verteices1); 
Feature feature = new Feature(lineShape2); 
InMemoryFeatureLayer inMemoryLayer = new InMemoryFeatureLayer(); 
inMemoryLayer.InternalFeatures.Add("Test", feature); 
inMemoryLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle.FillSolidBrush.Color = GeoColor.FromArgb(100, GeoColor.StandardColors.RoyalBlue); 
inMemoryLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle.OutlinePen.Color = GeoColor.StandardColors.Blue; 
inMemoryLayer.ZoomLevelSet.ZoomLevel01.DefaultLineStyle.OuterPen = new GeoPen(GeoColor.FromArgb(200, GeoColor.StandardColors.Red), 5); 
inMemoryLayer.ZoomLevelSet.ZoomLevel01.DefaultPointStyle.SymbolPen = new GeoPen(GeoColor.FromArgb(255, GeoColor.StandardColors.Green), 8); 
inMemoryLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20; 

LayerOverlay staticOverlay = new LayerOverlay(); 
staticOverlay.Layers.Add("InMemoryFeatureLayer", inMemoryLayer); 
winformsMap1.Overlays.Add(staticOverlay); 
winformsMap1.CurrentExtent = new RectangleShape(0, 100, 100, 0); 
winformsMap1.Refresh(); 






According to my test, it can work fine, I cannot find out any exceptions, please review your code or send us a complete sample so that we can reproduce this problem exactly, 



Thanks, 



Scott,



Getting my code to run as it does here might be tricky, so I cut the code out of the timer event and placed a simplified version of it into the initialization section and I get the same exception.  I think if I can figure out why it fails here, it will shed light on why it fails in the timer as well.



// INIT ThinkGeo MapSuite
try {
  winformsMap.MapUnit = GeographyUnit.DecimalDegree;
  winformsMap.BackgroundOverlay.BackgroundBrush = new GeoSolidBrush(GeoColor.GeographicColors.DeepOcean);
  winformsMap.CenterAt(new PointShape(-76.286, 36.827));
  winformsMap.ZoomToScale(winformsMap.ZoomLevelSet.ZoomLevel11.Scale);

  worldMapKitRenderLayer = new WorldMapKitRenderLayer(@"D:\data\ThinkGeo\ThinkGeoWorldMapKit");
  worldMapKitRenderLayer.LoadLayers();

  LayerOverlay worldMapKitRenderLayerOverlay = new LayerOverlay();          
  worldMapKitRenderLayerOverlay.Layers.Add(worldMapKitRenderLayer);
  winformsMap.Overlays.Add(worldMapKitRenderLayerOverlay);


  InMemoryFeatureLayer tracksFriendlyInMemoryFeatureLayer = new InMemoryFeatureLayer();
  tracksFriendlyInMemoryFeatureLayer.Open();
  tracksFriendlyInMemoryFeatureLayer.Columns.Add(new FeatureSourceColumn("Label"));
  tracksFriendlyInMemoryFeatureLayer.Close();
  tracksFriendlyInMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.DefaultTextStyle = new TextStyle("Label", new GeoFont("Verdana", 10, DrawingFontStyles.Bold), new GeoSolidBrush(GeoColor.StandardColors.White));
  tracksFriendlyInMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.DefaultTextStyle.BestPlacement = true;
  tracksFriendlyInMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.DefaultTextStyle.PointPlacement = PointPlacement.CenterRight;
  tracksFriendlyInMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.DefaultTextStyle.HaloPen = new GeoPen(GeoColor.StandardColors.Black, 1);   tracksFriendlyInMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.DefaultLineStyle.OuterPen = new GeoPen(GeoColor.StandardColors.DarkGreen, 2);
  tracksFriendlyInMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.DefaultPointStyle = new PointStyle(PointSymbolType.Square, new GeoSolidBrush(GeoColor.StandardColors.Green), new GeoPen(GeoColor.StandardColors.DarkGreen, 2), 10);
  tracksFriendlyInMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;

  LayerOverlay tracksFriendlyLayerOverlay = new LayerOverlay();
  tracksFriendlyLayerOverlay.Layers.Add(tracksFriendlyInMemoryFeatureLayer);

  winformsMap.Overlays.Add("Tracks|Friendly", tracksFriendlyLayerOverlay);


  // Something bad gets done to the map in here
  winformsMap.Overlays["Tracks|Friendly"].Lock.EnterWriteLock();
  LayerOverlay trackLayerOverlay = (LayerOverlay)winformsMap.Overlays["Tracks|Friendly"];
  InMemoryFeatureLayer trackInMemoryFeatureLayer = (InMemoryFeatureLayer)trackLayerOverlay.Layers[0];

  Feature feature = new Feature(new LineShape("LINESTRING(-76.286 36.827, -76.100 36.600)"));
  trackInMemoryFeatureLayer.InternalFeatures.Add("ID", feature);
  winformsMap.Overlays["Tracks|Friendly"].Lock.ExitWriteLock();
  // end of bad stuff


  winformsMap.Refresh();  // Exception thrown from here

} catch (Exception Exception) {
  log.Enqueue("Problem initializing mapping tools: " + Exception.Message);
}


Nate, 
  
 Thanks for your response. 
  
 I do not quite understand your meaning exactly. It seems you are making progress, I am happy to see its happening. 
  
 Any more questions just feel free to let me know. 
  
 Thanks. 
  
 Yale 


I would like to know if you get the same exception with this code that I do.  (The code in the last post I made)  If you are missing something, let me know.   I am not making any progress as I don't understand what the exception is trying to tell me.



Nate,


Thanks for your response. I am ashamed with my misunderstanding and I should have tried your code.
 
I think the problem is when the feature is added, the corresponding column value was not added which will be used for labeling. I change a bit for the code and have a try on the following code, it should work.

// INIT ThinkGeo MapSuite
try
{
winformsMap1.MapUnit = GeographyUnit.DecimalDegree;
winformsMap1.BackgroundOverlay.BackgroundBrush = new GeoSolidBrush(GeoColor.GeographicColors.DeepOcean);
winformsMap1.CenterAt(new PointShape(-76.286, 36.827));                  
winformsMap1.ZoomToScale(winformsMap1.ZoomLevelSet.ZoomLevel11.Scale);
 
worldMapKitRenderLayer = new WorldMapKitRenderLayer(@"D:\data\ThinkGeo\ThinkGeoWorldMapKit");
worldMapKitRenderLayer.LoadLayers();
 
LayerOverlay worldMapKitRenderLayerOverlay = new LayerOverlay();                worldMapKitRenderLayerOverlay.Layers.Add(worldMapKitRenderLayer);
winformsMap1.Overlays.Add(worldMapKitRenderLayerOverlay);
 
 
InMemoryFeatureLayer tracksFriendlyInMemoryFeatureLayer = new InMemoryFeatureLayer();
tracksFriendlyInMemoryFeatureLayer.Open();
tracksFriendlyInMemoryFeatureLayer.Columns.Add(new FeatureSourceColumn("Label"));
tracksFriendlyInMemoryFeatureLayer.Close();
tracksFriendlyInMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.DefaultTextStyle = new TextStyle("Label", new GeoFont("Verdana", 10, DrawingFontStyles.Bold), new GeoSolidBrush(GeoColor.StandardColors.White));
tracksFriendlyInMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.DefaultTextStyle.BestPlacement = true;
tracksFriendlyInMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.DefaultTextStyle.PointPlacement = PointPlacement.CenterRight;
tracksFriendlyInMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.DefaultTextStyle.HaloPen = new GeoPen(GeoColor.StandardColors.Black, 1); tracksFriendlyInMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.DefaultLineStyle.OuterPen = new GeoPen(GeoColor.StandardColors.DarkGreen, 2);
tracksFriendlyInMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.DefaultPointStyle = new PointStyle(PointSymbolType.Square, new GeoSolidBrush(GeoColor.StandardColors.Green), new GeoPen(GeoColor.StandardColors.DarkGreen, 2), 10);
tracksFriendlyInMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
 
LayerOverlay tracksFriendlyLayerOverlay = new LayerOverlay();
tracksFriendlyLayerOverlay.Layers.Add(tracksFriendlyInMemoryFeatureLayer);
 
winformsMap1.Overlays.Add("Tracks|Friendly", tracksFriendlyLayerOverlay);
 
 
// Something bad gets done to the map in here               
 winformsMap1.Overlays["Tracks|Friendly"].Lock.EnterWriteLock();
LayerOverlay trackLayerOverlay = (LayerOverlay)winformsMap1.Overlays["Tracks|Friendly"];
InMemoryFeatureLayer trackInMemoryFeatureLayer = (InMemoryFeatureLayer)trackLayerOverlay.Layers[0];
 
Feature feature = new Feature(new LineShape("LINESTRING(-76.286 36.827, -76.100 36.600)"));
feature.ColumnValues.Add("Label", "Add Label Here");
trackInMemoryFeatureLayer.InternalFeatures.Add("ID", feature);
winformsMap1.Overlays["Tracks|Friendly"].Lock.ExitWriteLock();
// end of bad stuff
 
winformsMap1.Refresh(); // Exception thrown from here
}
catch (Exception Exception)
{
     //log.Enqueue("Problem initializing mapping tools: " + Exception.Message);
     System.Diagnostics.Debug.WriteLine(Exception.Message);


 
Any more questions just feel free to let me know.
 
Thanks.
 
Yale

Thanks!  That was it.


I didn't realize that when you add column values to a layer, they are required to be set for every feature.



Nate, 
  
 Thanks for your letting us know your status. 
  
 Any more questions just feel free to let me know. 
  
 Thanks. 
  
 Yale