6

I'm using QGIS. I'm trying to either edit a .kml file someone else created in Google Earth or convert it to another editable format (like a .shp file). I've read the following threads without really being able to figure out what the solution is:

Is there a way to export QGIS layers to KML? (seems like this plugin doesn't exist? couldn't find it)

How to convert KML to shapefile without losing attributes using QGIS? (I tried the zonums converter mentioned in the answers section, but I lose most of the data in the original .kml file)

Converting KML to shapefile via ogr2ogr including all attributes (I'm enough of a newbie that I didn't really understand this thread)

I've also tried the kml to csv converter here http://www.gps-data-team.com/convert.php. Again, it strips most of the data, giving me only the points and names.

Edited to add: saving my .kml file as a .shp file does not work. I get the following error: Export to vector file failed. Error: creation of data source failed (OGR error:Failed to open shapefile .shp/.shp. It may be corrupt or read-only file accessed in update mode. )

Here is a snippet from the beginning of the file. I'm guessing by the comments below, it's just really poorly organized. You have these pieces with the pushpin markers:

    <?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<Document>
<name>Libanius&apos; Letters, Cribiore.kml</name>
<open>1</open>
<Style id="sn_ylw-pushpin00">
</Style>
<Style id="sh_ylw-pushpin2">
    <IconStyle>
        <scale>1.2</scale>
    </IconStyle>
</Style>
<StyleMap id="msn_grn-pushpin">
    <Pair>
        <key>normal</key>
        <styleUrl>#sn_grn-pushpin0</styleUrl>
    </Pair>
    <Pair>
        <key>highlight</key>
        <styleUrl>#sh_grn-pushpin</styleUrl>
    </Pair>
</StyleMap>
<Style id="sn_wht-pushpin">
    <IconStyle>
        <scale>1.1</scale>
        <Icon>
            <href>http://maps.google.com/mapfiles/kml/pushpin/wht-pushpin.png</href>
        </Icon>
        <hotSpot x="20" y="2" xunits="pixels" yunits="pixels"/>
    </IconStyle>
</Style>
<Style id="sn_grn-pushpin0">
    <IconStyle>
        <scale>1.1</scale>
        <Icon>
            <href>http://maps.google.com/mapfiles/kml/pushpin/grn-pushpin.png</href>
        </Icon>
        <hotSpot x="20" y="2" xunits="pixels" yunits="pixels"/>
    </IconStyle>
</Style>

....

Then about halfway through, there's a folder tag that contains the place marks :

    <Folder>
    <name>Libanius&apos; Letters, Cribiore</name>
    <description>Exported on Mon Oct 01 2012 16:11:04 GMT-0700 (PDT)</description>
    <Placemark id="18">
        <name>Antioch</name>
        <description>Home</description>
        <styleUrl>#msn_star</styleUrl>
        <ExtendedData>
            <Data name="searchrename">
                <value>Antiochia ad Orontem</value>
            </Data>
            <Data name="en_name">
                <value>Theoupolis</value>
            </Data>
            <Data name="featuretyp">
                <value>settlement</value>
            </Data>
            <Data name="pid">
                <value>658381</value>
            </Data>
            <Data name="gr_name">
                <value>Ἀντιόχεια ἡ Μεγάλη</value>
            </Data>
            <Data name="la_name">
                <value>Theoupolis</value>
            </Data>
            <Data name="timeperiod">
                <value>AHRL</value>
            </Data>
            <Data name="perseus_li">
                <value>http://www.perseus.tufts.edu/hopper/text?doc=Perseus%3Atext%3A1999.04.0006%3Aalp</value>
            </Data>
            <Data name="wiki_link">
                <value>http://en.wikipedia.org/wiki/Antioch</value>
            </Data>
            <Data name="path">
                <value>/places/658381</value>
            </Data>
            <Data name="x1">
                <value>36.15818</value>
            </Data>
            <Data name="y1">
                <value>36.202097</value>
            </Data>
        </ExtendedData>
        <Point>
            <coordinates>36.15818,36.202097,0</coordinates>
        </Point>

.....

I tried deleting all the pushpin crap in Oxygen and then reopening the layer in QGIS. The new kml file opens, and the points are there with the data I want. Still not editable, and when I try to save as shapefile I get: "Export to vector file failed. Error: creation of layer failed (OGR error:Failed to open Shapefile `LibaniusShapefile.shp'. )

