public static Geometry GridPoints(Geometry g, int nCells)
        {
            var env      = FunctionsUtil.GetEnvelopeOrDefault(g);
            var geomFact = FunctionsUtil.GetFactoryOrDefault(g);

            int nCellsOnSideY = (int)Math.Sqrt(nCells);
            int nCellsOnSideX = nCells / nCellsOnSideY;

            double cellSizeX = env.Width / (nCellsOnSideX - 1);
            double cellSizeY = env.Height / (nCellsOnSideY - 1);

            var pts = new CoordinateList();

            for (int i = 0; i < nCellsOnSideX; i++)
            {
                for (int j = 0; j < nCellsOnSideY; j++)
                {
                    double x = env.MinX + i * cellSizeX;
                    double y = env.MinY + j * cellSizeY;

                    pts.Add(new Coordinate(x, y));
                }
            }

            return(geomFact.CreateMultiPointFromCoords(pts.ToCoordinateArray()));
        }
        public static Geometry ToPoints(Geometry g1, Geometry g2)
        {
            var geoms = FunctionsUtil.BuildGeometry(g1, g2);

            return(FunctionsUtil.GetFactoryOrDefault(new[] { g1, g2 })
                   .CreateMultiPointFromCoords(geoms.Coordinates));
        }
Ejemplo n.º 3
0
        private static IGeometry CreateJ(IGeometry g)
        {
            var gf = FunctionsUtil.GetFactoryOrDefault(g);

            var jTop = new Coordinate[]
            {
                new Coordinate(0, HEIGHT),
                new Coordinate(J_WIDTH, HEIGHT),
                new Coordinate(J_WIDTH, J_RADIUS)
            };
            var jBottom = new Coordinate[]
            {
                new Coordinate(J_WIDTH - J_RADIUS, 0),
                new Coordinate(0, 0)
            };

            var gsf = new GeometricShapeFactory(gf);

            gsf.Base      = new Coordinate(J_WIDTH - 2 * J_RADIUS, 0);
            gsf.Size      = 2 * J_RADIUS;
            gsf.NumPoints = 10;
            var jArc = gsf.CreateArc(1.5 * Math.PI, 0.5 * Math.PI);

            var coordList = new CoordinateList();

            coordList.Add(jTop, false);
            coordList.Add(((IGeometry)jArc).Reverse().Coordinates, false, 1, jArc.NumPoints - 1);
            coordList.Add(jBottom, false);

            return(gf.CreateLineString(coordList.ToCoordinateArray()));
        }
        public static IGeometry Grid(IGeometry g, int nCells)
        {
            var geoms = new List <IGeometry>();

            var env      = FunctionsUtil.GetEnvelopeOrDefault(g);
            var geomFact = FunctionsUtil.GetFactoryOrDefault(g);

            var nCellsOnSide = (int)Math.Sqrt(nCells) + 1;
            var cellSizeX    = env.Width / nCellsOnSide;
            var cellSizeY    = env.Height / nCellsOnSide;

            for (var i = 0; i < nCellsOnSide; i++)
            {
                for (var j = 0; j < nCellsOnSide; j++)
                {
                    var x1      = env.MinX + i * cellSizeX;
                    var y1      = env.MinY + j * cellSizeY;
                    var x2      = env.MinX + (i + 1) * cellSizeX;
                    var y2      = env.MinY + (j + 1) * cellSizeY;
                    var cellEnv = new Envelope(x1, x2, y1, y2);

                    geoms.Add(geomFact.ToGeometry(cellEnv));
                }
            }
            return(geomFact.BuildGeometry(geoms));
        }
Ejemplo n.º 5
0
        public static IGeometry RandomSegmentsInGrid(IGeometry g, int nPts)
        {
            var env      = FunctionsUtil.GetEnvelopeOrDefault(g);
            var geomFact = FunctionsUtil.GetFactoryOrDefault(g);

            int nCell = (int)Math.Sqrt(nPts) + 1;

            double xLen = env.Width / nCell;
            double yLen = env.Height / nCell;

            var lines = new List <IGeometry>();

            for (int i = 0; i < nCell; i++)
            {
                for (int j = 0; j < nCell; j++)
                {
                    double x0 = env.MinX + i * xLen + xLen * RND.NextDouble();
                    double y0 = env.MinY + j * yLen + yLen * RND.NextDouble();
                    double x1 = env.MinX + i * xLen + xLen * RND.NextDouble();
                    double y1 = env.MinY + j * yLen + yLen * RND.NextDouble();
                    lines.Add(geomFact.CreateLineString(new[]
                    {
                        new Coordinate(x0, y0), new Coordinate(x1, y1)
                    }));
                }
            }
            return(geomFact.BuildGeometry(lines));
        }
        public static Geometry ToLines(Geometry g1, Geometry g2)
        {
            var geoms = FunctionsUtil.BuildGeometry(g1, g2);

            return(FunctionsUtil.GetFactoryOrDefault(new[] { g1, g2 })
                   .BuildGeometry(LinearComponentExtracter.GetLines(geoms)));
        }
