postgres12.0+postgis3.0以上版本
1.传入参数
layerName表名
zoom 级别
row 行号
col 列号
2.sql源码
SELECT
ST_AsMVT (tile,#{layerName},4096,'geom') tile
FROM
(
SELECT
st_asmvtgeom (geom,
ST_TileEnvelope(${zoom}, ${row}, ${col},st_makeenvelope (-180,-270,180,90, 4326)),
4096,
0,
TRUE
) AS geom
<if test="cols != null and cols != ''">
,${cols}
</if>
FROM
${layerName}
) AS tile
WHERE
tile.geom IS NOT NULL;
3.调用
List<Map> mpas = tileMapper.vectorTitle(tileParam);
byte[] dd = (byte[]) mpas.get(0).get("tile");
return dd;
postgres12.0+postgis3.0以下版本
1.传入参数
layerName表名
zoom 级别
row 行号
col 列号
2.数据转换
/**
* 瓦片获得范围
**/
public TileBox tile2boundingBox(final int row, final int col, final int zoom, String layerName) {
TileBox bb = new TileBox();
double[] xyArr= getExtent(zoom,row,col);
bb.setYmax(xyArr[3]);
bb.setYmin(xyArr[1]);
bb.setXmin(xyArr[0]);
bb.setXmax(xyArr[2]);
bb.setLayerName(layerName);
return bb;
}
public static double[] getExtent(int z,int x,int y){
double n = Math.pow(2, z);
double lon_min = (x / n) * 360.0 - 180.0;
double lat_min = 90.0 - (((y + 1) / n) * 360.0);
double lon_max = ((x + 1) / n) * 360.0 - 180.0;
double lat_max = 90.0 - ((y / n) * 360.0);
double[] xyArr={lon_min,lat_min,lon_max,lat_max};
return xyArr;
}
3.sql源码
SELECT
ST_AsMVT (tile,#{layerName},4096,'geom') tile
FROM
(
SELECT
st_asmvtgeom (t.geom,
st_makeenvelope (${Xmin}, ${Ymin}, ${Xmax},${Ymax}, 4326),
4096,
0,
TRUE
) AS geom
FROM
${layerName} t
) AS tile
WHERE
tile.geom IS NOT NULL;
4.调用
TileBox tileBox = tile2boundingBox(row, col, zoom, layerName);
List<Map> tiles = tileMapper.getSimpleTile(tileBox);
byte[] dd = (byte[]) tiles.get(0).get("tile");
return dd;