Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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.

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

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

    </style>
</head>
<body>
<script>

    var width = 600,
        height = 600;

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

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

    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];

        svg.append("path")
           .datum(units)
           .attr("class", "unit")
           .attr("d", path);
    });

</script>
</body>

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

EDIT - SOLUTION

  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
    

alternative:

     ogr2ogr "GEOJSON" -s_srs EPSG:21781 -t_srs EPSG:4326 outfile.geojson infile.shp
share|improve this question
    
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 Mar 13 '13 at 18:33
add comment

1 Answer 1

up vote 4 down vote accepted

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.

share|improve this answer
    
Thank you - I am still struggling. I am doing: 'ogr2ogr -f geoJSON -a_srs hexmap.prj hexmap_fix.geojson hexmap.shp' –  Chrugel Mar 13 '13 at 19:49
    
sry, on output is -t_srs, but I am wrong anyway... –  Chrugel Mar 13 '13 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 Mar 13 '13 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 Mar 14 '13 at 6:51
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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