Get SOSI into PostGIS and TileMill

Here’s an example on how to convert and import SOSI map data into PostgreSQL/PosGIS using Sosicon, then opening it in Mapbox’ design studio TileMill.

tilmill_featured
We are going to use the landcover SOSI file “NO_N250_Arealdekke.sos” from the N250 SOSI package provided for free by the Norwegian Mapping Authority.

From SOSI to SQL

Assuming that Sosicon is downloaded and installed on the system, we start by converting from SOSI to PostgreSQL.

Create a work folder and put the SOSI file in it. Google/Web Mercator is the prefered grid if we are going to view the data in TileMill. So we specify SRID 900913 when running sosicon, keeping all other parameters to their defaults. Enter your work folder from the command line and type:

sosicon -2psql -srid 900913 NO_N250_Arealdekke.sos

sosicon_example_1
Sosicon found 1.929 points, 770.352 line strings and 224.111 polygons.

It’s almost 17 million lines in the file, so be patient! The conversion takes a couple of minutes, my Macbook Pro used approximately 7 minutes to churn through the data.

If the conversion was successful, you should have an enormous file called “postgis_dump.sql”, 970 Mb of size, alongside the SOSI file in your work directory.

Import to PostgreSQL/PostGIS

Open psql command-line utility for PostgreSQL. Create a new PostGIS-enabled database, or use an existing one. Enter the target database and run the SQL script from within psql:

\i postgis_dump.sql

This will take some time as well. You will see the progress as it inserts the geometries in chunks of 50.000 records. The good news is that when the script is done, all your data is ready to hand in the database. Give it ten miutes or so.

Running the PosrgreSQL script takes quite some time.
Running the PosrgreSQL script takes quite some time.

Now, let us have a look at the database in TileMill. Create an empty TileMill project and add a PostGIS layer. Give it ID “polygons” and input the table name “sosicon.object_polygon” in the “Table or subquery” textbox.

The unique ID field is “id_object_polygon”, while the geometry field is “object_geom”. The connection string depends on your configuration.

Opening the imported PostGIS data from TileMill.
Opening the imported PostGIS data from TileMill.

If you look at the features table for your PostGIS layer, you can see the data exported from Sosicon.

The SOSI OBJTYPE attribute has been exported as an individual field. This makes it easy to apply idividual styles to different object types in the Carto stylesheet.

TileMill features table.
TileMill features table.

Styling in TileMill

By referencing the “objtype” attribute in the Carto stylesheet, you have full control over all the land cover features from the SOSI file. Hence, the following rule will render the lakes:

#polygoner[objtype='Innsj√ł'] {
  polygon-opacity:1;
  polygon-fill:#b8dee6;
}

Import the linestrings and points in the same manner, using the corresponding table names. It might be wise to specify subqueries to reduce the layer size, filtering out unwanted features and splitting the database content across several layers.

Keep in mind that single features (OBJTYPEs) may be represented as multiple geometry types. For example, small water streams are occationally represented as linestrings instead of polygons, and are thus stored in the object_linestring table.

Some features, read from PostGIS and styled in TileMill.
Some features, read from PostGIS and styled in TileMill.

Leave a Reply

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