示例#1
0
        /// <summary>
        /// Create web request to get bitmap block from google bitmap cache
        /// </summary>
        public static HttpWebRequest CreateGoogleWebRequest(GoogleBlock block)
        {
            var urlGoogle = CreateUrl(block);
            var oRequest  = (HttpWebRequest)WebRequest.Create(urlGoogle);

            oRequest.UserAgent = "www.simplemap.ru"; //!!!must have to retrieve image from google
            return(oRequest);
        }
示例#2
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;
        }
示例#3
0
 /// <summary>
 /// Create Url to get bitmap block from google bitmap cache
 /// </summary>
 public static string CreateUrl(GoogleBlock block)
 {
     return(String.Format(Properties.Settings.Default.GoogleUrl, block.X, block.Y, block.Level - 1));
 }
 /// <summary>
 /// Create web request to get bitmap block from google bitmap cache
 /// </summary>
 public static HttpWebRequest CreateGoogleWebRequest(GoogleBlock block)
 {
     var urlGoogle = CreateUrl(block);
     var oRequest = (HttpWebRequest)WebRequest.Create(urlGoogle);
     oRequest.UserAgent = "www.simplemap.ru"; //!!!must have to retrieve image from google
     return oRequest;
 }
 /// <summary>
 /// Create Url to get bitmap block from google bitmap cache
 /// </summary>
 public static string CreateUrl(GoogleBlock block)
 {
     return String.Format(Properties.Settings.Default.GoogleUrl, block.X, block.Y, block.Level - 1);
 }
        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);
            }
        }
        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});
        }
示例#8
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;
        }
示例#9
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;
 }
示例#10
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);
     }
 }
示例#11
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;
 }
示例#12
0
 public MapWorkerEvent(WorkerEventType eventType, GoogleBlock block, EventPriorityType priorityType)
     : base(eventType, true, priorityType)
 {
     Block = block;
 }
示例#13
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;
        }
示例#14
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);
            }
        }
示例#15
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);
                }
            }
        }
示例#16
0
 private void PutMapThreadEvent(WorkerEventType eventType, GoogleBlock block, EventPriorityType priorityType)
 {
     PutWorkerThreadEvent(new MapWorkerEvent(eventType, block, priorityType));
 }
示例#17
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);
                }
            }
        }