Resources‎ > ‎Project Tutorials‎ > ‎

Convert Shapefiles to KML

As noted in the Desktop GIS and Web Mapping sections, there are several reasons in this project to convert shapefiles to KML files after participants have created their GIS products using QGIS. 

The first is that the KML files, suitably styled and reorganized, can then be zipped to form kmz files.  These can then be provided as downloads at the participants' website for members of the public to use on their home PCs.  Assuming that Google Earth is installed on a PC, double-clicking on the kmz file will display the layer on Google Earth.  

Attribute information for each feature will be displayed as a generic table in a balloon by clicking on the feature.  You can style the generic table to display something more attractive and formatted more informatively by using the various styling options for KML files.

A second reason is that a KML file can be used as a vector data layer in both simple and GeoServer-based web maps using Open Layers, and also in mashup-type web maps.  In a web map served by QGIS server, KML files can also substitute for shapefiles and other vector layers.

There are several ways to convert shapefiles to the KML file format, using the open source desktop GIS tools downloaded earlier.  We will use QGIS, since the shapefiles are already loaded onto QGIS as part of a QGIS project.  As an alternative, we will introduce the use of the command-line OGR2OGR utility of the GDAL Library.

Option 1: Using QGIS to Convert Shapefiles to KML

1.  Fire up QGIS.  In the "Layer" menu, click on the "Save as..." item.

[Click image at right to enlarge.]

2.  In the "Save vector layer as..." dialogue that pops up, select "Keyhole Markup Language [KML]" from the Format drop-down menu.  

Then, in the "Save as" text box, use the "Browse" button to navigate to the folder of your choice, and name under which the KML file will be saved.

Specify "WGS 84" as the CRS (coordinate reference system).  KML files are assumed to be in the WGS 84 datum/geographic coordinate system, so we have to save the converted file in that geographic coordinate system.  The press "OK"

3.  You'll get a message that the file conversion was completed.  And you're done!

[Click image at right to enlarge.]

Option 2: Using the Command-line Utility ogr2ogr to Convert Shapefiles to KML

1.  Find the "OSGeo4W" command line window shortcut among the various desktop shortcuts that the OSGeo4W installer left on your desktop when you installed the open source desktop GIS software.  Click on it to get the command window.

2.  The command (DOS) window.  This is where you type in commands that invoke ogr2ogr and provide the command-line parameters.

[Click image at right to enlarge.]

The command line reference is at

3.  [Click image at right to enlarge.]  In the command line illustrated in the figure at right, the command runs an instance of the ogr2ogr utility, passing in the following parameters:
  1. output file format of KML
  2. output file name and location
  3. input file name and location
  4. output file datum/projection
Upon hitting "Enter" after entering the parameters, a new prompt appears, signalling the completion of the command.

After the KML file has been created, the file must still be processed using a text editor to:
  • consolidate the various data tags and styling information as much as possible using "style URLs" and data schema (if an attribute table is being read into the KML file from the shapefile);

  • consolidate as much as possible the geometry items belonging to a single feature by using <MultiGeometry> tags;

  • enter HTML text between <![CDATA[ and ]]>  tags in <description> text items;

  • for KMZ file usage, deploy <balloonStyle> tags to format the output information balloons that display the attribute data upon clicking the features in Google Earth,
among other things, to make the KML file as compact and as user-friendly as possible (with future file maintenance in mind).