Polygon shapefile in Tutorial 4 is actually polyline

Jul 13, 2011 at 3:23 AM

Hi guys,

I searched the discussion several times and haven't found any discussion on this problem yet. If anybody knows any previous post please send me the link.

I'm currently writing an app using DotSpatial dlls. When using Tutorial 4 to know to how to create shape file, I created a polygon shapefile. But when Load the file again, I found out that the code in the tutorial actually saved a polyline shapefile.

I suspect it's because of the following snippet in Map_MouseDown event:

case "polygon":

                    if (e.Button == MouseButtons.Left)
                        //left click - fill array of coordinates
                        Coordinate coord = map1.PixelToProj(e.Location);

                        if (polygonmouseClick)
                            //first time left click - create empty line feature
                            if (firstClick)
                                //Create a new List called polygonArray.

                                //this list will store the Coordinates
                                //We are going to store the mouse click coordinates into this array.

                                List<Coordinate> polygonArray = new List<Coordinate>();

                                //Create an instance for LinearRing class.
                                //We pass the polygon List to the constructor of this class
                                LinearRing polygonGeometry = new LinearRing(polygonArray);

                                //Add the polygonGeometry instance to PolygonFeature
                                IFeature polygonFeature = polygonF.AddFeature(polygonGeometry);

                                //add first coordinate to the polygon feature

                                //set the polygon feature attribute
                                polygonID = polygonID + 1;
                                polygonFeature.DataRow["PolygonID"] = polygonID;
                                firstClick = false;
                                //second or more clicks - add points to the existing feature
                                IFeature existingFeature = (IFeature)polygonF.Features[polygonF.Features.Count - 1];


                                //refresh the map if line has 2 or more points
                                if (existingFeature.Coordinates.Count >= 3)
                                    //refresh the map
                        //right click - reset first mouse click
                        firstClick = true;

In order to create a polygon, the end point and beginning point have to be at the same coordinate. How can I change the code to achieve that?

I would very much appreciate if anyone could point me in the right direction in changing the code so that it will save a polygon shapefile.



Jul 21, 2011 at 9:36 PM

Hi Kevin,

I'm surprised if the system design needs the start node to be listed in the polygon linestring twice, but I'm new to all this. Anyway, this code snippet adds the first node

back on to the end of the node list. Whether this is the correct solution I don't know, but it seems to work ok. Unfortunately it is in Visual Basic, but you will get the idea.

              'right click - reset first mouse click
              firstClick = True
              ' David Nicholls code to add first node to end of coordinate list:
              Dim latestFeature As IFeature = polygonF.Features(polygonF.Features.Count - 1)
              Dim firstCoord As Coordinate = latestFeature.Coordinates(0)
              If latestFeature.Coordinates.Count >= 3 Then
                   'refresh the map
              End If
 End If