Ejemplo n.º 7
0
        public static IGeometry RandomRadialPoints(IGeometry g, int nPts)
        {
            var    env      = FunctionsUtil.GetEnvelopeOrDefault(g);
            var    geomFact = FunctionsUtil.GetFactoryOrDefault(g);
            double xLen     = env.Width;
            double yLen     = env.Height;
            double rMax     = Math.Min(xLen, yLen) / 2.0;

            double centreX = env.MinX + xLen / 2;
            double centreY = env.MinY + yLen / 2;

            var pts = new List <IPoint>();

            for (int i = 0; i < nPts; i++)
            {
                double rand = RND.NextDouble();
                //use rand^2 to accentuate radial distribution
                double r   = rMax * rand * rand;
                double ang = 2 * Math.PI * RND.NextDouble();
                double x   = centreX + r * Math.Cos(ang);
                double y   = centreY + r * Math.Sin(ang);
                pts.Add(geomFact.CreatePoint(new Coordinate(x, y)));
            }
            return(geomFact.BuildGeometry(pts.ToArray()));
        }
        public static IGeometry Circumcentre(IGeometry g)
        {
            Coordinate[]     pts      = TrianglePts(g);
            Coordinate       cc       = Triangle.Circumcentre(pts[0], pts[1], pts[2]);
            IGeometryFactory geomFact = FunctionsUtil.GetFactoryOrDefault(g);

            return(geomFact.CreatePoint(cc));
        }
Ejemplo n.º 9
0
        public static Geometry Circumcentre(Geometry g)
        {
            var pts      = TrianglePts(g);
            var cc       = Triangle.Circumcentre(pts[0], pts[1], pts[2]);
            var geomFact = FunctionsUtil.GetFactoryOrDefault(g);

            return(geomFact.CreatePoint(cc));
        }
Ejemplo n.º 10
0
        public static IGeometry Incentre(IGeometry g)
        {
            var pts      = TrianglePts(g);
            var t        = new Triangle(pts[0], pts[1], pts[2]);
            var cc       = t.InCentre();
            var geomFact = FunctionsUtil.GetFactoryOrDefault(g);

            return(geomFact.CreatePoint(cc));
        }
 public static Geometry Centroid(Geometry g)
 {
     return(GeometryMapper.Map(g, itm =>
     {
         var pts = TrianglePts(itm);
         var cc = Triangle.Centroid(pts[0], pts[1], pts[2]);
         var geomFact = FunctionsUtil.GetFactoryOrDefault(g);
         return geomFact.CreatePoint(cc);
     }));
 }
        public static Geometry FindInteriorNodes(Geometry geom)
        {
            var intFinder = NodingIntersectionFinder.CreateInteriorIntersectionsFinder(new RobustLineIntersector());

            ProcessNodes(geom, intFinder);
            var intPts = intFinder.Intersections;

            return(FunctionsUtil.GetFactoryOrDefault((Geometry)null)
                   .CreateMultiPointFromCoords(Dedup(intPts)));
        }
        public static Geometry AngleBisectors(Geometry g)
        {
            var pts      = TrianglePts(g);
            var cc       = Triangle.InCentre(pts[0], pts[1], pts[2]);
            var geomFact = FunctionsUtil.GetFactoryOrDefault(g);
            var line     = new LineString[3];

            line[0] = geomFact.CreateLineString(new Coordinate[] { pts[0], cc });
            line[1] = geomFact.CreateLineString(new Coordinate[] { pts[1], cc });
            line[2] = geomFact.CreateLineString(new Coordinate[] { pts[2], cc });
            return(geomFact.CreateMultiLineString(line));
        }
        public static Geometry FindOneNode(Geometry geom)
        {
            var  nv     = new FastNodingValidator(SegmentStringUtil.ExtractNodedSegmentStrings(geom));
            bool temp   = nv.IsValid;
            var  intPts = nv.Intersections;

            if (intPts.Count == 0)
            {
                return(null);
            }
            return(FunctionsUtil.GetFactoryOrDefault((Geometry)null).CreatePoint((Coordinate)intPts[0]));
        }
