using Core.Gis.GeoApi.Geometries; using Core.GIS.NetTopologySuite.Geometries; namespace Core.GIS.NetTopologySuite.Precision { /// /// Allow computing and removing common mantissa bits from one or more Geometries. /// public class CommonBitsRemover { private readonly CommonCoordinateFilter ccFilter = new CommonCoordinateFilter(); /// /// /// public CommonBitsRemover() { CommonCoordinate = null; } /// /// The common bits of the Coordinates in the supplied Geometries. /// public ICoordinate CommonCoordinate { get; private set; } /// /// Add a point to the set of geometries whose common bits are /// being computed. After this method has executed the /// common coordinate reflects the common bits of all added /// geometries. /// /// A Geometry to test for common bits. public void Add(IGeometry geom) { geom.Apply(ccFilter); CommonCoordinate = ccFilter.CommonCoordinate; } /// /// Removes the common coordinate bits from a Geometry. /// The coordinates of the Geometry are changed. /// /// The Geometry from which to remove the common coordinate bits. /// The shifted Geometry. public IGeometry RemoveCommonBits(IGeometry geom) { if (CommonCoordinate.X == 0.0 && CommonCoordinate.Y == 0.0) { return geom; } ICoordinate invCoord = new Coordinate(CommonCoordinate); invCoord.X = -invCoord.X; invCoord.Y = -invCoord.Y; Translater trans = new Translater(invCoord); geom.Apply(trans); geom.GeometryChanged(); return geom; } /// /// Adds the common coordinate bits back into a Geometry. /// The coordinates of the Geometry are changed. /// /// The Geometry to which to add the common coordinate bits. /// The shifted Geometry. public void AddCommonBits(IGeometry geom) { Translater trans = new Translater(CommonCoordinate); geom.Apply(trans); geom.GeometryChanged(); } /// /// /// public class CommonCoordinateFilter : ICoordinateFilter { private readonly CommonBits commonBitsX = new CommonBits(); private readonly CommonBits commonBitsY = new CommonBits(); /// /// /// public ICoordinate CommonCoordinate { get { return new Coordinate(commonBitsX.Common, commonBitsY.Common); } } /// /// /// /// public void Filter(ICoordinate coord) { commonBitsX.Add(coord.X); commonBitsY.Add(coord.Y); } } /// /// /// private class Translater : ICoordinateFilter { private readonly ICoordinate trans = null; /// /// /// /// public Translater(ICoordinate trans) { this.trans = trans; } /// /// /// /// public void Filter(ICoordinate coord) { coord.X += trans.X; coord.Y += trans.Y; } } } }