Domanda

I wrote a simple function to parse a the results of d3.geom.voronoi into topoJSON format, which you can see here:

http://bl.ocks.org/emeeks/9908143

As far as I can tell, it matches up with the topoJSON generated in http://bl.ocks.org/mbostock/5249328 except that it doesn't use translate or scale (though I've generated it with a translate of (0,0) and a scale of (1) and received the same error). When I try to parse the features with topojson, I receive this error:

TypeError: Cannot read property 'length' of undefined
at arc (topojson.js:172:54)
at line (topojson.js:187:52)
at ring (topojson.js:193:20)
at Array.map (native)
at polygon (topojson.js:199:19)
at Object.geometryType.Polygon (topojson.js:214:37)
at geometry (topojson.js:205:71)
at object (topojson.js:218:12)
at feature (topojson.js:160:17)
at topojson.js:151:55

The Polygon example I based this on has an array of arrays for arcs, whereas the Linestring example I've compared it to has an array of arcs, so my assumption is that the Polygon by default is a multipolygon and I've followed the array of arrays for this example, but if I just make an array of arcs, it gives me the error "cannot call slice of undefined".

I'm pretty sure my vorToPoly function is mapping arcs correctly, but if someone could take a look at my object topoCollection (I've echoed it to the console) and tell me how it's not matching up with what topojson expects, I'd appreciate it.

È stato utile?

Soluzione

Mike Bostock pointed out that the error occurred because my topojson collection was referring to an arc that didn't exist because it started counting the arcs at 1 instead of 0. The problem occurred in this part of my vorToPoly code:

 topoArcs.push([[Math.floor(vorPolys[x][y][0]),Math.floor(vorPolys[x][y][1])],[Math.floor(vorPolys[x][nextVal][0]),Math.floor(vorPolys[x][nextVal][1])]]);
 arcHash[hashVal] = topoArcs.length;

It should have been flipped:

 arcHash[hashVal] = topoArcs.length;
 topoArcs.push([[Math.floor(vorPolys[x][y][0]),Math.floor(vorPolys[x][y][1])],[Math.floor(vorPolys[x][nextVal][0]),Math.floor(vorPolys[x][nextVal][1])]]);

That way the hash is starting at 0 instead of 1. Flipping this makes everything work just fine.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top