Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

topojson.js:187: Uncaught TypeError: Cannot read property 'type' of undefined

Tags:

d3.js

I a trying to make a map with D3 and TopoJSON for the Netherlands, including the provinces. This is the code:

<!DOCTYPE html>
<html lang="en">
    <head>
    <meta charset="utf-8">
    <title>D3 Test</title>
    <script type="text/javascript" src="http://localhost/webserver/d3/d3.js"></script>
    <script type="text/javascript" src="http://localhost/webserver/topojson/topojson.js"></script>
        <style type="text/css">

            div.bar {
                display: inline-block;
                width: 20px;
                height: 75px;
                background-color: teal;
                margin-right: 2px;
            }
            .pumpkin {
                fill: rgba(128, 0, 128, 0.75);
                stroke: yellow;
                stroke-width: 5;
            }
            .apple {
                fill: rgba(0, 255, 0, 0.55);
                stroke: green;
                stroke-width: 15;
            }
            .orange {
                fill: rgba(255, 255, 0, 0.55);
                stroke: orange;
                stroke-width: 10;
            }
            .subunit    { fill: #cdc; }
            .subunit-label  {
                fill: #777;
                fill-opacity: .25;
                font-size:  30px;
                font-weight: 300;
                text-anchor: middle;}

            .provincie  {fill: none; }
            .Utrecht    {fill: #ddd; }
            .Zuid-Holland   {fill: #dde; }
            .Noord-Holland  {fill: #dee; }
            .Drenthe    {fill: #aae; }
            .Gelderland     {fill: #eee; }
            .Friesland  {fill: #ddc; }
            .Groningen  {fill: #dcc; }
            .Limburg    {fill: #ccc; }
            .Noord-Brabant  {fill: #ddb; }
            .Overijssel     {fill: #dbb; }
            .Zeeland    {fill: #bbb; }
        </style>    
    </head>
    <body>
    <script type="text/javascript">

    var width = 960, height = 860;

    var projection = d3.geo.albers()
        .center([6, 49.40])
        .rotate([0, -1.9])
        .parallels([50, 60])
        .scale(11000)
        .translate([width / 2, height / 2]);

    var path = d3.geo.path()
        .projection(projection);


    var svg = d3.select("body").append("svg")
        .attr("width", width) 
        .attr("height", height);

    d3.json("http://localhost/webserver/data/nl.json", function(error, nl) {
        svg.selectAll(".subunit")
        .data(topojson.object(nl, nl.objects.subunits).geometries)
            .enter().append("path")
            .attr("class", function(d) { return "subunit " + d.id; })
        .attr("d", path);

        svg.selectAll(".subunit-label")
        .data(topojson.object(nl, nl.objects.subunits).geometries)
        .enter().append("text")
        .attr("x", -20)
        .attr("y", -50)
        .attr("class", function(d) { return "subunit-label " + d.id; })
        .attr("transform", function(d) { return "translate(" + path.centroid(d) + ")"; })
        .attr("dy", ".35em")
        .text(function(d) { return d.properties.name; });

        svg.selectAll(".provincie")
        .data(topojson.object(nl, nl.objects.provincies).geometries)
        .enter().append("path")
            .attr("class", function(d) { return "provincie " + d.properties.name; })
        .attr("d", path);

        svg.append("path")
        .datum(topojson.object(nl, nl.objects.places))
        .attr("d", path)
            .attr("class", "place");

        svg.selectAll(".place-label")
        .data(topojson.object(nl, nl.objects.places).geometries)
        .enter().append("text")
        .attr("class", "place-label")
        .attr("transform", function(d) { return "translate(" + projection(d.coordinates) + ")"; })
        .attr("dy", ".35em").text(function(d) { return d.properties.name; });

        svg.selectAll(".place-label")
        .attr("x", function(d) { return d.coordinates[0] > -1 ? 10 : -10; })
            .style("text-anchor", function(d) { return d.coordinates[0] > -1 ? "start" : "end"; });
        }); 
    </script>
    </body>
</html>  

The result is the map of the Netherlands, however it doesn't contain the provinces (with colors and borders).

I get the following error:

Uncaught TypeError: Cannot read property 'type' of undefined topojson.js:187

This is line 186 and 187:

function geometry(o) {

var t = o.type, g = t === "GeometryCollection" ? {type: t, geometries: o.geometries.map(geometry)}

like image 485
csnake Avatar asked Jan 14 '23 21:01

csnake


1 Answers

It looks like you are referencing an object that doesn't exist in your topology.

Is it possible your TopoJSON file uses the English spelling of "provinces" rather than the Dutch "provincies"? If it did, then nl.objects.provincies would be null, and you'd either need to refer to nl.objects.provinces in your code, or edit the TopoJSON file to use the Dutch spelling "provincies" instead.

Can you post the contents of nl.json so we can take a look (say, on dropbox)?

like image 135
mbostock Avatar answered May 24 '23 03:05

mbostock