ThinkGeo.com    |     Documentation    |     Premium Support

Moving feature, CenterAt & Projection

Dear all,


Since I migrated from 3.0.199 to 3.0.307 I'm no longer able to move dynamic feature & center on a feature in combinaison with projection.


I'm using the same projection for a ShapeFileFeatureLayer and a InMemoryFeatureLayer:

p = new Proj4Projection(Proj4Projection.GetEpsgParametersString (4326), Proj4Projection.GetGoogleMapParametersString()); 


Then I add some features on the InMemoryFeatureLayer with WGS84 coord; features are correctly shown on the map.


The issue is when I try to center on that feature; the map is in fact centered to (0,0); this worked with 3.0.199; here is my code:


feature = m_DynamicLayer.InternalFeatures[featureKey];

MainMap.CenterAt(feature);

MainMap.Refresh();


Also, when I try to move that feature to another WGS84 coord, the feature is moved to (0,0); again this worked in 3.0.199 and work if I do not use projection:


 


DynamicOverlay.Lock.EnterWriteLock();

PointShape pointShape = m_DynamicLayer.InternalFeatures[featureKey].GetShape() as PointShape;

pointShape.X = x;

pointShape.Y = y;

pointShape.Id = featureKey;

m_DynamicLayer.Open();

m_DynamicLayer.EditTools.BeginTransaction();

m_DynamicLayer.EditTools.Update(pointShape);

m_DynamicLayer.EditTools.CommitTransaction();

m_DynamicLayer.Close();

DynamicOverlay.Lock.ExitWriteLock();

MainMap.Refresh();


Any help is more than welcome.



Patrick,


 
I understand what is going on in your code and found that you made two minor mistakes:
1) The passed in “feature” parameter in the CenterAt API should be a “projected” feature, so we need to project it before we passed it in.
 
2) When we update a pointShape, we should project it too before we updated.
 
So just try that.
 
Following is my sample code, just take a reference.
 

  InMemoryFeatureLayer dynamicLayer = (InMemoryFeatureLayer)winformsMap1.FindFeatureLayer("DynamicLayer");
            Projection projection = dynamicLayer.FeatureSource.Projection;
            projection.Open();
            winformsMap1.CenterAt(projection.ConvertToExternalProjection(dynamicLayer.InternalFeatures[0]));
            winformsMap1.Refresh();

 

also:

            InMemoryFeatureLayer dynamicLayer = (InMemoryFeatureLayer)winformsMap1.FindFeatureLayer("DynamicLayer");
            winformsMap1.Overlays[1].Lock.EnterWriteLock();
            try
            {
                PointShape pointShape = (PointShape)dynamicLayer.InternalFeatures[0].GetShape();

                pointShape.Id = dynamicLayer.InternalFeatures[0].Id;
                pointShape.X = -90;
                pointShape.Y = -60;

                Projection projection = dynamicLayer.FeatureSource.Projection;
                projection.Open();

                PointShape projectedPointShape = (PointShape
projection.ConvertToExternalProjection(pointShape);

                projectedPointShape.Id = pointShape.Id;
 
                dynamicLayer.Open();
                dynamicLayer.EditTools.BeginTransaction();
                dynamicLayer.EditTools.Update(projectedPointShape);
                dynamicLayer.EditTools.CommitTransaction();
                dynamicLayer.Close();
            }
            finally
            {
                winformsMap1.Overlays[1].Lock.ExitWriteLock();
            }
            winformsMap1.Refresh();


 
Let me know if any another problem.
 
Yale

Yale,


Thank you for your code, it works ... however, I feel it's a strange behaviour.


How can you explain that we need to use internal proj coord to add feature and external proj coord to move them?


Same for centerat; while I understand that centerat(point) can be a internal or external projection; I feel that centerat(feature) should handle the projection.


Regards


Patrick.



Patrick, 
  
 Thanks for your reply. 
  
 The reason we use internal projection when we add feature to InMemoryFeatureLayer is that all the features in it should be internal data and when we draw it we will project it to external features just before drawing or spatial querying etc. 
  
 The reason we use external data when we update is that we use the EditTools to update features which will not handle the projection, still I think if we can handle it would be nice. 
  
 The reason we did not handle the projection in CenterAt is that the projection object is just a property for a layer , not a  property of MapControl. 
  
 If any more further questions, just let me know. 
 Thanks. 
  
 Yale.