Search code examples
svggeojsonogr2ogr

Convert svg to geojson fails with ogr2ogr


I've downloaded an .svg map of Finland from http://www.amcharts.com/svg-maps/?map=finland

I want to convert it to topojson to use it with d3.js. I first start with svg to geojson conversion, but it fails.

I tried:

ogr2ogr -f "GeoJSON" finland_kunta.json finlandHigh.svg 

The error I get:

FAILURE:
Unable to open datasource `finlandHigh.svg' with the following drivers.
  -> ESRI Shapefile
  -> MapInfo File
  -> UK .NTF
  -> SDTS
  -> TIGER
  -> S57
  -> DGN
  -> VRT
  -> REC
  -> Memory
  -> BNA
  -> CSV
  -> GML
  -> GPX
  -> KML
  -> GeoJSON
  -> GMT
  -> GPKG
  -> SQLite
  -> WAsP
  -> PCIDSK
  -> OpenFileGDB
  -> XPlane
  -> AVCBin
  -> AVCE00
  -> DXF
  -> Geoconcept
  -> GeoRSS
  -> GPSTrackMaker
  -> VFK
  -> PGDump
  -> OSM
  -> GPSBabel
  -> SUA
  -> OpenAir
  -> PDS
  -> WFS
  -> HTF
  -> AeronavFAA
  -> EDIGEO
  -> GFT
  -> GME
  -> SVG
  -> CouchDB
  -> Idrisi
  -> ARCGEN
  -> SEGUKOOA
  -> SEGY
  -> XLS
  -> ODS
  -> XLSX
  -> ElasticSearch
  -> PDF
  -> CartoDB
  -> SXF

I'm only starting to figure these tools out, so I'm not sure what exactly causes the error. When I open svg, it seems to look fine and have all the elements.


Solution

  • Actually in most cases you can use the SVG image as is, no need to convert it to topojson or GeoJSON but there are a few issues to consider:

    1. Is the original SVG file size too big for your application ? If it is then you can optimize the SVG file (For example, with: http://petercollingridge.appspot.com/svg-editor). If that's not enough then you can get a shapefile from another source (For example: http://www.naturalearthdata.com/downloads/) and convert it to GeoJSON (Use ogr2ogr, either through their web interface at http://ogre.adc4gis.com/ or you download it with GDAL at http://trac.osgeo.org/gdal/wiki/DownloadingGdalBinaries). If the resulting file is still too big you can compress with mapshaper (http://www.mapshaper.org/ - it takes both shapefiles and GeoJSON formats!) and if necessary (if your file size is still an issue) convert the resulting map from GeoJSON to topojson (see: http://github.com/mbostock/topojson/wiki).

    2. Do you need to use location data in your map ? (for example, you may need to mark where a certain city is in you map but the city is not drawn on your original SVG) Then it is easier to get a shapefile and convert it to GeoJSON (using ogr2ogr) because most likely you do not have geolocation information on the downloaded SVG file.

    3. If you're creating a choropleth and have no geolocation requirements then you can simply add the SVG directy to your page and use D3 to map data to your image! Once you have the SVG in your page then you can even manually edit all path data to include classes and ids that will make your D3 job easier.