Ejemplo n.º 15
0
        private static IGeometry FromSegmentStrings(IList <ISegmentString> segStrings)
        {
            var lines = new ILineString[segStrings.Count];
            int index = 0;

            foreach (var ss in segStrings)
            {
                var line = FunctionsUtil.GetFactoryOrDefault(null).CreateLineString(ss.Coordinates);
                lines[index++] = line;
            }
            return(FunctionsUtil.GetFactoryOrDefault(null).CreateMultiLineString(lines));
        }
        public static IGeometry AngleBisectors(IGeometry g)
        {
            Coordinate[]     pts      = TrianglePts(g);
            Triangle         t        = new Triangle(pts[0], pts[1], pts[2]);
            Coordinate       cc       = t.InCentre();
            IGeometryFactory geomFact = FunctionsUtil.GetFactoryOrDefault(g);

            ILineString[] line = new ILineString[3];
            line[0] = geomFact.CreateLineString(new[] { pts[0], cc });
            line[1] = geomFact.CreateLineString(new[] { pts[1], cc });
            line[2] = geomFact.CreateLineString(new[] { pts[2], cc });
            return(geomFact.CreateMultiLineString(line));
        }
Ejemplo n.º 17
0
        /// <summary>
        /// Reduces precision pointwise, then snap-rounds.
        /// Note that output set may not contain non-unique linework
        /// (and thus cannot be used as input to Polygonizer directly).
        /// UnaryUnion is one way to make the linework unique.
        /// </summary>
        /// <param name="geom">A geometry containing linework to node</param>
        /// <param name="scaleFactor">the precision model scale factor to use</param>
        /// <returns>The noded, snap-rounded linework</returns>
        public static Geometry SnapRoundWithPointwisePrecisionReduction(Geometry geom, double scaleFactor)
        {
            var pm = new PrecisionModel(scaleFactor);

            var roundedGeom = GeometryPrecisionReducer.ReducePointwise(geom, pm);

            var geomList = new List <Geometry>();

            geomList.Add(roundedGeom);

            var noder = new GeometryNoder(pm);
            var lines = noder.Node(geomList);

            return(FunctionsUtil.GetFactoryOrDefault(geom).BuildGeometry(lines.Cast <Geometry>().ToArray()));
        }
Ejemplo n.º 18
0
        public static IGeometry ComponentBuffers(IGeometry g, double distance)
        {
            var bufs = new List <IGeometry>();

            foreach (var comp in new GeometryCollectionEnumerator((IGeometryCollection)g))
            {
                if (comp is IGeometryCollection)
                {
                    continue;
                }
                bufs.Add(comp.Buffer(distance));
            }
            return(FunctionsUtil.GetFactoryOrDefault(g)
                   .CreateGeometryCollection(GeometryFactory.ToGeometryArray(bufs)));
        }
        /// <summary>
        /// Nodes the linework of a set of Geometrys using SnapRounding.
        /// </summary>
        /// <param name="geoms">A collection of geometries</param>
        /// <returns>A collection of LineString geometries representing the noded linework of the input</returns>
        public ICollection <IGeometry> Node(ICollection <IGeometry> geoms)
        {
            // get geometry factory
            _geomFact = FunctionsUtil.GetFactoryOrDefault(geoms);

            var segStrings = ExtractSegmentStrings(geoms);
            //Noder sr = new SimpleSnapRounder(pm);
            var sr = new MCIndexSnapRounder(_pm);

            sr.ComputeNodes(segStrings);

            var nodedLines = GetNodedLines(segStrings);

            return(nodedLines);
        }
Ejemplo n.º 20
0
        public static IGeometry NodeWithPointwisePrecision(IGeometry geom, double scaleFactor)
        {
            var pm = new PrecisionModel(scaleFactor);

            var roundedGeom = SimpleGeometryPrecisionReducer.Reduce(geom, pm);

            var geomList = new List <IGeometry>();

            geomList.Add(roundedGeom);

            var noder = new GeometryNoder(pm);
            var lines = noder.Node(geomList);

            return(FunctionsUtil.GetFactoryOrDefault(geom).BuildGeometry(CollectionUtil.Cast <IGeometry>((ICollection)lines)));
        }
