2017-08-30 78 views
0

我想在我的D3 geojson地圖上最小化格陵蘭和南極。我該怎麼做呢。我已經嘗試過縮放和翻譯方法,但他們只是在頁面上移動地圖而不提供最小化的y座標。如何最小化使用D3的墨卡託投影

image of map

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="utf-8"> 
    <script src="d3.v3.js"></script> 
    <script src="topojson.min.js"></script> 
    <style> 
    </style> 
    <script type="text/javascript"> 
     function draw(geo_data) { 
     "use strict"; 
     var margin = 75, 
      width = 1920 - margin, 
      height = 1080 - margin; 

     var svg = d3.select("body") 
      .append("svg") 
      .attr("width", width + margin) 
      .attr("height", height + margin) 
      .append('g') 
      .attr('class', 'map'); 

     var projection = d3.geo.mercator(); 

     var path = d3.geo.path().projection(projection); 


     var map = svg.selectAll('path') 
        .data(geo_data.features) 
        .enter() 
        .append('path') 
        .attr('d', path) 
        .style('fill', 'rgb(9, 157, 217)') 
        .style('stroke', 'black') 
        .style('stroke-width', 0.5); 

     }; 
     </script> 
    </head> 
<body> 
    <script type="text/javascript"> 

    /* 
     Use D3 to load the GeoJSON file 
    */ 
    //d3.json("world-topo-min.json", draw); 
    d3.json("world_countries.json", draw); 

    </script> 
</body> 
</html> 
+0

看一看文章[*互動地圖與d3.js *](HTTP:// www.tnoda.com/blog/2013-12-07)。在第* 3節。轉換文件*有一個關於排除南極洲的段落。這同樣適用於格陵蘭島,儘管我認爲如文中所示,最好只剪輯這個國家。 – altocumulus

+0

謝謝高積雲響應,但第3部分是在使用ogr2​​ogr實用程序將geojson轉換爲topojson的過程中排除Antartica。有沒有辦法將它從我已有的地圖中排除? – heisenberg

+0

你的'world_countries.json'文件是什麼樣的?你可以設置一個工作演示嗎? – altocumulus

回答

2

如果你想刪除的飛行對格陵蘭島和南極地區,你可以簡單地篩選以GeoJSON的FeatureCollection,即geo_data.features。在這個陣列中,您可以找到格陵蘭(「id」:「GRL」)以及南極洲(「id」:「ATA」)的功能。因此,你可以使用數組的.filter()方法來擺脫這兩個特徵留下其餘不變:

var map = svg.selectAll('path') 
    .data(geo_data.features.filter(d => d.id !== "GRL" && d.id !== "ATA))