OSM XML Guide: Nodes, Ways, and Relations Explained

The OpenStreetMap (OSM) XML data format is a structured schema used to store geospatial data via three core primitives: nodes, ways, and relations. These elements, enriched with key-value metadata tags, define points, lines, boundaries, and complex geographical relationships globally. This article breaks down the exact XML structure and attributes used to represent nodes, ways, relations, and tags in OpenStreetMap data files.

The Root Element

Every OSM XML file begins with the root <osm> element, which specifies metadata such as the API version, generator software, and copyright details. All geographic elements exist inside this container.

<?xml version="1.0" encoding="UTF-8"?>
<osm version="0.6" generator="CGImap 0.8.8">
  <!-- Nodes, Ways, Relations go here -->
</osm>

Nodes (<node>)

A node represents a single point on the Earth’s surface defined by latitude and longitude coordinates. Nodes are used either as standalone points of interest (POIs) or as vertex components within ways.

Key Attributes:

XML Representation:

<node id="12345678" lat="51.5074" lon="-0.1278" version="1" timestamp="2023-01-01T00:00:00Z" changeset="1" user="MapperName" uid="1001">
  <tag k="amenity" v="cafe"/>
  <tag k="name" v="Central Coffee"/>
</node>

If a node is purely structural (part of a way without individual POI metadata), it contains no child <tag> elements.


Ways (<way>)

A way is an ordered sequence of 2 to 2,000 nodes that represents linear features (such as roads, paths, and rivers) or area boundaries (such as buildings, forests, and lakes) when closed.

Key Characteristics:

XML Structure:

Ways contain <nd> (node reference) child elements defining geometry in order, followed by <tag> elements describing the feature.

<way id="98765432" version="2" timestamp="2023-01-02T12:00:00Z" changeset="2" user="MapperName" uid="1001">
  <nd ref="12345678"/>
  <nd ref="12345679"/>
  <nd ref="12345680"/>
  <nd ref="12345678"/> <!-- Identical first/last node creates a closed way -->
  <tag k="building" v="yes"/>
  <tag k="addr:housenumber" v="42"/>
</way>

Relations (<relation>)

A relation is a container element used to model complex relationships between multiple nodes, ways, and even other relations. Common use cases include transit routes, turn restrictions, and multipolygons (areas with inner holes or multiple disjoint outer perimeters).

XML Structure:

Relations contain <member> child elements along with <tag> metadata: * type: The primitive type of the member (node, way, or relation). * ref: The ID of the referenced element. * role: A string defining the member’s function within the relation (e.g., outer, inner, stop, via).

<relation id="55443322" version="1" timestamp="2023-01-03T15:30:00Z" changeset="3" user="MapperName" uid="1001">
  <member type="way" ref="98765432" role="outer"/>
  <member type="way" ref="98765433" role="inner"/>
  <tag k="type" v="multipolygon"/>
  <tag k="landuse" v="forest"/>
</relation>

Tags (<tag>)

Tags are child elements that attach semantic meaning to nodes, ways, or relations. They are structured as simple key-value string pairs using two attributes: * k: The tag key (the category or property, such as highway, name, or maxspeed). * v: The tag value (the specific property value, such as residential, Main Street, or 50).

<tag k="highway" v="residential"/>
<tag k="surface" v="asphalt"/>

Any OSM primitive without <tag> elements exists solely to provide geometry or structure for higher-level elements.