Can't add a second feature to a feature set.

Developer
Apr 7, 2011 at 10:13 PM

The annotated code sample below demonstrates and explains the error.
Incidentally, if you change the feature type to Point it does not crash at the same code but it will still crash if you, for example, right click and choose view attributes in the Demo app.
I am doing a lot of feature editing code and I get errors like this Alllll the time.  What am I doing wrong?  The code sample below sure like like it should work.

/////////////////////////////////////////////////////////////////////////////////
// any hard coded file name.  NB: there is a bug with file names that do not
// contain a directory name but that is a different issue
string sPolygonsFileName = @"d:\polygons.shp";

/////////////////////////////////////////////////////////////////////////////////
// just create a polygon feature set with a single text field
IFeatureSet fs1 = new FeatureSet(FeatureType.Polygon) { Name = "test" };


/////////////////////////////////////////////////////////////////////////////////
// The error does not occur until the file has been saved and reloaded.  As in real
// world situation the data would typically be coming for a file
fs1.SaveAs(sPolygonsFileName, true);
fs1 = new FeatureSet(sPolygonsFileName);

/////////////////////////////////////////////////////////////////////////////////
// add polygon feature with the DataRow populated
fs1.AddFeature(
    new Polygon(
        new Coordinate[] { 
            new Coordinate( 0,0 ),
            new Coordinate( 0,100 ),
            new Coordinate( 100,100 ),
            new Coordinate( 100,0 ),
            new Coordinate( 0,0 ) }));

/////////////////////////////////////////////////////////////////////////////////
// add add the layer to a map contol ( actually run this from inside a plugin but
// it makes no diffence
FeatureLayer l = (FeatureLayer)args.Map.Layers.Add(fs1);         
args.Map.MapFrame.Invalidate();

/////////////////////////////////////////////////////////////////////////////////
// add a second polygon feature with the DataRow populated
fs1.AddFeature(
    new Polygon(
        new Coordinate[] {
            new Coordinate( 0,0 ),
            new Coordinate( 0,100 ),
            new Coordinate( 100,100 ),
            new Coordinate( 100,0 ),
            new Coordinate( 0,0 ) }));
 
/////////////////////////////////////////////////////////////////////////////////
// crashes here with message "The given key was not present in the dictionary." 
args.Map.MapFrame.Invalidate();

// : in the library at.    DotSpatial.Sysmbology.DrawingFilter
//       public IDrawnState this[IFeature key]
//       {
//              get
//               {
//                     if (_isInitialized == false) DoInitialize();
//                     if (_drawnStates == null) return null;
//                     return _drawnStates[key];     ***** <=== the offending line
//               }
//               set
//               {
//                     if (_isInitialized == false) DoInitialize();
//                     // this will cause an exception if _drawnStates is null, but that might be what is needed
//                     if (_drawnStates[key] != value) _countIsValid = false;
//                     _drawnStates[key] = value;
//               }
//        }

Developer
Apr 28, 2011 at 6:44 PM

Bump...  

Anybody... please?

I really could use a hand with this.  I just cannot understand the DrawStates code?  If you can even point me to a working samples of editing features with Attributes or just tell me how to debug the AssignDrawStates code blocks.    Or even just  tell me that someone else has found a way to get it working and I must be doing it wrong.

Any hints at all that point me in the right direction would be very much appreciated.

 

Thanks in advance,

 

Garth

Jun 29, 2011 at 7:07 AM

fs1..InitializeVertices();

l.AssignFastDrawnStates();

write these codes above before args.Map.MapFrame.Invalidate();

Jun 30, 2011 at 7:57 AM
Edited Jun 30, 2011 at 8:02 AM

0.0

Jul 1, 2011 at 1:15 AM

you shoul add attribute to the new feature!

good luck!