PostgreSQL ST_AsMVT到VectorTiles到小叶层 [英] PostgreSQL ST_AsMVT to VectorTiles to Leaflet Layer

查看:146
本文介绍了PostgreSQL ST_AsMVT到VectorTiles到小叶层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从PostgreSQL数据库创建矢量切片,并通过flask将其提供给Leaflet映射.我关注了这篇 medium.com文章几乎一路走来.

I'm trying to create vector tiles from a PostgreSQL database and serve them via flask to a Leaflet map. I've followed this medium.com article which got me nearly all the way.

但是,当我打开带有Leaflet映射的页面时,在浏览器控制台中会显示以下内容:

However, when i open the page with the Leaflet map on it I get the following in the browser console:

index.js:191未捕获的错误:未实现的类型:4在Pbf.skip(index.js:191)在Pbf.readFields(index.js:41)在新的VectorTile $ 1(vectortile.js:8)在FileReader.(Leaflet.VectorGrid.Protobuf.js:124)

index.js:191 Uncaught Error: Unimplemented type: 4 at Pbf.skip (index.js:191) at Pbf.readFields (index.js:41) at new VectorTile$1 (vectortile.js:8) at FileReader. (Leaflet.VectorGrid.Protobuf.js:124)

要创建图块,请使用以下内容:

to create the tiles I use the below:

  def tile_ul(x, y, z):
    n = 2.0 ** z
    lon_deg = x / n * 360.0 - 180.0
    lat_rad = math.atan(math.sinh(math.pi * (1 - 2 * y / n)))
    lat_deg = math.degrees(lat_rad)
    return  lon_deg,lat_deg

    def get_tile(z,x,y):
        xmin,ymin = tile_ul(x, y, z)
        xmax,ymax = tile_ul(x + 1, y + 1, z)
        tile = None
        query = """SELECT ST_AsMVT(tile) FROM (SELECT id, ST_AsMVTGeom(geom, ST_Makebox2d(ST_transform(ST_SetSrid(ST_MakePoint(%s,%s),4326),3857),ST_transform(ST_SetSrid(ST_MakePoint(%s,%s),4326),3857)), 4096, 0, false) AS geom FROM "TimeZone (LineGridExp)") AS tile"""
cursor = db.connection.cursor()
cursor.execute(query,(xmin,ymin,xmax,ymax))
tile = str(cursor.fetchone()[0])
cursor.close()
return tile

@app.route('/tiles')
@app.route('/tiles/<int:z>/<int:x>/<int:y>', methods=['GET'])
def tiles(z=0, x=0, y=0):
    tile = get_tile(z, x, y)
    response = make_response(tile)
    response.headers['Content-Type'] = "application/octet-stream"
    return response

我将瓷砖添加到传单中,

To add tiles to leaflet i use:

var url = "http://localhost:5000/tiles/{z}/{x}/{y}"
var mapillaryLayer = L.vectorGrid.protobuf(url).addTo(mymap);

python端从客户端接收GET,并且不会引发任何错误.但是,我不确定SQL查询和检测到空白图块还是查询是否完全错误.

The python end receives GET from the client and doesn't throw any errors. However I'm not sure about the SQL query and detecting empty tiles or whether the query is simply wrong.

任何帮助将不胜感激.

汤姆

推荐答案

这完全是查询问题,不返回数据,也不返回字节,而不返回str:

It was all a matter of the query not returning data and also returning as Bytes and not str:

 def get_tile(z,x,y):
    xmin, ymin = tile_ul(x, y, z)
    xmax, ymax = tile_ul(x + 1, y + 1, z)
    query = """SELECT ST_AsMVT(tile) FROM (
               SELECT id, ST_AsMVTGeom(geom, ST_MakeEnvelope( %s, %s, %s, %s ,4326), 4096, 256, false ) geom 
               FROM reproject ) as tile"""
    cursor = db.connection.cursor()
    cursor.execute(query,(xmin,ymin,xmax,ymax))
    tile = bytes(cursor.fetchone()[0])
    cursor.close()
    return tile

这篇关于PostgreSQL ST_AsMVT到VectorTiles到小叶层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