Exemple #1
0
        private bool DrawImages(Rectangle localBlockView, GoogleRectangle localScreenView)
        {
            for (var x = localBlockView.Left; x <= localBlockView.Right; x++)
            {
                for (var y = localBlockView.Top; y <= localBlockView.Bottom; y++)
                {
                    var block = new GoogleBlock(x, y, Level);
                    var pt    = ((GoogleCoordinate)block).GetScreenPoint(localScreenView);

                    var bmp = FindImage(block);
                    if (bmp != null)
                    {
                        DrawBitmap(bmp, pt);
                    }
                    else
                    {
                        DrawBitmap(_emptyBlock, pt);
                        PutMapThreadEvent(WorkerEventType.DownloadImage, block, EventPriorityType.Idle);
                    }

                    if (Terminating)
                    {
                        return(true);
                    }

                    if (localScreenView.CompareTo(ScreenView) != 0)
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }
        public SpatialSheet <TNode> this[GoogleBlock block]
        {
            get
            {
                SpatialSheet <TNode> sheet;

                if (_children == null)
                {
                    _children = new SortedDictionary <GoogleBlock, SpatialSheet <TNode> >();
                }

                if (!_children.TryGetValue(block, out sheet))
                {
                    //нужен для тюнинга индекса, содержит количество ветвей индекса на каждом уровне
                    if (_sheet.Level > 0 && _sheet.Level <= _tree.NodeDimension.Length)
                    {
                        _tree.NodeDimension[_sheet.Level - 1]++;
                    }

                    sheet = new SpatialSheet <TNode>(
                        _tree,
                        _sheet.Level + 1,
                        _sheet.NextGoogleLevel,
                        (GoogleRectangle)block);
                    _children.Add(block, sheet);
                }
                return(sheet);
            }
        }
Exemple #3
0
        public static Bitmap DownloadImageFromGoogle(GoogleBlock block, bool getBitmap)
        {
            try
            {
                var oRequest  = GoogleMapUtilities.CreateGoogleWebRequest(block);
                var oResponse = (HttpWebResponse)oRequest.GetResponse();

                var bmpStream = new MemoryStream();
                var oStream   = oResponse.GetResponseStream();
                if (oStream != null)
                {
                    oStream.CopyTo(bmpStream);
                }
                oResponse.Close();
                if (bmpStream.Length > 0)
                {
                    WriteImageToFile(block, bmpStream);
                    return(getBitmap ? (Bitmap)Image.FromStream(bmpStream) : null);
                }
            }
            catch (Exception ex)
            {
                //do nothing
                System.Diagnostics.Trace.WriteLine(ex.Message);
            }
            return(null);
        }
Exemple #4
0
        private void DownloadImage(GoogleBlock block)
        {
            if (MapCache.ContainsKey(block))
            {
                var dimg = MapCache[block];
                if (dimg.Bmp != null) //to turn off compile warning
                {
                    dimg.Timestamp = DateTime.Now.Ticks;
                }
            }
            else
            {
                var bmp = DownloadImageFromFile(block) ?? DownloadImageFromGoogle(block, true);

                if (bmp != null)
                {
                    bmp = CreateCompatibleBitmap(bmp, GoogleBlock.BlockSize, GoogleBlock.BlockSize, PiFormat);

                    var dimg = new MapCacheItem {
                        Timestamp = DateTime.Now.Ticks, Bmp = bmp
                    };
                    MapCache[block] = dimg;

                    TruncateImageCache(block);

                    PutMapThreadEvent(WorkerEventType.DrawImage, block, EventPriorityType.Low);
                }
            }
        }
Exemple #5
0
        private void LineSheetAction(TNode node, SheetActionType actionType)
        {
            var blockViewLevel = NextGoogleLevel;

            var line = node.Rectangle;
            var rect = new GoogleRectangle(line, blockViewLevel).BlockView;

            var deltaX = (rect.Left <= rect.Right) ? 1 : -1;
            var deltaY = (rect.Top <= rect.Bottom) ? 1 : -1;

            //line search in daughter sheet
            for (var x = rect.Left; (deltaX == 1 && x <= rect.Right) || (deltaX == -1 && x >= rect.Right); x += deltaX)
            {
                for (var y = rect.Top; (deltaY == 1 && y <= rect.Bottom) || (deltaY == -1 && y >= rect.Bottom); y += deltaY)
                {
                    var block      = new GoogleBlock(x, y, blockViewLevel);
                    var googleRect = (GoogleRectangle)block;

                    if (googleRect.LineContains(line) != InterseptResult.None)
                    {
                        lock (this)
                        {
                            var sheet = Sheets[block];

                            sheet.SheetAction(node, actionType);

                            PostSheetAction(block, sheet, actionType);
                        }
                    }
                }
            }
        }
Exemple #6
0
        private static void WriteImageToFile(GoogleBlock block, Stream bmpStream)
        {
            var fileName = Properties.Settings.GetMapFileName(block);

            try
            {
                if (!File.Exists(fileName))
                {
                    var path    = Path.GetDirectoryName(fileName) ?? "";
                    var destdir = new DirectoryInfo(path);
                    if (!destdir.Exists)
                    {
                        destdir.Create();
                    }
                    var fileStream = File.Create(fileName);
                    try
                    {
                        bmpStream.Seek(0, SeekOrigin.Begin);
                        bmpStream.CopyTo(fileStream);
                    }
                    finally
                    {
                        fileStream.Flush();
                        fileStream.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                //do nothing
                System.Diagnostics.Trace.WriteLine(ex.Message);
            }
        }
Exemple #7
0
        private void RectangleSheetAction(TNode node, SheetActionType actionType)
        {
            var blockViewLevel = NextGoogleLevel;

            var rect = new GoogleRectangle(node.Rectangle, blockViewLevel).BlockView;

            var deltaX = (rect.Left <= rect.Right) ? 1 : -1;
            var deltaY = (rect.Top <= rect.Bottom) ? 1 : -1;

            //rectangle search in daughter sheet
            for (var x = rect.Left; (deltaX == 1 && x <= rect.Right) || (deltaX == -1 && x >= rect.Right); x += deltaX)
            {
                for (var y = rect.Top; (deltaY == 1 && y <= rect.Bottom) || (deltaY == -1 && y >= rect.Bottom); y += deltaY)
                {
                    var block = new GoogleBlock(x, y, blockViewLevel);
                    lock (this)
                    {
                        var sheet = Sheets[block];

                        sheet.SheetAction(node, actionType);

                        PostSheetAction(block, sheet, actionType);
                    }
                }
            }
        }
Exemple #8
0
        private void DownloadThread(CancellationToken ct)
        {
            var leftBound  = new Coordinate(Properties.Settings.Default.LeftMapBound, Properties.Settings.Default.TopMapBound);
            var rightBound = new Coordinate(Properties.Settings.Default.RightMapBound, Properties.Settings.Default.BottomMapBound);

            var rectBound = new CoordinateRectangle(leftBound, rightBound);

            var mapBlockCount = 0;

            for (var mapLevel = Properties.Settings.Default.MinZoomLevel; mapLevel <= Properties.Settings.Default.MaxZoomLevel; mapLevel++)
            {
                var mapWidth  = Convert.ToInt32((new GoogleCoordinate(rectBound.RightTop, mapLevel)).X - (new GoogleCoordinate(rectBound.LeftTop, mapLevel)).X) + 2 * GoogleBlock.BlockSize;
                var mapHeight = Convert.ToInt32((new GoogleCoordinate(rectBound.LeftBottom, mapLevel)).Y - (new GoogleCoordinate(rectBound.LeftTop, mapLevel)).Y) + 2 * GoogleBlock.BlockSize;

                var viewBound = rectBound.LineMiddlePoint.GetScreenViewFromCenter(mapWidth, mapHeight, mapLevel);
                var blockView = viewBound.BlockView;
                mapBlockCount += (blockView.Right - blockView.Left + 1) * (blockView.Bottom - blockView.Top + 1);
            }

            var mapBlockNumber = 0;

            BeginInvoke(ProgressEvent, new Object[] { mapBlockNumber * 100 / mapBlockCount, mapBlockNumber, mapBlockCount });

            for (var mapLevel = Properties.Settings.Default.MinZoomLevel; mapLevel <= Properties.Settings.Default.MaxZoomLevel; mapLevel++)
            {
                var mapWidth  = Convert.ToInt32((new GoogleCoordinate(rectBound.RightTop, mapLevel)).X - (new GoogleCoordinate(rectBound.LeftTop, mapLevel)).X) + 2 * GoogleBlock.BlockSize;
                var mapHeight = Convert.ToInt32((new GoogleCoordinate(rectBound.LeftBottom, mapLevel)).Y - (new GoogleCoordinate(rectBound.LeftTop, mapLevel)).Y) + 2 * GoogleBlock.BlockSize;

                var viewBound = rectBound.LineMiddlePoint.GetScreenViewFromCenter(mapWidth, mapHeight, mapLevel);
                var blockView = viewBound.BlockView;

                for (var x = blockView.Left; x <= blockView.Right; x++)
                {
                    for (var y = blockView.Top; y <= blockView.Bottom; y++)
                    {
                        var block = new GoogleBlock(x, y, mapLevel);

                        var fileName = Properties.Settings.GetMapFileName(block);
                        if (!File.Exists(fileName))
                        {
                            MapLayer.DownloadImageFromGoogle(block, false);
                        }

                        mapBlockNumber++;

                        BeginInvoke(ProgressEvent, new Object[] { mapBlockNumber * 100 / mapBlockCount, mapBlockNumber, mapBlockCount });

                        if (ct.IsCancellationRequested)
                        {
                            return;
                        }
                    }
                }
            }
            BeginInvoke(ProgressEvent, new Object[] { 101, mapBlockNumber, mapBlockCount });
        }
        public void Remove(GoogleBlock block)
        {
            if (_children != null)
            {
                //нужен для тюнинга индекса, содержит количество ветвей индекса на каждом уровне
                _tree.NodeDimension[_sheet.Level - 1]--;

                _children.Remove(block);
            }
        }
Exemple #10
0
 private static Bitmap FindImage(GoogleBlock block)
 {
     if (MapCache.ContainsKey(block))
     {
         var dimg = MapCache[block];
         dimg.Timestamp = DateTime.Now.Ticks;
         return(dimg.Bmp);
     }
     return(null);
 }
Exemple #11
0
        private void GetFullMapThread(CancellationToken ct)
        {
            var leftBound  = new Coordinate(Properties.Settings.Default.LeftMapBound, Properties.Settings.Default.TopMapBound);
            var rightBound = new Coordinate(Properties.Settings.Default.RightMapBound, Properties.Settings.Default.BottomMapBound);

            var rectBound = new CoordinateRectangle(leftBound, rightBound);

            try
            {
                var mapWidth  = Convert.ToInt32((new GoogleCoordinate(rectBound.RightTop, _mapLevel)).X - (new GoogleCoordinate(rectBound.LeftTop, _mapLevel)).X) + 2 * GoogleBlock.BlockSize;
                var mapHeight = Convert.ToInt32((new GoogleCoordinate(rectBound.LeftBottom, _mapLevel)).Y - (new GoogleCoordinate(rectBound.LeftTop, _mapLevel)).Y) + 2 * GoogleBlock.BlockSize;

                var image    = GraphicLayer.CreateCompatibleBitmap(null, mapWidth, mapHeight, _mapPiFormat);
                var graphics = Graphics.FromImage(image);

                var viewBound      = rectBound.LineMiddlePoint.GetScreenViewFromCenter(mapWidth, mapHeight, _mapLevel);
                var blockView      = viewBound.BlockView;
                var mapBlockCount  = (blockView.Right - blockView.Left + 1) * (blockView.Bottom - blockView.Top + 1);
                var mapBlockNumber = 0;

                BeginInvoke(ProgressEvent, new Object[] { mapBlockNumber * 100 / mapBlockCount, mapBlockNumber, mapBlockCount });

                for (var x = blockView.Left; x <= blockView.Right; x++)
                {
                    for (var y = blockView.Top; y <= blockView.Bottom; y++)
                    {
                        var block = new GoogleBlock(x, y, _mapLevel);
                        var bmp   = GraphicLayer.CreateCompatibleBitmap(
                            MapLayer.DownloadImageFromFile(block) ?? MapLayer.DownloadImageFromGoogle(block, true),
                            GoogleBlock.BlockSize, GoogleBlock.BlockSize, _mapPiFormat);

                        var rect = ((GoogleRectangle)block).GetScreenRect(viewBound);
                        graphics.DrawImageUnscaled(bmp, rect.Location.X, rect.Location.Y);

                        mapBlockNumber++;

                        BeginInvoke(ProgressEvent, new Object[] { mapBlockNumber * 100 / mapBlockCount, mapBlockNumber, mapBlockCount });

                        if (ct.IsCancellationRequested)
                        {
                            return;
                        }
                    }
                }

                BeginInvoke(SaveMapEvent, new Object[] { image });
            }
            catch (Exception e)
            {
                BeginInvoke(ProgressEvent, new Object[] { 101, 0, 0 });
                MessageBox.Show(e.Message, @"Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Exemple #12
0
// ReSharper restore UnusedMember.Local

        public static string GetMapFileName(GoogleBlock block)
        {
            var mapPath = Default.MapCacheLocalPath;

            if (!Path.IsPathRooted(mapPath))
            {
                mapPath = Path.Combine(
                    Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
                    mapPath);
            }
            var fileName = Path.Combine(mapPath, block.Level + "\\" + (block.X / 100) + "_" + (block.Y / 100) + "\\" + block.Level + "_" + block.X + "_" + block.Y + ".png");

            return(fileName);
        }
Exemple #13
0
 private void PostSheetAction(GoogleBlock block, SpatialSheet <TNode> sheet, SheetActionType actionType)
 {
     switch (actionType)
     {
     case SheetActionType.Delete:
     {
         //delete sheet from index without elements
         if (sheet.IsEmpty)
         {
             Sheets.Remove(block);
         }
     } break;
     }
 }
Exemple #14
0
 public static Bitmap DownloadImageFromFile(GoogleBlock block)
 {
     try
     {
         var fileName = Properties.Settings.GetMapFileName(block);
         if (File.Exists(fileName))
         {
             var bmp = (Bitmap)Image.FromFile(fileName);
             return(bmp);
         }
     }
     catch (Exception ex)
     {
         //do nothing
         System.Diagnostics.Trace.WriteLine(ex.Message);
     }
     return(null);
 }
Exemple #15
0
        private void DrawImage(GoogleBlock block)
        {
            if (Terminating)
            {
                return;
            }

            if (block.Level == Level && block.Pt.Contains(_blockView))
            {
                var bmp = FindImage(block);
                if (bmp != null)
                {
                    var rect = ((GoogleRectangle)block).GetScreenRect(ScreenView);
                    DrawBitmap(bmp, rect.Location);

                    FireIvalidateLayer(rect);
                }
            }
        }
Exemple #16
0
        private void TruncateImageCache(GoogleBlock newCacheItem)
        {
            while (MapCache.Count > MaxCacheSize)
            {
                var mt     = GoogleBlock.Empty;
                var lTicks = DateTime.Now.Ticks;

                foreach (var lt in MapCache)
                {
                    if (lt.Value.Timestamp < lTicks && lt.Key.CompareTo(newCacheItem) != 0)
                    {
                        mt     = lt.Key;
                        lTicks = lt.Value.Timestamp;
                    }
                }

                if (mt != GoogleBlock.Empty)
                {
                    MapCache.Remove(mt);
                }
            }
        }
Exemple #17
0
 public MapWorkerEvent(WorkerEventType eventType, GoogleBlock block, EventPriorityType priorityType)
     : base(eventType, true, priorityType)
 {
     Block = block;
 }
Exemple #18
0
 private void PutMapThreadEvent(WorkerEventType eventType, GoogleBlock block, EventPriorityType priorityType)
 {
     PutWorkerThreadEvent(new MapWorkerEvent(eventType, block, priorityType));
 }