12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- using DevExpress.XtraMap;
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.Linq;
- using System.Text;
- using System.IO;
- using System.Threading.Tasks;
- using System.Data.SQLite;
- using System.Data.Common;
- using System.Runtime.InteropServices;
- using System.Windows.Forms;
- using Ips.Library.Basic;
- namespace Ips.Library.DxpLib
- {
- public class SqliteTileGenerator : IImageTileSource
- {
- public SqliteTileGenerator(string filename = null)
- {
- FileName = filename.IfNullOrWhitespace(DefaultDBTileFile);
- ConnectionString = string.Format("Data Source=\"{0}\";Page Size=32768;Pooling=True", FileName);
- }
- public static string DefaultDBTileFile = Path.Combine(IpsPath.RootDir,"AppData","map","map.dat");
- public string Name => nameof(SqliteTileGenerator);
- public bool CanDisposeSourceImage => true;
- public string FileName { get; set; }
- public string ConnectionString { get; private set; }
- public int MapType { get; set; } = 1082287436;//MapConsts.NormalMapNum;
- 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})";
- public Image GetImage(int x, int y, int level, Size size)
- {
- Image ret = null;
- using (var con = new SQLiteConnection())
- {
- con.ConnectionString = ConnectionString;
- con.Open();
- using (var cmd = con.CreateCommand())
- {
- cmd.CommandText = string.Format(querySql, x, y, level, MapType);
- using (DbDataReader rd = cmd.ExecuteReader(System.Data.CommandBehavior.SequentialAccess))
- {
- if (rd.Read())
- {
- long length = rd.GetBytes(0, 0, null, 0, 0);
- byte[] tile = new byte[length];
- rd.GetBytes(0, 0, tile, 0, tile.Length);
- using (MemoryStream ms = new MemoryStream(tile, 0, tile.Length, false, true))
- {
- ret = Image.FromStream(ms);
- }
- tile = null;
- }
- rd.Close();
- }
- }
- }
- return ret;
- }
- }
- }
|