- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
问题,简而言之:如何使用 Google Maps API 以外的其他东西在墨卡托中准确地投影环绕的大圆半径?
问题,长篇大论:
所以我有一个难题。我运行了一个 map 应用程序,它使用 Google Maps API 将巨大的圆圈投影到墨卡托 map 上——它试图显示非常大、精确的半径,比如 13,000 公里的数量级。但是我不想再使用 Google Maps API,因为 Google 的新定价方案太疯狂了。所以我试图将代码转换为 Leaflet、Mapbox 或任何非 Google 的东西,但没有任何东西可以正确处理这些圆圈。
以下是 Google Maps API 如何处理以非洲北部为中心、半径为 13,000 公里的测地线圆:
这在直觉上看起来很奇怪,但却是正确的。波浪图案是由环绕地球的圆圈引起的。
D3.js 可以在正交投影中正确渲染它。所以这里是在 D3.js 中用 d3.geo.circle() 在地球上渲染的同一个圆,旋转两次:
这使得二维“波浪”图案更有意义,对吗?对。我喜欢它。完全适用于我的科学传播和所有目的。
但是当我将代码转换为 Leaflet 时,它根本不起作用。为什么?因为Leaflet的圆类根本就不是大圆。相反,它似乎只是一个随纬度稍微扭曲的椭圆,但不是以真正的测地线方式。相同的圆,相同的半径,相同的原点,我们得到:
大错特错,大错特错!除了看起来完全不现实之外,这是不正确的——澳大利亚不会在这样的圆半径内。这对我的申请很重要!这是做不到的。
好吧,我想,也许诀窍是尝试实现我自己的大圆圈类。我采用的方法是将圆点迭代为距原点的距离,但使用来自 this very helpful website 的“给定距离和距起点的方位角的目标点”计算来计算距离。 ,然后将它们投影为 Leaflet 中的多边形。这就是我得到的结果:
这看起来很糟糕,但实际上更接近准确!我们得到了波浪效应,这是正确的。像我一样,你可能会问,“这里到底发生了什么?”所以我做了一个版本,让我可以突出每个迭代点:
你可以很清楚地看到它正确地渲染了圆,但多边形错误地加入了它。它应该做的(人们可能会天真地认为)是将波形图包裹在墨卡托 map 投影的多个实例周围,不是在顶部天真地将它们连接起来,而是将它们球形地连接起来。像这个粗略的 Photoshop 渲染:
然后多边形会以某种方式“关闭”,表明多边形上方的所有内容也都包含在其中。
不过,我不知道如何在 Leaflet 中实现类似的功能。或者其他任何事情。也许我必须以某种方式自己处理原始 SVG,考虑到缩放状态?或者其他的东西?在我进入那些危险的杂草之前,我想我会征求任何建议/想法/等。也许有一些更明显的方法。
哦,我还尝试了另一件事:使用相同的 d3.geo.circle 构造函数,该构造函数在墨卡托/传单投影的正交投影中效果很好。它产生的结果或多或少与我的“天真”传单大圆实现相同:
我猜这是有希望的。但是,如果您移动原点的经度,D3.js 版本会以更奇怪的方式包装(红色的 D3.js,绿松石色的 Leaflet 类):
如果 D3.js 中有某种方法可以改变它的工作方式,我不会感到惊讶,但我还没有完全深入 D3.js 的兔子洞。我希望 D3.js 能让这个“更容易”(因为它是比 Leaflet 更完整的制图工具),所以我将继续研究这个。
我还没有尝试在 Mapbox-gl 中执行此操作(我想这是“尝试”列表中的下一个)。
反正。谢谢阅读。重申这个问题:如何使用 Google Maps API 以外的其他东西在墨卡托中准确地投影环绕的大圆半径?
最佳答案
是antimeridian cutting那是 GeoJSON需要在传单或 map 框中正确绘制。
对于 d3,它很简单 d3.geoCircle()
, 对于其他不处理反子午线切割的 map 服务,您可以使用 d3 来正确计算输入 json。
主要思想是将 d3 计算的坐标取消投影回 lat-lng,在其计算出的未 protected 特征上使用相同的投影,将被 d3 的反子午线分割。
见 projection.invert()
我开发了示例,运行代码片段并在 d3 绘图上拖动圆圈。
这是结果截图:
<!DOCTYPE html>
<html>
<head>
<script src="https://d3js.org/d3.v5.min.js"></script>
<script src="https://unpkg.com/leaflet@1.3.4/dist/leaflet.js"></script>
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.3.4/dist/leaflet.css"/>
</head>
<body style="margin: 0">
<svg style="display: inline-block"></svg>
<div id="map" style="display: inline-block; width: 350px; height: 260px"></div>
<script>
var tileLayer = L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png');
var map = L.map('map').setView([0,0], 0).addLayer(tileLayer);
var bounds = [260, 260];
var colorGenerator = d3.scaleOrdinal(d3.schemeCategory10);
var projection = d3.geoMercator().translate([bounds[0] / 2, bounds[1] / 2]).scale(40);
var geoPath = d3.geoPath().projection(projection);
var geoCircle = d3.geoCircle();
var svg = d3.select('svg')
.attr("width", bounds[0])
.attr("height", bounds[1])
.attr("viewbox", "0 0 " + bounds[0] + " " + bounds[1])
.append('g');
svg.append("g")
.append("path")
.datum(d3.geoGraticule())
.attr("stroke", "gray")
.attr('d', geoPath);
function addCircle(center, radius, color) {
var g = svg.append("g");
var drag = d3.drag().on("drag", dragged);
var xy = projection(center);
var path = g.append("path")
.datum({
type: "Polygon",
coordinates: [[]],
x: xy[0],
y: xy[1]
})
.classed("zone", "true")
.attr("fill", color)
.attr("stroke", color)
.attr("fill-opacity", 0.3)
.call(drag);
update(path.datum());
function dragged(d) {
g.raise();
d.x = d3.event.x;
d.y = d3.event.y;
update(d)
}
function update(d) {
center = projection.invert([d.x, d.y]);
var poly = geoCircle.center(center).radius(radius)();
d.coordinates[0] = poly.coordinates[0];
path.attr('d', geoPath);
d.geojson && d.geojson.remove();
d.geojson = L.geoJSON(unproject(path.attr('d')), {
color: color,
}).addTo(map);
}
function unproject(d) {
var features = d.toLowerCase().split('z').join('').split('m');
features.shift();
var coords = features.map(function (feature) {
return feature.split('l').map(function (pt) {
var xy = pt.split(',');
return projection.invert([+xy[0], +xy[1]]);
});
});
return {
type: 'MultiPolygon',
coordinates: [coords]
}
}
}
d3.range(0, 4).forEach(function (i) {
addCircle([-120 + i * 60, 0], i * 10 + 10, colorGenerator(i));
});
</script>
</body>
</html>
function unproject(d, projection) {
var features = d.toLowerCase().split('z').join('').split('m');
features.shift();
var coords = features.map(function (feature) {
return feature.split('l').map(function (pt) {
var xy = pt.split(',');
return projection.invert([+xy[0], +xy[1]]);
});
});
return {
type: 'MultiPolygon',
coordinates: [coords]
}
}
function unproject(geojson) {
var projected = d3.geoProject(geojson, projection);
if (projected.type === "MultiPolygon") {
projected.coordinates = projected.coordinates.map(function(arr) {
return [invert(arr[0])];
});
} else {
projected.coordinates[0] = invert(projected.coordinates[0]);
}
return projected;
}
function invert(coords) {
return coords.map(function(c) {
return projection.invert(c);
});
}
关于d3.js - 使用 D3.js、Leaflet 或 Mapbox 用墨卡托 map 包裹大圆圈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53022549/
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
我试图用这种形式简单地获取数字 28 integer+space+integer+integer+space+integer我试过这个正则表达式 \\s\\d\\d\\s 但我得到了两个数字11 和
最近一直在学习D语言。我一直对运行时感到困惑。 从我能收集到的关于它的信息中,(这不是很多)我知道它是一种有助于 D 的一些特性的运行时。像垃圾收集一样,它与您自己的程序一起运行。但是既然 D 是编译
想问一下这两个正则表达式有区别吗? \d\d\d 与 \d{3} 我已经在我的本地机器上使用 Java 和 Windows 操作系统对此进行了测试,两者都工作正常并且结果相同。但是,当在 linux
我正在学习 Go,而且我坚持使用 Go 之旅(exercise-stringer.go:https://tour.golang.org/methods/7)。 这是一些代码: type IPAddr
我在Java正则表达式中发现了一段令我困惑的代码: Pattern.compile( "J.*\\d[0-35-9]-\\d\\d-\\d\\d" ); 要编译的字符串是: String string
我在 ruby 代码上偶然发现了这个。我知道\d{4})\/(\d\d)\/(\d\d)\/(.*)/是什么意思,但是\1-\2-\3-\4 是什么意思? 最佳答案 \1-\2-\3-\4 是 b
我一直在努力解决这个问题,这让我很恼火。我了解 D 运行时库。它是什么,它做什么。我也明白你可以在没有它的情况下编译 D 应用程序。就像 XoMB 所做的那样。好吧,XoMB 定义了自己的运行时,但是
我有两个列表列表,子列表代表路径。我想找到所有路径。 List> pathList1 List> pathList2 当然是天真的解决方案: List> result = new ArrayList>
我需要使用 Regex 格式化一个字符串,该字符串包含数字、字母 a-z 和 A-Z,同时还包含破折号和空格。 从用户输入我有02-219 8 53 24 输出应该是022 198 53 24 我正在
目标是达到与this C++ example相同的效果: 避免创建临时文件。我曾尝试将 C++ 示例翻译为 D,但没有成功。我也尝试过不同的方法。 import std.datetime : benc
tl;dr:你好吗perfect forwarding在 D? 该链接有一个很好的解释,但例如,假设我有这个方法: void foo(T)(in int a, out int b, ref int c
有什么方法可以在 D 中使用abstract auto 函数吗? 如果我声明一个类如下: class MyClass { abstract auto foo(); } 我收到以下错误: mai
有没有人为内存中重叠的数组切片实现交集?算法在没有重叠时返回 []。 当 pretty-print (使用重叠缩进)内存中重叠的数组切片时,我想要这个。 最佳答案 如果您确定它们是数组,那么只需取 p
我已经开始学习 D,但我在使用 Andrei Alexandrescu 所著的 The D Programming Language 一书中提供的示例时遇到了一些麻烦。由于 int 和 ulong 类
如何创建一个不可变的类? 我的目标是创建一个实例始终不可变的类。现在我只是用不可变的方法和构造函数创建了一个“可变”类。我将其称为 mData,m 表示可变。然后我创建一个别名 alias immut
不久前我买了《The D Programming Language》。好书,很有教育意义。但是,我在尝试编译书中列出的语言功能时遇到了麻烦:扩展函数。 在这本书中,Andrei 写了任何可以像这样调用
我在 D http://www.digitalmars.com/d/2.0/lazy-evaluation.html 中找到了函数参数的惰性求值示例 我想知道如何在 D 中实现可能的无限数据结构,就像
这个问题在这里已经有了答案: 12 年前关闭。 Possible Duplicate: Could anyone explain these undefined behaviors (i = i++
当前是否可以跨模块扫描/查询/迭代具有某些属性的所有函数(或类)? 例如: source/packageA/something.d: @sillyWalk(10) void doSomething()
我是一名优秀的程序员,十分优秀!