Beispiel #1
0
        public static void DoTest(string[] inputWKT, string[] expectedOutputWKT, bool compareDirections)
        {
            var lineMerger = new LineMerger();

            lineMerger.Add(ToGeometries(inputWKT));
            Compare(ToGeometries(expectedOutputWKT), lineMerger.GetMergedLineStrings(), compareDirections);
        }
        public static IList <IGeometry> MergeLines(IGeometry geometry)
        {
            var lineMerger = new LineMerger();

            lineMerger.Add(geometry);
            return(lineMerger.GetMergedLineStrings());
        }
Beispiel #3
0
 public Draw(IConsole console, LineThickNess thickness = LineThickNess.Single, MergeOrOverlap mergeOrOverlap = MergeOrOverlap.Merge)
 {
     _console        = console;
     _mergeOrOverlap = mergeOrOverlap;
     _lineMerger     = new LineMerger();
     Thickness       = thickness;
 }
Beispiel #4
0
        public static Geometry MergeLines(Geometry g)
        {
            var merger = new LineMerger();

            merger.Add(g);
            var lines = merger.GetMergedLineStrings();

            return(g.Factory.BuildGeometry(lines));
        }
Beispiel #5
0
        public static IGeometry MergeLines(IGeometry g)
        {
            LineMerger merger = new LineMerger();

            merger.Add(g);
            IList <IGeometry> lines = merger.GetMergedLineStrings();

            return(g.Factory.BuildGeometry(lines));
        }
        private static IGeometry DissolveLines(IGeometry lines)
        {
            var dissolved = lines.Union();
            var merger    = new LineMerger();

            merger.Add(dissolved);
            var mergedColl = merger.GetMergedLineStrings();
            var merged     = lines.Factory.BuildGeometry(mergedColl);

            return(merged);
        }
Beispiel #7
0
        internal virtual void Run()
        {
            IList <IGeometry> lineStrings = Data;

            LineMerger lineMerger = new LineMerger();

            lineMerger.Add(lineStrings);
            var mergedLineStrings = lineMerger.GetMergedLineStrings();

            Console.WriteLine("Lines formed (" + mergedLineStrings.Count + "):");
            foreach (var obj in mergedLineStrings)
            {
                Console.WriteLine(obj);
            }
        }
Beispiel #8
0
        internal virtual void Run()
        {
            IList lineStrings = Data;

            LineMerger lineMerger = new LineMerger();

            lineMerger.Add(lineStrings);
            ICollection mergedLineStrings = lineMerger.GetMergedLineStrings();

            Console.WriteLine("Lines formed (" + mergedLineStrings.Count + "):");
            foreach (object obj in mergedLineStrings)
            {
                Console.WriteLine(obj);
            }
        }
