public static SqlGeometry CorrigerUnionGeometry(SqlGeometry geom, int srid)
        {
            SqlGeometry geomBase = SqlTypesExtensions.STGeomFromText("POINT EMPTY", srid);

            for (int i = 1; i <= geom.STNumGeometries(); i++)
            {
                SqlGeometry curGeom = geom.STGeometryN(i);
                if (curGeom.STDimension().Value == 2)
                {
                    SqlGeometry outerRing = curGeom.STExteriorRing();

                    List <SqlGeometry> holes = new List <SqlGeometry>();

                    for (int hole = 1; hole <= curGeom.STNumInteriorRing(); hole++)
                    {
                        SqlGeometry holeGeom = SqlTypesExtensions.PolygonFromRings(curGeom.STInteriorRingN(hole), null);                         // trou converti en polygone
                        double      aire     = holeGeom.GetAireEnMetres();
                        if (aire > AIRE_MINI_SCORIES)
                        {
                            List <SqlGeometry> nativeHoles = SqlTypesExtensions.ExteriorRingsFromPolygon(holeGeom);                            // polygone corrigé reconverti en linestring
                            holes.AddRange(nativeHoles);
                        }
                    }

                    curGeom  = SqlTypesExtensions.PolygonFromRings(outerRing, holes);
                    geomBase = geomBase.STUnion(curGeom);
                }
            }

            return(geomBase);
        }
        public static SqlGeometry PolygonFromRings(SqlGeometry outerRing, List <SqlGeometry> holes)
        {
            // Check si les parametres sont des LINESTRING
            #region Check params
            if (outerRing == null || outerRing.IsNull)
            {
                throw new ArgumentException("La boucle extérieure est null", "outerRing");
            }

            if (outerRing.STGeometryType().Value != OpenGisGeometryType.LineString.ToString())
            {
                throw new ArgumentException("La boucle extérieure doit être un LINESTRING", "outerRing");
            }
            if (holes != null)
            {
                foreach (var hole in holes)
                {
                    if (hole.STGeometryType().Value != OpenGisGeometryType.LineString.ToString())
                    {
                        throw new ArgumentException("Les boucles intérieures doivent être un LINESTRING", "holes");
                    }
                }
            }
            #endregion


            StringBuilder sb = new StringBuilder();
            sb.Append("POLYGON (");
            sb.Append(outerRing.ToString().Replace("LINESTRING ", ""));

            if (holes != null)
            {
                foreach (SqlGeometry hole in holes)
                {
                    SqlGeometry polyFromHole = PolygonFromRings(hole, null);

                    if (SqlTypesExtensions.GetAireEnMetres(polyFromHole) < AIRE_MINI_SCORIES)
                    {
                        continue;
                    }

                    //Debug.WriteLine(polyFromHole.STArea().Value);

                    sb.Append(",");
                    sb.Append(hole.ToString().Replace("LINESTRING ", ""));
                }
            }

            sb.Append(")");

            SqlGeometry ret = SqlTypesExtensions.STGeomFromText(sb.ToString(), outerRing.STSrid.Value);
            ret = ret.MakeValidIfInvalid(2);

            return(ret);
        }
        public void Initialize(string traceFileName)
        {
            try
            {
                _traceFileName = traceFileName;
                _filePath      = System.IO.Path.GetDirectoryName(_traceFileName);

                _traceLines = new ObservableCollection <TraceLineDesign>();
                using (FileStream fs = new FileStream(_traceFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    using (StreamReader sr = new StreamReader(fs))
                    {
                        string lineText = sr.ReadLine();                         // skip header
                        lineText = sr.ReadLine();
                        while (lineText != null)
                        {
                            // Parse line
                            TraceLineDesign traceLine = TraceLineDesign.Parse(lineText);

                            // Create geometry
                            if (string.IsNullOrEmpty(traceLine.GeometryDataFile) == false)
                            {
                                if (traceLine.GeometryDataFile.EndsWith("list.dat"))
                                {
                                    _listGeometryStyles[traceLine.UniqueId] = SqlGeomStyledFactory.Create(SqlTypesExtensions.ReadList(System.IO.Path.Combine(_filePath, traceLine.GeometryDataFile)),
                                                                                                          traceLine.FillColor,
                                                                                                          traceLine.StrokeColor,
                                                                                                          traceLine.StrokeWidth,
                                                                                                          traceLine.Label,
                                                                                                          traceLine.IsChecked);
                                }
                                else
                                {
                                    _listGeometryStyles[traceLine.UniqueId] = new List <SqlGeometryStyled>()
                                    {
                                        SqlGeomStyledFactory.Create(SqlTypesExtensions.Read(System.IO.Path.Combine(_filePath, traceLine.GeometryDataFile)),
                                                                    traceLine.FillColor,
                                                                    traceLine.StrokeColor,
                                                                    traceLine.StrokeWidth,
                                                                    traceLine.Label,
                                                                    traceLine.IsChecked)
                                    };
                                }
                            }

                            // Add to collection
                            _traceLines.Add(traceLine);

                            // next value
                            lineText = sr.ReadLine();
                        }
                    }
                }



                // Set to view model
                _viewModel.Traces = _traceLines;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
        }