Function Repository Resource:

OSMImport

Source Notebook

Import OSM (OpenStreetMap) data from XML files or the OSM API

Contributed by: Christopher Wolfram

ResourceFunction["OSMImport"][path]

imports the OSM XML file at path.

ResourceFunction["OSMImport"][GeoBoundsRegion[]]

imports OSM data within the specified region.

ResourceFunction["OSMImport"][entity]

imports OSM data within the bounding box of entity entity.

Details and Options

ResourceFunction["OSMImport"] outputs an association containing data on "Nodes” and "Ways". "Nodes" represent places, buildings, streetlamps, etc. while "Ways" represent roads, bike paths, sidewalks, etc.
Every node and way has a unique ID.
"Nodes" and "Ways" contain a "Tags" field that contains information about the type of node or way.
The "Nodes" field of each way contains the IDs for the sequence of nodes that make up that way.
When not importing from a path, ResourceFunction["OSMImport"] will use the OSM API.
The OSM API will not allow requests containing more than 50,000 nodes or spanning more than 0.5 degrees latitude and longitude.
For larger regions, OSM XML should be downloaded separately and then imported from a file.
ResourceFunction["OSMImport"][entity] uses GeoBounds to find the bounding box of entity.
ResourceFunction["OSMImport"] returns all ways that intersect the specified bounding box.

Examples

Basic Examples (2) 

Import OSM data from a region:

In[1]:=
osm = ResourceFunction["OSMImport"][
   GeoBoundsRegion[{{40.0923, 40.0991}, {-88.2586, -88.2479}}]];

Visualize the ways:

In[2]:=
GeoGraphics[
 Values[Line[Values[osm[["Nodes", #Nodes, "Position"]]]] & /@ osm["Ways"]]]
Out[2]=

Scope (3) 

Use the "Tags" field to select only rivers:

In[3]:=
osm = ResourceFunction["OSMImport"][
   GeoBoundsRegion[{{40.0111, 40.0526}, {-88.0891, -88.0180}}]];
In[4]:=
Select[osm["Ways"], #[["Tags", "waterway"]] === "river" &][[All, "Nodes"]] // Short
Out[4]=
In[5]:=
GeoGraphics[
 Values[Line[Values[osm[["Nodes", #Nodes, "Position"]]]] & /@ Select[osm["Ways"], #[["Tags", "waterway"]] === "river" &]]]
Out[5]=

Select only power lines:

In[6]:=
GeoGraphics[
 Values[Line[Values[osm[["Nodes", #Nodes, "Position"]]]] & /@ Select[osm["Ways"], #[["Tags", "power"]] === "line" &]]]
Out[6]=

Select only railways:

In[7]:=
GeoGraphics[
 Values[Line[Values[osm[["Nodes", #Nodes, "Position"]]]] & /@ Select[osm["Ways"], KeyExistsQ[#Tags, "railway"] &]]]
Out[7]=

Publisher

Christopher Wolfram

Requirements

Wolfram Language 11.3 (March 2018) or above

Version History

  • 1.0.0 – 16 January 2019

Related Resources

License Information