public void GetLength()
        {
            XYPolyline xyPolyline = new XYPolyline();

            xyPolyline.Points.Add(new XYPoint(6, 2));
            xyPolyline.Points.Add(new XYPoint(2, 2));
            xyPolyline.Points.Add(new XYPoint(8, 2));
            xyPolyline.Points.Add(new XYPoint(8, 4));
            xyPolyline.Points.Add(new XYPoint(5, 4));
            xyPolyline.Points.Add(new XYPoint(9, 7));

            Assert.AreEqual((double)20, xyPolyline.GetLength());
        }
Exemple #2
0
        public void SaveToShape(string ShapeFileName)
        {
            using (ShapeWriter sw = new ShapeWriter(ShapeFileName))
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("LinkID", typeof(string));
                dt.Columns.Add("FromNode", typeof(string));
                dt.Columns.Add("ToNode", typeof(string));
                dt.Columns.Add("SpecifiedLength", typeof(double));

                foreach (var b in Links.Values)
                {
                    GeoRefData grf = new GeoRefData();
                    var        l   = new XYPolyline();
                    l.Points.Add(new XYPoint(b.UpstreamNode.pfsnode.X, b.UpstreamNode.pfsnode.Y));
                    l.Points.Add(new XYPoint(b.DownstreamNode.pfsnode.X, b.DownstreamNode.pfsnode.Y));
                    grf.Geometry = l;
                    grf.Data     = dt.NewRow();
                    grf.Data[0]  = b.pfslink.LinkID;
                    grf.Data[1]  = b.pfslink.FromNode;
                    grf.Data[2]  = b.pfslink.ToNode;
                    grf.Data[3]  = b.pfslink.SpecifiedLength;
                    sw.Write(grf);
                }
            }

            if (Branches != null && Branches.Count > 0)
            {
                using (ShapeWriter sw = new ShapeWriter(Path.Combine(Path.GetDirectoryName(ShapeFileName), Path.GetFileNameWithoutExtension(ShapeFileName) + "_branches.shp")))
                {
                    DataTable dt = new DataTable();
                    dt.Columns.Add("Name", typeof(string));
                    dt.Columns.Add("Length", typeof(double));

                    foreach (var b in Branches)
                    {
                        var line = new XYPolyline();
                        line.Points.AddRange(b.Links.Select(p => p.UpstreamNode.Location));
                        line.Points.Add(b.Links.Last().DownstreamNode.Location);
                        GeoRefData grf = new GeoRefData();
                        grf.Geometry = line;
                        grf.Data     = dt.NewRow();
                        grf.Data[0]  = b.Name;
                        grf.Data[1]  = line.GetLength();

                        sw.Write(grf);
                    }
                }
            }
        }
        private void panelViewer_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            // write x and y coordinates
            double x = ((e.X - _margin) / _scale) + _minX;
            double y = _minY - (e.Y + _margin - panelViewer.ClientSize.Height) / _scale;

            if (_scale != double.MaxValue)
            {
                this.label3.Text = "(" + x.ToString("F3") + ", " + y.ToString("F3") + ")";
            }
            else
            {
                this.label3.Text = "";
            }


            // write elementSet ID and element index
            this.label4.Text  = " ";
            this.label7.Text  = " ";
            this.label8.Text  = " ";
            this.label9.Text  = " ";
            this.label10.Text = " ";
            this.label11.Text = " ";

            for (int elementSetNumber = 0; elementSetNumber < this._elementSets.Count; elementSetNumber++)
            {
                string elementID    = " ";
                int    elementIndex = -9;
                double distance     = 10e30;

                IElementSet elementSet = (IElementSet)_elementSets[elementSetNumber];

                if (elementSetNumber == 0)
                {
                    this.label7.Text = elementSet.Caption.Substring(0, Math.Min(20, elementSet.Caption.Length));
                }
                if (elementSetNumber == 1)
                {
                    this.label9.Text = elementSet.Caption.Substring(0, Math.Min(20, elementSet.Caption.Length));
                }

                for (int index = 0; index < elementSet.ElementCount; index++)
                {
                    if (((IElementSet)_elementSets[elementSetNumber]).ElementType == ElementType.Polygon)
                    {
                        XYPolygon xyPolygon = new XYPolygon();

                        for (int i = 0; i < elementSet.GetVertexCount(index); i++)
                        {
                            xyPolygon.Points.Add(new XYPoint(elementSet.GetVertexXCoordinate(index, i), elementSet.GetVertexYCoordinate(index, i)));
                        }

                        if (XYGeometryTools.IsPointInPolygon(x, y, xyPolygon))
                        {
                            elementID    = elementSet.GetElementId(index).Id;
                            elementIndex = index;
                        }
                    }


                    if (((IElementSet)_elementSets[elementSetNumber]).ElementType == ElementType.PolyLine)
                    {
                        XYPolyline xyPolyline = new XYPolyline();
                        for (int i = 0; i < elementSet.GetVertexCount(index); i++)
                        {
                            xyPolyline.Points.Add(new XYPoint(elementSet.GetVertexXCoordinate(index, i), elementSet.GetVertexYCoordinate(index, i)));
                        }
                        double xx = XYGeometryTools.CalculatePolylineToPointDistance(xyPolyline, new XYPoint(x, y));
                        if (xx < distance)
                        {
                            distance = xx;
                            if (xx < 0.3 * xyPolyline.GetLength())
                            {
                                elementIndex = index;
                                elementID    = elementSet.GetElementId(index).Id;
                            }
                        }
                    }

                    if (elementSetNumber == 0 && elementIndex >= 0)
                    {
                        this.label4.Text = "Index: " + elementIndex.ToString();
                        this.label8.Text = "ID: " + elementID.Substring(0, Math.Min(17, elementID.Length));
                    }
                    if (elementSetNumber == 1 && elementIndex >= 0)
                    {
                        this.label10.Text = "Index: " + elementIndex.ToString();
                        this.label11.Text = "ID: " + elementID.Substring(0, Math.Min(17, elementID.Length));
                    }
                }
            }
        }