4

I'm using the 1:50m Cultural Vectors shape file from naturalearthdata.com.

I use ogr2ogr to create a geoJson file with the following command:

ogr2ogr -f GeoJSON geo_world_50m.json ne_50m_admin_0_countries.shp

I then create a topoJson file with this command:

topojson --id-property iso_n3 -p name=admin -p name -p iso_a3=iso_a3 -p iso_a3 -o topo_world_50m.json geo_world_50m.json

Once I have my topoJson file, I load it in to Leaflet:

$.getJSON('topo_world_50m.json', function (data) {
    var country_geojson = topojson.feature(data, data.objects.geo_world_50m);
    country_layer.addData(country_geojson);
});

I've tried the 1:50m file as well as the 1:10m file from Natural Earth. Both give me this section of Russia that is reversed at the Finland border.

section of Russia that is reversed at the Finland border

Any ideas how to address this? Thanks

GrantE
  • 159
  • 1
  • 7

2 Answers2

3

So... this is a known issue on leaflet, I solved this way:

    function onEachShapeFeature(feature, layer){
        var bounds = layer.getBounds && layer.getBounds();
        // The precision might need to be adjusted depending on your data
        if (bounds && (Math.abs(bounds.getEast() + bounds.getWest())) < 0.1) {
            var latlongs = layer.getLatLngs();
            latlongs.forEach(function (shape) {
                shape.forEach(function (cord) {
                    if (cord.lng < 0) {
                        cord.lng += 360;
                    }   
                }); 
            }); 
            layer.setLatLngs(latlongs);
        }
    }
    var countries = L.geoJson(data, {
            onEachFeature: onEachShapeFeature,
    });

I know that is hacky... but was the best way I found.

Joac
  • 492
  • 3
  • 12
1

What happens if you use the geoJson? For admin 0 level geographies like this (country level) a geoJson might suffice in terms of detail. It sounds like something is being lost when you go from geo -> topo?

Ju66ernaut
  • 2,592
  • 3
  • 23
  • 36
  • You are probably right about the issue happening when I convert to topo. The topo files are so much smaller that it makes the extra step worth it. As a temporary solution, I've filtered out Russia and Fiji (has the same issue), since I don't need their shapes for this project. – GrantE Feb 19 '14 at 19:40
  • Converting directly from shp to topojson also has this issue. – GrantE Mar 07 '14 at 17:44
  • using a geojson file fixes it. – GrantE Mar 07 '14 at 18:33
  • Good to hear! Good luck. – Ju66ernaut Mar 07 '14 at 18:37