Exporting vector layer as kml in QGIS

In my previous post I described how to export a vector layer from GRASS GIS as kml file. The key was to use the dsco parameter to define the columns that should be used for the Name and Description fields in your kml file.

In QGIS you use the ‘save as’ option in QGIS. You can get this using the context menu (right click mouse on the layer) or the menu ‘Layer | Save as..’. In the menu that opens, there are two fields under the header ‘OGR creation options’; ‘Data source’ and ‘Layer’.

In the ‘Data source’ field, you can define which columns you want to use to fill the Name and Description fields in your kml file. You do this by defining the NameField and the DescriptionField. For example, NameField=PNVname and DescriptionField=PNVcode.

Note in the example above that each option should be entered on a new line.

Like in the v.out.ogr function in GRASS GIS, the ‘save as’ function in QGIS offers an interface to the underlying OGR library; and more specifically to the ogr2ogr function. The dsco parameter (OGR creation options) are format specific, here is a more detailed explanation when exporting to KML format. For other formats, see here.

One potential problem I just noticed is that all fields in the attribute table of the original vector layer are exported to the kml layer as ExtendedData. QGIS ignores this data when you open the attribute table of the kml file. But you can see the extended data is there when opening the kml file in a text editor or Google earth. If you do not need this information to be included in your kml file, better remove it as it adds to the size of your kml file. You can delete the extended data using the following grep statement on the command line:

grep -Ev ‘|SchemaData’ doc.kml >> doc2.kml

Here, grep returns all lines in doc.kml to the new file doc2.kml, except those lines containing the text ‘<SimpleData’, ‘ExtendedData>’ or ‘SchemaData’, which are all lines defining the extended data.

Edit: The OGR function ogr2ogr does offer the option to define which columns/ fields from the input layer should be included in the new layer. See also this question on the GIS stack exchange site.


15 thoughts on “Exporting vector layer as kml in QGIS

  1. Pingback: Exporting point file as kml in GRASS GIS | Ecostudies

  2. Pingback: Exporting vector layer as kml in QGIS | Ecostud...

      1. punditsguide

        Obviously I did read the documentation first, or I would not have had to engage is such a broad search for the answer.

        The reason you would want to do that is to substantially reduce the size of a KML file below external limits, such as those imposed by the Google Maps API, in cases where digits 7 through 14 give no real extra precision (such as with administrative boundaries in many kinds of analysis). The boundary is down the middle of the street for electoral boundaries. You are not going to increase the voting population if it is a centimetre off to one side of the yellow line or another, but meanwhile it would let you show many more districts on your map.

        There is an option to specify COORDINATE_PRECISION=6 as a Layer option when saving to GeoJSON, but not for KML. At least that’s the case for QGIS 1.7.3, and seems to be the case with OGR2OGR versions through the one bundled with FWTools 2.4.latest.

        I thought someone more experienced than me might have some clever workaround. Anyone?

      2. pvanb

        Fair enough. I wouldn’t know, but hopefully somebody will come up with a solution. If I come across one I’ll post it here.

      3. Thanks. It all seems to boil down to doing some further manipulation on the output one way or the other, which I already have a handle on. Worth a try, though. Cheers!

  3. Narelle

    Limiting the decimal places makes the resulting file smaller.
    I would like to be able to do this for save as geojson also.

  4. Freddy

    Many many thanks pvanb!!!
    I was looking for this brief, clear and precise answer during almost 12 hours.
    You are an great pegagoque for newbies like us.
    The strength of people like you is to make available, things can seem complex.
    I will add you in my little notes.
    Many thanks again pvanb!

  5. Do you know how to select columns to export when saving as GeoJSON?
    I’ve tried many things:
    All with and without the “skip attributes creation” checkbox checked.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s