I have a TopoJSON file which describes electoral districts in Montreal, and I would like to produce an adjacency matrix which would tell me which districts share a boundary with which other districts. Is there an easy way to do this?

Edit: here is the file https://github.com/nicolaskruchten/mtlelection2013bydistrict/blob/master/districts.topojson

1 Answer 1


You can get raw adjacency information as a side-effect of the topojson.mesh and topojson.meshArcs functions (assuming you like JavaScript). The optional third parameter to those is a function that will be called (perhaps repeatedly) for adjacent geometries. Here's some nodejs pseudo-code:

var topojson = require('topojson');
var fs = require('fs');
var inputTopology = JSON.parse(fs.readFileSync( ... ));
var objectOfInterest = inputTopology.objects.alldistricts;

topojson.meshArcs(inputTopology, objectOfInterest, function(distA, distB) {
    console.log(distA.properties.Name + " and " +
        distB.properties.Name + " are adjacent!");

It doesn't produce a real matrix, but that could be done with a little bit of extra code inside that function.

And note that the function will be called for each arc in the topology, so you'll get calls where distA and distB are the same object (when an arc is "exterior"), and you'll potentially get multiple calls for the same distA and distB if one of their shared edges is made up of multiple arcs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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