title: d3.js svg drawing contour lines from (geo/topo)json
body: <p>I am looking for some guidance on how to create SVG image composed of paths based on contour line data coming from (geo/topo)JSON and render it with d3.js.</p>
  
  <p>This is an image of what I'd like to create essentially:
  <img src="https://i.stack.imgur.com/Iv4nQ.jpg" alt="Example of final product. (Note: the colors are not true to the contours in the example, this is just from photoshop)"></p>
  
  <p>Ultimately, I'd like the image to be able to scale responsively, animate (draw in lines, change color interactively) the paths, and be able to swap out different data files to render other images.</p>
  
  <p>I have looked through several guides, tutorials etc, but it seems I am missing some steps or implementing the wrong procedure thus far, so I thought I'd ask here. Thanks for any help.</p>
  
  <p>I'm trying to clarify the best way to do this and figure out what I'm misinterpreting in terms of the capabilities/procedures in d3.js.</p>
  
  <p>I'm creating contour lines from DEM (Digital Elevation Model) data by extracting contours in QGIS. To get the JSON from these I have tried saving as GeoJSON in QGIS, and converting Shapefiles (ESRI .shp) to GeoJSON in OGR2OGR. I have also tried using Node's Topojson (<a href="https://www.npmjs.org/package/topojson" rel="nofollow noreferrer">https://www.npmjs.org/package/topojson</a>). For the most part, I've been able to get JSON files, although there might be something wrong with the way I'm converting them, or the original contour data coming from QGIS that makes it incompatible with what I've tried in d3.</p>
  
  <p>The results that I have gotten when the JSON gets rendered basically look like black boxes  (looks like improperly rendered polygons gone wild inside the svg container). If I change the fill color to none I see crazy lines all over. I read somewhere that TopoJSON has to be polygons not lines or polylines since it uses arcs, but I got the same results with GeoJSON. </p>
  
  <p>I'm wondering if this is a problem with the way I've exported the contour lines from QGIS and converted to JSON, perhaps the projections are not lining up? There also might be some errors in the lines that were created automatically when converting, but I'm not sure how to fix these. As you can see in the example image, some of my contours are not closed loops because they extend beyond the boundaries of the area I have data for/want to show. Does it make sense that these are supposed to be rendered as polylines or paths?</p>
  
  <p>Also a note, I am not particularly interested in the "geospaciality" of these lines in terms of their lat-long or geographic location in this project. Basically just using contour lines from real geographic data to display the line patterns. </p>
  
  <p>Here is my code:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  var width = 500,
      height = 500;
  
  var svg = d3.select("#section-1-svg").append("svg")
      .attr("width", width)
      .attr("height", height);
  
  d3.json("contour.json", function(error, contour) {
      console.log(contour);
  });
  
  var path = d3.geo.path()
      .projection(d3.geo.mercator());
  
      d3.json("contour.json", function(error, json) {
  
      svg.selectAll("path")
          .data(json.features)
          .enter()
          .append("path")
          .attr("d", path);
  });
  &lt;/script&gt;
  </code></pre>
  
  <p>So other than just getting this to render on a basic level, other questions are:
  Will I be able to select individual lines to animate with for example stroke-dashoffset?
  Does this source JSON still contain elevation data so I can color lines based on elevation?</p>
  
  <p>Thanks for your help in getting me on track with this! I would appreciate anyone clarifying this for me and informing me on the best way to render this.</p>
  
  <p><strong>EDIT</strong>: Using the code from user1614080's example, I'm rendering the lines thus:</p>
  
  <pre><code>    svg.selectAll("path")
      .data(topo).enter()
      .append("path")
      .style("fill", "none")
      .style("stroke", function(d, i) {
          return interp(cScale(d.properties.ELEV));
      })
  .attr("d", path)
  .each(function(d) {
      d.totalLength = this.getTotalLength();
      console.log(d.totalLength);
  
  })
  .attr("stroke-dasharray", function(d) { return d.totalLength + " " + d.totalLength;})
  .attr("stroke-dashoffset", function(d) { return d.totalLength; })
  .transition()
      .delay(function(d, i) { return i * 200; })
      .duration(4000)
      .ease("linear")
      .attr("stroke-dashoffset", "0");
      });
  </code></pre>
  
  <p>But I can't get the effect I need (lines drawing in). They seem to fade in from small dashes instead of drawing the entire dash. I can see in browser that the stroke-dasharray and offset is being assigned correctly, just can't figure out why the transition is not respected. </p>