4

I'm trying to draw svg map with d3 from topojson file, but all I got is messed up lines.

I'm using most of the code I found on http://www.tnoda.com/blog/2013-12-07. When I use topojson files from that site, everything works fine. I tought maybe the problem is in my topojson file, but when I import it in mapshaper, I get normal map.

plnkr: http://plnkr.co/edit/TYiT5AoI29nEHC3Fre6D?p=preview

Here is my code:

index.html

<!DOCTYPE html>
<html>
  <head>
    <link rel="stylesheet" href="style.css" type="text/css">    
    <script src="//code.jquery.com/jquery-2.0.0.js"></script>    
    <script src="//d3js.org/d3.v3.min.js"></script>
    <script src="//d3js.org/topojson.v1.min.js"></script>
  </head>
  <body>
    <div id="map"></div>
    <script src="script.js"></script>
  </body>
</html>

script.js

var m_width = $("#map").width(), 
    width = 800, 
    height = 500

var projection = d3.geo.mercator()
    .scale(105)
    .translate([width / 2, height / 1.5]);

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

var svg = d3.select("#map").append("svg")
    .attr("width", m_width)
    .attr("height", m_width * height / width);

var g = svg.append("g");

d3.json("zupanije.max.topo.json", function(error, us) {
    g.append("g")
        .attr("id", "states")   
        .selectAll("path")
        .data(topojson.feature(us, us.objects.states).features) 
        .enter()
        .append("path")
        .attr("id", function(d) { return d.id; })       
        .attr("d", path)
});

styles.css

#map {
    background-color: #fff;
    border: 1px solid #ccc;
}
.background {
    fill: none;
    pointer-events: all;
}
#states{
    cursor: pointer;
    fill: #cde;
    stroke: #fff;
    stroke-linejoin: round;
    stroke-linecap: round;
}
#states .active {
    fill: #89a;
}
pre.prettyprint {
    border: 1px solid #ccc;
    margin-bottom: 0;
    padding: 9.5px;
}
akrelj
  • 151
  • 4
  • 18
  • It's quite artistic. Try your code with the original world data from your tutorial ([here](https://github.com/tomnoda/interactive_d3_map)), so you can check if it s failing on the code or on the data part. Set the scale to 150 (default), also. – Hugolpz Mar 12 '15 at 13:39
  • I already tried it with original data, and it works fine. That is why my guess was that something is wrong with my topojson file, so I imported it in http://www.mapshaper.org/, and I got normal map of Croatia (which is what I was trying to get with d3). So it seems that data is ok. Btw - I tried playing with scale also, setting it to 150, 50, 1000 etc, but it didn't help. Also with center. But all I got was another artistic image :) – akrelj Mar 12 '15 at 14:08
  • 2
    Follow this Q/A about Ghana, apply to Croatia, should go faster. http://stackoverflow.com/q/28556524/1974961 – Hugolpz Mar 12 '15 at 14:36
  • 1
    Ok, this time I converted shapefile directly to topojson with cmd, skipping geojson format; inspected it (http://jsoneditoronline.org/), then tried to preview it (http://hugolpz.github.io/distillery/). After few browser crashes I was finally able to preview it and it looked a lot like those images I am constantly getting. So I guess it must be data-related problem, which I'm not happy about because I can't get those data from anywhere online. Thank you very much for your help, at least now I'm sure where the problem is. – akrelj Mar 12 '15 at 15:18
  • I figured out what was wrong in topojson, maybe it can help someone... scale and translate properties (inside transform property) were way off, and that caused these wrong images for my map. – akrelj Mar 12 '15 at 16:04
  • yes, you can help others by writing down your solution. You can answer your own question, validate it, and we will vote for it – Hugolpz Mar 12 '15 at 17:12

1 Answers1

1

I was having the exact same problem and spent hours re-converting my SHP file to GeoJSON/Topojson in command line with different settings. The solution is quite simple! - Get QGIS here: https://www.qgis.org/en/site/forusers/download.html - open your SHP file or GeoJSON file - Select the layer you want to export - Go to Layer > Save as - Format: Geojson - CSR: WGS 84, EPSG: 4326 - Save.

Enjoy!

  • Jup, I'm using QGIS and topojson in my project http://arda-maps.org as well. It's a pretty awesome and easy combination of technologies. Make sure you use the correct projection. In my case it's not so important because I create an own world. What do you want to have at the end? Own world or an existing projection? – kwoxer Jun 11 '15 at 18:53
  • I imported shp file, went to layer > save as > ok, and it gave me an error: "OGR error: Failed to create GeoJSON datasource: proba.geojson". Also, it is a huge program with lots of capabilities that a novice like me doesn't know how to use. I need a program that is simple and dummy-proof for someone who doesn't create GIS but instead gets the data from someone who does, and just want to export it to topojson in the right way. In this program I can't even find how to export to topojson. It only gives me geojson option. – akrelj Aug 06 '15 at 12:06