Example #1
0
        public void Renderer(string server, string service, string user = "", string pwd = "")
        {
            ISpatialReference sRef = SpatialReference.FromID($"epsg:{ _epsg }");
            if (sRef == null)
            {
                throw new Exception($"Can't load SpatialReference EPSG:{ _epsg }");
            }

            IEnvelope metadataBBox = _metadata.GetEPSGEnvelope(_epsg);
            if (metadataBBox == null)
            {
                throw new Exception($"Can't get extent for EPSG:{ _epsg } from metadata");
            }
            if (_bbox == null)
            {
                _bbox = metadataBBox;
            }
            else if (metadataBBox.Contains(_bbox))
            {
                throw new Exception($"BBox must be completly contained in tile cache BBox: { metadataBBox.ToBBoxString() }");
            }

            double width = _bbox.Width;
            double height = _bbox.Height;

            double dpu = 1.0;
            if (sRef.SpatialParameters.IsGeographic)
            {
                GeoUnitConverter converter = new GeoUnitConverter();
                dpu = converter.Convert(1.0, GeoUnits.Meters, GeoUnits.DecimalDegrees);
            }

            Grid grid = new Grid(
                (_orientation == GridOrientation.UpperLeft ?
                    _metadata.GetOriginUpperLeft(_epsg) :
                    _metadata.GetOriginLowerLeft(_epsg)),
                _metadata.TileWidth, _metadata.TileHeight, 96.0,
                _orientation);

            int level = 0;
            foreach (double scale in _metadata.Scales)
            {
                double res = scale / (96.0 / 0.0254) * dpu;
                grid.AddLevel(level++, res);
            }

            ServerConnection connector = new ServerConnection(server);

            int step = _cacheFormat == "compact" ? 128 : 1;

            #region Count Tiles

            int featureMax = 0;
            foreach (double scale in _preRenderScales ?? _metadata.Scales)
            {
                double res = scale / (96.0 / 0.0254) * dpu;
                int col0 = grid.TileColumn(_bbox.minx, res), col1 = grid.TileColumn(_bbox.maxx, res);
                int row0 = grid.TileRow(_bbox.maxy, res), row1 = grid.TileRow(_bbox.miny, res);

                featureMax += Math.Max(1, (Math.Abs(col1 - col0) + 1) * (Math.Abs(row1 - row0) + 1) / step / step);
            }

            #endregion

            Console.WriteLine($"TilesCount: { featureMax }");

            RenderTileThreadPool threadPool = new RenderTileThreadPool(connector, service, user, pwd, _maxParallelRequests);

            var thread = threadPool.FreeThread;
            if (_orientation == GridOrientation.UpperLeft)
            {
                thread.Start($"init/{ _cacheFormat }/ul/{ _epsg }/{ _imageFormat.Replace(".", "") }");
            }
            else
            {
                thread.Start($"init/{ _cacheFormat }/ll/{ _epsg }/{ _imageFormat.Replace(".", "") }");
            }

            foreach (double scale in _preRenderScales ?? _metadata.Scales)
            {
                double res = scale / (96.0 / 0.0254) * dpu;
                int col0 = grid.TileColumn(_bbox.minx, res), col1 = grid.TileColumn(_bbox.maxx, res);
                int row0 = grid.TileRow(_bbox.maxy, res), row1 = grid.TileRow(_bbox.miny, res);
                int cols = Math.Abs(col1 - col0) + 1;
                int rows = Math.Abs(row1 - row0) + 1;
                col0 = Math.Min(col0, col1);
                row0 = Math.Min(row0, row1);

                int tilePos = 0;
                Console.WriteLine();
                Console.WriteLine("Scale: " + scale.ToString() + " - " + Math.Max(1, (rows * cols) / step / step).ToString() + " tiles...");

                string boundingTiles = _cacheFormat == "compact" ? "/" + row0 + "|" + (row0 + rows) + "|" + col0 + "|" + (col0 + cols) : String.Empty;

                for (int row = row0; row < (row0 + rows) + (step - 1); row += step)
                {
                    for (int col = col0; col < (col0 + cols) + (step - 1); col += step)
                    {
                        while ((thread = threadPool.FreeThread) == null)
                        {
                            Thread.Sleep(50);
                            //if (!_cancelTracker.Continue)
                            //    return;
                        }
                        if (_orientation == GridOrientation.UpperLeft)
                            thread.Start("tile:render/" + _cacheFormat + "/ul/" + _epsg + "/" + scale.ToDoubleString() + "/" + row + "/" + col + _imageFormat + boundingTiles);
                        else
                            thread.Start("tile:render/" + _cacheFormat + "/ll/" + _epsg + "/" + scale.ToDoubleString() + "/" + row + "/" + col + _imageFormat + boundingTiles);

                        tilePos++;
                        if (tilePos % 5 == 0 || _cacheFormat == "compact")
                            Console.Write($"...{ tilePos }");

                        //if (!_cancelTracker.Continue)
                        //    return;
                    }
                }
            }

            while (threadPool.IsFinished == false)
            {
                Thread.Sleep(50);
            }

            if (!String.IsNullOrEmpty(threadPool.Exceptions))
            {
                //MessageBox.Show(threadPool.Exceptions, "Exceptions", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        private void Run()
        {
            if (_metadata == null || _mapServerClass == null || _mapServerClass.Dataset == null || _preRenderScales.Count == 0)
            {
                return;
            }

            string server  = ConfigTextStream.ExtractValue(_mapServerClass.Dataset.ConnectionString, "server");
            string service = ConfigTextStream.ExtractValue(_mapServerClass.Dataset.ConnectionString, "service");
            string user    = ConfigTextStream.ExtractValue(_mapServerClass.Dataset.ConnectionString, "user");
            string pwd     = ConfigTextStream.ExtractValue(_mapServerClass.Dataset.ConnectionString, "pwd");

            ISpatialReference sRef = SpatialReference.FromID("epsg:" + _selectedEpsg);

            if (sRef == null)
            {
                return;
            }

            IEnvelope extent = _metadata.GetEPSGEnvelope(_selectedEpsg);

            if (extent == null)
            {
                return;
            }
            if (_bounds == null)
            {
                _bounds = extent;
            }

            double width  = extent.Width;
            double height = extent.Height;

            double dpu = 1.0;

            if (sRef.SpatialParameters.IsGeographic)
            {
                GeoUnitConverter converter = new GeoUnitConverter();
                dpu = converter.Convert(1.0, GeoUnits.Meters, GeoUnits.DecimalDegrees);
            }

            Grid grid = new Grid(
                (_orientation == GridOrientation.UpperLeft ?
                 _metadata.GetOriginUpperLeft(_selectedEpsg) :
                 _metadata.GetOriginLowerLeft(_selectedEpsg)),

                /*new Point(extent.minx, extent.maxy) :
                 * new Point(extent.minx, extent.miny))*/
                _metadata.TileWidth, _metadata.TileHeight, 96.0,
                _orientation);

            int level = 0;

            foreach (double scale in _metadata.Scales)
            {
                double res = scale / (96.0 / 0.0254) * dpu;
                grid.AddLevel(level++, res);
            }

            MapServerConnection connector = new MapServerConnection(server);
            ProgressReport      report    = new ProgressReport();

            _cancelTracker.Reset();

            int step = _cacheFormat == "compact" ? 128 : 1;

            #region Count Tiles
            report.featureMax = 0;
            foreach (double scale in _preRenderScales)
            {
                double res = scale / (96.0 / 0.0254) * dpu;
                int    col0 = grid.TileColumn(_bounds.minx, res), col1 = grid.TileColumn(_bounds.maxx, res);
                int    row0 = grid.TileRow(_bounds.maxy, res), row1 = grid.TileRow(_bounds.miny, res);

                report.featureMax += Math.Max(1, (Math.Abs(col1 - col0) + 1) * (Math.Abs(row1 - row0) + 1) / step / step);
            }
            #endregion

            RenderTileThreadPool threadPool = new RenderTileThreadPool(connector, service, user, pwd, _maxParallelRequests);

            var thread = threadPool.FreeThread;
            if (_orientation == GridOrientation.UpperLeft)
            {
                thread.Start("init/" + _cacheFormat + "/ul/" + cmbEpsg.SelectedItem.ToString() + "/" + _imgExt.Replace(".", ""));
            }
            else
            {
                thread.Start("init/" + _cacheFormat + "/ll/" + cmbEpsg.SelectedItem.ToString() + "/" + _imgExt.Replace(".", ""));
            }

            foreach (double scale in _preRenderScales)
            {
                double res = scale / (96.0 / 0.0254) * dpu;
                int    col0 = grid.TileColumn(_bounds.minx, res), col1 = grid.TileColumn(_bounds.maxx, res);
                int    row0 = grid.TileRow(_bounds.maxy, res), row1 = grid.TileRow(_bounds.miny, res);
                int    cols = Math.Abs(col1 - col0) + 1;
                int    rows = Math.Abs(row1 - row0) + 1;
                col0 = Math.Min(col0, col1);
                row0 = Math.Min(row0, row1);

                if (ReportProgress != null)
                {
                    report.Message = "Scale: " + scale.ToString() + " - " + Math.Max(1, (rows * cols) / step / step).ToString() + " tiles...";
                    ReportProgress(report);
                }

                string boundingTiles = _cacheFormat == "compact" ? "/" + row0 + "|" + (row0 + rows) + "|" + col0 + "|" + (col0 + cols) : String.Empty;

                for (int row = row0; row < (row0 + rows) + (step - 1); row += step)
                {
                    for (int col = col0; col < (col0 + cols) + (step - 1); col += step)
                    {
                        while ((thread = threadPool.FreeThread) == null)
                        {
                            Thread.Sleep(50);
                            if (!_cancelTracker.Continue)
                            {
                                return;
                            }
                        }
                        if (_orientation == GridOrientation.UpperLeft)
                        {
                            thread.Start("tile:render/" + _cacheFormat + "/ul/" + cmbEpsg.SelectedItem.ToString() + "/" + scale.ToString(_nhi) + "/" + row + "/" + col + _imgExt + boundingTiles);
                        }
                        else
                        {
                            thread.Start("tile:render/" + _cacheFormat + "/ll/" + cmbEpsg.SelectedItem.ToString() + "/" + scale.ToString(_nhi) + "/" + row + "/" + col + _imgExt + boundingTiles);
                        }

                        if (ReportProgress != null)
                        {
                            report.featurePos++;
                            if (report.featurePos % 5 == 0 || _cacheFormat == "compact")
                            {
                                ReportProgress(report);
                            }
                        }
                        if (!_cancelTracker.Continue)
                        {
                            return;
                        }
                    }
                }
            }

            while (threadPool.IsFinished == false)
            {
                Thread.Sleep(50);
            }

            if (!String.IsNullOrEmpty(threadPool.Exceptions))
            {
                MessageBox.Show(threadPool.Exceptions, "Exceptions", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }