コード例 #1
0
ファイル: Program.cs プロジェクト: freeExec/SimplifyPolyFile
        static void Main(string[] args)
        {
            if (args.Length != 2)
            {
                Console.WriteLine("Usage: {0} filename.poly tolerance-degree", AppDomain.CurrentDomain.FriendlyName);
                return;
            }

            if (!File.Exists(args[0]))
            {
                Console.WriteLine("File {0} not found.", args[0]);
                return;
            }

            System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;

            double tolerance;

            if (!Double.TryParse(args[1], out tolerance))
            {
                Console.WriteLine("Tolerance is not a number.");
                return;
            }

            tolerance *= 10000000;

            var outputFilepath = Path.GetFileNameWithoutExtension(Path.GetFullPath(args[0])) + "-simplify.poly";

            var polyFile = PolyFile.FromFile(args[0]);

            int pointsOrig = 0;
            int pointsSimp = 0;

            for (int i = 0; i < polyFile.Outer.Count; i++)
            {
                pointsOrig += polyFile.Outer[i].Nodes.Count;
                var simplify = Simplify.SimplifyDouglasPeuckerPolyline(polyFile.Outer[i].Nodes, tolerance);
                pointsSimp       += simplify.Count;
                polyFile.Outer[i] = new Polygon(simplify);
            }
            for (int i = 0; i < polyFile.Inner.Count; i++)
            {
                pointsOrig += polyFile.Inner[i].Nodes.Count;
                var simplify = Simplify.SimplifyDouglasPeuckerPolyline(polyFile.Inner[i].Nodes, tolerance);
                pointsSimp       += simplify.Count;
                polyFile.Inner[i] = new Polygon(simplify);
            }

            polyFile.Save(outputFilepath);

            Console.WriteLine("Points: {0} -> {1} ({2:P0})", pointsOrig, pointsSimp, pointsSimp / (float)pointsOrig);
        }
コード例 #2
0
        public static PolyFile FromFile(string filePath)
        {
            if (!File.Exists(filePath))
            {
                Console.Error.WriteLine("File {0} not found.", filePath);
                throw new FileNotFoundException();
            }

            var polyFile = new PolyFile();

            string[] borderLines = File.ReadAllLines(filePath /*, Encoding.GetEncoding(1251)*/);
            polyFile.Name = borderLines[0];
            Polygon polyline     = null;
            bool    polygonInner = false;

            for (int i = 1; i < borderLines.Length; i++)
            {
                var line = borderLines[i];
                if (line[0] != ' ')
                {
                    if (line == "END")
                    {
                        if (polyline != null && polyline.IsClose)
                        {
                            if (polygonInner)
                            {
                                polyFile.Inner.Add(polyline);
                            }
                            else
                            {
                                polyFile.Outer.Add(polyline);
                            }
                        }
                        polyline = null;
                        continue;
                    }
                    polyline     = new Polygon();
                    polygonInner = (line[0] == '!');

                    continue;
                }

                var coords = borderLines[i].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                int lon    = ((int)Math.Round(Double.Parse(coords[0], System.Globalization.CultureInfo.InvariantCulture) * 10000000));
                int lat    = ((int)Math.Round(Double.Parse(coords[1], System.Globalization.CultureInfo.InvariantCulture) * 10000000));

                polyline.AddNode(lon, lat);
            }

            return(polyFile);
        }