2

This is actually a two-part question. I'm looking to merge some regional polygons together in R:

library(raster)
Finland_map <- getData(name = "GADM", path = tempdir(), country = "Finland", level = 2)
Aland_map <- getData(name = "GADM", path = tempdir(), country = "Åland", level = 0)

One merge I'd like to do is to include Aland as a region into the rest of Finland. Unfortunately, I can't use rbind(Finland_map, Aland_map) because the levels of the files I'm using are not the same. How do I get past that?

The second merge involves a couple of regions within Finland. Eastern Uusimaa and Uusimaa were amalgamated in 2011. I've seen similar questions and answers on the topic (such as this one), but I'm not sure on how to use raster::union() when the two polygons are within the same SP DF.

4

The two objects have the following column names in common:

> intersect(names(Finland_map), names(Aland_map))
[1] "OBJECTID" "ID_0"     "ISO"     

so they can be minimally bound this way:

> common = intersect(names(Finland_map), names(Aland_map))
> All_map = rbind(Finland_map[,common], Aland_map[,common])

If you want to construct new columns from combining data from the parts, you can do:

> All_map$NAME = c(Finland_map$NAME_2, Aland_map$NAME_LOCAL)

but you'll have to work out the names of the columns that go together - you might want NAME_ISO ("ÅLAND ISLANDS") from Aland_map instead.

You can maximally bind the objects by using a "filling" rbind function, such as bind_cols from dplyr, which sets all the missing bits to NA values. Do a simple rbind as above and then stomp on the data part of the object:

> All_map = rbind(Finland_map[,common], Aland_map[,common])
> All_map@data = dplyr::bind_rows(Finland_map@data, Aland_map@data)

Now you have lots of things with lots of NA values, because Aland has one row and lots of columns, so they are all NA for the Finnish rows.

You can use gUnaryUnion after giving the two regions to merge the same name:

> Finland_map$NAME_2[c(8,12)] = "Merged Uusimaa"
> Fin_uni = gUnaryUnion(Finland_map,id=Finland_map$NAME_2)

But that also merges the three Päijänne Tavastia features - do you want that? If not, make the two regions have common IDS but make sure everything else is unique:

> Finland_map$OBJECTID[c(8,12)] = 999
> Fin_uni = gUnaryUnion(Finland_map,id=Finland_map$OBJECTID)
> length(Fin_uni)
[1] 20
> names(Fin_uni)
 [1] "1"   "2"   "3"   "4"   "5"   "6"   "7"   "9"   "10"  "11"  "13"  "14" 
[13] "15"  "16"  "17"  "18"  "19"  "20"  "21"  "999"

Note this returns a SpatialPolygons object, not a data frame - it doesn't know what to do with the attributes of merged features and you've not said what you want to do. Merge the plain data frames as you wish and then attach them to the SpatialPolygon as per the usual methods for making SpatialPolygonsDataFrames.

4

You can combine Spatial objects with different variables with the raster::bind function

FinAla <- bind(Finland_map, Aland_map)

Although in this case, that does create a lot of fields you might not want. So perhaps do

Aland_map$NAME_0 <- "Åland"
FinAla <- bind(Finland_map, Aland_map[, c('ISO', 'NAME_0')])

To merge polygons, you can use raster::aggregate

# which records include "Uusimaa"?
i <- grep("Uusimaa", Finland_map$NAME_2)
# change them to "Uusimaa" 
Finland_map$NAME_2[i] <- "Uusimaa"

# aggregate
Fin <- aggregate(Finland_map, c('NAME_1', 'NAME_2'))
2
  • I'm sure these vector functions would get a lot more love if they weren't in a package called raster :(
    – Spacedman
    Dec 15 '17 at 18:39
  • I thought that "RApid SpaTial Environment for R" captured the general purpose.... Dec 15 '17 at 19:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.