Converting GeoJSON to Vector Tiles on the fly w/ MapBox GL JS?

时间秒杀一切 提交于 2021-02-19 04:18:38

问题


My map's source file is GeoJSON, but it is very large and very slow. Is there a way to convert this to vector tiles on the fly using MapBox GL JS? (Load the GeoJSON, pre-process the file into vector tiles, and show use the vector tiles as the base map.) It seems that vector tiles are much faster.

I've tried all the GeoJSON-VT tutorials and examples that I could, like the one on MapBox's site, but it just says that GeoJSON-VT works under the hood, so it isn't much help. The others mostly apply to Leaflet, not MapBox GL JS.

Meanwhile, every example I find that uses a large dataset always does so via vector tiles:

map.addSource('x', {
        "type": "vector",
        "url": "url"
    });

For reference, I am loading my file using this method:

map.addSource('x', {
        type: 'geojson',
        data: 'file.geojson'
    });

回答1:


In case your GeoJSON file is static, you could use mapbox/tippecanoe to convert the GeoJSON to an .mbtiles file. You could then either upload the file to Mapbox as a tileset (about tilesets) or you could serve your own vector tile source from a web server using the .mbtiles file (reference implementation).

If your GeoJSON file is more dynamic, things get a little bit more complicated. I have never used it, but Mapbox Dataset API might be a good solution for that.




回答2:


To the best of my knowledge, Mapbox-GL-JS uses GeoJSON-VT to automatically convert client-side-loaded GeoJSON files into vector tiles within the browser - so it's already doing what you're asking for.

If this is still "slow", probably the problem is the actual loading and processing - so pre-generating and serving vector tiles is the right answer.



来源:https://stackoverflow.com/questions/40557887/converting-geojson-to-vector-tiles-on-the-fly-w-mapbox-gl-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!