ThinkGeo.com    |     Documentation    |     Premium Support

How can I change the X and Y of my internal feature in InMemoryFeatureLayer?

  I have an InMemoryFeatureLayer with various Features representing vehicles. Each feature is made of a PointShape. I add each Feature with a key. Later when I want to access a Feature from the InternalFeatures collection of the InMemoryFeatureLayer, I use the key. I can access the collumn values and change it without a problem but I can't seem to figure out how to do that for the PointShape associated with it. See my code:


            InMemoryFeatureLayer inMemoryFeatureLayer = new InMemoryFeatureLayer();

            inMemoryFeatureLayer.Open();

            inMemoryFeatureLayer.Columns.Add(new FeatureSourceColumn("MyColumn"));

            inMemoryFeatureLayer.Close();


            Feature newFeature = new Feature(new PointShape(X1,Y1));

            newFeature.ColumnValues["MyColumn"] = "Value1";

            inMemoryFeatureLayer.InternalFeatures.Add("Point1",newFeature);


            Feature newFeature2 = new Feature(new PointShape(X2,Y2));

            newFeature2.ColumnValues["MyColumn"] = "Value2";

            inMemoryFeatureLayer.InternalFeatures.Add("Point2",newFeature2);


            LayerOverlay pointOverLayer = new LayerOverlay();

            pointOverLayer.LocalCache = LocalCacheMode.AutomaticCaching;

            pointOverLayer.Layers.Add(inMemoryFeatureLayer);

            winformsMap1.Overlays.Add("PointsOverlay", pointOverLayer);


 


Later, I want to change some values of, let's say, "Point1".


I can change the value of the column without a problem:


           inMemoryFeatureLayer.InternalFeatures["Vehicle1"].ColumnValues["MyColumn""] = "NewValue1";


But I cannot change the X and Y of the PointShape of the Feature:


          inMemoryFeatureLayer.InternalFeatures["Point1"].GetShape()   ???



Adolfo, 
  
 here is my code, I hope this helps: 
  
 Overlay.Lock.EnterWriteLock(); 
 try 
 { 
 Feature feature = Layer.InternalFeatures[featureKey]; 
 PointShape pointShape = feature.GetShape() as PointShape; 
 pointShape.X = x; 
 pointShape.Y = y; 
 PointShape.Id = featureKey; 
  
 Layer.Open(); 
 Layer.EditTools.BeginTransaction(); 
 Layer.EditTools.Update(PointShape); 
 Layer.EditTools.CommitTransaction(); 
 Layer.Close() 
 } 
 finaly 
 { 
 Overlay.Lock.ExitWriteLock(); 
 } 
  


Adolfo, 
  
 Thanks for your post! 
  
 I think the answer is exactly what Patrick talked about; the reason is that the Feature is structure type which should be updated by our Edit system. 
  
 Thanks for your sharing, Patrick! 
  
 Let me know if you have more problems. 
  
 Thanks. 
  
 Yale 


Hey, I know this is old but I’m having the same problem and the solution posted isn’t fixing it for me.  
  
  Feature GPSFeature = new Feature(new PointShape(Longitude, Latitude)); 
  
  
             pointLayer.InternalFeatures.Add(“Key”,GPSFeature); 
  
 LayerOverlay pointOverlay = (LayerOverlay)winformsMap1.Overlays[“PointOverlay”]; 
             InMemoryFeatureLayer pointLayer = pointOverlay.Layers[“PointLayer”] as InMemoryFeatureLayer; 
  
             Feature GPSFeature = pointLayer.InternalFeatures[“Key”]; 
             PointShape point = GPSFeature.GetShape() as PointShape; 
             point.X = longitude; 
             point.Y = latitude; 
             point.Id = “Key”; 
  
             pointLayer.FeatureSource.Open(); 
             pointLayer.EditTools.BeginTransaction(); 
             pointLayer.EditTools.Update(GPSFeature); 
             pointLayer.EditTools.CommitTransaction(); 
             pointLayer.FeatureSource.Close(); 
  
             winformsMap1.Refresh();

Gary, 
  
 I got a few questions for you: 
  
 1. What are you seeing happen?  Are you getting an exception, is the point not moving are you getting multiple points or nothing is happening at all? 
  
 In looking at your code I think there may be an issue with how you are using the ID and the key, in order for the Update to work on on the Edit Tools you will want to make sure your ID’s of the original feature and the new updated feature are the same.  This is different than the key because the Key is how you reference the feature in the collection.  While these can be the same it’s not a strict requirement so they could be different and causing your issue. 
  
 2. Are you only wanting to show the last point?  If so it might be able to delete all the features and add then add the latest one.   You could do this a couple of ways such as calling the clear on the internal features colleciton, using the Delete in the edit tools, or just Creating a new InMemoryLayer each time you get a new GPS Location and replace the old InMemory Layer with your new one. 
  
 Hopefully this provides some help, but if we know the answers to the questions we can probably narrow down the problem some more. 
  
 Thanks! 


Hi, thanks for the reply. Your first solution worked, I added a Key to my PointShape so now my points have the same ID and when updated the point moves to another position. The problem is it keeps moving to the center of the map. No matter what coordinates I give to point.X and point.Y the marker moves to the centre of the map(I mean the center of the world, just south of Africa; not the center of whatever the screen is displaying).

Gary, 
  
 If that is happening it means your longitude and latitude aren’t getting set properly and are most likely 0,0 as that is the location out in the ocean by Africa.  I would recommend looking at the WellKnownText of the GPSFeature right before you call the update to see what the coordinates look like.  Below is the method you can use to see the WellKownText of the feature: 
  
 GPSFeature.GetWellKnownText(); 
  
 Thanks

the value that appears from that is the original values set to the starting point. This makes sense though since I created a new instance of GPSFeature and created a new instance of point and changed the values of this new object. It’s not untill I do the update that the new instances overwrite the old instances right? I’m not 100% sure on how the update works but that is how I imagined it.

I checked points after the points are changed and updated. The values are decreasing and approach 0,0. I don’t know why this is happening, I have no idea why the points are not being assigned the values I’m giving them.

The point has a unit or some extra value of E which I don't understand. Also the second value goes to 0.




Gary, 
  
 I think the E is because the value is really long value, the real problem question is why the second value is zero.   
  
 I would put break points on the two lines of code below and make sure the X & Y are getting set properly.  You should see the point.X and point.Y values changes right after you assign them and make sure the longitude and latitude variables contain the right values. 
  
 point.X = longitude;  
 point.Y = latitude;  
  
 Let us know what you find. 
  
 Thanks!