Center of a polygon

Topics: Español, SharpMap Project, SharpMap v0.9 / v1.x
Aug 20, 2009 at 5:08 PM

Hi guys!

Can anyone tell me if sharpmap has a way to get the center of a polygon o a linestring?

For example:  POLYGON ((-62.8403860515939 8.25688116763887, -62.8404356104508 8.25692910148681, -62.8403858267597 8.25697806224301, -62.8403860515939 8.25688116763887))

THX in advance

Mr. Musso

Aug 21, 2009 at 7:06 AM

Something like this should help you with what you are doing to extract the coordinates.  This example below uses point files, but line files should be almost the same but perhaps a different cast (SharpMap.Geometries.Point) to something else.   Look for .Centroid in your geometry.

 

            //calculate the HASC, placename, and x,y for each city
            string sCityShapeFilePath = sCityShapeFilePath.Substring(0, sCityShapeFilePath.LastIndexOf(".")) + ".shp";
            SharpMap.Data.Providers.ShapeFile shapeFile = new SharpMap.Data.Providers.ShapeFile(sCityShapeFilePath);
            shapeFile.Open();
            int iTotal = shapeFile.GetFeatureCount();
            for (uint i = 0; i < iTotal; i++)
            {
                SharpMap.Data.FeatureDataRow row = shapeFile.GetFeature(i);
                SharpMap.Geometries.Point p = (SharpMap.Geometries.Point)row.Geometry;
                string output = "X,Y";
                //get the data
                output = p.X.ToString() + "," + p.Y.ToString();
                sb.AppendLine(output);
            }
            shapeFile.Close();