Esempio n. 1
0
        public async Task <string> StartProcessing(InstructionSet instructions)
        {
            string validationResult = Validate(instructions);

            if (validationResult != null)
            {
                return(validationResult);
            }

            Size?dims = ImageHelper.GetDimensions(instructions.InputPath);

            if (dims == null)
            {
                return(null);
            }

            string      inputPath       = instructions.InputPath;
            int         minZoom         = instructions.MinimumZoom;
            int         maxZoom         = instructions.MaximumZoom;
            var         inputSize       = (Size)dims;
            string      outputDirectory = instructions.OutputDirectory;
            bool        skipExisting    = instructions.SkipExisting;
            string      baseName        = instructions.OutputName;
            ImageFormat outputFormat    = instructions.OutputFormat;
            string      outputExtension = outputFormat.GetFileExtension();
            int         outputSize      = instructions.OutputSize;

            return(await Task <string> .Run(() =>
            {
                try
                {
                    var baseImage = Image.FromFile(inputPath) as Bitmap;

                    int processed = 0;
                    for (int zoom = minZoom; zoom <= maxZoom; zoom++)
                    {
                        if (ProgressChanged != null)
                        {
                            ProgressChanged(this, new ProgressChangedEventArgs(processed, "Processing zoom " + zoom));
                        }

                        int tiles = 1 << zoom;

                        int tileSize = inputSize.Width / tiles;

                        for (int tileX = 0; tileX < tiles; tileX++)
                        {
                            for (int tileY = 0; tileY < tiles; tileY++)
                            {
                                string outputFile = Path.Combine(outputDirectory,
                                                                 string.Format("{0}.{1}.{2}.{3}{4}", baseName, zoom, tileX, tileY, outputExtension));

                                if (skipExisting && File.Exists(outputFile))
                                {
                                    continue;
                                }

                                var baseTile = new Bitmap(tileSize, tileSize);
                                using (Graphics g = Graphics.FromImage(baseTile))
                                {
                                    g.DrawImage(baseImage, new RectangleF(0, 0, tileSize, tileSize),
                                                new RectangleF(tileSize *tileX, tileSize *tileY, tileSize, tileSize),
                                                GraphicsUnit.Pixel);
                                }

                                Bitmap tile = ResizeImage(baseTile, new Size(outputSize, outputSize));

                                tile.Save(outputFile, outputFormat);

                                baseTile.Dispose();
                                tile.Dispose();

                                if (ProgressChanged != null)
                                {
                                    ProgressChanged(this,
                                                    new ProgressChangedEventArgs(++processed, "Processing zoom " + zoom));
                                }
                            }
                        }
                    }

                    return null;
                }
                catch (Exception e)
                {
                    return e.Message;
                }
            }));
        }
