Converting KML files to shapefiles with extra data

GIS data comes in tons of different formats (and some formats that are the same, but with different CRSs, etc.), and often you have to convert one to the other. The ogr2ogr tool that comes with GDAL is basically the GIS version of this, except actually useful.

However, when you have a KML file with ExtendedData tags like this:

    <SchemaData schemaUrl="#FED_35_final">
        <SimpleData name="FEDNUM">35032</SimpleData>
        <SimpleData name="ED_NAMEE">Guelph</SimpleData>
        <SimpleData name="ED_NAMEF">Guelph</SimpleData>

and you try to convert it to a shapefile, it may not work. The reason is that you need to have GDAL built with LibKML support. If you do, you’ll see something like this in the output of ogrinfo --formats:

  -> "LIBKML" (read/write)
  -> "KML" (read/write)

If not, you’ll just see the entry for “KML”. Note that here “KML” refers to GDAL’s builtin KML support, which should always be there, but does not support extended data attributes. The Arch packages for GDAL don’t have LibKML support enabled, and it turns out that GDAL needs to have a more recent version of LibKML. So I adapted an existing AUR package to make libkml-git and put it on the AUR. To get GDAL to use it, simply build one of the GDAL packages on the AUR and edit the PKGBUILD to add --with-libkml and libkml to the depends field.

Hopefully that’s helpful to someone down the road.

Posted January 26, 2014 under code, planetkde.