ThinkGeo.com    |     Documentation    |     Premium Support

CreateShapeFile exception

Hi,


I wanted to create a ShapeFile from an InMemoryFeatureLayer. In this InMemoryFeatureLayer, it stores some data retrieved from different database tables, but these tables have the same data columns, for example they all have an ID column which stores a sequence number. When I create the ShapeFile, if the InMemoryFeatureLayer stores data from only one database table, everything works very well; but when the InMemoryFeatureLayer stores data from more than one database tables, I got following exception the program executes the "AddFeature(feature)" method:


"An item with the same key has already been added"


Any help is greatly appreciated.


Rose



Hi Rose,


Thanks for your post!
 
It seems that some features have the same id, and you can not add them to one feature layer.  For example, the features ids from one data table may be “1”, “2”, “3”…, and the ids from other data table also could be “1”, “2”, “3”… So you should write the code like this:
 

inMemoryLayer.BeginTransaction();
foreach (Feature feature in allFeaturesInTable1)
{
   inMemoryLayer.AddFeature(new Feature(feature.GetWellKnownBinary(), Guid.NewGuid().ToString(), feature.ColumnValues));
}

foreach (Feature feature in allFeaturesInTable2)
{
   inMemoryLayer.AddFeature(new Feature(feature.GetWellKnownBinary(), Guid.NewGuid().ToString(), feature.ColumnValues));
}
inMemoryLayer.CommitTransaction();

 


Hope this can help you, and any more questions please let me know.
 
Thanks,
 
sun

Sun:


I have no problem to add the data from different tables to the InMemoryFeatureLayer, when I added the data, I specified the key, and the key is guaranteed unique, it is a combination of two fields.


The problem is when I create ShapeFile from the InMemoryFeatureLayer


Rose



Sun:


I have found where the problem is.


When add features to the shape file, I used following Feature constructor:


Feature Constructor(BaseShape,Generic IDictionary) .


By changing to following constructor, it works:


Feature Constructor(String,String,Generic IDictionary)


 


Rose


 



Yes, this is same as the InMemoryFeatureLayer. When you add a feature to a ShapeFileFeatureLayer, it will add the feature to a dictionary temporarily and the key is the feature’s id by default. So you need to specify the id of the feature to avoid duplicate ids.


Any more questions please let me know.
 
Thanks,
 
sun