仅显示单个州和完整美国县地图中的县

Display only a single state with counties from a full US counties map

本文关键字:地图 美国县 显示 单个州      更新时间:2023-09-26

是否可以从完整的美国县地图中显示单个州?

我知道我可以得到每个州和县的形状文件并创建topojson,但如果可能的话,我宁愿使用一个文件。

以Mike Bostock的美国全县地图为例。美国县地图有没有可能从这里只显示NY ?

一旦你建立了你的d3。如果你的数据有一个'state'字段:

d3.json(filename, function(error, data){ var single=data.filter(function(d){ return d.state==='Ohio';} }

然后使用新的单一变量作为d3的数据

我最终完成了项目的边界框,并在我想要显示的状态周围创建了一个剪切路径。

NY State W/Counties - Clipped

我知道这是一个很晚的回答。然而,这可能对将来的其他人有帮助。

这是一个如何从美国地图文件中提取一个州的例子:

<!DOCTYPE html>
<meta charset="utf-8">
<style>
path {
        fill: lightgray;
        stroke: #000000;
        stroke-width: 1.5;
        }

path:hover {
        fill:orange;
        cursor:pointer;
      }
#state-borders {
  fill: white;
  stroke: #000000;
  stroke-width: 10.5px;
  stroke-linejoin: round;
  stroke-linecap: round;
  pointer-events: none;
}

</style>
<body> 
  <script src="http://d3js.org/d3.v4.min.js" charset="utf-8"></script>
  <script src="https://d3js.org/topojson.v1.min.js"></script>
<script>
var width = 960,
    height = 500,
    centered;
var projection =  d3.geoAlbersUsa()
    .scale(1370)
    .translate([width / 2, height / 2]);
var path = d3.geoPath()
    .projection(projection);
var svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height);

d3.json("us-counties-github.json",function(json){
  console.log(json);

    svg.selectAll("path")
       .attr("id", "state_fips")
       .data(topojson.feature(json, json.objects.collection).features.filter(function(d) { return d.properties.state_fips == 36; }))
       .enter()
       .append("path")
       .attr("d", path)
       .attr("stroke","white")
       .attr("fill", "gray");

    });
</script>

*我从https://raw.githubusercontent.com/deldersveld/topojson/master/countries/united-states/us-albers-counties.json下载了数据集"这些县都包含在这个文件中"。