Esempio n. 2
0
        public async Task <string> StartProcessing(InstructionSet instructions)
        {
            string validationResult = Validate(instructions);

            if (validationResult != null)
            {
                return(validationResult);
            }

            string inputPath = instructions.InputPath;
            string tempPath  =
                Path.Combine(Path.GetDirectoryName(inputPath), Path.GetFileNameWithoutExtension(inputPath)) + ".temp";
            int         resizeFactor    = instructions.PreprocessorResizeFactor;
            bool        doResize        = resizeFactor > 1;
            double      origSize        = instructions.MapSize;
            double      targetSize      = origSize * resizeFactor;
            int         outputSize      = instructions.OutputSize;
            string      cssPath         = instructions.InputPath + ".mapcss";
            string      baseName        = instructions.OutputName;
            ImageFormat outputFormat    = instructions.OutputFormat;
            string      outputExtension = outputFormat.GetFileExtension();

            int    minZoom      = instructions.MinimumZoom;
            int    maxZoom      = instructions.MaximumZoom;
            string outputDir    = instructions.OutputDirectory;
            bool   skipExisting = instructions.SkipExisting;

            return(await Task <string> .Run(() =>
            {
                if (doResize)
                {
                    XDocument doc = XDocument.Load(inputPath);

                    IEnumerable <XElement> nodes = doc.Root.Elements("node");

                    int nodeCount = 0;
                    foreach (XElement node in nodes)
                    {
                        XAttribute lat = node.Attribute("lat");
                        XAttribute lon = node.Attribute("lon");

                        double newLat = double.Parse(lat.Value, CultureInfo.InvariantCulture) * resizeFactor;
                        double newLon = double.Parse(lon.Value, CultureInfo.InvariantCulture) * resizeFactor;

                        lat.Value = newLat.ToString("0.000000000000000000", CultureInfo.InvariantCulture);
                        lon.Value = newLon.ToString("0.000000000000000000", CultureInfo.InvariantCulture);

                        nodeCount++;
                    }

                    doc.Save(tempPath);

                    inputPath = tempPath;
                }

                MapCSSInterpreter mapCss;
                RenderingInstance renderer;

                FileStream mapCssStream = new FileInfo(cssPath).OpenRead();
                try
                {
                    mapCss = new MapCSSInterpreter(mapCssStream, new MapCSSDictionaryImageSource());
                }
                catch (Exception e)
                {
                    return e.Message;
                }
                finally
                {
                    mapCssStream.Dispose();
                }

                FileStream inputStream = new FileInfo(inputPath).OpenRead();
                try
                {
                    renderer = RenderingInstance.Build(new XmlOsmStreamSource(inputStream), mapCss);
                }
                catch (Exception e)
                {
                    return e.Message;
                }
                finally
                {
                    inputStream.Dispose();
                }

                int processed = 0;
                for (int zoom = minZoom; zoom <= maxZoom; zoom++)
                {
                    if (ProgressChanged != null)
                    {
                        ProgressChanged(this, new ProgressChangedEventArgs(processed, "Processing zoom " + zoom));
                    }

                    int tiles = 1 << zoom;
                    for (int x = 0; x < tiles; x++)
                    {
                        for (int y = 0; y < tiles; y++)
                        {
                            string outfile = string.Format(@"{3}\{5}.{2}.{0}.{1}{4}", x, y, zoom, outputDir,
                                                           outputExtension, baseName);

                            if (skipExisting && File.Exists(outfile))
                            {
                                continue;
                            }

                            Bitmap outstr = renderer.Render(x, y, zoom, targetSize, outputSize);
                            outstr.Save(outfile, outputFormat);
                            outstr.Dispose();

                            if (ProgressChanged != null)
                            {
                                ProgressChanged(this,
                                                new ProgressChangedEventArgs(++processed, "Processing zoom " + zoom));
                            }
                        }
                    }
                }

                inputStream.Dispose();

                return null;
            }));
        }
Esempio n. 3
0
 public string Validate(InstructionSet instructions)
 {
     return(ImageDefaults.Validate(instructions));
 }
Esempio n. 4
0
        public async Task <string> StartProcessing(InstructionSet instructions)
        {
            string validationResult = Validate(instructions);

            if (validationResult != null)
            {
                return(validationResult);
            }

            Size?dims = ImageHelper.GetDimensions(instructions.InputPath);

            if (dims == null)
            {
                return(null);
            }

            string      inputPath       = instructions.InputPath;
            int         minZoom         = instructions.MinimumZoom;
            int         maxZoom         = instructions.MaximumZoom;
            string      outputDirectory = instructions.OutputDirectory;
            string      baseName        = instructions.OutputName;
            ImageFormat outputFormat    = instructions.OutputFormat;
            string      outputExtension = outputFormat.GetFileExtension();
            int         outputSize      = instructions.OutputSize;

            return(await Task <string> .Run(() =>
            {
                try
                {
                    int processed = 0;
                    for (int zoom = minZoom; zoom <= maxZoom; zoom++)
                    {
                        if (ProgressChanged != null)
                        {
                            ProgressChanged(this, new ProgressChangedEventArgs(processed, "Processing zoom " + zoom));
                        }

                        int tiles = 1 << zoom;


                        var input = new MagickImage(inputPath);

                        input.Resize(outputSize *tiles, outputSize *tiles);

                        var x = -1;
                        var y = 0;
                        foreach (var tile in input.CropToTiles(outputSize, outputSize))
                        {
                            x++;
                            y += x / tiles;
                            x %= tiles;

                            var outPath = Path.Combine(outputDirectory,
                                                       string.Format("{0}.{1}.{2}.{3}{4}", baseName, zoom, x, y, outputExtension));

                            if (!instructions.SkipExisting || !File.Exists(outPath))
                            {
                                tile.Write(outPath);
                            }

                            if (ProgressChanged != null)
                            {
                                ProgressChanged(this,
                                                new ProgressChangedEventArgs(++processed, "Processing zoom " + zoom));
                            }
                        }


//                        RunMagick(string.Format(
//                            "\"{6}\" -resize {0}x{0} -crop {1}x{1} -set filename:tile \"%[fx:page.x/{1}].%[fx:page.y/{1}]\" " +
//                            "+repage +adjoin \"{2}/{3}.{4}.%[filename:tile]{5}\"", outputSize * tiles, outputSize,
//                            outputDirectory, baseName, zoom, outputExtension, inputPath));
                    }
                }
                catch (Exception e)
                {
                    return e.Message;
                }

                return null;
            }));
        }