Topojson.neighbors() with preprojected data

100 views
Skip to first unread message

Chris Wilson

unread,
Oct 29, 2014, 1:53:19 PM10/29/14
to d3...@googlegroups.com
I've been playing around with an app to redraw state borders by moving counties between states. This requires using topojson.neighbors() since only adjacent counties should be eligible for inclusion in a state.

I'm connecting neighbors to each county for convenient lookup after the page loads:

var counties = topojson.feature(topology, topology.objects.counties).features;
var neighbors = topojson.neighbors(topology.objects.counties.geometries);

counties.forEach(function(county, i) {
    county.neighbors = neighbors[i].map(function(n) { // convert neighbors from indices to objects
    return counties[n];
    });
});

I normally use a pre-projected topojson file for something like this, but I've noticed that the behavior from neighbors() is not precisely the same. The attached screenshots show the neighboring counties for Crawford, IA. In the NON-preprojected case, using the exact us.json file from Mike's demos, Woodbury county (to the northwest) is not a neighbor. This the correct case -- the two counties are kitty corner. 

When I use a preprojected file, they are considered neighbors.

Is this the inevitable result of information loss? I've made the pre-projected file a few months ago using Mike's instructions straight out of the box. 

Thanks!
not_preprojected.png
preprojected.png

Jason Davies

unread,
Oct 29, 2014, 2:35:50 PM10/29/14
to d3...@googlegroups.com
Hi Chris,

This sounds like a bug. Would you mind posting the two JSON files
somewhere to make it easier to diagnose?

--
Jason Davies, http://www.jasondavies.com/

Chris Wilson

unread,
Oct 30, 2014, 7:55:34 AM10/30/14
to d3...@googlegroups.com, ja...@jasondavies.com
Certainly!

Here's the preprojected file, where I see the weird neighbors: https://dl.dropboxusercontent.com/u/2902359/borders/counties.json

And the comparison file is just from the public examples: http://bl.ocks.org/mbostock/raw/4090846/us.json

And here are the two demos I used for my screenshots. The red county (Crawford) is the problematic one (among others, I imagine). 

Jason Davies

unread,
Oct 30, 2014, 8:07:21 AM10/30/14
to Chris Wilson, d3...@googlegroups.com
And the command-line options used to pre-project?

Thanks,

Chris Wilson

unread,
Oct 30, 2014, 8:23:43 AM10/30/14
to Jason Davies, d3...@googlegroups.com
Forgive me -- I see where the error was introduced. The preprojection works fine. It's when I do the clipping to the land boundaries that it gets messed up. In a moment of weakness--we should all be honest about our dark histories--I used QGIS. 

What's the appropriate Makeable solution for this sort of clipping? 

Thank you! 

Jason Davies

unread,
Oct 30, 2014, 8:30:57 AM10/30/14
to Chris Wilson, d3...@googlegroups.com
On Thu, Oct 30, 2014 at 08:23:19AM -0400, Chris Wilson wrote:
> Forgive me -- I see where the error was introduced. The preprojection works
> fine. It's when I do the clipping to the land boundaries that it gets
> messed up. In a moment of weakness--we should all be honest about our dark
> histories--I used QGIS.

I forgive you.

> What's the appropriate Makeable <http://bost.ocks.org/mike/make/> solution
> for this sort of clipping?

Can you explain in more detail? What are you clipping against the land
boundaries?

In general, we do not currently have the ability to programmatically
clip non-convex (or spherical) geometry in D3, so instead you can use
something like SVG clipPath when rendering.

However, arbitrary clipping is something that I’m working on at the
moment (spherical and Cartesian).
Reply all
Reply to author
Forward
0 new messages