Esempio n. 1
0
        public void Test(string fileName, int width, int height)
        {
            var vanakBbx1 = WorldfileManager.ReadImageBoundingBox(fileName, width, height);

            var vanakWorldfile = Worldfile.Read(fileName);

            var vanakBbx2 = vanakWorldfile.GetBoundingBox(width, height);

            Assert.AreEqual(vanakBbx1, vanakBbx2);

            var calculatedVanakWorldfile = WorldfileManager.Create(vanakBbx2, width, height);

            var delta = 1E-7;

            Assert.AreEqual(vanakWorldfile.XPixelSize, calculatedVanakWorldfile.XPixelSize, delta);
            Assert.AreEqual(vanakWorldfile.YPixelSize, calculatedVanakWorldfile.YPixelSize, delta);
            Assert.AreEqual(vanakWorldfile.CenterOfUpperLeftPixel.X, calculatedVanakWorldfile.CenterOfUpperLeftPixel.X, delta);
            Assert.AreEqual(vanakWorldfile.CenterOfUpperLeftPixel.Y, calculatedVanakWorldfile.CenterOfUpperLeftPixel.Y, delta);
        }
        public static void CreateFromWebMercatorWorldfile(string imageFileName, double pixelSize, string outputDirectory = null)
        {
            var bitmap = new BitmapImage(new Uri(imageFileName));

            var worldfileName = WorldfileManager.TryGetAssociatedWorldfileName(imageFileName);

            if (worldfileName == null)
            {
                System.Diagnostics.Debug.Print($"Worldfile not found: '{worldfileName}'");
                return;
            }

            var webMercatorBoundingBox = WorldfileManager.ReadImageBoundingBox(worldfileName, (int)bitmap.Width, (int)bitmap.Height);

            int zoomLevel = WebMercatorUtility.GetZoomLevel(bitmap.Height * pixelSize / webMercatorBoundingBox.Height);

            if (zoomLevel >= 20)
            {
                zoomLevel = 19;
            }

            if (outputDirectory == null)
            {
                //outputDirectory = $"{System.IO.Path.GetDirectoryName(imageFileName)}\\{System.IO.Path.GetFileNameWithoutExtension(imageFileName)}";
                outputDirectory = Path.Combine(Path.GetDirectoryName(imageFileName), Path.GetFileNameWithoutExtension(imageFileName));
            }

            if (!Directory.Exists($"{outputDirectory}"))
            {
                Directory.CreateDirectory($"{outputDirectory}");
            }

            WriteBoundingBox(webMercatorBoundingBox, outputDirectory);

            while (zoomLevel > 0 && zoomLevel < 20)
            {
                var tiles = WebMercatorUtility.WebMercatorBoundingBoxToGoogleTileRegions(webMercatorBoundingBox, zoomLevel);

                if (tiles.Count < 1)
                {
                    break;
                }

                var subDirectory = Path.Combine(outputDirectory, zoomLevel.ToString());

                if (!Directory.Exists(subDirectory))
                {
                    Directory.CreateDirectory(subDirectory);
                }

                SplitIntoGoogleTiles(bitmap, webMercatorBoundingBox, tiles, subDirectory);

                zoomLevel--;
            }

            var zipFileName = Path.ChangeExtension(imageFileName, "pyrmd");

            if (File.Exists(zipFileName))
            {
                File.Delete(zipFileName);
            }

            ZipFile.CreateFromDirectory(outputDirectory, Path.ChangeExtension(imageFileName, "pyrmd"));

            Sta.Common.Helpers.IOHelper.DeleteDirectory(outputDirectory);
        }