I was recently asked “what is the length of the coastline of each municipality of Denmark?”
For anyone acquainted with GIS, this is known as the ‘coastline paradox’; depending at which scale you look at the coast, the amount of details you can see are different. This wikipedia image explains it quite well:
When using a GIS to measure the coastline, its length depends on the scale and accuracy (=amount of details) at which the data was created. It also depends on other factors such as the definition of the coastline (was it measured at high or low side – in marshland area, this can change a lot), the date of the data (erosion or man-made structure can modify the coastline substantially), and other locally relevant factors.
The challenge here was first that the datasets I had access to contain either the whole coastline, without the municipality name attributes, or the municipality borders, but including also some inland borders between the municipalities. What I wanted to do therefore was to cut the coastline in smaller portions, one for each coastal municipality. Denmark has, since 2007, 98 municipalities; 70 of which are coastal cities. It also has only one land border with Germany, which is about 68km.
Pulling the coastline out of the municipality layer can easily be done in QGIS only, without using POSTGIS or other GIS database.
1 – I downloaded the free dataset of municipalities layer from kortforygningen (the Danish GIS data distributor), at the 1:10,000 scale. Very good accuracy, but 50MB shapefile. The shapefile contains 1580 polygons (that’s a loooot of islands!) representing the 98 municipalities.
Note: the shapefile is in UTM32N, the local projection of Denmark.
2 – I transformed the polygons to polylines with the municipality file:
Vector > Geometry Tools > Polygons to lines
The shapefile is now made of 1589 polylines; each limit of municipalities are now lines. So the coastline is not one continuous line, but parts of closed polylines replacing the polygons. We need to break the lines at each intersection so we can separate the coastlines and the inner borders.
3 – Breaking the lines, in QGIS, is easily done with the cleaning tool:
Processing > Toolbox > v.clean, using the Cleaning tool ‘break’.
This tool will break the lines at each intersection, so the coastline is now made of multiple lines, separating each municipality. However, there are still the inland borders, which also appear twice: one per municipality on each side of the border. We don’t want the inner borders, so we need to separate them from the coastlines.
Note: In QGIS 1.12, even if you create a new file instead of using a temporary file for the ‘Cleaned’ and ‘Error’ file, they will appear as ‘Cleaned’ and ‘Error’ layers in the Layers Panel. However, if you double click on the layer to see the Layer Properties, you’ll see that the Layer source has a shapefile with the name you gave to it before its creation. You can then copy that name into the Layer name field to be able to identify the layer in the Layer Panel.
4 – Finding the duplicate lines, using the cleaning tool again:
Processing > Toolbox > v.clean, with the Cleaning tool ‘rmdupl’. This produced two files:
– a ‘cleaned’ file, with the coastline and the inland borders without duplicates.
– an ‘error’ file generated through this tool containing only the inland borders (which have been removed from the cleaned file.
Note: Some features in the ‘cleaned’ shapefile have no geometry. They can be filtered out by selecting the features with a value not null in the ‘cat’ field. In the Attribute Table, click on ‘Show All Features’ at the bottom left, then Column filter, and select ‘cat’.
I used the error file to remove the inland borders left in the cleaned file by doing a symmetrical difference.
5 – Taking the symmetrical difference between the municipality and the duplicates:
Vector > Geoprocessing Tools > Symmetrical difference
Input vector layer: the ‘Cleaned’ file we just created, with coastline and inner borders without duplicates
Difference layer: the ‘Error’ file, with only the inner borders.
The result is the outer borders of Denmark, per municipality.
Stay tuned for the results in the next blog post!