plast1cd0nk3y
plast1cd0nk3y

Reputation: 418

How can I add a point to an sf object?

I have an sf object that stores point data. But I am having trouble understanding how to add a point to this object.

I know how to create two points in separate objects:

# Create sfg objects with coordinates of Los Angeles and Amsterdam
la_sfg <- st_point(c(-118.2615805, 34.1168926))
amsterdam_sfg <- st_point(c(4.8979755, 52.3745403))

And I know how to combine these two objects into one geometry set:

# Create sfc object with multiple sfg objects
points_sfc <- st_sfc(la_sfg, amsterdam_sfg, crs = 4326)
points_sfc

And I know also how to add attributes to these points using a dataframe:

# Create a data frame of attributes for the two points
data <- data.frame(name = c("Los Angeles", "Amsterdam"),
                   language = c("English", "Dutch"),
   
                weather = c("sunny", "rainy/cold"))

# Make sf object from separate data frame and sfc objects
city_profiles <- st_sf(data, geometry = points_sfc)

Now let's say I have another point coordinate with the following information:

name = Toronto
language = English
Coordinates =   c(-79.450717,43.691589)
weather = hot

I'm having trouble figuring out how to create an sfg object and then add it to my pre-existing feature collection. For example, intuitively I feel like I'd do something like this:

# Create sfg object
toronto <- st_point(name = "toronto", language = "English",weather = "cold", geometry=c(-79.450717,43.691589))

and then use rbind to add this feature to city_profiles. However this is not the correct syntax and only returns errors.

Upvotes: 3

Views: 2473

Answers (1)

Arthur Welle
Arthur Welle

Reputation: 698

As SymbolixAU said you have to go all sf steps from all points and then rbind the results. Or bind it first and then go all the sf steps with all points in the same data.frame. For example:

library(tidyverse)
library(sf)
library(rnaturalearth)

world <- rnaturalearth::ne_countries(scale = "small",
                                     returnclass = "sf") 

la_ams <- data.frame(name = c("Los Angeles", "Amsterdam"), 
                     language = c("English", "Dutch"),
                     weather = c("sunny", "rainy/cold"), 
                     lon = c(-118.2615805, 4.8979755), 
                     lat = c(34.1168926, 52.3745403))

la_ams <- sf::st_as_sf(la_ams, 
                       coords = c("lon", "lat"),
                       crs = 4269) 

ggplot() +
  geom_sf(data = world,
          mapping = aes(geometry = geometry),
          fill = "white") +
  geom_sf(data = la_ams,
          aes(geometry = geometry),
          size = 3,
          color = "red") +
  theme_bw()

enter image description here

Add another point:

toronto <- data.frame(name = c("toronto"), 
                     language = c("English"),
                     weather = c("cold"), 
                     lon = c(-79.450717), 
                     lat = c(43.691589))

toronto <- sf::st_as_sf(toronto, 
                       coords = c("lon", "lat"),
                       crs = 4269) 

cities <- rbind(la_ams, toronto)

ggplot() +
  geom_sf(data = world,
          mapping = aes(geometry = geometry),
          fill = "white") +
  geom_sf(data = cities,
          aes(geometry = geometry),
          size = 3,
          color = "red") +
  theme_bw()

enter image description here

Upvotes: 4

Related Questions