ThinkGeo.com    |     Documentation    |     Premium Support

Given a list of vertex, how to draw a polygon on the map control

Hi,


Given a list of vertex ( X/Ys), how to draw a polygon on the map control programmatically?


Thank you.


 


Kathy



You basically need to add the vertices (X and Y) to the polygon. To take a concrete case, let say that you have a text file with a list of X and Y delimited by a comma on each line. Keep in mind that the last vertex need to have the same X and Y as the first vertex. See the code below on how to build the polygon and display it on the map programatically. See in bold the part specific to the polygon building:


 


StreamReader mGPSData = new StreamReader("C:\\ThinkGeo\\Support\\Posts\\7330\\polygon.txt");
 
            PolygonShape polygonShape = new PolygonShape();
            RingShape ringShape = new RingShape();
 
            string line = null; 
            
 
            while (line != "")
            {
                line = mGPSData.ReadLine();
                if (line != "")
                {
                    string[] strSplit = line.Split(',');
                    ringShape.Vertices.Add(new Vertex(Convert.ToDouble(strSplit[0]), Convert.ToDouble(strSplit[1])));
                }
                else
                {
                    break;
                }
            }
            ringShape.Vertices.Add(new Vertex(ringShape.Vertices[0].X, ringShape.Vertices[0].Y));
            polygonShape.OuterRing = ringShape;
 
 
            InMemoryFeatureLayer inMemoryFeatureLayer = new InMemoryFeatureLayer();
            inMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.CreateSimpleAreaStyle(GeoColor.FromArgb(150, GeoColor.StandardColors.Red));
            inMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
 
            inMemoryFeatureLayer.Open();
            inMemoryFeatureLayer.EditTools.BeginTransaction();
            inMemoryFeatureLayer.EditTools.Add(new Feature(polygonShape));
            inMemoryFeatureLayer.EditTools.CommitTransaction();
            inMemoryFeatureLayer.Close();
 
            LayerOverlay layerOverlay = new LayerOverlay();
            layerOverlay.Layers.Add("Polygon", inMemoryFeatureLayer);
 
            winformsMap1.Overlays.Add(layerOverlay);
 
            winformsMap1.Refresh(layerOverlay);

Hi Val, 



Thank you very much for your help. 

Now I can draw a polygon on the map by using a list of vertices. 

I also want user to be able to edit/delete the polygon on the map. How can I do that? 

Thank you. 



Kathy



Katherine you may want to look at the WinForm C# Sample Application if you have that.  
 Under Editing Feature Layers the Track and Edit Shapes.  
 This example should help you.

Bob thanks for your help. Kathy, I think the Track And Edit Shapes sample can definitely help you. 
  
 Any more questions just feel free to let me know. 
  
 Thanks. 
  
 Yale 


I let everybody know that today a project in the Code Community was published on that subject. The code above has been cleaned up and the project shows the creation of polygons from text files in a more real world situation.

 


code.thinkgeo.com/projects/show/createpolygons


 



Val, 
  
 Thanks for your works. That definitely makes things easy. 
  
 Thanks. 
  
 Yale