【发布时间】:2018-01-07 08:57:35
【问题描述】:
我在大约以太平洋为中心的世界地图上使用 d3.geo.conicEquidistant() 投影,我的目标是从一点画圈,说明从地图上这一点到特定距离,例如 1000 公里.
在给定特定公里数的情况下,我如何计算正确的投影半径?
这是我的代码示例(是的,它是关于从朝鲜发射的导弹。人为地使用平壤作为发射点 :),基于这个问题:Scale a circle's radius (given in meters) to D3.js d3.geo.mercator map
<!DOCTYPE html>
<meta charset="utf-8">
<style>
path {
stroke: white;
stroke-width: 0.25px;
fill: grey;
}
circle {
stroke: red;
stroke-width: 1px;
stroke-dasharray: 5;
fill: transparent;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://d3js.org/topojson.v0.min.js"></script>
<script>
var width = 960,
height = 500;
var projection = d3.geo.conicEquidistant()
.center([0, 5 ])
.scale((width + 1) / 2 / Math.PI)//.scale(150)
.rotate([-160,0]);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var path = d3.geo.path()
.projection(projection);
var g = svg.append("g");
var missiles = [
{
name: "missile1",
location: { // appx lat&long of pyongyang
latitude: 125.6720717,
longitude: 39.0292506
},
reach: 1000 //radius of circle in kilometers on map
},
{
name: "missile2",
location: { // appx lat&long of pyongyang
latitude: 125.6720717,
longitude: 39.0292506
},
reach: 3500 //radius of circle in kilometers on map
},
];
// load and display the World
d3.json("https://s3-us-west-2.amazonaws.com/vida-public/geo/world-topo-min.json", function(error, topology) {
g.selectAll("path")
.data(topojson.object(topology, topology.objects.countries)
.geometries)
.enter()
.append("path")
.attr("d", path)
});
svg.selectAll(".pin")
.data(missiles)
.enter().append("circle", ".pin")
.attr("r", scaledRadius)
/*function(d) {
return d.reach
})*/
.attr("transform", function(d) {
return "translate(" + projection([
d.location.latitude,
d.location.longitude
]) + ")"
})
</script>
</body>
</html>
我查看了这个答案,在上面进行了尝试,但不太能够将它应用到我的代码中(老实说,我对 d3 很陌生,可能有一些非常明显的错误): https://stackoverflow.com/a/31616927/4623519
另一个问题:这个解决方案是特定于墨卡托投影的吗?
(让我们忽略我们确实需要在此投影中隐藏南极洲。)
【问题讨论】:
-
你能把我们链接到工作代码,或者用jsfiddle这个吗?
-
这是我在用 Andrew 的回答更改我的代码后所做的一个 jfiddle:jsfiddle.net/y8qn8tr1 - 出于某种原因,世界地图路径没有显示在小提琴中......
-
访问geojson时尝试https://而不是http://:jsfiddle.net/38he3mt9
-
啊,太好了,谢谢!