Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
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);
        }