D3js无法将经度/纬度点正确投影到topojson地图上

约书亚·克莱门斯(Joshua Clemens)

我正在使用topojson与D3js绘制世界地图。我相信这很好。但是,当我尝试将纬度/经度点投影到地图上时,投影函数将为负值的点返回“ NaN”,而正在工作的点的投影会偏离该国家在世界地图上的位置。

这是我正在读取的数据:assets / data / gcf.csv

Coords,Country,Frequency
"[38.9597594, 34.9249653]",Turkey,1
"[61.0666922, -107.9917071]",Canada,3
"[52.5001698, 5.7480821]",Netherlands,1
"[42.6384261, 12.674297]",Italy,1
"[39.7837304, -100.4458825]",United States,69
"[59.6749712, 14.5208584]",Sweden,1
"[35.000074, 104.999927]",China,5
"[64.6863136, 97.7453061]",Russian Federation,2
"[36.5748441, 139.2394179]",Japan,1

Index.html

<div id="worldmap"></div>
<script src="../assets/js/graphs/map.js"></script>

资产/js/graphs/map.js

var width = 960;
var height = 500;

// set projection
var projection = d3.geo.mercator();

// create path variable
var path = d3.geo.path()
.projection(projection); 

// Define the div for the tooltip
var div = d3.select("body").append("div")   
.attr("class", "tooltip")               
.style("opacity", 0);

d3.json("https://unpkg.com/world-atlas@1/world/110m.json", function(error1, topo) {
if(error1) console.log("Error: topo json not loaded.");
d3.csv("../assets/data/gcf.csv", function(error2, data) {
if(error2) console.log("Error: Coord/frequency data not loaded.");

countries = topojson.feature(topo, topo.objects.countries).features;

projection
.scale(width / 2 / Math.PI)
.translate([(width / 2)-30, (height / 2)+50]);

data.forEach(function(d) {      
// + symbol convert from string representation of a number to an actual number
d.Frequency = +d.Frequency;
d.Coords = d.Coords.replace(/[\[\]"]+/g, '');
d.Coords = d.Coords.split(',');
d.Coords = d.Coords.map(x => parseFloat(x));
d.Country = d.Country;
console.log("Country:", d.Country, ". Projection: ", projection(d.Coords)[0], projection(d.Coords)[1]);
});

// create svg variable
var svg = d3.select("#worldmap").append("svg")
.attr("width", width)
.attr("height", height);

svg.append("rect")
.attr("width", "100%")
.attr("height","100%")
.attr("fill","#A2E8E8");

// add countries from topojson
svg.selectAll("path")
.data(countries).enter()
.append("path")
.attr("class", "feature")
.style("fill", "#71945A")
.attr("d", path);

// put boarder around countries 
svg.append("path")
.datum(topojson.mesh(topo, topo.objects.countries, function(a, b) { return a !== b; }))
.attr("class", "mesh")
.attr("d", path);
aa = [38.9597594, 34.9249653];


// add circles to svg
svg.selectAll("circle")
.data(data).enter()
.append("circle")
.attr("r", function(d) {
return d.Frequency === 1 ? d.Frequency* 10 : d.Frequency / 2
})
.attr("transform", function(d) {
return "translate(" + projection([
  (d.Coords[0]),
  (d.Coords[1])
]) + ")";
})
.attr("fill", "red")
.on("mouseover", function(d) {      
div.transition()        
.duration(200)      
.style("opacity", .9);      
div .html(d.Country)    
.style("left", (d3.event.pageX) + "px")     
.style("top", (d3.event.pageY - 28) + "px");    
})                  
.on("mouseout", function(d) {       
div.transition()        
.duration(500)      
.style("opacity", 0);   
});         
});
});

世界地图样式:

.feature {
fill: none;
stroke: grey;
stroke-width: 1px;
stroke-linejoin: round;
}

.mesh {
fill: none;
stroke: black;
stroke-width: 2px;
stroke-linejoin: round;
}
安德鲁·里德(Andrew Reid)

您的纬度和经度相反-d3 geoProjection以[x,y]或[long,lat]格式获取坐标。您正在为投影提供[lat,long]数组,但是如果您切换以下代码:

.attr("transform", function(d) { 
    return "translate(" + projection([d.Coords[0],d.Coords[1]]) + ")";
})

至:

.attr("transform", function(d) { 
    return "translate(" + projection([d.Coords[1],d.Coords[0]]) + ")";
})

您会得到想要的地图:

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

topojson / D3 /经度+纬度的地图

使用D3将纬度/经度放置在azimuthalEqualArea地图上

在地图上绘制纬度/经度 - D3.js v4

如何根据纬度/经度在D3地图上的两个点之间绘制线/链接?

澳大利亚的D3JS TopoJSON地图:投影准确但未呈现任何国家

将TOAST地图上的点转换为球面上的纬度/经度

D3.js-地图上的绘制点由于投影错误而失败

以浮点精度而不是整数将纬度/经度投影到像素

d3js强制在地图上进行布局

传单地图上的d3js强制布局

将跨越多个UTM区域的一组纬度/经度点投影到单个网格中

d3js遮罩以在条形图上显示点

在地图上绘制的经/纬度点放置不正确

QGIS和PostGIS(地图点(美国地图上的经纬度与经度)

在地图上绘制纬度经度坐标

在Mollweide投影中获取我的世界地图中某点的经度纬度

d3js v5 + Topojson v3地图未呈现

如何成功地将地图放入具有正确地理比例的经度/纬度边界框并绘制 GPS 点

在叶片地图上从数据框绘制纬度经度点-iPython

应用缩放后,确定地图上以像素为单位的点的经度/纬度坐标

如何使用D3.js在地图上绘制点?

d3.js topojson县/州地图上的Mousemove功能-按住鼠标选择多个县

如何使用从服务中检索到的纬度和经度并将它们显示在地图上?

我可以获取地理位置(纬度和经度),但无法在地图上找到位置

如何在D3.js中的地图上绘制经纬度对

获取世界环绕时在Google地图上显示的总纬度/经度-Javascript v3 API

在d3js中寻找具有特定投影的世界地图

d3js v5 + topojson v3将方形质心缩放

地图 - 像素到纬度经度计算