PostgreSQL ST_AsMVT 到 VectorTiles 到 Leaflet 层

2024-06-21

我正在尝试从 PostgreSQL 数据库创建矢量切片,并通过 Flask 将它们提供给 Leaflet 地图。我已经关注了这个medium.com 文章 https://medium.com/tantotanto/vector-tiles-postgis-and-openlayers-258a3b0ce4b6这几乎让我一路走来。

但是,当我打开带有传单地图的页面时,我在浏览器控制台中看到以下内容:

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

要创建图块,我使用以下命令:

  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

要将图块添加到传单,我使用:

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

python端接收来自客户端的GET并且不会抛出任何错误。 但是我不确定 SQL 查询和检测空图块或查询是否完全错误。

任何帮助将不胜感激。

Tom


这完全是查询不返回数据并且返回字节而不是字符串的问题:

 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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

PostgreSQL ST_AsMVT 到 VectorTiles 到 Leaflet 层 的相关文章

随机推荐