D3.js未在topojson中显示所有地图元素

发布于 2025-01-22 23:42:08 字数 1877 浏览 0 评论 0原文

答:我已经下载了两个geojsons

  1. 地图
  2. 美国

世界 $ GEOJSON-MERGE GZ_2010_US_040_20M.JSON custom.geo.json> commined.json

C.我然后将world_map_with_usa_states.json转换为mapshaper.org上的topojson,

加载world_map_with_with_usa_states.json.json on https://mapshaper.org/ 向美国和其他国家/地区显示

,但D3.js并未显示美国州

d3.js代码

let mapURl = 'world_map_with_USA_states.json'

let countriesData
const width = 900
const height = 600
const svg = d3.select('body').append('svg')
                             .attr('width', width)
                             .attr('height', height)

const projection = d3.geoMercator().scale(140)
                                   .translate([width/2, height/1.4]) 

const path = d3.geoPath(projection)

const g = svg.append('g')

d3.json(mapURl)
  .then((data, error) =>{
      if (error){
          console.log(error)
      }else{
        console.log(data)
        countriesData = topojson.feature(data, data.objects.combined)

        g.selectAll('path').data(countriesData.features).enter().append('path')
                                                                .attr('class', 'country')
                                                                .attr('d', path)
      }
  })

d3.js地图(顶部)与。 mapshaper.org map(底部)

”在此处输入映像说明“

“ alt =” mapshaper.org apojson的地图“

https://i.sstatic.net/znsk.png

A. I have downloaded two geojsons

  1. world map
  2. USA states

B. I then merge the two geojsons with geojson-merge on the commandline to give me a world_map_with_USA_states.json

$ geojson-merge gz_2010_us_040_00_20m.json custom.geo.json > combined.json

C. I then convert the world_map_with_USA_states.json to topojson on mapshaper.org

Loading the world_map_with_USA_states.json on https://mapshaper.org/ shows USA states and other countries

But d3.js does not show the USA states

d3.js code

let mapURl = 'world_map_with_USA_states.json'

let countriesData
const width = 900
const height = 600
const svg = d3.select('body').append('svg')
                             .attr('width', width)
                             .attr('height', height)

const projection = d3.geoMercator().scale(140)
                                   .translate([width/2, height/1.4]) 

const path = d3.geoPath(projection)

const g = svg.append('g')

d3.json(mapURl)
  .then((data, error) =>{
      if (error){
          console.log(error)
      }else{
        console.log(data)
        countriesData = topojson.feature(data, data.objects.combined)

        g.selectAll('path').data(countriesData.features).enter().append('path')
                                                                .attr('class', 'country')
                                                                .attr('d', path)
      }
  })

d3.js map (top) Vs. mapshaper.org map (bottom)

enter image description here

mapshaper.org map with the same topojson

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文