Sunday, 23 June 2013

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.

8 comments:

  1. 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

    ReplyDelete
  2. 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

    ReplyDelete
  3. Her Pernille!

    Hvilke bakgrunnskart er det du sikter til?

    ReplyDelete
  4. 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

    ReplyDelete
  5. 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

    ReplyDelete
  6. 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.

    ReplyDelete
  7. Thank you for the shapefiles. Especially for the fact that you have matched each municipality with each ISO number! thanks!

    ReplyDelete