public static async Task DownloadMap(Models.Map map)
        {
            //Make sure the folder for the offline maps exists
            InitMBTilesFolder();

            //Calculate the the sqlite / mbtiles metadata
            Point          p        = Utils.Misc.CalculateCenter(map.BoundsRight, map.BoundsTop, map.BoundsLeft, map.BoundsBottom);
            metadataValues metadata = new metadataValues
            {
                name        = map.Name,
                description = "Created by hajk",
                version     = "1",
                minzoom     = map.ZoomMin.ToString(),
                maxzoom     = map.ZoomMax.ToString(),
                center      = p.X.ToString().Replace(",", ".") + "," + p.Y.ToString().Replace(",", "."),
                bounds      = map.BoundsTop.ToString().Replace(",", ".") + "," + map.BoundsLeft.ToString().Replace(",", ".") + "," + map.BoundsBottom.ToString().Replace(",", ".") + "," + map.BoundsRight.ToString().Replace(",", "."),
                format      = "png",
                type        = "png",
            };

            SQLiteConnection conn = MBTilesWriter.CreateDatabase(MainActivity.rootPath + "/MBTiles/" + map.Name + ".mbtiles", metadata);

            if (conn == null)
            {
                return;
            }

            for (int zoom = map.ZoomMin; zoom <= map.ZoomMax; zoom++)
            {
                var leftBottom = Tile.CreateAroundLocation(map.BoundsLeft, map.BoundsBottom, zoom);
                var topRight   = Tile.CreateAroundLocation(map.BoundsRight, map.BoundsTop, zoom);

                var minX = Math.Min(leftBottom.X, topRight.X);
                var maxX = Math.Max(leftBottom.X, topRight.X);
                var minY = Math.Min(leftBottom.Y, topRight.Y);
                var maxY = Math.Max(leftBottom.Y, topRight.Y);

                var tiles = new AwesomeTiles.TileRange(minX, minY, maxX, maxY, zoom);

                var tilesCount = tiles.Count;
                totalTilesCount += tilesCount;
                Log.Information($"Need to download {tilesCount} tiles for zoom level {zoom}");

                await DownloadTiles(tiles, zoom, conn);
            }
            conn.Close();
            Log.Information($"Done downloading map for {metadata.name}");
        }
        private static async Task DownloadTiles(AwesomeTiles.TileRange range, int zoom, SQLiteConnection conn)
        {
            string OSMServer = Preferences.Get("OSMServer", PrefsActivity.OSMServer_s);

            //Same, but without parallell processing.

            /*            foreach (var tile in range)
             *          {
             *              byte[] data = null;
             *              for (int i = 0; i < 10; i++)
             *              {
             *                  var url = OSMServer + $"{zoom}/{tile.X}/{tile.Y}.png";
             *                  data = await DownloadImageAsync(url);
             *                  if (data != null)
             *                      break;
             *
             *                  Thread.Sleep(10000);
             *              }
             *
             *              Log.Information($"Zoomindex: {zoom}, x/y: {tile.X}/{tile.Y}, ID: {tile.Id}. Done:{++done}/{totalTilesCount}");
             *              WriteOsmSQlite(data, zoom, tile.X, tile.Y);
             *          };*/

            await range.ParallelForEachAsync(async tile =>
            {
                byte[] data = null;
                for (int i = 0; i < 10; i++)
                {
                    var url = OSMServer + $"{zoom}/{tile.X}/{tile.Y}.png";
                    data    = await DownloadImageAsync(url);
                    if (data != null)
                    {
                        break;
                    }
                    Thread.Sleep(10000);
                }

                Log.Information($"Zoomindex: {zoom}, x/y: {tile.X}/{tile.Y}, ID: {tile.Id}. Done:{++done}/{totalTilesCount}");
                WriteOsmSQlite(data, zoom, tile.X, tile.Y, conn);
            });
        }
Beispiel #3
0
 /// <summary>
 /// Creates a new range center first enumerable.
 /// </summary>
 /// <param name="tileRange"></param>
 public TileRangeCenterFirst(TileRange tileRange)
 {
     _tileRange = tileRange;
 }
Beispiel #4
0
 /// <summary>
 /// Disposes of all resources associated with this object.
 /// </summary>
 public void Dispose()
 {
     _range = null;
 }
Beispiel #5
0
 /// <summary>
 /// Creates the enumerator.
 /// </summary>
 /// <param name="range"></param>
 public TileRangeCenteredEnumerator(TileRange range)
 {
     _range = range;
 }
Beispiel #6
0
 public TileRangeEnumerator(TileRange range)
 {
     _range = range;
 }