public TilingScheme getBaiDuSchema(double left, double right, double top, double bottom) { TilingScheme tilingScheme = getBaiDuSchema(); Point leftBottom = new Point(left, bottom); Point rightTop = new Point(right, top); tilingScheme.InitialExtent = new Envelope(leftBottom.X, leftBottom.Y, rightTop.X, rightTop.Y); return(tilingScheme); }
/// <summary> /// /// </summary> /// <param name="levels"></param> /// <param name="extent">sr=3857</param> /// <returns></returns> public static long CalculateTileCount(int[] levels, ESRI.ArcGIS.Client.Geometry.Envelope extent) { long total = 0; int constTileSize = 256; LODInfo[] LODs = new LODInfo[20]; const double cornerCoordinate = 20037508.3427892; double resolution = cornerCoordinate * 2 / 256; double scale = 591657527.591555; for (int i = 0; i < LODs.Length; i++) { LODs[i] = new LODInfo() { Resolution = resolution, LevelID = i, Scale = scale }; resolution /= 2; scale /= 2; } PBS.Util.Point TileOrigin = new PBS.Util.Point(-cornerCoordinate, cornerCoordinate); foreach (int level in levels) { LODInfo lod = LODs[level]; double oneTileDistance = lod.Resolution * constTileSize; //calculate start tile and end tile int startTileRow = (int)(Math.Abs(TileOrigin.Y - extent.YMax) / oneTileDistance); int startTileCol = (int)(Math.Abs(TileOrigin.X - extent.XMin) / oneTileDistance); int endTileRow = (int)(Math.Abs(TileOrigin.Y - extent.YMin) / oneTileDistance); int endTileCol = (int)(Math.Abs(TileOrigin.X - extent.XMax) / oneTileDistance); //"startR,startC,endR,endC" total += Math.Abs((endTileCol - startTileCol + 1) * (endTileRow - startTileRow + 1)); } return(total); }
protected override void DoConvertToMBTiles(string outputPath, string name, string description, string attribution, int[] levels, Geometry geometry, bool doCompact) { Util.Envelope initial; Point pLeftTop; Point pRightBottom; if (geometry is Envelope) { initial = geometry as Envelope; pLeftTop = Utility.GeographicToWebMercator(new Util.Point(initial.XMin, initial.YMax)); pRightBottom = Utility.GeographicToWebMercator(new Util.Point(initial.XMax, initial.YMin)); } else { Polygon temp = geometry as Polygon; pLeftTop = new Util.Point(temp.Extent.XMin, temp.Extent.YMax); pRightBottom = new Util.Point(temp.Extent.XMax, temp.Extent.YMin); Point GPS_pLeftTop = Utility.WebMercatorToGeographic(pLeftTop); Point GPS_pRightBottom = Utility.WebMercatorToGeographic(pRightBottom); initial = new Envelope(GPS_pLeftTop.X, GPS_pRightBottom.Y, GPS_pRightBottom.X, GPS_pLeftTop.Y); } Envelope downloadExtentMercator = new Envelope(pLeftTop.X, pRightBottom.Y, pRightBottom.X, pLeftTop.Y); _outputFile = outputPath; _downloadGeometry = geometry; _convertingStatus.IsInProgress = true; try { CreateMBTilesFileAndWriteMetaData(outputPath, name, description, attribution, initial); if (autoCorrectCoord) { CreateCCMBTilesFile(outputPath, name, description, attribution, initial); } _outputconn = new SQLiteConnection("Data source = " + base._outputFile); _outputconn.Open(); #region calculate startCol/Row and endCol/Row and tiles count of each level _convertingStatus.TotalCount = 0; string[] keyTileInfos = new string[levels.Length]; int[] tilesCountOfLevel = new int[levels.Length]; for (int i = 0; i < levels.Length; i++) { int level = TilingScheme.LODs[levels[i]].LevelID; RCRange range = BaiDuMapManager.inst.getBaiduRCRangeFromGPS(initial, level); int startTileRow = range.MinRow; int startTileCol = range.MinCol; int endTileRow = range.MaxRow; int endTileCol = range.MaxCol; keyTileInfos[i] = string.Format("{0},{1},{2},{3}", startTileRow, startTileCol, endTileRow, endTileCol); tilesCountOfLevel[i] = Math.Abs((endTileCol - startTileCol + 1) * (endTileRow - startTileRow + 1)); _convertingStatus.TotalCount += tilesCountOfLevel[i]; } _totalCount = _convertingStatus.TotalCount; _completeCount = _errorCount = 0; _wroteBytes = _wroteCounts = 0; #endregion int arcgisTileCount = 0; for (int i = 0; i < levels.Length; i++) { int level, startR, startC, endR, endC;//startTileRow,startTileCol,... level = TilingScheme.LODs[levels[i]].LevelID; startR = int.Parse(keyTileInfos[i].Split(new char[] { ',' })[0]); startC = int.Parse(keyTileInfos[i].Split(new char[] { ',' })[1]); endR = int.Parse(keyTileInfos[i].Split(new char[] { ',' })[2]); endC = int.Parse(keyTileInfos[i].Split(new char[] { ',' })[3]); _convertingStatus.Level = level; _convertingStatus.LevelTotalCount = tilesCountOfLevel[i]; _convertingStatus.LevelCompleteCount = _convertingStatus.LevelErrorCount = 0; _levelTotalCount = _convertingStatus.LevelTotalCount; _levelCompleteCount = _levelErrorCount = 0; SaveOneLevelTilesToMBTiles(level, startR, startC, endR, endC); if (autoCorrectCoord) { RCRange range = BaiDuMapManager.inst.getArcgisRCRangeFromMercator(downloadExtentMercator, levels[i]); startR = range.MinRow; startC = range.MinCol; endR = range.MaxRow; endC = range.MaxCol; recordScope(level, startR, startC, endR, endC); arcgisTileCount += (endR - startR + 1) * (endC - startC + 1); } if (_convertingStatus.IsCancelled) { _convertingStatus.IsCompletedSuccessfully = false; break; } } recordCount(arcgisTileCount); if (doCompact) { _convertingStatus.IsDoingCompact = true; _convertingStatus.SizeBeforeCompact = new FileInfo(_outputFile).Length; CompactMBTiles(_outputFile); _convertingStatus.IsDoingCompact = false; _convertingStatus.SizeAfterCompact = new FileInfo(_outputFile).Length; } if (!_convertingStatus.IsCancelled) { _convertingStatus.IsCompletedSuccessfully = true; } } finally { if (_correctOutputconn != null) { _correctOutputconn.Dispose(); } RecoverFailure(); _convertingStatus.IsInProgress = false; _convertingStatus.IsCommittingTransaction = false; _convertingStatus.IsDoingCompact = false; _outputconn.Close(); _outputconn.Dispose(); } }
protected override void CreateMBTilesFileAndWriteMetaData(string outputPath, string name, string description, string attribution, Geometry geometry) { //check the tile dimension if (!TilingScheme.TileCols.Equals(256) || !TilingScheme.TileRows.Equals(256)) { throw new Exception("The size of a tile is not 256*256!"); } //create new sqlite database SQLiteConnection.CreateFile(outputPath); using (SQLiteConnection conn = new SQLiteConnection("Data source = " + outputPath)) { conn.Open(); using (SQLiteTransaction transaction = conn.BeginTransaction()) { using (SQLiteCommand cmd = new SQLiteCommand(conn)) { #region create tables and indexes //ref http://mapbox.com/developers/mbtiles/ //metadata table cmd.CommandText = "CREATE TABLE metadata (name TEXT, value TEXT)"; cmd.ExecuteNonQuery(); //images table cmd.CommandText = "CREATE TABLE images (tile_data BLOB, tile_id TEXT)"; cmd.ExecuteNonQuery(); cmd.CommandText = "CREATE TABLE errorInfo (url TEXT, retryTimes INTEGER, recovered INTEGER, descrption TEXT)"; cmd.ExecuteNonQuery(); //map table cmd.CommandText = "CREATE TABLE map (zoom_level INTEGER, tile_column INTEGER, tile_row INTEGER, tile_id TEXT)"; cmd.ExecuteNonQuery(); //tiles view cmd.CommandText = @"CREATE VIEW tiles AS SELECT map.zoom_level AS zoom_level, map.tile_column AS tile_column, map.tile_row AS tile_row, images.tile_data AS tile_data FROM map JOIN images ON images.tile_id = map.tile_id"; cmd.ExecuteNonQuery(); //indexes cmd.CommandText = "CREATE UNIQUE INDEX images_id on images (tile_id)"; cmd.ExecuteNonQuery(); cmd.CommandText = "CREATE UNIQUE INDEX map_index on map (zoom_level, tile_column, tile_row)"; cmd.ExecuteNonQuery(); cmd.CommandText = @"CREATE UNIQUE INDEX name ON metadata (name)"; cmd.ExecuteNonQuery(); #endregion #region write metadata //name cmd.CommandText = @"INSERT INTO metadata(name,value) VALUES (""name"",""" + name + @""")"; cmd.ExecuteNonQuery(); //type cmd.CommandText = "INSERT INTO metadata(name,value) VALUES ('type','baselayer')"; cmd.ExecuteNonQuery(); //version cmd.CommandText = "INSERT INTO metadata(name,value) VALUES ('version','1.2')"; cmd.ExecuteNonQuery(); //description cmd.CommandText = @"INSERT INTO metadata(name,value) VALUES (""description"",""" + description + @""")"; cmd.ExecuteNonQuery(); //format string f = TilingScheme.CacheTileFormat.ToString().ToUpper().Contains("PNG") ? "png" : "jpg"; cmd.CommandText = "INSERT INTO metadata(name,value) VALUES ('format','" + f + "')"; cmd.ExecuteNonQuery(); //bounds Point bottomLeft = Utility.WebMercatorToGeographic(new Point(geometry.Extent.XMin, geometry.Extent.YMin)); Point upperRight = Utility.WebMercatorToGeographic(new Point(geometry.Extent.XMax, geometry.Extent.YMax)); cmd.CommandText = "INSERT INTO metadata(name,value) VALUES ('bounds','" + string.Format("{0},{1},{2},{3}", bottomLeft.X.ToString(), bottomLeft.Y.ToString(), upperRight.X.ToString(), upperRight.Y.ToString()) + "')"; cmd.ExecuteNonQuery(); //attribution cmd.CommandText = @"INSERT INTO metadata(name,value) VALUES (""attribution"",""" + attribution + @""")"; cmd.ExecuteNonQuery(); #endregion } transaction.Commit(); } } }
private LatLng DoOffset(LatLng loc) { Point p = GoogleDoOffset.transform(loc.longitude, loc.latitude); return(new LatLng(p.Y, p.X)); }