2

I digitized an area using Google Earth and saved it into a .kml file. I want to convert this .kml file from Google Earth into a text file, in point form containing tab-delimited x and y values.

I don't know how to convert .kml into text/csv. How can I do this? Or is there another way to get x and y values from this file?

0

3 Answers 3

3

The KML should be a text file already - try opening it up in Notepad and you will see. I think opening the file in Excel and formatting there is probably your best bet (you might have to rename it to .xml)

3
  • 3
    Excel is an awful tool for manipulating XML, or text, for that matter.
    – Vince
    Commented Feb 5 at 5:11
  • 2
    Depends on how good you are with excel I suppose - Notepad ++ is also an option?
    – LeasMaps
    Commented Feb 5 at 6:55
  • 1
    And if it's a .KMZ file, just unzip it first Commented Feb 5 at 14:44
2

Although you can process the kml file as an XML document, the following procedure used entirely QGIS functionality.

You can load the .kml file directly in QGIS.

Then, Select your layer in the Layers toolbar, right mouse click, Export -> Save Features As ... . Select Format the value "Comma Separated Values" and set the filename. In section Layer Option, set "AS_XY" in field Geometry.

Now you can open the saved csv file with any text editor or Excel.

0

Programmers approach, open up a text editor

paste the following content and save it as .html (make sure that the actual file ending is ".html")

open the file with your browser, then it should be self-explanatory

(if you want me to, I can explain what this all does, but not sure if thats interesting for any non-programmer)

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>KML Coordinate Extractor</title>
</head>
<body>
  <input type="file" id="kmlFileInput" accept=".kml">
  <button onclick="extractCoordinates()">Extract Coordinates</button>

  <script>
    function extractCoordinates() {
      const kmlFileInput = document.getElementById('kmlFileInput');
      const kmlFile = kmlFileInput.files[0];
      if (!kmlFile) {
        alert("Please select a KML file.");
        return;
      }

      const reader = new FileReader();
      reader.onload = function(event) {
        const kmlContent = event.target.result;
        const parser = new DOMParser();
        const xmlDoc = parser.parseFromString(kmlContent, "text/xml");
        const coordinates = [];

        const coordinatesNodes = xmlDoc.querySelectorAll("coordinates");
        coordinatesNodes.forEach(coordNode => {
          const coordStr = coordNode.textContent.trim();
          const coordList = coordStr.split(/\s+/);
          coordList.forEach(coord => {
            coordinates.push(coord);
          });
        });

        const outputContent = coordinates.join("\n");
        const outputFileName = "coordinates.txt";
        download(outputContent, outputFileName, "text/plain");
      };

      reader.readAsText(kmlFile);
    }

    function download(content, filename, contentType) {
      const a = document.createElement("a");
      const file = new Blob([content], { type: contentType });

      a.href = URL.createObjectURL(file);
      a.download = filename;
      a.click();

      URL.revokeObjectURL(a.href);
    }
  </script>
</body>
</html>

My test kml was

    <?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>Polygon ohne Namen.kml</name>
    <StyleMap id="m_ylw-pushpin">
        <Pair>
            <key>normal</key>
            <styleUrl>#s_ylw-pushpin</styleUrl>
        </Pair>
        <Pair>
            <key>highlight</key>
            <styleUrl>#s_ylw-pushpin_hl</styleUrl>
        </Pair>
    </StyleMap>
    <Style id="s_ylw-pushpin">
        <IconStyle>
            <scale>1.1</scale>
            <Icon>
                <href>http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png</href>
            </Icon>
            <hotSpot x="20" y="2" xunits="pixels" yunits="pixels"/>
        </IconStyle>
    </Style>
    <Style id="s_ylw-pushpin_hl">
        <IconStyle>
            <scale>1.3</scale>
            <Icon>
                <href>http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png</href>
            </Icon>
            <hotSpot x="20" y="2" xunits="pixels" yunits="pixels"/>
        </IconStyle>
    </Style>
    <Placemark>
        <name>Polygon ohne Namen</name>
        <styleUrl>#m_ylw-pushpin</styleUrl>
        <Polygon>
            <tessellate>1</tessellate>
            <outerBoundaryIs>
                <LinearRing>
                    <coordinates>
                        6.88497484597166,46.75877722587381,0 7.421167215232956,46.78825491317144,0 7.810778749838581,47.17899958496335,0 7.30216390992223,47.55564166585733,0 6.623527739724664,47.10104241420296,0 6.88497484597166,46.75877722587381,0 
                    </coordinates>
                </LinearRing>
            </outerBoundaryIs>
        </Polygon>
    </Placemark>
</Document>
</kml>

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.