【SAS Planet 下载地图瓦片-读取】

   SAS Planet下载地图瓦片请看上一篇 详细介绍了下载方法

 【SAS Planet 下载地图瓦片】-CSDN博客

准备工作:

1.提前下载好地图瓦片数据

 SAS Planet下载地图瓦片默认存储路径如下

【SAS Planet 下载地图瓦片-读取】_第1张图片

   默认存储格式为 .sqlitedb

【SAS Planet 下载地图瓦片-读取】_第2张图片

2.提前准备好 java开发环境和开发工具,新建 一个 spring boot 工程,集成 maven。

 在pom.xml 下新增sqlite3驱动包配置,然后更新工程 maven



    org.xerial
    sqlite-jdbc
    3.32.3.2

  读取SAS Planet下载的地图瓦片后台代码如下:

package com.api.controller;

import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Api(value="读取sqlite文件瓦片",tags={"读取sqlite文件瓦片"})
@CrossOrigin  // 允许跨域访问
@RestController
public class sqliteTilesController {

    @GetMapping(value = "/getCacheTiles/{layerName}/{level}/{x}/{y}")
    public ResponseEntity getCacheTiles(@PathVariable("layerName")String layerName, @PathVariable("level")Integer level, @PathVariable("x")Integer x, @PathVariable("y")Integer y) {
        ResponseEntity response=null;

        Statement stmt = null;
        Connection conn = null;
        try {
            //String path="E:\\WJ_Data\\SAS.Planet.Release.200606\\cache_sqlite\\Google_Sat_RU_SD\\"+layerName+"\\z"+level+"\\0\\0\\0.0.sqlitedb";

            String basePath="E:\\WJ_Data\\SAS.Planet.Release.200606\\cache_sqlite\\";


            //地图下载(SAS.Planet) 下载的瓦片存储在sqlitedb文件里 路径规则 规则计算
            // 将十进制数转换为二进制字符串再右移后还原成十进制数
            Integer shrX1= shrnNumberValue(x,10);//
            Integer shrY1= shrnNumberValue(y,10);//

            Integer shrX2= shrnNumberValue(x,8);//
            Integer shrY2= shrnNumberValue(y,8);//
            String  fullPath=basePath+layerName+"\\z"+level+"\\"+shrX1+"\\"+shrY1+"\\"+shrX2+"."+shrY2+".sqlitedb";
            //jdbc url
            String urlStr="jdbc:sqlite:"+fullPath;

            conn = DriverManager.getConnection(urlStr);
            conn.setAutoCommit(false);

            System.out.println("Opened database successfully");
            stmt = conn.createStatement();
            //ResultSet rs = stmt.executeQuery( "SELECT * FROM 't'" );
            String sqlStr="SELECT * FROM  't' where x="+x+" and y="+y;//"SELECT * FROM 't'
            ResultSet rs = stmt.executeQuery(sqlStr);
            while ( rs.next() ) {
                int tilesX = rs.getInt("x");
                int tilesY = rs.getInt("y");
                int v= rs.getInt("v");
                String c= rs.getString("c");
                long h= rs.getLong("h");
                long d= rs.getLong("d");

                String Str="瓦片信息  level:"+level+", x:"+x+", y="+y;
                System.out.println( Str );

                //获取图片,图片列的索引为8
                byte[] bytes = (byte[] )rs.getObject(8);
                response= ResponseEntity.ok().contentType(MediaType.parseMediaType("image/jpg")).body(bytes);

            }
            rs.close();
            stmt.close();
            conn.close();
        } catch ( Exception e ) {
            System.err.println( e.getClass().getName() + ": " + e.getMessage() );
        }
        System.out.println("Operation done successfully");
        return response;
    }

    //将十进制数转换为二进制字符串再右移后还原成十进制数
    public int shrnNumberValue(int value,int shr) {
        // value 十进制数
        // shr  将一个数在二进制上右位移位数
        String binary = Integer.toString(value, 2); // 十进制转换为二进制字符串
        int length= binary.length();
        if(length>shr){
            String  newBinary=binary.substring(0,binary.length()-shr);
            int decimal = Integer.parseInt(newBinary, 2); // 二进制字符串解析为十进制数
            return  decimal;
        }else{
            return  0;
        }
    }

}

 获取瓦片接口:"http://localhost:2022/getCacheTiles/Google_Sat_RU_SD/{z}/{x}/{y}"

  前端页面调用代码如下


 
   
   
    Custom LERC Layer | Sample | ArcGIS Maps SDK for JavaScript 4.28
   

   

   

   
   
 

 
   


 

效果如下

   

你可能感兴趣的:(java,spring,boot,SAS,Planet)