使用 Javascript 将 GeoJSON 转换为 SVG
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8234918/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Transform GeoJSON to SVG with Javascript
提问by camilokawerin
回答by Mr.Hunt
You can use d3.jslibrary. Following code snippet will do the job:
您可以使用d3.js库。以下代码片段将完成这项工作:
Include d3.js in your html file
在 html 文件中包含 d3.js
<script src="files/d3.v3.min.js"></script>
Assuming, you have div with id map in your html file:
假设您的 html 文件中有带有 id 映射的 div:
<div id="map"></div>
Following js code will add map to your div map. geoJsonObj is your geojson.
以下 js 代码会将地图添加到您的 div 地图。geoJsonObj 是您的 geojson。
var svg = d3.select("#map").append("svg")
.attr("width", width)
.attr("height", height);
svg.append("g")
.selectAll("path")
.data(geoJsonObj.features)
.enter().append("path")
.attr("d", path);
To see a working example, go here. Note that the example uses topojson as input to the .data() attribute.
要查看工作示例,请转到此处。请注意,该示例使用 topojson 作为 .data() 属性的输入。
回答by Gagan
There is basic tool available to convert geojson to svg geojson2svgand as npm modulealso. The output from geojson2svg is svg string so this tool can be used in browser as well as with node.js.
有基本的工具可用于将 geojson 转换为 svg geojson2svg和npm 模块。geojson2svg 的输出是 svg 字符串,所以这个工具可以在浏览器和 node.js 中使用。
Its very easy to covert svg string to dom element. This has been explained by bobinceherevery nicely with JavaScript code. I have made an npm modulefor convenience.
将 svg 字符串转换为 dom 元素非常容易。bobince在这里用 JavaScript 代码很好地解释了这一点。为方便起见,我制作了一个npm 模块。
geojson2svg gives you more flexibility but for quick solution d3 is best.
geojson2svg 为您提供了更大的灵活性,但对于快速解决方案 d3 是最好的。
Disclaimer: I am the author of geojson2svg.
免责声明:我是 geojson2svg 的作者。
回答by Arend
You can look into GDAL, not sure if it fully supports SVG creation, but GDAL can generally convert all geo formats into other geoformats.
你可以看看GDAL,不确定它是否完全支持SVG创建,但GDAL一般可以将所有geo格式转换为其他geoformat。