Ejemplo n.º 21
0
        /// <summary>Reduces precision pointwise, then snap-rounds.
        /// <para/>
        /// Note that output set may not contain non-unique linework
        /// (and thus cannot be used as input to Polygonizer directly).
        /// <c>UnaryUnion</c> is one way to make the linework unique.
        /// </summary>
        /// <param name="geom">A Geometry containing linework to node</param>
        /// <param name="scaleFactor">The precision model scale factor to use</param>
        /// <returns>The noded, snap-rounded linework</returns>
        public static IGeometry SnapRoundLines(
            IGeometry geom, double scaleFactor)
        {
            IPrecisionModel pm = new PrecisionModel(scaleFactor);

            var roundedGeom = GeometryPrecisionReducer.ReducePointwise(geom, pm);

            var geomList = new List <IGeometry>();

            geomList.Add(roundedGeom);

            var noder = new GeometrySnapRounder(pm);
            var lines = noder.Node(geomList);

            return(FunctionsUtil.GetFactoryOrDefault(geom).BuildGeometry(lines));
        }
Ejemplo n.º 22
0
        public static Geometry PerpendicularBisectors(Geometry g)
        {
            var pts      = TrianglePts(g);
            var cc       = Triangle.Circumcentre(pts[0], pts[1], pts[2]);
            var geomFact = FunctionsUtil.GetFactoryOrDefault(g);
            var line     = new LineString[3];
            var p0       = (new LineSegment(pts[1], pts[2])).ClosestPoint(cc);

            line[0] = geomFact.CreateLineString(new[] { p0, cc });
            var p1 = (new LineSegment(pts[0], pts[2])).ClosestPoint(cc);

            line[1] = geomFact.CreateLineString(new[] { p1, cc });
            var p2 = (new LineSegment(pts[0], pts[1])).ClosestPoint(cc);

            line[2] = geomFact.CreateLineString(new[] { p2, cc });
            return(geomFact.CreateMultiLineString(line));
        }
Ejemplo n.º 23
0
        public static IGeometry RandomLineString(IGeometry g, int nPts)
        {
            var    env      = FunctionsUtil.GetEnvelopeOrDefault(g);
            var    geomFact = FunctionsUtil.GetFactoryOrDefault(g);
            double width    = env.Width;
            double hgt      = env.Height;

            var pts = new Coordinate[nPts];

            for (int i = 0; i < nPts; i++)
            {
                double xLen = width * RND.NextDouble();
                double yLen = hgt * RND.NextDouble();
                pts[i] = RandomPtAround(env.Centre, xLen, yLen);
            }
            return(geomFact.CreateLineString(pts));
        }
Ejemplo n.º 24
0
        public static IGeometry RandomPoints(IGeometry g, int nPts)
        {
            var    env      = FunctionsUtil.GetEnvelopeOrDefault(g);
            var    geomFact = FunctionsUtil.GetFactoryOrDefault(g);
            double xLen     = env.Width;
            double yLen     = env.Height;

            var pts = new List <IPoint>();

            for (int i = 0; i < nPts; i++)
            {
                double x = env.MinX + xLen * RND.NextDouble();
                double y = env.MinY + yLen * RND.NextDouble();
                pts.Add(geomFact.CreatePoint(new Coordinate(x, y)));
            }
            return(geomFact.BuildGeometry(pts.ToArray()));
        }
Ejemplo n.º 25
0
        public static IGeometry CheckNoding(Geometry geom)
        {
            var segs = CreateSegmentStrings(geom);
            var nv   = new FastNodingValidator(segs);

            nv.FindAllIntersections = true;
            var res    = nv.IsValid;
            var intPts = nv.Intersections;
            var pts    = new IPoint[intPts.Count];

            for (var i = 0; i < intPts.Count; i++)
            {
                var coord = intPts[i];
                // use default factory in case intersections are not fixed
                pts[i] = FunctionsUtil.GetFactoryOrDefault(null).CreatePoint(coord);
            }
            return(FunctionsUtil.GetFactoryOrDefault(null).CreateMultiPoint(
                       pts));
        }
