2014-09-23 75 views
0

d3.nest()如何与geojson文件一起使用?使用d3.nest()和geojson文件

我GeoJSON的数据格式如下:

"features": [ 
{ "type": "Feature", "properties": { "neighborhood": "Allerton", "boroughCode": "2", "borough": "Bronx", "@id": "http:\/\/nyc.pediacities.com\/Resource\/Neighborhood\/Allerton" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -73.848597000000183, 40.871670000000115 ], [ -73.845822536836778, 40.870239076236174 ], [ -73.854559184633743, 40.859953835764252 ], [ -73.854665433068263, 40.859585694988056 ], [ -73.856388703358959, 40.857593635304482 ], [ -73.868881809153407, 40.857223150158326 ], [ -73.868317552728243, 40.857862062258313 ], [ -73.869553714672321, 40.857784095600181 ], [ -73.871024857620654, 40.857309948816905 ], [ -73.870480549987164, 40.865413584098484 ], [ -73.87055489856489, 40.869702798589863 ], [ -73.86721594442561, 40.869689663636713 ], [ -73.85745, 40.869533000000182 ], [ -73.855550000000108, 40.871813000000145 ], [ -73.853597967576576, 40.873288368674203 ], [ -73.848597000000183, 40.871670000000115 ] ] ] } } 

但我的窝命令:

var nested_data = d3.nest() 
    .key(function(d, i) { console.log(d); return d.features.properties.neighborhood; }) 
    .entries(map); 

返回一个空数组。

我想嵌套我的数据更容易过滤它。这是建议吗?

回答

2

假设你以GeoJSON看起来像下面

var map = { 
    type: "FeatureCollection", 
    "features": [ 
    { 
    "type": "Feature", 
    "properties": { 
     "neighborhood": "Allerton", 
     "boroughCode": "2", 
     "borough": "Bronx", 
     "@id": "http:\/\/nyc.pediacities.com\/Resource\/Neighborhood\/Allerton" 
    }, 
    "geometry": { /* various coordinates, etc */ } 
    ] 
} 

所以,你想要做的是:

d3.nest() 
    .key(function(d, i) { 
    return d.properties.neighborhood; 
    }) 
    .entries(map.features); 

你想传递map.features因为这是你的阵列。