ThinkGeo.com    |     Documentation    |     Premium Support

Update Feature Issue

Hello all


I'm confused in which Feature be updated?


I create aFeature, and I add this Feature in my ShapeFile.


 ShapeFileFeatureLayer Road= (ShapeFileFeatureLayer)(winformsMap1.FindFeatureLayer("Road"));

  Road.Open();

  Road.EditTools.BeginTransaction();

  Road.EditTools.Add(Feature);

  Road.EditTools.CommitTransaction();

  Road.Close();


Then, I create an newFeature.


The newFeature coordinates copy from Feature, but some coordinates have modified.


Now, I update newFeature to my ShapeFile.


 ShapeFileFeatureLayer Road= (ShapeFileFeatureLayer)(winformsMap1.FindFeatureLayer("Road"));

  Road.Open();

  Road.EditTools.BeginTransaction();

  Road.EditTools.Update(newFeature);

  Road.EditTools.CommitTransaction();

  Road.Close();


How could I know which feature be updated?


If you have  more suggestion, please tell me. Thanks.


 



Does anyone reply this? 


Carol, 
  
 The EditTools.Update(feature) method will update the feature with the same id. Feature has a property feature.Id, in your 2nd part codes, when Road.EditTools.CommitTransaction() is called, it then searches in the ShapeFeatureFeatureSource to find out the feature with the same Id as newFeature.Id, and then replace that feature with newFeature. 
  
 Thanks, 
  
 Ben

Thanks for your reply, Ben 
 as follows code, 
  
 Feature newFeature = new Feature(new LineShape(ModifiedVertex), newColumnsData); 
  
 how can I set its FeatureID?? feature.id is readOnly, I can’t change it. 


Carol, 
  
 You can set the id to a shape and pass that shape to a feature. For example:  
  
 
            PointShape a = new PointShape();
            a.Id = "100";
            Feature feature = new Feature(a); // feature.Id then equals to "100"
 
  
 Thanks, 
  
 Ben

Hi, I’m having a problem with this actually. I am trying up update a line in the DragInteractiveOverlay, this is a class I found in one of the samples that inherits from EditInteractiveOverlay. I got the EditTools.Update to work for a InMemoryFeatureLayer but I can’t seem to get it to work with this InteractiveOverlay. The Overlay accepts Features so I give it a feature for an Id, then I update it with a new feature with the same Id and nothing happens.  
 line1.Id = “LineFeature”; 
 LineFeature = new Feature(line1); 
 dragInteractiveOverlay.EditShapesLayer.InternalFeatures.Add(LineFeature); 
  
 line1.Vertices.Add(v); 
 line1.Id = “LineFeature”; 
 LineFeature = new Feature(line1); 
  
 dragInteractiveOverlay.EditShapesLayer.Open(); 
 dragInteractiveOverlay.EditShapesLayer.EditTools.BeginTransaction(); 
 dragInteractiveOverlay.EditShapesLayer.EditTools.Update(LineFeature); 
 dragInteractiveOverlay.EditShapesLayer.EditTools.CommitTransaction(); 
 dragInteractiveOverlay.EditShapesLayer.Close();

Hi Gary,  
  
 Are you refreshing the map after the update like the code below? 
  
 winformsMap1.Refresh(); 
  
 Thanks!

Yes I’m refreshing at the end, what I found to work is to clear the overlay, and then add the new line. This isn’t very efficient though, I’d rather update lines than having to remove and re-add every time I want to extend a line.

Thanks Gary, in looking at your code the update should work, can you provide a simple sample self contained sample that recreates the issue and I can look at it.  I wasn’t able to recreate the behavior here.   It looks like your current approach is also working by clearing out the feature and then adding it back new.  From an efficiency standpoint I don’t think there is much impact either way so feel free to use the way which is easiest for you. 
  
 Thanks!

Here is a sample of code I took form my application that simulates the problem. If you check "Create Line" and then click on the map, it does not update the line. 



004_003_002_001_test.zip (371 KB)

 Gary,


Thanks for the sample!  I was able to figure out what was going on, the problem isn't acutally in the Update instead it's when the original feature is added to the EditLayer in your form load.   In the code you provided you were adding the feature with the following line of code in the form load.


dragInteractiveOverlay.EditShapesLayer.InternalFeatures.Add(LineFeature);


Since this feature was added outside of the EditTools API the EditTools didn't register the ID and so when the Update was called in your click event the feature wasn't found and the update failed.  


In order to resolve this instead of using the Add off of the InternalFeatures collection you should use the EditTools API to add your feature like the code below in the form load, this way the Adding and Updating will be done by the same API's and be consistent.



            dragInteractiveOverlay.EditShapesLayer.Open();


            dragInteractiveOverlay.EditShapesLayer.EditTools.BeginTransaction();


            dragInteractiveOverlay.EditShapesLayer.EditTools.Add(LineFeature);


            dragInteractiveOverlay.EditShapesLayer.EditTools.CommitTransaction();


            dragInteractiveOverlay.EditShapesLayer.Close();


As a rule of thumb I would never use the InternalFeatures collection i and always use the EditTools when the features have the possiblity of changing.  The InternalFeatures collection is a quick and dirty way to access the features and hasn't been fully tested for editing scenarios.  


One other helpfull tidbit of information when using the EditTools is that you can actually check the results of the transaction.  Many people don't know this but it can be very helpfull in debugging issues when editing featurs.  To see the results of the transaction you just interogate the TransactionResult object returned from the CommitTransaction method.  



TransactionResult tr = dragInteractiveOverlay.EditShapesLayer.EditTools.CommitTransaction();


Thanks for reporting the issue and let us know if you have any additional questions.




Thanks!



Clint thank you so much, I finally have everything working properly. You really know this software inside out, how could I have possibly caught an error like that? I’m curious to how you learned all of this to such extent and how long it took you.

Thanks for the kind words Gary!  Yes that one was a difficult issue to figure out on the surface, I had to ask someone else here who knows the internals of the EditTools to help me out.