ThinkGeo.com    |     Documentation    |     Premium Support

Help - How to move a polygon over time?

I looked at the airplane bitmap example but could not translate it to get a polygon created in a layer to move over time. 


I created a POLYGON on a layer. It shows up fine. 


                  inMemoryLayer.InternalFeatures.Add(vehicleid + "M1", new Feature(BaseShape.CreateShapeFromWellKnownData("POLYGON(" ...... ")));


Now I want to move the polygon over time.. I used the AirPlane example to start with but the polygons dont move. How do I get these to move???


 


 


       void timer_Tick(object sender, EventArgs e)

       {

         //InMemoryFeatureLayer bitmapLayer = (InMemoryFeatureLayer)winformsMap1.FindFeatureLayer("BitmapLayer");

         //PointShape pointShape = bitmapLayer.InternalFeatures[0].GetShape() as PointShape;

         //LineShape airLine = ((MultilineShape)bitmapLayer.InternalFeatures[1].GetShape()).Lines[0];

         InMemoryFeatureLayer VehicleOverlay = (InMemoryFeatureLayer)winformsMap1.FindFeatureLayer("InMemoryFeatureLayer");

         int cnt = VehicleOverlay.InternalFeatures.Count;

         VehicleOverlay.Open();

         VehicleOverlay.EditTools.BeginTransaction();



         for(int zz = 0; zz < cnt; zz++)

           {

             BaseShape bs = VehicleOverlay.InternalFeatures[zz].GetShape();

             String s = bs.GetType().Name;

             if (s == "PolygonShape")

               {   

                bs.TranslateByOffset(10.0,0.0);

                VehicleOverlay.EditTools.Update(bs);

               }             

           }

         VehicleOverlay.EditTools.CommitTransaction();  

         VehicleOverlay.Close();





         //index += 30;

         //if (index < airLine.Vertices.Count)

         //{

         //  Vertex newPosition = airLine.Vertices[index];

         //  pointShape.X = newPosition.X;

         //  pointShape.Y = newPosition.Y;

         //  pointShape.Id = "Plane";



         //  bitmapLayer.Open();

         //  bitmapLayer.EditTools.BeginTransaction();

         //  bitmapLayer.EditTools.Update(pointShape);

         //  bitmapLayer.EditTools.CommitTransaction();

         //  bitmapLayer.Close();

         //}

         //else

         //{

         //  index = 0;

         //}



         



         winformsMap1.Refresh(winformsMap1.Overlays["VehicleOverlay"]);

         winformsMap1.Refresh();

       }

 



 


Bob,
 
Thanks for your post and the information is really helpful.
 
The problem is that you don’t use our API properly, to be honest it’s a little strange that make user confused. 
 
If you want to update feature from inMemoryFeatureLayer, the key and feature id should be the same, you use vehicleid + "M1" as the key but don’t assign the id to feature, so it will use random GUID as feature id. The right code is:
                inMemoryLayer.InternalFeatures.Add(vehicleid + "M1", new Feature(BaseShape.CreateShapeFromWellKnownData("POLYGON( ...... )").GetWellKnownBinary(), vehicleid + "M1"));
 
Sorry for inconvenient.
 
Please let me know if you have more questions.
 
James

That worked.  Thanks James. This is getting closer to closing the  
 gap and moving this forward.  Is there a find for the Shape?  
  
 Something like  
 BaseShape bs = VehicleOverlay.Find(vechileID + "M1"); 
 Otherwise how to manage many shapes?  
 Thanks

Scratch that last question.  
 I found that BaseShape .id is the actual string I created which I can use to identify the vehicle.  
 Thanks again probably more questions tonight.

I do have another question. How do I control the OutlinePen.Color for an individual shape?


I need to change the color of the shape on the fly to indicate different states of the vehicle.



And one last issue that I have had.  
 Here are a group of points. I am trying to create a polygon from them and its throwing an exception on the -1081.583 
  
 InputString  =  "-1081.583 376.025, -1085.583 373.625, -1085.583 340.375, -1081.583 342.775, -1077.583 340.375, -1077.583 373.625, -1081.583 376.025"; 
  
 inMemoryLayer.InternalFeatures.Add(vehicleid + "_M1", new Feature(BaseShape.CreateShapeFromWellKnownData("POLYGON(" + 
                                                   InputString  + ")").GetWellKnownBinary(), vehicleid + "_M1")); 


Bob,


 You may have a more precise anwer from James, but I let you know that on my side, I could have your Polygon added to an InMemoryFeatureLayer with the following code. You can see how your Polygon looks like:



 



 string InputString = "(-1081.583 376.025, -1085.583 373.625, -1085.583 340.375, -1081.583 342.775, -1077.583 340.375, -1077.583 373.625, -1081.583 376.025)";

            InMemoryFeatureLayer inMemoryFeatureLayer = new InMemoryFeatureLayer();
            inMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.CreateSimpleAreaStyle(GeoColor.FromArgb(150, GeoColor.StandardColors.Red), GeoColor.StandardColors.Black);
            inMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.DefaultPointStyle = PointStyles.CreateSimpleCircleStyle(GeoColor.StandardColors.Green, 12);
            inMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;

            inMemoryFeatureLayer.InternalFeatures.Add(new Feature(BaseShape.CreateShapeFromWellKnownData("POLYGON(" + InputString + ")")));

            LayerOverlay myOverlay = new LayerOverlay();
            myOverlay.Layers.Add("myLayer", inMemoryFeatureLayer);
            winformsMap1.Overlays.Add("MyOverlay", myOverlay);


 


Gentlemen,
 
First, thank Val for your sharing. 
 
Compare the code between Val’s and Bob’s, I found that the reason of exception is that used the wrong format of the well known text, the correct example is POLYGON((-1081.583 376.025,-1085.583 373.625,-1085.583 340.375,-1081.583 342.775,-1077.583 340.375,-1077.583 373.625,-1081.583 376.025)), it must contains double parenthesis, Polygon((……)). 
 
Well-known text (WKT) is a text markup language for representing vector geometry objects on a mapspatial reference systems of spatial objects and transformations between spatial reference systems.
The other shapes’ WKT you can look at the detail description in Wikipefia en.wikipedia.org/wiki/Well-known_text
 
 
About the question for how to find shape, I think you can use InternalFeatures property of InMemoryFeatureLayer, it’s a GeoCollection that contains key and value pair just like Dictionary.
            Feature feature = inMemoryLayer.InternalFeatures[vechileID + "M1"];
            BaseShape bs = feature.GetShape();
 
When you use the code above, perhaps you will encounter the warning said InternalFeatures is obsolete, don’t care it which will be fixed in the next release version.
 
About the question for how to control the OutlinePen.Color, we have two ways to make it so far.
ü        Use different layer for each shape, set different style for each layer.
ü        Use ValueStyle which allow you to match a value with data in the feature to determine how to draw that feature, you have a sample shows how to use it, you can look at DrawAFeatureBasedOnAValue at Styles catalog in HowDoI samples.
In the future, we might add new object called MapShape which can make it easily.
 
Please let me know if you have more questions
 
Thanks
James

Use different layer for each shape, set different style for each layer>> 
 How will layers effect performance, or general resources? In other words 
 can I have say 200 to 400 layers? Will that effect anything?   
  
 I am looking at the code for DrawAFeatureBasedOnAValue ask questions later.  
  
 Bob

 I don't have an exact measure to give you on the effect of performance or general resources by using different layer for each shape. James should have a more complete answer on that later.

 In the meantime, I want to let you know that we have a project in the Code Community "Map Shapes". It is designed to respond to the limitations of using InMemoryFeatureLayers in the case where each feature is displayed differently. In that project, you will also find a thourough explanation on the history and purpose of Map Shapes. In futures versions of Map Suite, Map Shapes will be part of the API itself.


code.thinkgeo.com/projects/show/mapshapes



Bob, 
  
 Have you ever tried the Code Community “Map Shapes” which mentioned by Val? I am sure that can help you. 
  
 About your question, I think it doesn’t have much difference for either way: Styles by layers or ValueStyle. 
  
 I have tested and try to follow your description that I set hundreds inMemory layers, each layer has its own member variable which will cost some memory, the main part is InternalFeatures, both way will contains the same features in memory, so the entire memory usage is pretty much the same. About the drawing speed, the styles by layers will a little slower than valueStyle, it can not aware when you play the map (such as pan, zoom in/out).  
  
 I recommend you use the ValueStyle and only keep one inMemory layer, and if you want to add more features, you can use BuildIndex to create spatial index and let the performance better. 
  
 The magnitude of your features is not so big, if the number is more than a hundred thousand or million, it’s will be the problem both way. 
  
 Let me know if you have more questions 
  
 James