ThinkGeo.com    |     Documentation    |     Premium Support

Finding Last Feature added to a Shapefile

 I am implementing an Undo/Redo for deletion of roads in my shapefiles.  When a road is deleted I add it to an InMemoryFeatureLayer that is not visible and delete it from the shapefile.  If the user undoes that action, I add it back to the shapefile and also add it to a Redo dictionary containing the feature and the layer it came from.  When the user clicks redo (enabled only after an undo was executed), I find the layer the last undo feature was added to and attempt to delete the last item added.  I have other logic in the program to clear the redo stack when necessary.  Anyways, in order to avoid pulling in the entire collection of features from the ShapeFileFeatureLayer and finding the ID of the last one in the collection, which can take up to a minute with some of the shapefiles being used, I would like to query for the last item added.  Is there any way to get this information other than pull all features from the shapefile and reference the last item in the list?


 


In short, is there a simple method to delete the last feature added to a shapefile/ShapefileFeatureLayer or at least get the ID for the last feature added?



Hi Jake,


The ID of the last feature added is  the count of features in the ShapeFileFeatureLayer. So you can get the ID simply by the following code: 

ShapeFileFeatureLayer originalLayer = new ShapeFileFeatureLayer(shapeFileName, ShapeFileReadWriteMode.ReadWrite);
            
originalLayer.Open();
int count = originalLayer.QueryTools.GetCount();
string id = count.ToString(CultureInfo.InvariantCulture);

 
then you can delete the feature by the following code:

 

originalLayer.EditTools.BeginTransaction();
originalLayer.EditTools.Delete(id);
originalLayer.EditTools.CommitTransaction();

Any more questions please let me know.



Thanks,
sun

That does work, and I have used that when I needed to get just the last item added.  What I would like is the last "active" feature ID.  Meaning if I delete a feature, what the the next one in the "active" list.  
  
 Sample scenario: 
 Shapefile contains 3 roads, user deletes them all. 
 User clicks undo 3 times.  The shapefile has 3 "active" roads again with ids 4,5,6. 
 User clicks redo 2 times. The program should delete lines with id of 6 then 5. 
 Shapefile should contain 1 "active" road with id of 4. 
  
 This gets even more complicated if they click undo again 
 Now shapefile contains 2 roads with ID 4,7 
 if they click redo 2 times, program should delete lines with id 7 and 4 and there are no more lines in shapefile. 
  
 I can accomplish this right now by using the GetAllFeatures() from the ShapeFileFeatureLayer and referencing the last item in the list.  It works fine if the Shapefile has < 100000 items, but anything above that, it gets really slow trying to get all features.

Jake,


So does the GetCount function satisfy your requirement? And I don’t think the GetAllFeature is needed in your case. Could you please supply a simple sample to show your undo and redo logic so that we can look into the code and give you some suggestion?
Any more questions please let me know.
Thanks,
sun 

No, GetCount will not work for what we are doing.  It will work if we only want to undo/redo 1 feature, but when you get to 2 or more features you don't get the correct ID from GetCount.  Attached is the code fragments for redo/undo functions.  



1274-undo-redo.txt (7.76 KB)

Jake, 
  
 I think Sun’ idea is we will do these kinds of operations in memory and then commit the final overall changes (from all the changes done) to shape files. 
  
 And if I am not making mistake, your idea is every operations should be committed to shape file, one possible solution for this we can keep a variable which record the last valid RecordId for every operations(ReDo or Undo). We will do some researches on this and let you know as soon as possible. 
  
 Any more questions just feel free to let me know. 
  
 Thanks. 
  
 Yale