Beispiel #9
0
        /** erzeugt aus einer Collection von LineString's eine Collection von LineStrings,
         *  wo die zusammengehörende Teile zu einem LineString zusammengefasst werden
         * @param geometry
         * @return
         */
        public static List <ILineString> mergeLines(IGeometryFactory gf, List <ILineString> lines)
        {
            LineMerger lineMerger = new LineMerger();

            List <ILineString> .Enumerator iterator = lines.GetEnumerator();
            while (iterator.MoveNext())
            {
                lineMerger.Add(iterator.Current);
            }
            List <ILineString> mergedLineStrings = lineMerger.GetMergedLineStrings().Select(g => (ILineString)g).ToList();

            foreach (LineString line in mergedLineStrings)
            {
                line.SRID = gf.SRID;
            }
            return(mergedLineStrings);
        }
        /**
         * Nodes a LineString and returns a List of Noded LineString's. Used to
         * repare auto-intersecting LineString and Polygons. This method cannot
         * process CoordinateSequence. The noding process is limited to 3d
         * geometries.<br/>
         * Preserves duplicate coordinates.
         *
         * @param coords coordinate array to be noded
         * @param gf geometryFactory to use
         * @return a list of noded LineStrings
         */
        private ISet <LineString> nodeLineString(Coordinate[] coords, GeometryFactory gf)
        {
            MCIndexNoder noder = new MCIndexNoder();

            noder.SegmentIntersector = new IntersectionAdder(new RobustLineIntersector());
            List <ISegmentString> list = new ();

            list.Add(new NodedSegmentString(coords, null));
            noder.ComputeNodes(list);
            List <LineString> lineStringList = new ();

            foreach (NetTopologySuite.Geometries.Geometry segmentString in noder.GetNodedSubstrings())
            {
                lineStringList.Add(gf.CreateLineString(
                                       segmentString.Coordinates
                                       ));
            }

            // WARNING : merger loose original linestrings
            // It is useful for LinearRings but should not be used for (Multi)LineStrings
            LineMerger merger = new LineMerger();

            merger.Add(lineStringList);
            lineStringList = (List <LineString>)merger.GetMergedLineStrings();

            // Remove duplicate linestrings preserving main orientation
            ISet <LineString> lineStringSet = new HashSet <LineString>();

            foreach (LineString line in lineStringList)
            {
                // TODO as equals makes a topological comparison, comparison with line.reverse maybe useless
                if (!lineStringSet.Contains(line) && !lineStringSet.Contains((LineString)line.Reverse()))
                {
                    lineStringSet.Add(line);
                }
            }
            return(lineStringSet);
        }
