Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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'
    });
like image 209
Andrew Avatar asked Oct 16 '25 02:10

Andrew


1 Answers

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.

like image 88
forrert Avatar answered Oct 17 '25 15:10

forrert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!