A Loading vector data
Instead of loading a shapefile into R (see section 2, we can construct vector objects ourselves from data.frames. To create point vector data, we can use still the vect
function in one of its many ways (see the help file of the vect
function: ?vect
).
Let’s create the cities
object but now from a data.frame with the data. We can create an object called cities
as a data.frame first:
### Create a data.frame with information
cities <- data.frame(city = c("Wageningen", "Paris", "London", "Berlin", "Brussels", "Vienna"),
lon = c(5.663443, 2.291764, -0.1249526, 13.37664, 4.342029, 16.38053),
lat = c(51.985526, 48.858159, 51.4986466, 52.51320, 50.894980, 48.19038))
Or, alternatively, we could the data from a separate file (e.g. a csv file).
Now we have the data stored in a data.frame
, where the lon columns holds the longitude coordinate, whereas the lat column holds the latitude coordinate. We can convert this data.frame into spatial vector (a SpatVector
object) using the vect
function:
### Convert a data.frame to SpatVector
cities <- vect(cities,
geom = c("lon", "lat"), # columns holding the longitude/latitude info
crs = crs("+init=epsg:4326")) # the associated crs of coordinates
# Inspect
cities
## class : SpatVector
## geometry : points
## dimensions : 6, 1 (geometries, attributes)
## extent : -0.1249526, 16.38053, 48.19038, 52.5132 (xmin, xmax, ymin, ymax)
## coord. ref. : lon/lat WGS 84
## names : city
## type : <chr>
## values : Wageningen
## Paris
## London
Now we are good to go: the cities
object created above is the same as the shapefile loaded in section 2!