Beispiel #11
0
        /// <summary>
        /// Gets the contours from the raster.
        /// </summary>
        /// <param name="rst">Raster to get the contours from.</param>
        /// <param name="x">The x values.</param>
        /// <param name="y">The y values.</param>
        /// <param name="zlev">Level to get the contours for.</param>
        /// <returns>The contours that were found.</returns>
        public static IList <IGeometry> GetContours(ref Raster rst, double[] x, double[] y, double zlev)
        {
            List <LineString> lsList = new List <LineString>();

            double[] xx = new double[3];
            double[] yy = new double[3];
            double[] zz = new double[3];

            for (int j = 0; j < rst.NumColumns - 1; j++)
            {
                bool jpari = (int)(j / 2.0) * 2 == j;

                for (int i = 0; i < rst.NumRows - 1; i++)
                {
                    bool ipari = (int)(i / 2.0) * 2 == i;

                    if (jpari == ipari)
                    {
                        xx[0] = x[j];
                        yy[0] = y[i];
                        zz[0] = rst.Value[i, j];

                        xx[1] = x[j];
                        yy[1] = y[i + 1];
                        zz[1] = rst.Value[i + 1, j];

                        xx[2] = x[j + 1];
                        yy[2] = y[i];
                        zz[2] = rst.Value[i, j + 1];

                        Coordinate[] c = Intersect(xx, yy, zz, zlev);
                        if (c != null)
                        {
                            lsList.Add(new LineString(c));
                        }

                        xx[0] = x[j + 1];
                        yy[0] = y[i];
                        zz[0] = rst.Value[i, j + 1];

                        xx[1] = x[j];
                        yy[1] = y[i + 1];
                        zz[1] = rst.Value[i + 1, j];

                        xx[2] = x[j + 1];
                        yy[2] = y[i + 1];
                        zz[2] = rst.Value[i + 1, j + 1];

                        Coordinate[] c1 = Intersect(xx, yy, zz, zlev);
                        if (c1 != null)
                        {
                            lsList.Add(new LineString(c1));
                        }
                    }
                    else
                    {
                        xx[0] = x[j];
                        yy[0] = y[i];
                        zz[0] = rst.Value[i, j];

                        xx[1] = x[j];
                        yy[1] = y[i + 1];
                        zz[1] = rst.Value[i + 1, j];

                        xx[2] = x[j + 1];
                        yy[2] = y[i + 1];
                        zz[2] = rst.Value[i + 1, j + 1];

                        Coordinate[] c = Intersect(xx, yy, zz, zlev);
                        if (c != null)
                        {
                            lsList.Add(new LineString(c));
                        }

                        xx[0] = x[j];
                        yy[0] = y[i];
                        zz[0] = rst.Value[i, j];

                        xx[1] = x[j + 1];
                        yy[1] = y[i + 1];
                        zz[1] = rst.Value[i + 1, j + 1];

                        xx[2] = x[j + 1];
                        yy[2] = y[i];
                        zz[2] = rst.Value[i, j + 1];

                        Coordinate[] c1 = Intersect(xx, yy, zz, zlev);
                        if (c1 != null)
                        {
                            lsList.Add(new LineString(c1));
                        }
                    }
                }
            }

            LineMerger lm = new LineMerger();

            lm.Add(lsList);

            IList <IGeometry> merged = lm.GetMergedLineStrings();

            return(merged);
        }
        public IEnumerable <IdentifiedObject> Transform(CimContext context, IEnumerable <IdentifiedObject> input)
        {
            HashSet <PhysicalNetworkModel.IdentifiedObject> dropList = new HashSet <IdentifiedObject>();

            List <PhysicalNetworkModel.IdentifiedObject> addList = new List <IdentifiedObject>();

            foreach (var inputCimObject in input)
            {
                // Find connectivity nodes outside substations
                if (inputCimObject is ConnectivityNode && !dropList.Contains(inputCimObject) && !inputCimObject.IsInsideSubstation(context))
                {
                    ConnectivityNode cn = inputCimObject as ConnectivityNode;

                    // Handle that acls cn relationship might have changed
                    var cnNeighborsx = cn.GetNeighborConductingEquipments(context);

                    // If two acls and we are above low voltage, merge them
                    if (cnNeighborsx.Count(o => o.BaseVoltage > 5000) > 0)
                    {
                        // acls <-> acls
                        if (cnNeighborsx.Count(o => o is ACLineSegmentExt) == 2)
                        {
                            var acls = cnNeighborsx.Where(o => o is ACLineSegmentExt).ToList();

                            var acls1 = acls[0] as ACLineSegment;
                            var acls2 = acls[1] as ACLineSegment;

                            // NEVER MERGE
                            bool theSame = false;

                            // Compate bch
                            if (acls1.bch != null && acls2.bch != null && !CompareAclsValue(acls1.length.Value, acls1.bch.Value, acls2.length.Value, acls2.bch.Value))
                            {
                                theSame = false;
                            }

                            // Compate b0ch
                            if (acls1.b0ch != null && acls2.b0ch != null && !CompareAclsValue(acls1.length.Value, acls1.b0ch.Value, acls2.length.Value, acls2.b0ch.Value))
                            {
                                theSame = false;
                            }

                            // Compare gch
                            if (acls1.gch != null && acls2.gch != null && !CompareAclsValue(acls1.length.Value, acls1.gch.Value, acls2.length.Value, acls2.gch.Value))
                            {
                                theSame = false;
                            }

                            // Compare g0ch
                            if (acls1.g0ch != null && acls2.g0ch != null && !CompareAclsValue(acls1.length.Value, acls1.g0ch.Value, acls2.length.Value, acls2.g0ch.Value))
                            {
                                theSame = false;
                            }

                            // Compare r
                            if (acls1.r != null && acls2.r != null && !CompareAclsValue(acls1.length.Value, acls1.r.Value, acls2.length.Value, acls2.r.Value))
                            {
                                theSame = false;
                            }

                            // Compare r0
                            if (acls1.r0 != null && acls2.r0 != null && !CompareAclsValue(acls1.length.Value, acls1.r0.Value, acls2.length.Value, acls2.r0.Value))
                            {
                                theSame = false;
                            }

                            // Compare x
                            if (acls1.x != null && acls2.x != null && !CompareAclsValue(acls1.length.Value, acls1.x.Value, acls2.length.Value, acls2.x.Value))
                            {
                                theSame = false;
                            }

                            // Compare x0
                            if (acls1.x0 != null && acls2.x0 != null && !CompareAclsValue(acls1.length.Value, acls1.x0.Value, acls2.length.Value, acls2.x0.Value))
                            {
                                theSame = false;
                            }


                            // If the cables have the same eletrical charastica, merge them
                            if (theSame)
                            {
                                // ACLS 1 will survive, ACLS 2 and the CN will die
                                dropList.Add(cn);
                                dropList.Add(acls2);

                                // drop acls 2 terminals
                                foreach (var tc in context.GetConnections(acls2))
                                {
                                    dropList.Add(tc.Terminal);
                                }

                                var loc1 = context.GetObject <LocationExt>(acls1.Location.@ref);
                                var loc2 = context.GetObject <LocationExt>(acls2.Location.@ref);

                                LineMerger lm = new LineMerger();

                                // Convert to NTS geometries
                                lm.Add(GetGeometry(loc1));
                                lm.Add(GetGeometry(loc2));

                                // Merge the two line strings
                                var mergedLineList = lm.GetMergedLineStrings();

                                if (mergedLineList.Count != 1)
                                {
                                    throw new Exception("Cannot merge ACLS: " + acls1.mRID + " and " + acls2.mRID);
                                }

                                // Overwrite loc 1 coordinated with merged strings
                                loc1.coordinates = GetPoints((ILineString)mergedLineList[0]).ToArray();

                                // Sum length
                                acls1.length.Value += acls2.length.Value;

                                // Sum bch
                                if (acls1.bch != null && acls2.bch != null)
                                {
                                    acls1.bch.Value += acls2.bch.Value;
                                }

                                // Sum b0ch
                                if (acls1.b0ch != null && acls2.b0ch != null)
                                {
                                    acls1.b0ch.Value += acls2.b0ch.Value;
                                }

                                // Sum gch
                                if (acls1.gch != null && acls2.gch != null)
                                {
                                    acls1.gch.Value += acls2.gch.Value;
                                }

                                // Sum g0ch
                                if (acls1.g0ch != null && acls2.g0ch != null)
                                {
                                    acls1.g0ch.Value += acls2.g0ch.Value;
                                }

                                // Sum r
                                if (acls1.r != null && acls2.r != null)
                                {
                                    acls1.r.Value += acls2.r.Value;
                                }

                                // Sum r0
                                if (acls1.r0 != null && acls2.r0 != null)
                                {
                                    acls1.r0.Value += acls2.r0.Value;
                                }

                                // Sum x
                                if (acls1.x != null && acls2.x != null)
                                {
                                    acls1.x.Value += acls2.x.Value;
                                }

                                // Sum x0
                                if (acls1.x0 != null && acls2.x0 != null)
                                {
                                    acls1.x0.Value += acls2.x0.Value;
                                }

                                // Find cn in the other end of ACLS 2
                                var acls2otherEndCn = context.GetConnections(acls2).Find(o => o.ConnectivityNode != cn);

                                // Get terminal of ACLS 1 that point to ACLS 2
                                var acls1Terminal = acls1.GetTerminal(acls2, true, context);

                                // Disconnect ACLS 2 terminals
                                var acls2connections = context.GetConnections(acls2);

                                List <Terminal> terminalsToDisconnect = new List <Terminal>();

                                foreach (var acls2con in acls2connections)
                                {
                                    terminalsToDisconnect.Add(acls2con.Terminal);
                                }

                                foreach (var t2d in terminalsToDisconnect)
                                {
                                    context.DisconnectTerminalFromConnectitityNode(t2d);
                                }

                                // Change terminal of ACLS 1 to point to ACLS 2 other end CN
                                context.ConnectTerminalToAnotherConnectitityNode(acls1Terminal, acls2otherEndCn.ConnectivityNode);
                            }
                            else
                            {
                                // Cable are not the same, we need to add a susbstation to act as a junction

                                // Create muffe station
                                var st = new PhysicalNetworkModel.Substation();
                                st.mRID    = Guid.NewGuid().ToString();
                                st.name    = "Junction";
                                st.PSRType = "Junction";
                                addList.Add(st);

                                // Create voltage level
                                var vl = new PhysicalNetworkModel.VoltageLevel();
                                vl.mRID                = Guid.NewGuid().ToString();
                                vl.BaseVoltage         = acls1.BaseVoltage;
                                vl.name                = "VL";
                                vl.EquipmentContainer1 = new VoltageLevelEquipmentContainer()
                                {
                                    @ref = st.mRID
                                };
                                addList.Add(vl);

                                // Relate cn to voltage level
                                if (_mappingContext.ConnectivityNodeToVoltageLevel.ContainsKey(cn))
                                {
                                    _mappingContext.ConnectivityNodeToVoltageLevel.Remove(cn);
                                }

                                _mappingContext.ConnectivityNodeToVoltageLevel.Add(cn, vl);
                            }
                        }
                        // <> 2 kabler
                        else
                        {
                            // Create muffe station
                            var st = new PhysicalNetworkModel.Substation();
                            st.mRID    = Guid.NewGuid().ToString();
                            st.name    = "MUFFE";
                            st.PSRType = "Junction";
                            addList.Add(st);

                            // Create voltage level
                            var vl = new PhysicalNetworkModel.VoltageLevel();
                            vl.mRID                = Guid.NewGuid().ToString();
                            vl.BaseVoltage         = cnNeighborsx[0].BaseVoltage;
                            vl.name                = "VL";
                            vl.EquipmentContainer1 = new VoltageLevelEquipmentContainer()
                            {
                                @ref = st.mRID
                            };
                            addList.Add(vl);

                            // Relate cn to voltage level
                            if (_mappingContext.ConnectivityNodeToVoltageLevel.ContainsKey(cn))
                            {
                                _mappingContext.ConnectivityNodeToVoltageLevel.Remove(cn);
                            }

                            _mappingContext.ConnectivityNodeToVoltageLevel.Add(cn, vl);
                        }
                    }
                }
            }

            // return objects, except the one dropped
            foreach (var inputObj in input)
            {
                if (!dropList.Contains(inputObj))
                {
                    yield return(inputObj);
                }
            }

            // yield added objects,
            foreach (var inputObj in addList)
            {
                yield return(inputObj);
            }
        }
