Search code examples
mapnikmapbox-gl-js

mapnik vector tile vs raw geojson performance


I am experiencing > 2s processing time for converting a 120 MB geojson file to protobuf through Mapnik Vector Tile node bindings.

On the other end serving the raw geosjon file takes under 200 ms.

Is it normal ? If yes, what is the point of serving vector tiles over geojson (I am viewing it with mapbox-gl-js)?

Here is an extract of my code :

// Load GeoJson into memory

var fs = require("fs");
var content = JSON.parse(fs.readFileSync("us_counties.json"));


// Initialise Mapnik and mercator object

mapnik.register_default_fonts();
mapnik.register_default_input_plugins();

var mercator = new SphericalMercator({
  size: 256
 });

// Vector Tile Router

router.get('/:z/:x/:y.pbf', function(req, res) {

  var bbox = mercator.bbox(
    +req.params.x,
    +req.params.y,
    +req.params.z,
    false,
    '4326'
  );

  // Convert GEOJSON to protobuf VectorTile and Serve

  var vtile = new mapnik.VectorTile(+req.params.z, +req.params.x, +req.params.y)
  vtile.addGeoJSON(JSON.stringify(content), 'fixture_layer')

  res.setHeader('Content-Encoding', 'deflate')
  res.setHeader('Content-Type', 'application/x-protobuf')


  zlib.deflate(vtile.getData(), function(err, pbf) {
    res.send(pbf);
  })
});

Solution

  • GeoJSON and vector tiles each have strengths and weaknesses as data formats for Mapbox GL.

    • vector tiles require more preprocessing than GeoJSON
    • GeoJSON must be downloaded in its entirety whereas vector tiles can be downloaded in tiles as needed to fill the viewport (it would be near impossible to download all of Open Street Map as a single X00 GB GeoJSON!)
    • GeoJSON may be modified clientside, whereas vector tiles cannot
    • GeoJSON is human-readable whereas vector tiles are not

    If GeoJSON is working for you, I encourage you to use it! Just be aware of cases where your users may be unable to quickly download 120 MB.