Importing OpenStreetMap data or other large XML files with Integration Services (SSIS)

25.06.2015 Hilmar Buchta

SQL Server 2005-2016

Last year I wrote about importing data from OpenStreetMap using PowerQuery. You can find the blog post here. In that post I loaded a relatively small area as an example. The reason was, that the XML DOM parser in PowerQuery loads the full XML document into memory before being able to access it. If you need to process larger areas, the approach with the DOM parser won’t work. For example, in a recent project I had to load all addresses in Berlin. I took the OSM file from The file is bzip-compressed down to 68 MB. Once downloaded it expands to about 940 MB (yes, XML is very talkative and compresses very well…). At first, I tried to load the file using PowerQuery and the script from my blog post. But since the DOM parser creates a memory consuming object model, it failed at about 30% of the load and 20 minutes with an out-of-memory error (using 25GB).

So, if you need to load a large XML file in general, you’re well advised to use a a different parsing approach. So this blog post is about using a lightweight XML parser for reading the file from above. For example, a SAX parser reads the file once from the beginning to the end (forward only) firing events. In C# the XMLReader follows a similar approach. Both parsers do not allow you to search the XML file randomly (for example with XPATH), to insert elements or to go back and forth. But what they do is that they read the file in a very efficient way.

Let’s have a look at a typical node element from the OpenStreetMap OSM file:

  1. <node id=„256922190“ visible=„true“ version=„7“ changeset=„29687333“ timestamp=„2015-03-23T20:13:41Z“ user=„atpl_pilot“      
  2. uid=„881429“ lat=„52.5379749“ lon=„13.2888659“>
  3.   <tag k=„addr:city“ v=„Berlin“/>
  4.   <tag k=„addr:country“ v=„DE“/>
  5.   <tag k=„addr:housenumber“ v=„24“/>
  6.   <tag k=„addr:postcode“ v=„13627“/>
  7.   <tag k=„addr:street“ v=„Halemweg“/>
  8.   <tag k=„addr:suburb“ v=„Charlottenburg-Nord“/>
  9.   <tag k=„amenity“ v=„library“/>
  10.   <tag k=„layer“ v=„1“/>
  11.   <tag k=„name“ v=„Stadtteilbibliothek Halemweg“/>
  12.   <tag k=„ref:isil“ v=„none“/>
  13. </node>

You can clearly see the geo coordinates (latitude and longitude) as well as the address (in key/value pairs below the node). I wasn’t interested in points of interest (POIs) but you can also see that the amenity key contains information about the point of interest. In this case, we have a library.

Since PowerQuery uses the DOM parser and because I wanted the import process to run scheduled I used Integration Services (SSIS) to load the file. First I had to create a database table like this:

  1. CREATE TABLE [dbo].[OSMAddress](
  2.     [latitude] [real] NULL,
  3.     [longitude] [real] NULL,
  4.     [street] [nvarchar](255) NULL,
  5.     [housenumber] [nvarchar](20) NULL,
  6.     [postcode] [nvarchar](20) NULL,
  7.     [city] [nvarchar](255) NULL,
  8.     [country] [nvarchar](2) NULL
  9. )

Next, I used a very simple data flow to populate the table:


The main logic is contained in the script component. This is the code for the CreateNewOutputRows event in the script component (please note that his code is without any error handling for simplicity here):

  1. public override void CreateNewOutputRows()
  2. {
  3.     float latitude = 1;
  4.     float longitude = 1;
  5.     String city = null;
  6.     String country = null;
  7.     String street = null;
  8.     String housenumber = null;
  9.     String postcode = null;
  10.     using (XmlReader reader = XmlReader.Create(Variables.OpenStreetmapFile))
  11.     {
  12.         while (reader.Read())
  13.         {
  14.             switch (reader.NodeType)
  15.             {
  16.                 case XmlNodeType.Element:
  17.                     if (reader.Name.Equals(„node“))
  18.                     {
  19.                         if (reader.HasAttributes)
  20.                         {
  21.                             String lt = reader.GetAttribute(„lat“);
  22.                             String lg = reader.GetAttribute(„lon“);
  23.                             if (lt != null && lg != null)
  24.                             {
  25.                                 if (!(float.TryParse(lt, out latitude) && float.TryParse(lg, out longitude)))
  26.                                     latitude=longitude=-1;
  27.                             }
  28.                         }
  29.                     }
  30.                     else if (reader.Name.Equals(„tag“))
  31.                     {
  32.                         if (latitude > 1 && longitude > 1)
  33.                         {
  34.                             String k = reader.GetAttribute(„k“);
  35.                             String v = reader.GetAttribute(„v“);
  36.                             if (k!=null && v!=null) {
  37.                                 switch (k)
  38.                                 {
  39.                                     case „addr:city“:        city = v; break;
  40.                                     case „addr:country“:     country = v; break;
  41.                                     case „addr:housenumber“: housenumber = v; break;
  42.                                     case „addr:postcode“:    postcode = v; break;
  43.                                     case „addr:street“:     street =v; break;
  44.                                 }
  45.                             }
  46.                         }
  47.                     }
  48.                     break;
  49.                 case XmlNodeType.EndElement:
  50.                     if (reader.Name.Equals(„node“))
  51.                     {
  52.                         if (latitude > 1 && longitude > 1 && street != null && city!=null && housenumber!=null)
  53.                         {
  54.                             Output0Buffer.AddRow();
  55.                    = city.Substring(0, Math.Min(city.Length,255));
  56.                    = (country==null)?„“:country.Substring(0, Math.Min(country.Length,2));
  57.                             Output0Buffer.housenumber = housenumber.Substring(0, Math.Min(housenumber.Length,20));
  58.                             Output0Buffer.latitude = latitude;
  59.                             Output0Buffer.longitude = longitude;
  60.                             Output0Buffer.postcode = (postcode==null)?„“:postcode.Substring(0, Math.Min(postcode.Length,20));
  61.                             Output0Buffer.street = street.Substring(0, Math.Min(street.Length,255));
  62.                         }
  63.                         latitude = longitude = 1;
  64.                         street = postcode = housenumber = country = city = null;
  65.                     }
  66.                     break;
  67.             }
  68.         }
  69.     }
  70. }

The package took about 10 seconds to extract all of the addresses from the OSM file into the database table: A quite impressive result compared to the 20 minutes without success from above. So this clearly shows the advantage of XML parsers like SAX or XMLReader when it comes to reading larger files. If you go for larger areas it’s better to directly stream from the bzip2 compressed file instead of decompressing the file first. For example, the OSM file for Germany (complete) is about 4GB in size (bzip2 compressed) and expands to a single 48GB XML-file. I used SharpZipLib to decompress the file on the fly which saves a lot of disk space and IO. Using this approach I created the following visualization showing the concentration of fuel stations in Germany:


Of course you could retrieve much more information from the OSM file than I did here. For example, you can read borders (city, state etc.), points of interests (restaurants, airports etc.), sometimes even the location of trees. The file format is described here:

Your email address will not be published. Required fields are marked *

Join #teamoraylispeople

Gestalte mit uns
die Welt der Daten