示例#1
0
文件: DXF.cs 项目: dd-bim/IfcTerrain
        public static Result ReadDXFIndPoly(bool is3d, DxfFile dxfFile, string layer, string breaklinelayer, double minDist, string logFilePath, string verbosityLevel, bool breakline)
        {
            var result = new Result();

            if (!UnitToMeter.TryGetValue(dxfFile.Header.DefaultDrawingUnits, out double scale))
            {
                scale = 1.0;
            }
            var pp = new Mesh(is3d, minDist);

            Logger logger = LogManager.GetCurrentClassLogger();

            foreach (var entity in dxfFile.Entities)
            {
                if (entity.Layer == layer)
                {
                    switch (entity.EntityType)
                    {
                    case DxfEntityType.Vertex:
                        var vtx = (DxfVertex)entity;
                        pp.AddPoint(Point3.Create(vtx.Location.X, vtx.Location.Y, vtx.Location.Z));
                        break;

                    case DxfEntityType.Line:
                        var line = (DxfLine)entity;
                        int p1   = pp.AddPoint(Point3.Create(line.P1.X * scale, line.P1.Y * scale, line.P1.Z * scale));
                        int p2   = pp.AddPoint(Point3.Create(line.P2.X * scale, line.P2.Y * scale, line.P2.Z * scale));
                        pp.FixEdge(p1, p2);
                        break;

                    case DxfEntityType.Polyline:
                        var poly = (DxfPolyline)entity;
                        int last = -1;
                        foreach (var v in poly.Vertices)
                        {
                            int curr = pp.AddPoint(Point3.Create(v.Location.X * scale, v.Location.Y * scale, v.Location.Z * scale));
                            if (last >= 0)
                            {
                                pp.FixEdge(last, curr);
                            }
                            last = curr;
                        }
                        break;
                    }
                }
            }
            if (!pp.Points.Any() || !pp.FixedEdges.Any())
            {
                result.Error = Properties.Resources.errNoLineData;
                logger.Error("Error. No line data found");
                return(result);
            }
            result.Mesh = pp;

            logger.Info("Reading DXF-data successful");
            logger.Info(pp.Points.Count + " points, " + pp.FixedEdges.Count + " lines and " + pp.FaceEdges.Count + " faces read");
            return(result);
        }
示例#2
0
文件: DXF.cs 项目: possner/IfcTerrain
        public static Result ReadDXFTin(bool is3d, DxfFile dxfFile, string layer, double minDist, string logFilePath, string verbosityLevel)
        {
            double minDistSq = minDist * minDist;
            var    result    = new Result();

            if (!UnitToMeter.TryGetValue(dxfFile.Header.DefaultDrawingUnits, out double scale))
            {
                scale = 1.0;
            }
            var tin = new Mesh(is3d, minDist);

            //Serilog.Log.Logger = new LoggerConfiguration()
            //                   .MinimumLevel.Debug()
            //                   .WriteTo.File(logFilePath)
            //                   .CreateLogger();
            var logger = LogManager.GetCurrentClassLogger();

            foreach (var entity in dxfFile.Entities)
            {
                if (entity.Layer == layer && entity is Dxf3DFace face)
                {
                    var p1 = Point3.Create(face.FirstCorner.X * scale, face.FirstCorner.Y * scale, face.FirstCorner.Z * scale);
                    var p2 = Point3.Create(face.SecondCorner.X * scale, face.SecondCorner.Y * scale, face.SecondCorner.Z * scale);
                    var p3 = Point3.Create(face.ThirdCorner.X * scale, face.ThirdCorner.Y * scale, face.ThirdCorner.Z * scale);
                    var p4 = Point3.Create(face.FourthCorner.X * scale, face.FourthCorner.Y * scale, face.FourthCorner.Z * scale);
                    if (Vector3.Norm2(p4 - p3) < minDistSq)
                    {
                        int i1 = tin.AddPoint(p1);
                        int i2 = tin.AddPoint(p2);
                        int i3 = tin.AddPoint(p3);
                        try
                        {
                            tin.AddFace(new[] { i1, i2, i3 });
                        }
                        catch
                        {
                            logger.Error("Redundant Face in Mesh found! Ignored during processings");
                        }
                    }
                }
            }
            if (!tin.Points.Any() || !tin.FaceEdges.Any())
            {
                result.Error = Properties.Resources.errNoLineData;
                logger.Error("Error. No line data found");
                return(result);
            }
            result.Mesh = tin;
            logger.Info("Reading DXF-data successful");
            logger.Info(tin.Points.Count + " Points, " + tin.FixedEdges.Count + " Lines and " + tin.FaceEdges.Count + " Faces read");
            return(result);
        }
