1

I'm new to Mapbox tile-reduce library. I would like to process OSM tiles for Portugal.

I've download the mbtiles from Portugal. I've installed tile-reduce with:

npm ini
npm install @mapbox/tile-reduce

I've downloaded the tile-reduce/examples/count/ example. My index.js is:

'use strict';
var tileReduce = require('@mapbox/tile-reduce');
var path = require('path');
var numFeatures = 0;
tileReduce({
    zoom: 12,
    map: path.join(__dirname, '/count.js'),
    sources: [{name: 'portugal', mbtiles: path.join(__dirname, 'portugal.mbtiles'), raw: true}]
})
.on('reduce', function (num) {
    numFeatures += num;
})
.on('end', function () {
    console.log('Features total: %d', numFeatures);
});

My count.js is:

'use strict';

module.exports = function(data, tile, writeData, done) {
  var count = 0;
  if (data.portugal.roads) count += data.portugal.roads.length;
  if (data.portugal.buildings) count += data.portugal.buildings.length;
  done(null, count);
};

I was expecting to have the number of roads and buildings on the output, but the result is 0 features.

jgr@dusseldorf:/media/bonn/mapbox$ node index.js
Starting up 8 workers... Job started.
Processing tile coords from "portugal" source.
1876 tiles processed in 1s.
Features total: 0

What am I doing wrong?

1

The tile-reduce example does not work with OSM QA TILES COUNTRY EXTRACTS. The tile content is different.

To count roads and buildings using tiles from country extracts, we need to iterate over all features in the tile.

The following count.js works:

'use strict';

module.exports = function (data, tile, writeData, done) {
    var count = 0;
    for (var i = 0; i < data.portugal.osm.length; i++) {
        var feature = data.portugal.osm.feature(i);
        if (feature.properties.highway || feature.properties.building) {
            count += 1;
        }
    }
    done(null, count);
};

On the output we can see the number of roads and buildings:

jgr@dusseldorf:/media/bonn/mapbox$ node index.js
Starting up 8 workers... Job started.
Processing tile coords from "portugal" source.
1876 tiles processed in 2s.
Features total: 1389373

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.