SqliteTileGenerator.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using DevExpress.XtraMap;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.Drawing.Imaging;
  6. using System.Linq;
  7. using System.Text;
  8. using System.IO;
  9. using System.Threading.Tasks;
  10. using System.Data.SQLite;
  11. using System.Data.Common;
  12. using System.Runtime.InteropServices;
  13. using System.Windows.Forms;
  14. using Ips.Library.Basic;
  15. namespace Ips.Library.DxpLib
  16. {
  17. public class SqliteTileGenerator : IImageTileSource
  18. {
  19. public SqliteTileGenerator(string filename = null)
  20. {
  21. FileName = filename.IfNullOrWhitespace(DefaultDBTileFile);
  22. ConnectionString = string.Format("Data Source=\"{0}\";Page Size=32768;Pooling=True", FileName);
  23. }
  24. public static string DefaultDBTileFile = Path.Combine(IpsPath.RootDir,"AppData","map","map.dat");
  25. public string Name => nameof(SqliteTileGenerator);
  26. public bool CanDisposeSourceImage => true;
  27. public string FileName { get; set; }
  28. public string ConnectionString { get; private set; }
  29. public int MapType { get; set; } = 1082287436;//MapConsts.NormalMapNum;
  30. public const string querySql = "SELECT Tile FROM TilesData WHERE id = (SELECT id FROM Tiles WHERE X={0} AND Y={1} AND Zoom={2} AND Type={3})";
  31. public Image GetImage(int x, int y, int level, Size size)
  32. {
  33. Image ret = null;
  34. using (var con = new SQLiteConnection())
  35. {
  36. con.ConnectionString = ConnectionString;
  37. con.Open();
  38. using (var cmd = con.CreateCommand())
  39. {
  40. cmd.CommandText = string.Format(querySql, x, y, level, MapType);
  41. using (DbDataReader rd = cmd.ExecuteReader(System.Data.CommandBehavior.SequentialAccess))
  42. {
  43. if (rd.Read())
  44. {
  45. long length = rd.GetBytes(0, 0, null, 0, 0);
  46. byte[] tile = new byte[length];
  47. rd.GetBytes(0, 0, tile, 0, tile.Length);
  48. using (MemoryStream ms = new MemoryStream(tile, 0, tile.Length, false, true))
  49. {
  50. ret = Image.FromStream(ms);
  51. }
  52. tile = null;
  53. }
  54. rd.Close();
  55. }
  56. }
  57. }
  58. return ret;
  59. }
  60. }
  61. }