Beispiel #13
0
        public static IList <IGeometry> GetContours(ref DotSpatial.Data.Raster rst, double[] x, double[] y, double zlev)
        {
            List <LineString> lsList = new List <LineString>();

            bool ipari, jpari;

            double[] xx = new double[3];
            double[] yy = new double[3];
            double[] zz = new double[3];

            for (int j = 0; j < rst.NumColumns - 1; j++)
            {
                if (((int)((double)j / 2.0)) * 2 == j)
                {
                    jpari = true;
                }
                else
                {
                    jpari = false;
                }

                for (int i = 0; i < rst.NumRows - 1; i++)
                {
                    if (((int)((double)i / 2.0)) * 2 == i)
                    {
                        ipari = true;
                    }
                    else
                    {
                        ipari = false;
                    }

                    if (!jpari && !ipari || jpari && ipari)
                    {
                        xx[0] = x[j];
                        yy[0] = y[i];
                        zz[0] = rst.Value[i, j];

                        xx[1] = x[j];
                        yy[1] = y[i + 1];
                        zz[1] = rst.Value[i + 1, j];

                        xx[2] = x[j + 1];
                        yy[2] = y[i];
                        zz[2] = rst.Value[i, j + 1];

                        {
                            Coordinate[] c = Intersect(xx, yy, zz, zlev);
                            if (c != null)
                            {
                                lsList.Add(new LineString(c));
                            }
                        }

                        xx[0] = x[j + 1];
                        yy[0] = y[i];
                        zz[0] = rst.Value[i, j + 1];

                        xx[1] = x[j];
                        yy[1] = y[i + 1];
                        zz[1] = rst.Value[i + 1, j];

                        xx[2] = x[j + 1];
                        yy[2] = y[i + 1];
                        zz[2] = rst.Value[i + 1, j + 1];

                        {
                            Coordinate[] c = Intersect(xx, yy, zz, zlev);
                            if (c != null)
                            {
                                lsList.Add(new LineString(c));
                            }
                        }
                    }

                    if (jpari && !ipari || !jpari && ipari)
                    {
                        xx[0] = x[j];
                        yy[0] = y[i];
                        zz[0] = rst.Value[i, j];

                        xx[1] = x[j];
                        yy[1] = y[i + 1];
                        zz[1] = rst.Value[i + 1, j];

                        xx[2] = x[j + 1];
                        yy[2] = y[i + 1];
                        zz[2] = rst.Value[i + 1, j + 1];

                        {
                            Coordinate[] c = Intersect(xx, yy, zz, zlev);
                            if (c != null)
                            {
                                lsList.Add(new LineString(c));
                            }
                        }

                        xx[0] = x[j];
                        yy[0] = y[i];
                        zz[0] = rst.Value[i, j];

                        xx[1] = x[j + 1];
                        yy[1] = y[i + 1];
                        zz[1] = rst.Value[i + 1, j + 1];

                        xx[2] = x[j + 1];
                        yy[2] = y[i];
                        zz[2] = rst.Value[i, j + 1];

                        {
                            Coordinate[] c = Intersect(xx, yy, zz, zlev);
                            if (c != null)
                            {
                                lsList.Add(new LineString(c));
                            }
                        }
                    }
                }
            }

            LineMerger lm = new LineMerger();

            lm.Add(lsList);

            IList <IGeometry> merged = (IList <IGeometry>)lm.GetMergedLineStrings();

            return(merged);
        }
