My 'handcrafted' cartogram (How to create contiguous area cartograms in QGIS or R?) is ready to embed in code - I want to present it online with D3.js.

Therefore I produced a topoJSON file of it - but all I get are weird shapes all around the globe.

Here is the (simplified) cartogram in geoJSON (file:removed) - it works perfectly with QGis. Also I checked the geometry in QGis and it doesn't display an error [Vector > Geometry Tools > Check geometry validity].

I tried both: From QGis > geoJSON > topoJSON (with topojson), and QGis > ESRI shapefile > geoJSON (with ogr2ogr) > topoJSON (with topojson).

Also I tried reprojection with ogr2ogr:

ogr2ogr "ESRI Shapefile" -t_srs EPSG:4326 outfile.shp infile.shp

And I did the whole thing with UTF-8 flag:

topojson --shapefile-encoding utf8 -o outfile.json infile.shp

Here is the (simplified) cartogram in topoJSON (file:removed), and it looks ok to me - even the json-validators are happy with it.

But my D3.js code just results in weird shapes and lines.

    <meta charset="utf-8">
    <script src="http://d3js.org/d3.v3.min.js"></script>
    <script src="http://d3js.org/topojson.v0.min.js"></script>

    .unit {
        fill: none;
        stroke: #111;
        stroke-linejoin: round;


    var width = 600,
        height = 600;

    var projection = d3.geo.albers() // also: orthographic & mercator wont work
        .rotate([0, 0])
        .center([8.43, 46.8]) // Switzerland

    var path = d3.geo.path()

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

    d3.json("hexmap.json", function(error, hexm) {
        var units = topojson.object(hexm, hexm.objects.hexmap).geometries[25];

           .attr("class", "unit")
           .attr("d", path);


I just don't know where I am going wrong... Is it the projection? The conversion? Are the files ok?


  1. use a transferable projection in the first place.
  2. the correct ogr2ogr command:

     ogr2ogr "GEOJSON" -s_srs infile.prj -t_srs EPSG:4326 outfile.geojson infile.shp


     ogr2ogr "GEOJSON" -s_srs EPSG:21781 -t_srs EPSG:4326 outfile.geojson infile.shp
  • I just realised there are a few duplicate nodes in the geoJSON file! Maybe thats the problem. But this is bizarre: I checked the geometry before export (as described above) and there isn't a single error... the duplicate nodes only appear, when re-import the created geoJSON file.
    – Chrugel
    Commented Mar 13, 2013 at 18:33

1 Answer 1


You can only use ogr2ogr to reproject your shapefile if ogr2ogr knows the source projection, which is typically defined in "infile.prj" (assuming your shapefile is "infile.shp"). Your GeoJSON file does not include any embedded projection information, so trying to reproject as follows has no effect:

ogr2ogr -f 'GeoJSON' -t_srs 'EPSG:4326' hexmap-still-broken.geojson hexmap.geojson

If you look at the hexmap.geojson (or the hexmap-still-broken.geojson, which has the same problem), you'll see that the coordinates are obviously not in longitude and latitude:

[ 624601.329864838044159, 267651.03375 ], [ 623590.96689375618007, 265901.03375 ],
[ 621570.240951592335477, 265901.03375 ], [ 620559.877980510587804, 267651.03375 ],
[ 621570.240951592335477, 269401.03375 ], [ 623590.96689375618007, 269401.03375 ],

To reproject, you'll either need your input .prj file, or you must use the -s_srs argument to tell ogr2ogr the projection of the input.

  • Thank you - I am still struggling. I am doing: 'ogr2ogr -f geoJSON -a_srs hexmap.prj hexmap_fix.geojson hexmap.shp'
    – Chrugel
    Commented Mar 13, 2013 at 19:49
  • sry, on output is -t_srs, but I am wrong anyway...
    – Chrugel
    Commented Mar 13, 2013 at 20:01
  • ok I am tired - I did ' ogr2ogr -f geoJSON -s_srs hexmap.prj hexmap_fix.geojson hexmap.shp ' but without success.
    – Chrugel
    Commented Mar 13, 2013 at 20:08
  • New day, new luck! First: Today I understand @mbostocks answer and finally managed to use the proper TWO flags (-s_srs AND -t_srs TOGETHER). Second: I initially worked in some exotic non-transferable (by ogr2ogr EPSG:21780) projection - after starting again with EPSG:21781 the reprojection to EPSG:4326 was possible. Hooray!
    – Chrugel
    Commented Mar 14, 2013 at 6:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.