pgsql+postgis 实时返回mvt(Mapbox Vector Tiles) 矢量瓦片。

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 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 
    

(经纬度底图适用以下语句)

SELECT ST_AsMVT ( fmvt, 'mvt', 4096, 'geom' ) as "mvt" FROM (SELECT dwmc,
        ST_AsMVTGeom  ( st_simplify (dgeom,${tolerance},true),
       ST_MakeEnvelope ( ${xmin}, ${ymin}, ${xmax}, ${ymax}, 4490 ), 4096, 64, TRUE) AS geom FROM "${jbxxbm}" 
      WHERE dgeom &&   ST_MakeEnvelope ( ${xmin}, ${ymin}, ${xmax}, ${ymax}, 4490) ${conds}  ) AS fmvt

xyz转换为瓦片空间范围方法:
下面为node 代码:

// 墨卡托
xyz2lonlat_w(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 };
  }
// 经纬度
xyz2lonlat_c(x,y,z){
    const n = Math.pow(2, z + 1);
    const m = Math.pow(2, z);
 
    const lon_min = (x / n) * 360.0 - 180.0;
    const lat_min= 90 - y * (180 / m); 
    
    const lon_max = ((x +1)/ n) * 360.0 - 180.0;
    const lat_max= 90 - (y+1) * (180 / m); 
    
    return { lon_min, lat_min,lon_max,lat_max };
}
  • 6
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值