简体   繁体   English

在D3.js地图可视化中使用来自两个单独的geoJSON文件的数据

[英]Using data from two separate geoJSON files in D3.js map visualization

I am making an interactive map visualization of the world, where the user should be able to zoom in on a country and then see more details of that country (for example states if the country is the US). 我正在对世界进行交互式地图可视化,用户应该能够放大一个国家,然后查看该国家的更多详细信息(例如,说明该国家是否为美国)。

As of now, I have one geoJSON file for the data of the countries in the world and another one for the provinces of China. 到目前为止,我有一个geoJSON文件用于存储世界各国的数据,另一个有用于中国各省的数据。 The problem is that I need to have the China province map placed where China is on the world map. 问题是我需要将中国省地图放在中国在世界地图上的位置。 And even though the shape of the Chinese border is the same for the both maps, the scaling is not and it is also a problem with the translation of the China map. 即使两个地图的中文边框形状相同,缩放比例也不一样,这也是中国地图翻译的问题。

How can I make sure that the map over China gets placed in the same place as China is on the world map? 如何确定中国的地图与中国在世界地图上的位置相同?

The end result should be something similar to this: http://techslides.com/demos/d3/us-zoom-county.html 最终结果应该类似于以下内容: http : //techslides.com/demos/d3/us-zoom-county.html

But in that case all the data is in a single geoJSON file, containing both US states and counties for each state. 但是在那种情况下,所有数据都在一个geoJSON文件中,包含美国各州和每个州的县。

If there is some way to merge two or more geoJSON files that would be awesome and would probably work in my case. 如果有某种方法可以合并两个或多个geoJSON文件,那将非常棒,并且可能适合我的情况。

Any ideas of how to use data from different geoJSON files in the same map visualization? 关于如何在同一地图可视化中使用来自不同geoJSON文件的数据的任何想法?

Thanks in advance! 提前致谢!

I managed to solve the problem and it was easier than I thought. 我设法解决了这个问题,这比我想象的要容易。 The solution was to first calculate the bounding area of china then to draw the map there. 解决方案是先计算中国的边界区域,然后在其中绘制地图。 Here are some code: 这是一些代码:

var projectionChina = d3.geoMercator();
var pathChina = d3.geoPath(projection);

g.append("g")
  .attr("class", "china")
  .attr("width", boundingArea.width)
  .attr("height", boundingArea.height)
  .selectAll("path")
    .data(data.geometries)
  .enter().append("path")
    .attr("d", pathChina);

Where boundingArea is the rectangle that encapsulates the Chinese border. 其中boundingArea是封装中文边框的矩形。

Hope this helps some one with the same problem. 希望这对遇到同样问题的人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM