Home / Coordinates and Location Formats

How to Get Coordinates From a KML File: Text Editor, Google Earth, QGIS, and Code

A crescent view of Earth at night with city lights and vast oceans

Photo: Zelch Csaba / Pexels

Someone sends you a KML file (a route from a colleague, an export from a GPS app, a boundary from a planning portal) and what you actually need is the numbers: the latitude and longitude of the points inside it. Good news: KML is one of the friendliest formats in mapping. It is plain text XML, the coordinates are not encoded or compressed, and you can read them with nothing but a text editor. This guide starts with the one fact that prevents the classic error, then extracts coordinates by hand, with Google Earth, with QGIS, and with code.

Interactive tool

Pull Coordinates Out of a KML File

Check off each step as you finish it. Your progress stays on this page.

0 of 6 complete

0% done

First, the Fact That Prevents Every Error: KML Is Longitude First

Open any KML file and find a <coordinates> element. It contains tuples in this order:

longitude, latitude, altitude

That is the reverse of the order most people say coordinates in, and it is the single most common KML mistake: reading -0.1276, 51.5072 (a point in London) as latitude -0.1276, longitude 51.5072, which lands in the Indian Ocean. KML inherited its order from the x, y convention of geometry: longitude is the x-axis value, latitude the y. Altitude, in meters, is optional and often absent or zero. Multiple points (in a LineString route or a Polygon ring) appear as tuples separated by spaces, sometimes spread across lines, in the same lon, lat, alt pattern throughout. Everything in this article follows from reading that element correctly. Our companion guide on converting GeoJSON to KML covers the same convention from the other direction, since GeoJSON is longitude-first too.

If the File Is a KMZ, Unzip It First

A KMZ is simply a KML file zipped up, usually with any attached images or icons bundled alongside. If your file ends in .kmz, rename a copy to .zip (or open it with any archive tool) and extract it. Inside you will find the main file, almost always named doc.kml. That is the file every method below works on. Google Earth and QGIS open KMZ directly, so unzipping is only necessary for the text editor and code methods, but it is worth knowing the trick because a KMZ otherwise looks like opaque binary to anyone who tries to peek inside it.

Method 1: Read It in a Text Editor (Fastest for a Few Points)

  1. Open the .kml file in any text editor (Notepad, TextEdit, VS Code, or a browser).
  2. Search for <coordinates>. In a small file there will be one block per feature.
  3. Read the tuple: the first number is longitude, the second is latitude, the third, if present, is altitude in meters.
  4. For a Point placemark, that single tuple is your answer. For a LineString or Polygon, you get the full vertex list in order, which is exactly what you want for a route or boundary.

Context clues help you pick the right block in a busy file: each coordinates element sits inside a Placemark whose <name> appears nearby, so search for the place name first, then read the next coordinates block below it. This method needs no software and no conversion, and for one or two points it is genuinely the fastest path in this article.

Earth at night seen from space, city lights tracing the continents
Earth at night seen from space, city lights tracing the continents

Method 2: Open It in Google Earth

If you want the coordinates on a map rather than in text, open the file in Google Earth (it accepts KML and KMZ natively, by double-click or drag and drop). The file's placemarks appear in the Places panel and on the globe. For a point placemark, right-click it and open its properties: the latitude and longitude fields show the position in the display format you have set under Tools, Options (decimal degrees is the useful one). For lines and polygons, hovering the cursor shows the position readout in the status bar at the bottom of the window, which is the practical way to sample vertices visually. Google Earth is the verification method: extract numbers by any other route in this article, then drop them here to see the point sitting where you expected. A point in the ocean when you expected London means the order trap got you; swap the pair and check again.

Method 3: QGIS, Including a Full CSV of Every Placemark

For a file with many features, QGIS turns extraction into an export:

  1. Drag the KML into QGIS (or Layer, Add Layer, Add Vector Layer). KML files often contain several geometry types; QGIS offers them as separate layers.
  2. To inspect individual features, use the Identify tool and click, or open the attribute table and enable geometry display.
  3. To export every coordinate, right-click the layer, Export, Save Features As, choose CSV, and in the layer options set GEOMETRY = AS_XY (for points) so the output includes X (longitude) and Y (latitude) columns. For lines and polygons, export as GeoJSON or use the geometry in WKT form instead, since a shape with many vertices does not fit one row sensibly.

This is the method that scales: a thousand placemarks become a spreadsheet in one operation, ready for whatever the coordinates were actually for. Our QGIS beginner material covers adding and styling layers if the interface is new to you.

Method 4: Parse It in Python

KML is XML, so a few lines of Python extract every coordinate pair without any GIS library:

```python

import re

text = open('doc.kml', encoding='utf-8').read()

for block in re.findall(r'<coordinates>(.*?)</coordinates>', text, re.S):

for tup in block.split():

parts = tup.split(',')

lon, lat = float(parts[0]), float(parts[1])

alt = float(parts[2]) if len(parts) > 2 else None

print(lat, lon, alt)

```

For structured work (names attached to points, styles, folders), use a real parser: the built-in xml.etree.ElementTree handles KML's structure, remembering that KML elements live in the http://www.opengis.net/kml/2.2 namespace, and dedicated libraries such as fastkml model placemarks directly. The command-line alternative is GDAL's ogr2ogr, which converts KML to CSV, GeoJSON, or a shapefile in one command and is the right tool when this job recurs weekly. Whichever route you choose, print the first result and sanity check it on a map before processing the file's thousand siblings; a swapped pair caught on point one is a lesson, and caught on point one thousand is a project delay.

Which Method Should You Use?

One or two points and no patience: the text editor. A visual check or a presentation: Google Earth. Many features, or coordinates destined for a spreadsheet or another GIS: QGIS with the CSV export. Automation, pipelines, or a folder full of KML files arriving every week: Python or ogr2ogr. The methods agree because they all read the same element; pick by volume and by what the numbers are for, not by which tool you happen to have open. And whichever you pick, keep the order mantra in front of you the whole time: KML speaks longitude first.

Know Your Geometry: What the Coordinates Element Is Describing

The same element does four jobs in KML, and knowing which one you are reading prevents misreading a boundary as a route. A Point holds exactly one tuple: the location of a placemark. A LineString holds a sequence of tuples that form a path, in walking order, which is what GPS track exports and drawn routes produce. A Polygon wraps its coordinates in a LinearRing inside boundary elements, and the ring closes on itself: the first and last tuples are identical, a detail that confuses people counting vertices, who find one more point than they drew. A MultiGeometry bundles several of the above under one placemark, so a single named feature can own a point and a polygon at once, and its coordinates appear in separate blocks. When you extract from a real-world file, expect a mixture, and let the surrounding tags tell you what each block is. The placemark's name, found just above its geometry, is the label that makes a raw list of numbers into a usable dataset, so extract names alongside coordinates whenever the file has more than a handful of features; a spreadsheet of anonymous points is a riddle you set for your future self.

Precision, Altitude, and the Fine Print

Three details separate a careful extraction from a sloppy one. Precision: KML coordinates are usually written to six or more decimal places. Six decimal places of latitude is about a tenth of a meter, far finer than the GPS that recorded most files, so trailing digits are arithmetic, not accuracy; do not quote a trailhead to the centimeter because the file had the digits. Altitude: the third value is in meters, but its meaning depends on the file's altitudeMode: clampToGround ignores it entirely (the common case in casual exports, where the altitude is often a row of zeros), while absolute altitude is height above sea level. A column of zeros means "not recorded," not "at sea level," and treating it as real elevation data corrupts any analysis downstream. Order in exports: when you convert KML onward (to CSV for a spreadsheet, or into another GIS), check the output's axis order rather than assuming it: most tools write X as longitude to match KML, but some regional and legacy pipelines flip to latitude first, and the swap is silent. The ocean test from the FAQ is the universal detector: plot one known point from every new dataset before you trust the batch.

Related Guides

The closest companions to this guide are the coordinates guides below. Each one picks up a thread this page starts:

Frequently Asked Questions

What order are coordinates in a KML file?

Longitude, latitude, then altitude if present. This is the reverse of how people usually say coordinates, and misreading the order is the most common KML error. A London point appears as -0.1276, 51.5072: longitude first.

How do I open a KMZ file to see its coordinates?

A KMZ is a zip archive. Rename a copy to .zip and extract it to find doc.kml inside, then use any method in this guide. Google Earth and QGIS also open KMZ directly without unzipping.

Can I get coordinates from KML without any GIS software?

Yes. KML is plain XML text. Open it in a text editor, find the coordinates element, and read the tuples directly: first number longitude, second latitude. For a single point this is the fastest method of all.

How do I export all placemark coordinates from a KML to a spreadsheet?

Load the KML in QGIS, right-click the layer, Export, Save Features As CSV, and set the GEOMETRY layer option to AS_XY. The CSV gains X and Y columns (longitude and latitude) for every point feature.

Why do my extracted coordinates land in the ocean?

You almost certainly read them as latitude, longitude. KML stores longitude first. Swap the pair and the point should land where you expected; verify in Google Earth before using the data further.

Photo credits: Zelch Csaba (Pexels); Zelch Csaba (Pexels). Photos are served from this site; follow the credit to the library page for the original.