Vector layer buffering in R
Buforowanie jest jednym z podstawowych narzędzi GIS używanym w analizach przestrzennych. Ale jak to zrobić w R? Wczytajmy najpierw warstwę kilku szlaków kolejowych z Open Street Map (pobierz) do zmiennej kolej, którą wykorzystamy do prezentacji możliwości funkcji buforowania:
Buffering is one of the basic GIS tools used in spatial analysis. But how to do it in R? Let’s first load a layer with several railroads from Open Street Map (download) into the railroad variable, which we will use to demonstrate the capabilities of buffering:
library(rgdal)
railroad = readOGR("D:/","railroad",stringsAsFactors = FALSE)
Let’s display the loaded layers:
plot(railroad)
Display summary for loaded layer:
summary(railroad)
Object of class SpatialLinesDataFrame
Coordinates:
min max
x 638667.5 678777.7
y 490053.6 519583.1
Is projected: TRUE
proj4string :
[+proj=tmerc +lat_0=0 +lon_0=19 +k=0.9993 +x_0=500000 +y_0=-5300000 +ellps=GRS80 +units=m +no_defs]
Data attributes:
osm_id code fclass name
Length:213 Min. :6101 Length:213 Length:213
Class :character 1st Qu.:6101 Class :character Class :character
Mode :character Median :6101 Mode :character Mode :character
Mean :6101
3rd Qu.:6101
Max. :6106
The loaded layer contains 213 linear objects representing sections of rail lines located in one of the districts.
For basic spatial analysis on vector data, we use the rgeos library, which we need to install and initialize:
install.packages(„rgeos”)
library(rgeos)
To buffer vector layers, we use the gBuffer function from the library we installed earlier. We use it to create a buffer around a path line of size 1 km:
buffer = gBuffer(railroad, width = 1000)
Let’s display the generated buffer and the path lines:
plot(buffer, col = „red”)
plot(railroad, add = TRUE)
Let’s check how many elements the buffer has:
length(buffer)
[1] 1
Bufor ma jeden element, to znaczy że bufory dla poszczególnych odcinków linii kolejowej zostały ze sobą połączone. W przypadku, gdy chcemy z użyciem tej funkcji otrzymać bufory dla każdego elementu zbioru musimy dodać do funkcji argument byid równy TRUE:
The buffer has one element, which means that the buffers for each line segment have been linked together. If we want to use this function to get buffers for each element of the set, we need to add an argument byid equal to TRUE to the function:
buffer = gBuffer(railroad, width = 1000, byid = TRUE)
Let’s display the result of the function:
plot(buffer, col = "red")
plot(railroad, add = TRUE)
Let’s check how many elements the buffer now has:
length(railroad)
[1] 213
A separate buffer has been created for each element. We can also define a different buffer value for each element in the function gBuffer. Let’s create a vector w that contains a buffer width of 500m:
w = rep(500,length(railroad))
Let’s change one of the vector values to 2000m:
w[86] = 2000
Now let’s put the vector w we created in the width attribute in the gBuffer function:
buffer = gBuffer(railroad, width = w, byid = TRUE)
Display the result:
plot(buffer, col = "red")
plot(railroad, add = TRUE)
You can see in the map that one of the railroads has a much larger buffer area than the others. This is how we defined it in our w vector.
To summarize, the gBuffer function is used for buffering in R. We can freely modify the generated buffers according to our needs by defining appropriate attributes of the function. In R you can find many similar functions in other libraries, but we leave that to the R snoopers 🙂