示例#3
0
        /// <summary>
        ///  Liest das erste TIN aus einer LandXML Datei
        /// </summary>
        /// <param name="fileName">  </param>
        /// <returns>  </returns>
        public static Result ReadTIN(bool is3d, string fileName, double minDist)
        {
            Serilog.Log.Logger = new LoggerConfiguration()
                                 .MinimumLevel.Debug()
                                 //.WriteTo.File(@"D:\\Daten\\myapp.txt", rollingInterval: RollingInterval.Day)
                                 .WriteTo.File(System.Configuration.ConfigurationManager.AppSettings["LogFilePath"])
                                 .CreateLogger();

            var result = new Result();

            try
            {
                using (var reader = XmlReader.Create(fileName))
                {
                    bool isRelief = false;
                    reader.MoveToContent();
                    while (!reader.EOF && (reader.NodeType != XmlNodeType.Element || !(isRelief = reader.LocalName == "ReliefFeature")))
                    {
                        reader.Read();
                    }
                    if (isRelief)
                    {
                        string id        = reader.MoveToFirstAttribute() && reader.LocalName == "id" ? reader.Value : null;
                        bool   insideTin = false;
                        while (!reader.EOF && (reader.NodeType != XmlNodeType.Element || !(insideTin = reader.LocalName == "tin")))
                        {
                            reader.Read();
                        }
                        if (insideTin)
                        {
                            bool insideTri = false;
                            while (!reader.EOF && (reader.NodeType != XmlNodeType.Element || !(insideTri = reader.LocalName == "trianglePatches")))
                            {
                                reader.Read();
                            }
                            if (insideTri)
                            {
                                while (!reader.EOF && (reader.NodeType != XmlNodeType.Element || !(insideTri = reader.LocalName == "Triangle")))
                                {
                                    reader.Read();
                                }
                                if (insideTri)
                                {
                                    var tin = new Mesh(is3d, minDist);
                                    while (reader.NodeType == XmlNodeType.Element && reader.LocalName == "Triangle" &&
                                           XElement.ReadFrom(reader) is XElement el)
                                    {
                                        var      posList = el.Descendants().Where(d => d.Name.LocalName == "posList" && !d.IsEmpty);
                                        string[] pl;
                                        if (posList.Any() &&
                                            (pl = posList.First().Value.Split(new[] { ' ', '\r', '\n', '\t' }, StringSplitOptions.RemoveEmptyEntries)).Length == 12 &&
                                            pl[0] == pl[9] && pl[1] == pl[10] && pl[2] == pl[11] &&
                                            Point3.Create(pl, out var pt1) &&
                                            Point3.Create(pl, out var pt2, 3) &&
                                            Point3.Create(pl, out var pt3, 6))
                                        {
                                            tin.AddFace(new[] { pt1, pt2, pt3 });
                                        }
                                        reader.Read();
                                    }
                                    if (!tin.Points.Any() || !tin.FaceEdges.Any())
                                    {
                                        result.Error = string.Format(Properties.Resources.errNoTINData, Path.GetFileName(fileName));
                                        Log.Error("No TIN-data found");
                                        return(result);
                                    }
                                    result.Mesh = tin;
                                    Log.Information("Reading GML-data successful");
                                    Log.Information(tin.Points.Count + " points, " + tin.FixedEdges.Count + " lines and " + tin.FaceEdges.Count + " faces read");
                                    return(result);
                                }
                            }
                        }
                    }
                    result.Error = string.Format(Properties.Resources.errNoTIN, Path.GetFileName(fileName));
                    Log.Error("No TIN-data found");
                    return(result);
                }
            }
            catch
            {
                result.Error = string.Format(Properties.Resources.errFileNotReadable, Path.GetFileName(fileName));
                Log.Error("File not readable");
                return(result);
            }
        } //End ReadTIN
