using GeoAPI.Geometries; using GisSharpBlog.NetTopologySuite.Geometries; namespace GisSharpBlog.NetTopologySuite.Algorithm { /// /// Computes the centroid of a linear point. /// Algorithm: /// Compute the average of the midpoints /// of all line segments weighted by the segment length. /// public class CentroidLine { private ICoordinate centSum = new Coordinate(); private double totalLength = 0.0; /// /// /// public CentroidLine() { } /// /// Adds the linestring(s) defined by a Geometry to the centroid total. /// If the point is not linear it does not contribute to the centroid. /// /// The point to add. public void Add(IGeometry geom) { if (geom is ILineString) Add(geom.Coordinates); else if (geom is IGeometryCollection) { IGeometryCollection gc = (IGeometryCollection) geom; foreach (IGeometry geometry in gc.Geometries) Add(geometry); } } /// /// /// public ICoordinate Centroid { get { ICoordinate cent = new Coordinate(); cent.X = centSum.X / totalLength; cent.Y = centSum.Y / totalLength; return cent; } } /// /// Adds the length defined by an array of coordinates. /// /// An array of Coordinates. public void Add(ICoordinate[] pts) { for (int i = 0; i < pts.Length - 1; i++) { double segmentLen = pts[i].Distance(pts[i + 1]); totalLength += segmentLen; double midx = (pts[i].X + pts[i + 1].X) / 2; centSum.X += segmentLen * midx; double midy = (pts[i].Y + pts[i + 1].Y) / 2; centSum.Y += segmentLen * midy; } } } }