pgsql+postgis 实时返回mvt(Mapbox Vector Tiles) 切片。
xmin,ymin,xmax,ymax 为瓦片范围坐标,下面有x,y,z转换为瓦片空间范围方法。
SELECT ST_AsMVT ( fmvt, 'pointLayer', 4096, 'geom' ) as "mvt" FROM (SELECT dwmc,ST_AsMVTGeom (ST_Transform ( st_simplify (dgeom,${tolerance},true), 3395),ST_Transform ( ST_MakeEnvelope ( ${xmin}, ${ymin}, ${xmax}, ${ymax}, 4490 ), 3395 ), 4096, 64, TRUE) AS geom FROM "${geomtable}" WHERE dgeom && ST_Transform (ST_Transform ( ST_MakeEnvelope ( ${xmin}, ${ymin}, ${xmax}, ${ymax}, 4490), 3395), 4490) ${conds} ) AS fmvt
多个图层
select (select (SELECT ST_AsMVT ( fmvt, 'pointLayer', 4096, 'geom' ) as "mvt" FROM (SELECT dwmc,ST_AsMVTGeom (ST_Transform ( st_simplify (dgeom,${tolerance},true), 3395),
ST_Transform ( ST_MakeEnvelope ( ${xmin}, ${ymin}, ${xmax}, ${ymax}, 4490 ), 3395 ), 4096, 64, TRUE) AS geom FROM "${geomtable}" WHERE dgeom && ST_Transform (ST_Transform ( ST_MakeEnvelope ( ${xmin}, ${ymin}, ${xmax}, ${ymax}, 4490), 3395), 4490) ${conds} ) AS fmvt)||(SELECT ST_AsMVT ( fmvt, 'pointLayer', 4096, 'geom' ) as "mvt" FROM (SELECT dwmc,ST_AsMVTGeom (ST_Transform ( st_simplify (dgeom,${tolerance},true), 3395),ST_Transform ( ST_MakeEnvelope ( ${xmin}, ${ymin}, ${xmax}, ${ymax}, 4490 ), 3395 ), 4096, 64, TRUE) AS geom FROM "${geomtable2}" WHERE dgeom && ST_Transform (ST_Transform ( ST_MakeEnvelope ( ${xmin}, ${ymin}, ${xmax}, ${ymax}, 4490), 3395), 4490) ${conds} ) AS fmvt)) as mvt
xyz转换为瓦片空间范围方法:
下面为node 代码:
xyz2lonlat(x, y, z) {const n = Math.pow(2, z);const lon_min = (x / n) * 360.0 - 180.0;const lat_rad = Math.atan(Math.sinh(Math.PI * (1 - (2 * y) / n)));const lat_min = (180 * lat_rad) / Math.PI;const lon_max = ((x+1) / n) * 360.0 - 180.0;const lat_rad_max = Math.atan(Math.sinh(Math.PI * (1 - (2 * (y+1)) / n)));const lat_max = (180 * lat_rad_max) / Math.PI;return { lon_min, lat_min,lon_max,lat_max };}