I think I'm not sure what a well-formed KML file should look like.

Thanks.

7
  • add qgis2kml: you can find it here: plugins.qgis.org/plugins/qgis2kml/version/0.2. its status is experimental, so fetching it in qgis requires to select "any status". but from the description: This plugin convert a vector file loaded into QGIS to kml with style. So this plugin seems not doe what you want
    – Kurt
    Commented Oct 14, 2012 at 5:29
  • 1
    QGIS can open KML files and save them as shapefiles. I attempted this just now and didn't see any loss of attributes. Does this not work for you?
    – L_Holcombe
    Commented Oct 14, 2012 at 6:46
  • Converting to a shapefile is your best bet. How much control of the original kml do you have?
    – Willy
    Commented Oct 14, 2012 at 10:11
  • @Willy: edited original question to address the the first part of your comment. The original kml was created by someone who is a friend but knows less about this than I do -- 1st time she's created a kml file in GE and she's never used QGIS; I'm trying to help her. Commented Oct 14, 2012 at 18:49
  • "Failed to open shapefile .shp/.shp" looks like you did not gave a filename for the output. After deleting the blanks in the first line and adding </Placemark> </Folder> </Document> </kml> to your snippet, I managed to open the file in Qgis and GE, but lost the attribute data (also in GE). No problem on saving with me.
    – AndreJ
    Commented Oct 15, 2012 at 11:18

3 Answers 3

5

I'd recommend ogr2ogr. But your success with any method will depend on the kml file itself: kml is a hierarchical file format which can contain different geometry types with each a different set of attributes. Unless your KML file is well structured, you can not convert it to a shapefile (with one geometry and the same attributes everywhere). Perhaps you should check that first in google earth? Which format do your attributes have (extended data, xml,...)? You can post a part here for more help (ctrl copy from google earth and search for relevant bits).

4
  • To clarify structure, an ideal structure would be to have only polygons in one file, lines in another, points in a third kml file. Some key field names in kml are 'name' and 'description' If you are saving from a shapefile to kml, naming two of the attibutes 'name, description' will often help to get a successful export.
    – Willy
    Commented Oct 14, 2012 at 10:10
  • Thx. I am a novice. I still am unsure how to post part of the file here despite your effort to provide instruction. The kml file has name, description, timestamp, begin, end, altitudeMode, tessellate, extrude, visibility, searchrename, en_name, featuretyp, pid, gr_name, la_name, timeperiod, perseus_li, wiki_link, path, x1, y1 fields. I want to keep data from description, timeperiod, perseus, wiki, and all name fields. Metadata in preferences says the only type of feature is a point. When I try to save as shape file I get Export to vector file failed Error: creation of data source failed... Commented Oct 14, 2012 at 18:42
  • Figured out how to post a couple of snippets. Thx Commented Oct 14, 2012 at 19:09
  • Finally got it working: OSGeo4W or the MS4W package contain a gdal version 1.9.2 with libkml. ogr2ogr kmlexport.shp -lco ENCODING=UTF-8 kmltest1.kml creates a shapefile readable by Qgis Lisboa, with correct greek letters.
    – AndreJ
    Commented Oct 17, 2012 at 15:46
3

A XSLT transformation from KML to GML might solve your problem. GML attributes can be read completely by Quantum GIS. KML and GML are both XML files, so transformation should be possible.

Here

http://spanring.eu/blog/2005/12/11/kml2gml/

https://stackoverflow.com/questions/10195399/transforming-xml-formerly-kml-using-xslt

are some older examples, but not yet knowing ExtendedData type. I guess for someone familiar with XSLT it should not be too difficult. Maybe someone at stackoverflow can help you with this.

2

I think I finally answered my own question. I don't know XSLT well enough to try that option, and the KML file it turns out was really messed up. I suspect that is why "save as" did not work, and the online converters didn't work.

I opened the file in Oxygen, edited out lots of extraneous information at the top and bottom of the file, and then made sure the file was well structured, with the same number of subfields or attributes (sorry, I don't know all the terminology) per data point. I then opened this new file in QGIS and it worked. I could save it as a .shp file and edit the attributes table.

It was time intensive, though. It's not an ideal strategy for a large file. I had few data points. I wonder why the Google Earth .kml file was so poorly structured.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.