Skip to main content

Splitting and clipping shapefiles with QGIS

In my last blog post, we improved a dataset containing all municipalities of Norway by merging polygons in QGIS. The municipality polygons include the sea areas, which makes it harder for people to recognize the shapes, as we're used to see Norway with all its fjords and islands. So let's use QGIS to remove the sea from all municipalities.

This is a two-step process. First we need to create a shapefile containing only sea areas. Let's open "NO_Arealdekke_pol.shp" in QGIS. This file contains land cover data, where one of the categories is sea areas ("Havflate").  



  • Open the attribute table and select all sea areas (OBJTYPE = Havflate).  
  • While the areas are selected, right-click on the layer name in the left column and select "Save Selection As...".
  • Save the shapefile with a new name ("NO_Havflate_pol.shp"). 
  • Open the new shapefile and click the "Toggle Editing" button.
  • Mark all sea areas with "Select Features by Rectangle".
  • Click the "Merge Selected Features" button.
  • Save your changes. 
  • You now have a shapefile with one large polygon of all sea areas in Norway. 



Next, we'll use our new shapefile to remove sea areas from the municipality dataset we created in the last blog post:
  • Open both shapefiles in QGIS.
  • In the main menu, select Vector -> Geoprocessing Tools -> Difference.
  • Select the municipality dataset as "Input vector layer".
  • Select the sea areas dataset as "Difference layer".
  • Choose a new name for "Output shapefile".
  • Press OK
We now have a new shapefile were the municipalities are only covering land areas. The clipping didn't work perfectly, and you'll se some parts left in the ocean. You can remove this line by clicking "Toggle Editing" and then on the "Delete Part" button. Click on the line in the ocean until all parts are removed.



The standard way to identify municipalties in Norway is four digit number, where the two first digits represent the county of the municipality. The dataset from the Norwegian Mapping Authority is missing leading zeros, which might give us problems when combining different datasets. We can use the Field Calculator in QGIS to add a new column with a 4 digits id:


You now have a shapefile with all the municipalities in Norway following the coastline. Finally I've merged all municipalities belonging to the same county to create a new shapefile of all the counties of Norway. You can download the shapefiles from GitHub ("NO_Kommuner_pol.shp" and "NO_Fylker_pol.shp").

Counties of Norway.

In the next blog post we're going to convert the shapefiles to TopoJSON, a compact vector format supported by D3.js

Kayaking around Foldøy island.

Comments

Nathan said…
In QGIS 2.0 you can use the lpad or rpad functions in order to add the missing zeros:

rpad('145', 4, 0)

will give you 0145
Pernille said…
Hei Bjørn,

Takk for supre tips!!! Hvor har du fått tak i de kule bakgrunnskartene (baseline maps) av Norge? Er dette noe som ligger åpent eller har du laget dem selv?

Jeg er relativ ny i QGIS og ønsker å lage noen kart av enkelte kommuner i Norge. Jeg ønsker gjerne et godt bakgrunnsbilde for å vise topologien, men jeg synes de kartene som ligger som "open baseline maps" ofte inkluderer unødvendig informasjon (les stedsnavn etc).

Bra blogg! =)

Pernille
Bjørn Sandvik said…
Her Pernille!

Hvilke bakgrunnskart er det du sikter til?
Pernille said…
Hei igjen,

Beklager sent svar, jeg var ikke klar over at du hadde svart så raskt =)

Jeg tenker på de bildene du har liggende øverst i bloggen din (de som skifter). Utrolig fine! Men dette er kanskje ikke bakgrunnsbilder?

Vet du eventuelt om noe liknende?

Takk igjen =)

Hilsen Pernille
Bjørn Sandvik said…
Kartene som vises øverst er fra New Zealand og er laget på denne måten:

Creating a shaded relief map of New Zealand

Doing magic with TileMill
Pernille said…
Ok =) Tror dette er noe komplisert for min del på dette stadiet =) Har forsøkt å få det til for Norge, men må nok få noe større forståelse for GIS først.

Takk igjen for svar! =)

God påske!

Hilsen Pernille
Stein Magne Os said…
Hei Bjørn,

Er det mulig å bruke denne metoden for å lage eit kommunekart (over Norge) som kan lastast inn i CartoDB som bakgrunnskart? Finnast det evt andre opne datasett til dette?

Eg er ute etter å bruke CartoDB til å visualisere datasett for kommunar i staden for tettstader, byar osv.
Morsy said…
Thank you for the shapefiles. Especially for the fact that you have matched each municipality with each ISO number! thanks!

Popular posts from this blog

Creating a WebGL Earth with three.js

This blog post will show you how to create a WebGL Earth with three.js , a great JavaScript library which helps you to go 3D in the browser. I was surprised how easy it seemed when reading a blog post  by Jerome Etienne . So I decided to give it a try using earth textures  from one of my favourite cartographers, Tom Patterson . WebGL is a JavaScript API for rendering interactive 3D graphics in modern web browsers without the use of plug-ins. Three.js is built on top of WebGL, and allows you to create complex 3D scenes with a few lines of JavaScript. If your browser supports WebGL you should see a rotating Earth below: [ Fullscreen ] To be able to display something with three.js, you need three things: a scene, a camera and a renderer. var width  = window.innerWidth,     height = window.innerHeight; var scene = new THREE.Scene(); var camera = new THREE.PerspectiveCamera(45, width / height, 0.01, 1000); camera.position.z = 1.5; var rende...

Thematic Mapping Engine

It's time to introduce the Thematic Mapping Engine (TME). In my previous blog posts, I've shown various techniques of how geobrowsers can be used for thematic mapping. The goal has been to explore the possibilites and to make these techniques available to a wider audience. The Tematic Mapping Engine provides an easy-to-use web interface where you can create visually appealing maps on-the-fly. So far only prism maps are supported, but other thematic mapping techniques will be added in the upcoming weeks. The engine returns a KMZ file that you can open in Google Earth or download to your computer. My primary data source is UNdata . The above visualisation is generated by TME ( download KMZ ) and shows child mortaility in the world ( UNdata ). The Thematic Mapping Engine is also an example of what you can achieve with open source tools and datasets in the public domain: A world border dataset is loaded into a MySQL database . The same database contains tables with statistics ...

Creating 3D terrains with Cesium

Previously, I’ve used three.js to create 3D terrain maps in the browser ( 1 , 2 , 3 , 4 , 5 , 6 ). It worked great for smaller areas, but three.js doesn’t have built-in support for tiling and advanced LOD algorithms needed to render large terrains. So I decided to take Cesium for a spin. Cesium is a JavaScript library for creating 3D globes and 2D maps in the browser without a plugin. Like three.js, it uses WebGL for hardware-accelerated graphics. Cesium allows you to add your own terrain data, and this blog post will show you how. Impressed by the terrain rendering in @CesiumJS - with a 10m elevation model for Norway! Farewell Google Earth. pic.twitter.com/RQKvfu2hBb — Bjørn Sandvik (@thematicmapping) October 4, 2014 Compared to  the dying Google Earth plugin , it's quite complicated to get started with Cesium. The source code is well documented and the live coding Sandcastle is great, but there is a lack of tutorials  and my development slows down when ...