score:0

In regards to:

I am running into an error in displaying my map. The error: TypeError: counties[i].properties is undefined

We need to examine both the referenced world topojson and US topojson.

In the world topojson, the geometries are defined like this:

"geometries":[
{"type":"Polygon","id":1,"arcs":[[0,1,2,3,4,5,6,7]],"properties":{"admin":"Afghanistan","id":"AFG"}},
{"type":"Polygon","id":4,"arcs":[[16,17,18,19,20]],"properties":{"admin":"Albania","id":"ALB"}},

There is a properties attribute to each geometry.

In the US topojson you reference, the geometries are defined like this:

"geometries":[
{"type":"Polygon","arcs":[[0,1,2,3,4]],"id":"05089"},
{"type":"Polygon","arcs":[[5,6,7,8,9]],"id":"06079"},
{"type":"Polygon","arcs":[[10,11,12,13,14,15,16]],"id":"17111"},

There is no properties attribute assigned to each geometry. So, when you call for the properties of a geometry, it'll return undefined.

So instead of using counties[i].properties.id, you might find more success with counties[i].id.


It may also be worth noting, if you are comparing examples, that the conversion from topojson to geojson (topojson.feature(us, us.objects.counties).features) will modify the structure of the data as well:

d3.json("//d3js.org/us-10m.v1.json",function(error,us) {
  console.log("Number of county geometries:");
  console.log(us.objects.counties.geometries.length);
  console.log("Example county id:");
  console.log(us.objects.counties.geometries[0].id);  
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<script src="https://d3js.org/topojson.v2.min.js"></script>

d3.json("//d3js.org/us-10m.v1.json",function(error,us) {
  var geojson = topojson.feature(us, us.objects.counties).features;
  console.log("Number of features");
  console.log(geojson.length);
  console.log("Example county ID");
  console.log(geojson[0].id);
  
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<script src="https://d3js.org/topojson.v2.min.js"></script>


Related Query

More Query from same tag