利用MBTiles技术原理减轻离线地图的存储量

来源:互联网 发布:布袋除尘器设计软件 编辑:程序博客网 时间:2024/06/08 03:25

问题来源

已切好的地图瓦片存放在文件系统中,会产生大量的文件碎片,占用空间会比实际容量大很多。

MBTiles原理

利用数据表里的byte来存储图片,使用时通过查询数据表就可以读取图片,优点是大大减少存储量,缺点是大量的查询会拖慢服务器,较适合离线地图使用。

java代码

文件的内容输出成字节流
/** * 获得指定文件的byte数组  * @param file 文件 * @return */    private byte[] getBytes(File file){          byte[] buffer = null;          try {            FileInputStream fis = new FileInputStream(file);              ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);              byte[] b = new byte[1024];              int n;              while ((n = fis.read(b)) != -1) {                  bos.write(b, 0, n);              }              fis.close();              bos.close();              buffer = bos.toByteArray();          } catch (FileNotFoundException e) {              e.printStackTrace();          } catch (IOException e) {              e.printStackTrace();          }          return buffer;      }  
遍历文件夹的输出存储
/** * 通过文件夹导入记录 * @param file 源文件 * @param parentPath 父路径 */public void picToRow(File file, String parentPath){if(file.exists()) {          if(file.isDirectory()) { //属于文件夹              //遇到文件夹时递归到下一级              File[] fs = file.listFiles();              for(File f : fs) {              picToRow(f, concat(parentPath, file.getName()));              }          } else { //已是文件              String[] path = file.getParent().split(Matcher.quoteReplacement(File.separator)); //图片的格式为z/y/x.png的处理方式              int level = Integer.parseInt(path[path.length-2]);              int row = Integer.parseInt(path[path.length-1]);              String name = file.getName().split("\\.")[0];              int col = Integer.parseInt(name);              byte[] data = getBytes(file);          /*自行补充insert 语句来插入记录*/          }     }}
反向通过byte在前台生成图片
response.setContentType("image/png"); //application/octet-streamresponse.addHeader("Content-disposition", "attachment; filename=" + name);response.getOutputStream().write(data); //data即为byte流response.getOutputStream().flush();






0 0