示例#4
0
        /// <summary>
        /// Liest das erste TIN aus einer LandXML Datei
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="errors"></param>
        /// <returns></returns>
        public static Result ReadTIN(bool is3d, string fileName, double minDist, string logFilePath, string verbosityLevel)
        {
            var result = new Result();

            //Serilog.Log.Logger = new LoggerConfiguration()
            //                   .MinimumLevel.Debug()
            //                   .WriteTo.File(logFilePath)
            //                   .CreateLogger();
            var logger = LogManager.GetCurrentClassLogger();

            try
            {
                using (var reader = XmlReader.Create(fileName))
                {
                    XElement el;
                    double?  scale     = null;
                    var      pntIds    = new Dictionary <string, int>();
                    var      mesh      = new Mesh(is3d, minDist);
                    bool     insideTin = false;
                    reader.MoveToContent();
                    // Parse the file and display each of the nodes.
                    while (!reader.EOF)
                    {
                        if (reader.NodeType == XmlNodeType.Element)
                        {
                            switch (reader.LocalName)
                            {
                            case "Metric":
                            case "Imperial":
                                el = XElement.ReadFrom(reader) as XElement;
                                if (el != null)
                                {
                                    var att = el.Attribute("linearUnit");
                                    if (att != null && ToMeter.TryGetValue(att.Value.ToLower(), out double tscale))
                                    {
                                        scale = tscale;
                                    }
                                }
                                break;

                            case "Definition":
                                if (reader.MoveToFirstAttribute() &&
                                    reader.LocalName == "surfType" &&
                                    reader.Value.ToUpper() == "TIN")
                                {
                                    insideTin = true;
                                }
                                break;

                            case "P":
                                el = XElement.ReadFrom(reader) as XElement;
                                if (el != null)
                                {
                                    var att = el.Attribute("id");
                                    if (att != null && Point3.Create(
                                            el.Value.Replace(',', '.').Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries), out var pt))
                                    {
                                        pntIds.Add(att.Value, mesh.AddPoint(scale.HasValue ? scale.Value * pt : pt));
                                    }
                                }
                                break;

                            case "F":
                                el = XElement.ReadFrom(reader) as XElement;
                                if (el != null)
                                {
                                    string[] pts = el.Value.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                                    if (pts.Length == 3 &&
                                        pntIds.TryGetValue(pts[0], out int p1) &&
                                        pntIds.TryGetValue(pts[1], out int p2) &&
                                        pntIds.TryGetValue(pts[2], out int p3))
                                    {
                                        mesh.AddFace(new[] { p1, p2, p3 });
                                    }
                                }
                                break;

                            default:
                                reader.Read();
                                break;
                            }
                        }
                        else if (insideTin && reader.NodeType == XmlNodeType.EndElement && reader.Name == "Definition")
                        {
                            if (!mesh.Points.Any() || !mesh.FaceEdges.Any())
                            {
                                result.Error = string.Format(Properties.Resources.errNoTINData, Path.GetFileName(fileName));
                                logger.Error("No TIN-data found");
                                return(result);
                            }
                            logger.Info("Reading LandXML-Data successful");
                            logger.Info(mesh.Points.Count + " points, " + mesh.FixedEdges.Count + " lines and " + mesh.FaceEdges.Count + " faces read");

                            result.Mesh = mesh;
                            return(result);
                        }
                        else
                        {
                            reader.Read();
                        }
                    }
                }
            }
            catch
            {
                result.Error = string.Format(Properties.Resources.errFileNotReadable, Path.GetFileName(fileName));
                logger.Error("File not readable");
                return(result);
            }
            result.Error = string.Format(Properties.Resources.errNoTIN, Path.GetFileName(fileName));
            logger.Error("No TIN-data found");
            return(result);
        } //End ReadTIN
示例#5
0
文件: DXF.cs 项目: dd-bim/IfcTerrain
        public static Result ReadDXFTin(bool is3d, DxfFile dxfFile, string layer, string breaklinelayer, double minDist, bool breakline)
        {
            //TODO
            //use is3d is not requiered anymore!
            //
            double minDistSq = minDist * minDist;
            var    result    = new Result();

            if (!UnitToMeter.TryGetValue(dxfFile.Header.DefaultDrawingUnits, out double scale))
            {
                scale = 1.0;
            }

            //TIN Builder
            var tinB = Tin.CreateBuilder(true);

            Dictionary <int, Line3> breaklines = new Dictionary <int, Line3>(); //Dictionary für Punkte der Bruchkanten

            Logger logger = LogManager.GetCurrentClassLogger();

            int index = 0;

            #region PNR - Counter
            int pnr = 0;
            #endregion

            foreach (var entity in dxfFile.Entities)
            {
                if (entity.Layer == layer && entity is Dxf3DFace face)
                {
                    var p1 = Point3.Create(face.FirstCorner.X * scale, face.FirstCorner.Y * scale, face.FirstCorner.Z * scale);
                    var p2 = Point3.Create(face.SecondCorner.X * scale, face.SecondCorner.Y * scale, face.SecondCorner.Z * scale);
                    var p3 = Point3.Create(face.ThirdCorner.X * scale, face.ThirdCorner.Y * scale, face.ThirdCorner.Z * scale);
                    var p4 = Point3.Create(face.FourthCorner.X * scale, face.FourthCorner.Y * scale, face.FourthCorner.Z * scale);
                    if (Vector3.Norm2(p4 - p3) < minDistSq)
                    {
                        //Punkte hinzufügen & jeweils eine Punktnummer hochzählen
                        tinB.AddPoint(pnr++, p1);
                        tinB.AddPoint(pnr++, p2);
                        tinB.AddPoint(pnr++, p3);

                        //Schleife zum erzeugen des Dreiecks
                        for (int i = pnr - 3; i < pnr; i++)
                        {
                            tinB.AddTriangle(i++, i++, i++);
                        }
                    }
                }

                if (entity.Layer == breaklinelayer && breakline == true)
                {
                    switch (entity.EntityType)
                    {
                    /*case DxfEntityType.Vertex: //Punkt
                     *  var vtx = (DxfVertex)entity;
                     *  pp_bl.AddPoint(Point3.Create(vtx.Location.X, vtx.Location.Y, vtx.Location.Z));
                     *  break;*/
                    case DxfEntityType.Line:     //Linie
                        var        line = (DxfLine)entity;
                        Point3     p1   = Point3.Create(line.P1.X * scale, line.P1.Y * scale, line.P1.Z * scale);
                        Point3     p2   = Point3.Create(line.P2.X * scale, line.P2.Y * scale, line.P2.Z * scale);
                        Vector3    v12  = Vector3.Create(p2);
                        Direction3 d12  = Direction3.Create(v12, scale);
                        Line3      l    = Line3.Create(p1, d12);
                        try
                        {
                            breaklines.Add(index, l);
                            index++;
                        }
                        catch
                        {
                            index++;
                        }
                        break;

                        /*case DxfEntityType.Polyline: //Bögen
                         *  var poly = (DxfPolyline)entity;
                         *  int last = -1;
                         *  foreach (var v in poly.Vertices)
                         *  {
                         *      int curr = pp_bl.AddPoint(Point3.Create(v.Location.X * scale, v.Location.Y * scale, v.Location.Z * scale));
                         *      if (last >= 0)
                         *      {
                         *          pp_bl.FixEdge(last, curr);
                         *      }
                         *      last = curr;
                         *  }
                         * break;*/
                    }
                }
            }

            /* ÜBERARBEITEN mit neuer Abfrage!
             * if(!tin.Points.Any() || !tin.FaceEdges.Any())
             * {
             *  result.Error = Properties.Resources.errNoLineData;
             *  logger.Error("Error. No line data found");
             *  return result;
             * }
             */
            //TIN aus TIN-Builder erzeugen
            Tin tin = tinB.ToTin(out var pointIndex2NumberMap, out var triangleIndex2NumberMap);
            //Result beschreiben
            result.Tin = tin;

            //Fehler für Bruchkanten abfangen lassen
            try
            {
                result.Breaklines = breaklines;
                logger.Info(breaklines.Count + " breaklines read");
            }
            catch
            {
                logger.Error("Breaklines could not be processed.");
            }
            //logging

            logger.Info("Reading DXF-data successful");
            logger.Info(result.Tin.Points.Count() + " points; " + result.Tin.NumTriangles + " triangels processed");
            return(result);
        }