Ejemplo n.º 26
0
        private static IGeometry CreateS(IGeometry g)
        {
            var gf = FunctionsUtil.GetFactoryOrDefault(g);

            double centreX = WIDTH - S_RADIUS;

            var top = new[]
            {
                new Coordinate(WIDTH, HEIGHT),
                new Coordinate(centreX, HEIGHT)
            };
            var bottom = new[]
            {
                new Coordinate(centreX, 0),
                new Coordinate(WIDTH - 2 * S_RADIUS, 0)
            };

            var gsf = new GeometricShapeFactory(gf);

            gsf.Centre    = new Coordinate(centreX, HEIGHT - S_RADIUS);
            gsf.Size      = 2 * S_RADIUS;
            gsf.NumPoints = 10;
            var arcTop = gsf.CreateArc(0.5 * Math.PI, Math.PI);

            var gsf2 = new GeometricShapeFactory(gf);

            gsf2.Centre    = new Coordinate(centreX, S_RADIUS);
            gsf2.Size      = 2 * S_RADIUS;
            gsf2.NumPoints = 10;
            var arcBottom = (ILineString)((IGeometry)gsf2.CreateArc(1.5 * Math.PI, Math.PI)).Reverse();

            var coordList = new CoordinateList();

            coordList.Add(top, false);
            coordList.Add(arcTop.Coordinates, false, 1, arcTop.NumPoints - 1);
            coordList.Add(new Coordinate(centreX, HEIGHT / 2));
            coordList.Add(arcBottom.Coordinates, false, 1, arcBottom.NumPoints - 1);
            coordList.Add(bottom, false);

            return(gf.CreateLineString(coordList.ToCoordinateArray()));
        }
        private static IGeometry FontGlyph(IGeometry g, String text, Font font)
        {
            var env      = FunctionsUtil.GetEnvelopeOrDefault(g);
            var geomFact = FunctionsUtil.GetFactoryOrDefault(g);

            var textGeom = FontGlyphReader.Read(text, font, geomFact);
            var envText  = textGeom.EnvelopeInternal;

            if (g != null)
            {
                // transform to baseline
                var baseText0 = new Coordinate(envText.MinX, envText.MinY);
                var baseText1 = new Coordinate(envText.MaxX, envText.MinY);
                var baseGeom0 = new Coordinate(env.MinX, env.MinY);
                var baseGeom1 = new Coordinate(env.MaxX, env.MinY);
                AffineTransformation trans = AffineTransformationFactory.CreateFromBaseLines(baseText0, baseText1,
                                                                                             baseGeom0, baseGeom1);
                return(trans.Transform(textGeom));
            }
            return(textGeom);
        }
Ejemplo n.º 28
0
        public static IGeometry HaltonPointsWithBases(IGeometry g, int nPts, int basei, int basej)
        {
            var env   = FunctionsUtil.GetEnvelopeOrDefault(g);
            var pts   = new Coordinate[nPts];
            var baseX = env.MinX;
            var baseY = env.MinY;

            var i = 0;

            while (i < nPts)
            {
                var x = baseX + env.Width * HaltonOrdinate(i + 1, basei);
                var y = baseY + env.Height * HaltonOrdinate(i + 1, basej);
                var p = new Coordinate(x, y);
                if (env.Contains(p))
                {
                    pts[i++] = new Coordinate(p);
                }
            }
            return(FunctionsUtil.GetFactoryOrDefault(g).CreateMultiPoint(pts));
        }
Ejemplo n.º 29
0
        private static IGeometry CreateT(IGeometry g)
        {
            var gf = FunctionsUtil.GetFactoryOrDefault(g);

            var tTop = new[]
            {
                new Coordinate(J_WIDTH, HEIGHT),
                new Coordinate(WIDTH - S_RADIUS - 5, HEIGHT)
            };
            var tBottom = new[]
            {
                new Coordinate(J_WIDTH + 0.5 * T_WIDTH, HEIGHT),
                new Coordinate(J_WIDTH + 0.5 * T_WIDTH, 0)
            };
            var lines = new[]
            {
                gf.CreateLineString(tTop),
                gf.CreateLineString(tBottom)
            };

            return(gf.CreateMultiLineString(lines));
        }
Ejemplo n.º 30
0
        public static IGeometry RandomRectilinearWalk(IGeometry g, int nPts)
        {
            var    env      = FunctionsUtil.GetEnvelopeOrDefault(g);
            var    geomFact = FunctionsUtil.GetFactoryOrDefault(g);
            double xLen     = env.Width;
            double yLen     = env.Height;

            var pts = new Coordinate[nPts];

            bool xory = true;

            for (int i = 0; i < nPts; i++)
            {
                Coordinate pt;
                if (i == 0)
                {
                    pt = RandomPtAround(env.Centre, xLen, yLen);
                }
                else
                {
                    double dist = xLen * (RND.NextDouble() - 0.5);
                    double x    = pts[i - 1].X;
                    double y    = pts[i - 1].Y;
                    if (xory)
                    {
                        x += dist;
                    }
                    else
                    {
                        y += dist;
                    }
                    // switch orientation
                    xory = !xory;
                    pt   = new Coordinate(x, y);
                }
                pts[i] = pt;
            }
            return(geomFact.CreateLineString(pts));
        }