Beispiel #14
0
        private Geometry ComputeHull(IList <QuadEdgeTriangle> tris)
        {
            var quadEdgeTriangleList = new SortedList <QuadEdge, QuadEdgeTriangle>(new QuadEdgeComparer());

            foreach (var triangle in tris)
            {
                CheckMaxEdgeAndListBorderTriangles(quadEdgeTriangleList, triangle);
            }

            while (quadEdgeTriangleList.Count != 0)
            {
                var triangle = quadEdgeTriangleList.Last();
                var edge     = triangle.Key;
                Debug.WriteLine(triangle.Value);
                quadEdgeTriangleList.Remove(edge);

                if (edge.Length < _tolerance)
                {
                    break;
                }

                if (!RemovalGivesRegularPolygon(triangle.Value))
                {
                    continue;
                }

                var neighbours = triangle.Value.GetNeighbours();
                triangle.Value.Kill();
                foreach (var neighbour in from n in neighbours
                         where n != null && n.IsLive()
                         select n)
                {
                    Debug.WriteLine(neighbour);
                    CheckMaxEdgeAndListBorderTriangles(quadEdgeTriangleList, neighbour);
                }
            }
            var lineMerger = new LineMerger();
            var fact       = _geom.Factory;

            foreach (var triangle in tris)
            {
                if (!triangle.IsLive())
                {
                    continue;
                }
                for (int i = 0; i < 3; i++)
                {
                    if (triangle.GetAdjacentTriangleAcrossEdge(i)?.IsLive() != true)
                    {
                        lineMerger.Add(triangle.GetEdge(i).ToLineSegment().ToGeometry(fact));
                    }
                }
            }
            var mergedLine = (LineString)lineMerger.GetMergedLineStrings().FirstOrDefault();

            if (mergedLine?.IsRing ?? false)
            {
                var concaveHull = fact.CreatePolygon(mergedLine.CoordinateSequence);
                return(concaveHull);
            }
            else
            {
                throw new TopologyException("Can't generate a hull.");
            }
        }