示例#6
0
        /// <summary>
        /// Liest Daten aus REB Datei(en) DA45, DA49 und DA58
        /// </summary>
        /// <param name="fileNames"></param>
        /// <param name="errors"></param>
        /// <returns></returns>
        public static RebDaData ReadREB(string[] fileNames)
        {
            var rebData = new RebDaData();

            try
            {
                foreach (string fileName in fileNames)
                {
                    if (File.Exists(fileName))
                    {
                        using (var sr = new StreamReader(fileName))
                        {
                            string line;
                            bool   read = true;
                            while (read && (line = sr.ReadLine()) != null)
                            {
                                // Koordinaten
                                if (line.Length >= 40 &&
                                    line[0] == '4' &&
                                    line[1] == '5')
                                {
                                    if (int.TryParse(line.Substring(2, 7), out int nr) &&
                                        !rebData.Points.ContainsKey(nr) &&
                                        long.TryParse(line.Substring(10, 10), out long x) &&
                                        long.TryParse(line.Substring(20, 10), out long y) &&
                                        long.TryParse(line.Substring(30, 10), out long z)
                                        )
                                    {
                                        var p = Point3.Create(x * 0.001, y * 0.001, z * 0.001);
                                        rebData.Points.Add(nr, p);
                                    }
                                }
                                // Bruch-/Randlinien
                                else if (line.Length >= 30 &&
                                         line[0] == '4' &&
                                         line[1] == '9')
                                {
                                    if (int.TryParse(line.Substring(7, 2), out int hz) &&
                                        long.TryParse(line.Substring(10, 10), out long p1) &&
                                        long.TryParse(line.Substring(20, 10), out long p2))
                                    {
                                        if (rebData.Lines.TryGetValue(hz, out var ls))
                                        {
                                            ls = new List <RLine>();
                                            rebData.Lines.Add(hz, ls);
                                        }
                                        ls.Add(new RLine {
                                            P1 = p1, P2 = p2
                                        });
                                    }
                                }
                                // TIN
                                else if (line.Length >= 50 &&
                                         line[0] == '5' &&
                                         line[1] == '8')
                                {
                                    if (int.TryParse(line.Substring(7, 2), out int hz) &&
                                        long.TryParse(line.Substring(20, 10), out long p1) &&
                                        long.TryParse(line.Substring(30, 10), out long p2) &&
                                        long.TryParse(line.Substring(40, 10), out long p3))
                                    {
                                        if (!rebData.Tris.TryGetValue(hz, out var ts))
                                        {
                                            ts = new List <RTri>();
                                            rebData.Tris.Add(hz, ts);
                                        }
                                        ts.Add(new RTri {
                                            P1 = p1, P2 = p2, P3 = p3
                                        });
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch
            {
                return(null);
            }
            return(rebData);
        } //End ReadTIN
示例#7
0
        /// <summary>
        /// Liest das erste TIN aus einer LandXML Datei
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="errors"></param>
        /// <returns></returns>
        public static Result ReadTIN(bool is3d, string fileName, double minDist, string logFilePath, string verbosityLevel)
        {
            var result = new Result();

            //Serilog.Log.Logger = new LoggerConfiguration()
            //                   .MinimumLevel.Debug()
            //                   .WriteTo.File(logFilePath)
            //                   .CreateLogger();
            var logger = LogManager.GetCurrentClassLogger();

            try
            {
                using (var reader = XmlReader.Create(fileName))
                {
                    XElement el;
                    double?  scale  = null;
                    var      pntIds = new Dictionary <string, int>();

                    //TIN-Builder erzeugen
                    var tinB = Tin.CreateBuilder(true);
                    //PNR "künstlich erzeugen" & für TIN nutzen
                    int pnr = 0;

                    bool insideTin = false;
                    reader.MoveToContent();
                    // Parse the file and display each of the nodes.
                    while (!reader.EOF)
                    {
                        if (reader.NodeType == XmlNodeType.Element)
                        {
                            switch (reader.LocalName)
                            {
                            case "Metric":
                            case "Imperial":
                                el = XElement.ReadFrom(reader) as XElement;
                                if (el != null)
                                {
                                    var att = el.Attribute("linearUnit");
                                    if (att != null && ToMeter.TryGetValue(att.Value.ToLower(), out double tscale))
                                    {
                                        scale = tscale;
                                    }
                                }
                                break;

                            case "Definition":
                                if (reader.MoveToFirstAttribute() &&
                                    reader.LocalName == "surfType" &&
                                    reader.Value.ToUpper() == "TIN")
                                {
                                    insideTin = true;
                                }
                                break;

                            case "P":
                                el = XElement.ReadFrom(reader) as XElement;
                                if (el != null)
                                {
                                    var att = el.Attribute("id");
                                    if (att != null && Point3.Create(
                                            el.Value.Replace(',', '.').Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries), out var pt))
                                    {
                                        Point3 point = Point3.Create(pt.Y, pt.X, pt.Z);
                                        tinB.AddPoint(pnr, point);
                                        pntIds.Add(att.Value, pnr++);
                                    }
                                }
                                break;

                            case "F":
                                el = XElement.ReadFrom(reader) as XElement;
                                if (el != null)
                                {
                                    string[] pts = el.Value.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                                    if (pts.Length == 3 &&
                                        pntIds.TryGetValue(pts[0], out int p1) &&
                                        pntIds.TryGetValue(pts[1], out int p2) &&
                                        pntIds.TryGetValue(pts[2], out int p3))
                                    {
                                        //mesh.AddFace(new[] { p1, p2, p3 });
                                        tinB.AddTriangle(p1, p2, p3);
                                    }
                                }
                                break;

                            default:
                                reader.Read();
                                break;
                            }
                        }
                        else if (insideTin && reader.NodeType == XmlNodeType.EndElement && reader.Name == "Definition")
                        {
                            /*
                             * if(!mesh.Points.Any() || !mesh.FaceEdges.Any())
                             * {
                             *  result.Error = string.Format(Properties.Resources.errNoTINData, Path.GetFileName(fileName));
                             *  logger.Error("No TIN-data found");
                             *  return result;
                             * }
                             */
                            logger.Info("Reading LandXML-Data successful");
                            logger.Info(result.Tin.Points.Count() + " points; " + result.Tin.NumTriangles + " triangels processed");

                            //TIN aus TIN-Builder erzeugen
                            Tin tin = tinB.ToTin(out var pointIndex2NumberMap, out var triangleIndex2NumberMap);
                            //Result beschreiben
                            result.Tin = tin;

                            //result.Mesh = mesh;
                            return(result);
                        }
                        else
                        {
                            reader.Read();
                        }
                    }
                }
            }
            catch
            {
                result.Error = string.Format(Properties.Resources.errFileNotReadable, Path.GetFileName(fileName));
                logger.Error("File not readable");
                return(result);
            }
            result.Error = string.Format(Properties.Resources.errNoTIN, Path.GetFileName(fileName));
            logger.Error("No TIN-data found");
            return(result);
        } //End ReadTIN
示例#8
0
        private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); //initalisieren des Loggers in diesem Reader

        public static Result ReadPostGIS(bool is3d, double minDist, string Host, int Port, string User, string Password, string DBname, string schema, string tintable, string tincolumn, string tinidcolumn, int tinid, bool postgis_bl, string bl_column, string bl_table, string bl_tinid)
        {
            double scale  = 1.0;
            var    result = new Result();

            //
            var tinB = Tin.CreateBuilder(true);

            Dictionary <int, Line3> breaklines = new Dictionary <int, Line3>();

            try
            {
                //prepare string for database connection
                string connString =
                    string.Format(
                        "Host={0};Port={1};Username={2};Password={3};Database={4};",
                        Host,
                        Port,
                        User,
                        Password,
                        DBname
                        );

                //TIN Request
                using (var conn = new NpgsqlConnection(connString))
                {
                    //open database connection
                    conn.Open();
                    NpgsqlConnection.GlobalTypeMapper.UseLegacyPostgis();

                    //ÜBERARBEITEN ggf. weitere Request-Möglichkeiten???

                    //select request for tin without breaklines via TIN ID
                    string tin_select = "SELECT " + "ST_AsEWKT(" + tincolumn + ") as wkt FROM " + schema + "." + tintable + " WHERE " + tinidcolumn + " = " + tinid;

                    //select request for breaklines via TIN ID + JOIN
                    string bl_select = null;
                    if (postgis_bl == true)
                    {
                        bl_select = "SELECT ST_AsEWKT(" + bl_table + "." + bl_column + ") FROM " + schema + "." + bl_table + " JOIN " + schema + "." + tintable + " ON (" + bl_table + "." + bl_tinid + " = " + tintable + "." + tinidcolumn + ") WHERE " + tintable + "." + tinidcolumn + " = " + tinid;
                    }
                    //TIN abfragen
                    using (var command = new NpgsqlCommand(tin_select, conn))
                    {
                        var reader = command.ExecuteReader();
                        Logger.Info("The following REQUEST have been sent: \n" + tin_select);
                        while (reader.Read())
                        {
                            //read column --> as WKT

                            string geom_string = (reader.GetValue(0)).ToString();
                            //Split - CRS & TIN

                            string[] geom_split = geom_string.Split(';');
                            //String for EPSG - Code --> Weiterverarbeitung???

                            string tin_epsg = geom_split[0];

                            //Gesamtes TIN
                            string tin_gesamt = geom_split[1];

                            //Split für den Anfang des TINs
                            char[] trim = { 'T', 'I', 'N', '(' };
                            tin_gesamt = tin_gesamt.TrimStart(trim);

                            //Split für jedes Dreieck
                            string[] separator  = { ")),((" };
                            string[] tin_string = tin_gesamt.Split(separator, System.StringSplitOptions.RemoveEmptyEntries);

                            //Jedes Dreieck durchlaufen
                            int pnr = 0; //initalisieren
                            foreach (string face in tin_string)
                            {
                                //Punkte - Split über Komma
                                string[] face_points = face.Split(',');

                                //Split über Leerzeichen
                                //FirstCorner
                                string[] P1 = face_points[0].Split(' ');

                                double P1X = Convert.ToDouble(P1[0], CultureInfo.InvariantCulture);
                                double P1Y = Convert.ToDouble(P1[1], CultureInfo.InvariantCulture);
                                double P1Z = Convert.ToDouble(P1[2], CultureInfo.InvariantCulture);

                                //P1
                                var p1 = Point3.Create(P1X * scale, P1Y * scale, P1Z * scale);

                                //SecoundCorner
                                string[] P2 = face_points[1].Split(' ');

                                double P2X = Convert.ToDouble(P2[0], CultureInfo.InvariantCulture);
                                double P2Y = Convert.ToDouble(P2[1], CultureInfo.InvariantCulture);
                                double P2Z = Convert.ToDouble(P2[2], CultureInfo.InvariantCulture);

                                //P2
                                var p2 = Point3.Create(P2X * scale, P2Y * scale, P2Z * scale);

                                //ThirdCorner
                                string[] P3 = face_points[2].Split(' ');

                                double P3X = Convert.ToDouble(P3[0], CultureInfo.InvariantCulture);
                                double P3Y = Convert.ToDouble(P3[1], CultureInfo.InvariantCulture);
                                double P3Z = Convert.ToDouble(P3[2], CultureInfo.InvariantCulture);

                                //P3
                                var p3 = Point3.Create(P3X * scale, P3Y * scale, P3Z * scale);

                                //Punkte hinzufügen & jeweils eine Punktnummer hochzählen
                                tinB.AddPoint(pnr++, p1);
                                tinB.AddPoint(pnr++, p2);
                                tinB.AddPoint(pnr++, p3);

                                //Schleife zum erzeugen des Dreiecks
                                for (int i = pnr - 3; i < pnr; i++)
                                {
                                    tinB.AddTriangle(i++, i++, i++);
                                }
                            }
                        }
                        conn.Close();
                    }

                    //TIN aus TIN-Builder erzeugen
                    Tin tin = tinB.ToTin(out var pointIndex2NumberMap, out var triangleIndex2NumberMap);
                    //Result beschreiben
                    result.Tin = tin;


                    if (postgis_bl == true)
                    {
                        conn.Open();
                        //Bruchkanten abfragen
                        int index_poly = 0;
                        int index      = 0;
                        using (var command = new NpgsqlCommand(bl_select, conn))
                        {
                            var reader = command.ExecuteReader();
                            Logger.Info("The following REQUEST have been sent: \n" + bl_select);

                            while (reader.Read())
                            {
                                string polyline_string = (reader.GetValue(0)).ToString();

                                string[] poly_split = polyline_string.Split(';');

                                //Gesamte Polyline
                                string poly_gesamt = poly_split[1];

                                //Split für den Anfang des TINs
                                char[] trim = { 'L', 'I', 'N', 'E', 'S', 'T', 'R', 'I', 'N', 'G', '(' };
                                poly_gesamt = poly_gesamt.TrimStart(trim);

                                char[] trimEnd = { ')' };
                                poly_gesamt = poly_gesamt.TrimEnd(trimEnd);

                                //Split für jeden Punkt
                                string[] separator = { "," };
                                string[] polyline  = poly_gesamt.Split(separator, System.StringSplitOptions.RemoveEmptyEntries);

                                //Jeden Punkt in der Polyline durchlaufen
                                int i = 0;
                                int j = 1;
                                do
                                {
                                    string[] point_start_values = polyline[i].Split(' ');
                                    double   p1X = Convert.ToDouble(point_start_values[0], CultureInfo.InvariantCulture);
                                    double   p1Y = Convert.ToDouble(point_start_values[1], CultureInfo.InvariantCulture);
                                    double   p1Z = Convert.ToDouble(point_start_values[2], CultureInfo.InvariantCulture);
                                    Point3   p1  = Point3.Create(p1X * scale, p1Y * scale, p1Z * scale);

                                    string[]   point_end_values = polyline[j].Split(' ');
                                    double     p2X = Convert.ToDouble(point_end_values[0], CultureInfo.InvariantCulture);
                                    double     p2Y = Convert.ToDouble(point_end_values[1], CultureInfo.InvariantCulture);
                                    double     p2Z = Convert.ToDouble(point_end_values[2], CultureInfo.InvariantCulture);
                                    Point3     p2  = Point3.Create(p2X * scale, p2Y * scale, p2Z * scale);
                                    Vector3    v12 = Vector3.Create(p2);
                                    Direction3 d12 = Direction3.Create(v12, scale);
                                    Line3      l   = Line3.Create(p1, d12);
                                    try
                                    {
                                        breaklines.Add(index, l); //Breakline hinzufügen
                                        index++;
                                    }
                                    catch
                                    {
                                        index++;
                                    }
                                    i++;
                                    j++;
                                } while (j < polyline.Length);

                                index_poly++;
                            }
                            result.Breaklines = breaklines;
                        }

                        //close database connection
                        conn.Close();
                    }
                    Logger.Info("All database connections have been disconnected.");
                    Logger.Info("Reading PostGIS successful");
                    Logger.Info(result.Tin.Points.Count() + " points; " + result.Tin.NumTriangles + " triangels processed");
                }
            }
            catch (Exception e)
            {
                //
                Console.WriteLine(e.ToString());
                Logger.Error("Database connection failed!");
            }
            Console.ReadLine();
            return(result);
        }
示例#9
0
        /// <summary>
        ///  Liest das erste TIN aus einer GML Datei
        /// </summary>
        /// <param name="fileName">  </param>
        /// <returns>  </returns>
        public static Result ReadTIN(bool is3d, string fileName, double minDist, string logFilePath, string verbosityLevel)
        {
            var logger = LogManager.GetCurrentClassLogger();

            //TIN-Builder
            var tinB = Tin.CreateBuilder(true);
            int pnr  = 0;

            var result = new Result();

            try
            {
                using (var reader = XmlReader.Create(fileName))
                {
                    bool isRelief = false;
                    reader.MoveToContent();
                    while (!reader.EOF && (reader.NodeType != XmlNodeType.Element || !(isRelief = reader.LocalName == "ReliefFeature")))
                    {
                        reader.Read();
                    }
                    if (isRelief)
                    {
                        string id        = reader.MoveToFirstAttribute() && reader.LocalName == "id" ? reader.Value : null;
                        bool   insideTin = false;
                        while (!reader.EOF && (reader.NodeType != XmlNodeType.Element || !(insideTin = reader.LocalName == "tin")))
                        {
                            reader.Read();
                        }
                        if (insideTin)
                        {
                            bool insideTri = false;
                            while (!reader.EOF && (reader.NodeType != XmlNodeType.Element || !(insideTri = reader.LocalName == "trianglePatches")))
                            {
                                reader.Read();
                            }
                            if (insideTri)
                            {
                                while (!reader.EOF && (reader.NodeType != XmlNodeType.Element || !(insideTri = reader.LocalName == "Triangle")))
                                {
                                    reader.Read();
                                }
                                if (insideTri)
                                {
                                    //var tin = new Mesh(is3d, minDist);
                                    while (reader.NodeType == XmlNodeType.Element && reader.LocalName == "Triangle" &&
                                           XElement.ReadFrom(reader) is XElement el)
                                    {
                                        var      posList = el.Descendants().Where(d => d.Name.LocalName == "posList" && !d.IsEmpty);
                                        string[] pl;
                                        if (posList.Any() &&
                                            (pl = posList.First().Value.Split(new[] { ' ', '\r', '\n', '\t' }, StringSplitOptions.RemoveEmptyEntries)).Length == 12 &&
                                            pl[0] == pl[9] && pl[1] == pl[10] && pl[2] == pl[11] &&
                                            Point3.Create(pl, out var pt1) &&
                                            Point3.Create(pl, out var pt2, 3) &&
                                            Point3.Create(pl, out var pt3, 6))
                                        {
                                            //first Add points to tin with point index (pnr)
                                            tinB.AddPoint(pnr++, pt1);
                                            tinB.AddPoint(pnr++, pt2);
                                            tinB.AddPoint(pnr++, pt3);

                                            //adding Triangle to TIN-Builder
                                            tinB.AddTriangle(pnr - 3, pnr - 2, pnr - 1, true);

                                            //tin.AddFace(new[] { pt1, pt2, pt3 });
                                        }
                                        reader.Read();
                                    }

                                    /*
                                     * if(!tin.Points.Any() || !tin.FaceEdges.Any())
                                     * {
                                     *  result.Error = string.Format(Properties.Resources.errNoTINData, Path.GetFileName(fileName));
                                     *  logger.Error("No TIN-data found");
                                     *  return result;
                                     * }
                                     */
                                    //result.Mesh = tin;
                                    logger.Info("Reading GML-data successful");
                                    logger.Info(result.Tin.Points.Count() + " points; " + result.Tin.NumTriangles + " triangels processed");

                                    //TIN aus TIN-Builder erzeugen
                                    Tin tin = tinB.ToTin(out var pointIndex2NumberMap, out var triangleIndex2NumberMap);
                                    //Result beschreiben
                                    result.Tin = tin;
                                    //Result übergeben
                                    return(result);
                                }
                            }
                        }
                    }
                    result.Error = string.Format(Properties.Resources.errNoTIN, Path.GetFileName(fileName));
                    logger.Error("No TIN-data found");
                    return(result);
                }
            }
            catch
            {
                result.Error = string.Format(Properties.Resources.errFileNotReadable, Path.GetFileName(fileName));
                logger.Error("File not readable");
                return(result);
            }
        } //End ReadTIN
示例#10
0
        public static Result ReadGrid(bool is3d, string fileName, double minDist, int size, bool bBox, double bbNorth, double bbEast, double bbSouth, double bbWest)
        {
            var result = new Result();
            var mesh   = new BimGisCad.Collections.Mesh(is3d, minDist);

            #region Read File for extent
            int           counter = 0;
            string        line;
            List <double> xList = new List <double>();
            List <double> yList = new List <double>();
            List <double> zList = new List <double>();

            System.IO.StreamReader file = new System.IO.StreamReader(fileName);

            while ((line = file.ReadLine()) != null)
            {
                string[] str = line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                if (str.Length > 2 &&
                    double.TryParse(str[0], NumberStyles.Float, CultureInfo.InvariantCulture, out double x) &&
                    double.TryParse(str[1], NumberStyles.Float, CultureInfo.InvariantCulture, out double y) &&
                    double.TryParse(str[2], NumberStyles.Float, CultureInfo.InvariantCulture, out double z))
                {
                    xList.Add(x);
                    yList.Add(y);
                    zList.Add(z);
                }
                counter++;
            }

            double xMin = xList.Min();
            double xMax = xList.Max();
            double yMin = yList.Min();
            double yMax = yList.Max();

            int xExtent = (int)(xMax - xMin);
            int yExtent = (int)(yMax - yMin);

            file.Close();
            #endregion

            #region Fill Grid

            //Size of the grid
            int xCount = xExtent / size;
            int yCount = yExtent / size;

            //!!!überprüfen ob 0 oder 1 !!!
            var grid = new List <Dictionary <int, Point3> >();
            for (int rowcount = 0; rowcount <= yCount; rowcount++)
            {
                grid.Add(new Dictionary <int, Point3>());
            }

            System.IO.StreamReader file2 = new System.IO.StreamReader(fileName);

            while ((line = file2.ReadLine()) != null)
            {
                string[] str = line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                if (str.Length > 2 &&
                    double.TryParse(str[0], NumberStyles.Float, CultureInfo.InvariantCulture, out double x) &&
                    double.TryParse(str[1], NumberStyles.Float, CultureInfo.InvariantCulture, out double y) &&
                    double.TryParse(str[2], NumberStyles.Float, CultureInfo.InvariantCulture, out double z))
                {
                    if (bBox)
                    {
                        if (y >= bbSouth && y <= bbNorth && x >= bbWest && x <= bbEast)
                        {
                            Point3 p     = Point3.Create(x, y, z);
                            int    xGrid = (int)(x - xMin);
                            int    yGrid = (int)(y - yMin);

                            grid[yGrid].Add(xGrid, p);
                        }
                    }
                    else
                    {
                        Point3 p     = Point3.Create(x, y, z);
                        int    xGrid = (int)(x - xMin);
                        int    yGrid = (int)(y - yMin);

                        grid[yGrid].Add(xGrid, p);
                    }
                }
            }
            file2.Close();
            #endregion

            #region Create simple Mesh on Grid

            for (int row = 0; row < yCount; row++)
            {
                for (int column = 0; column <= xCount; column++)
                {
                    if (grid[row].ContainsKey(column))
                    {
                        Point3 cp = grid[row][column];
                        //mesh.AddPoint(cp);

                        //Triangle1 TopLeft
                        if (grid[row + 1].ContainsKey(column + 1) && grid[row + 1].ContainsKey(column))
                        {
                            Point3 tp = grid[row + 1][column];
                            Point3 tr = grid[row + 1][column + 1];

                            mesh.AddFace(new[] { cp, tp, tr });
                        }

                        //Triangle2 BottomRight
                        if (grid[row + 1].ContainsKey(column + 1) && grid[row].ContainsKey(column + 1))
                        {
                            Point3 br = grid[row][column + 1];
                            Point3 tr = grid[row + 1][column + 1];

                            mesh.AddFace(new[] { cp, tr, br });
                        }

                        //Triangle on left Edge of Terrain
                        if (grid[row].ContainsKey(column - 1) is false && grid[row + 1].ContainsKey(column) && grid[row + 1].ContainsKey(column - 1))
                        {
                            Point3 tp = grid[row + 1][column];
                            Point3 tl = grid[row + 1][column - 1];

                            mesh.AddFace(new[] { cp, tl, tp });
                        }
                    }
                }
            }
            #endregion

            result.Mesh = mesh;
            return(result);
        }