Main Content

Create Base Layer from OpenStreetMap Data

Since R2023b

When you are in an offline environment, you can create provide geographic context for your data by creating a simple base layer from data stored in an OpenStreetMap® file. This example shows how to create a base layer for several city blocks in Shibuya, Tokyo, Japan by reading building and road data from the file.

Read Building Data

Read the buildings layer from an OpenStreetMap file [1] by using the readgeotable function. The function derives building information from the file and stores the result in a geospatial table. The table represents the buildings using polygon shapes in geographic coordinates.

filename = "shibuya.osm";
buildingsLayer = readgeotable(filename,Layer="buildings");

Read Road Data

The lines layer of an OpenStreetMap file represents line-like features such as roads, sidewalks, and subway routes. Read the lines layer from the file into a geospatial table. The table represents the lines using polygon shapes in geographic coordinates.

linesLayer = readgeotable(filename,Layer="lines");

A table row represents a principal road when the highway table variable has a value of "motorway", "trunk", "primary", "secondary", "tertiary", or "residential". Create a new geospatial table from the table rows that represent principal roads.

roadtypes = ["motorway","trunk","primary","secondary","tertiary","residential"];
rows = ismember(linesLayer.highway,roadtypes);
roads = linesLayer(rows,:);

Display Base Layer

Set up a new map using a projected coordinate reference system (CRS) that is appropriate for Japan. Provide geographic context for the area by displaying the buildings and roads.

proj = projcrs(32654);

hold on
title("Buildings and Principal Roads")

Load and Plot Data

Read a driving track from a GPX file into a geospatial table. Extract the coordinates from the table and display the track on the map.

trk = readgeotable("shibuya_track.gpx",Layer="track_points");

[1] You can download OpenStreetMap files from, which provides access to crowd-sourced map data all over the world. The data is licensed under the Open Data Commons Open Database License (ODbL),

See Also


Related Topics