Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make a data frame into a simple features data frame?

Tags:

r

sf

I've got a table with place references and x and y coordinates in a given coordinate reference system. I want to turn that into a simple features data frame. How can I create that?

I thought it might be:

data_frame(place = "London", 
           lat = 51.5074, lon = 0.1278, 
           epsg = 4326) %>%
  group_by(place) %>%
  mutate(feature = st_point(c(lon, lat)))

But that leads to an error:

Error in mutate_impl(.data, dots) : Column feature must be length 1 (the group size), not 2

This is probably pretty simple to do, I'm just not seeing it readily discussed in the documentation. Most spatial analysts seem to demand better data by default :).

I also thought to try:

data_frame(place = "London", 
           lat = 51.5074, lon = 0.1278, 
           epsg = 4326) %>%
  group_by(place) %>%
  do(with(., {
    p <- st_point(c(lon, lat))
    pcol <- st_as_sfc(p)
    st_as_sf(data_frame(place = place,
                        point = pcol),
             crs = epsg)
  }))

At the end of the pipe, I want a simple features data frame that I can plot and manipulate like any other.

Another rub with what I'm trying to do is that I've got a data frame with a column for EPSG. I need to create this simple features data frame for each place and combine that all together into a larger simple features data frame.

like image 867
wdkrnls Avatar asked Mar 08 '18 20:03

wdkrnls


People also ask

How do you convert a data frame into a data table?

Method 1 : Using setDT() method table package, which needs to be installed in the working space. The setDT() method can be used to coerce the dataframe or the lists into data. table, where the conversion is made to the original dataframe. The modification is made by reference to the original data structure.

What is a simple feature in R?

Simple features are implemented as R native data, using simple data structures (S3 classes, lists, matrix, vector). Typical use involves reading, manipulating and writing of sets of features, with attributes and geometries. As attributes are typically stored in data.

What is St_as_sf?

st_as_sf: Convert foreign object to an sf object logical; the value for fill that was used in the call to map. group. logical; if TRUE , group id labels from map by their prefix before : crs. coordinate reference system to be assigned; object of class crs.

How do I remove a geometry from a Dataframe in R?

Use the head() function to check if your new data frame has a geometry column. Drop the geometry column from species_counts using st_set_geometry() . Confirm the geometry column has been dropped by re-running head() .


1 Answers

Your attempt and the accepted answers are unnecessarily complicated and terribly confusing. Just go with st_as_sf (which by the way also easily migrates all objects from the outdated sp class (SpatialPolygonsDataFrames and the like)):

df <- data.frame(place = "London", 
       lat = 51.5074, lon = 0.1278,
       population = 8500000) # just to add some value that is plotable
projcrs <- "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
df <- st_as_sf(x = df,                         
           coords = c("lon", "lat"),
           crs = projcrs)

And we are done, as easy as that.

Just to visualise it:

library(tmap)
data("World")    
tm_shape(World[World$iso_a3 == "GBR", ]) + tm_polygons("pop_est") + 
    tm_shape(df) + tm_bubbles("population")

tmap way

Or with the new amazing geom_sf from ggplot2:

library(ggplot2)
ggplot(World) + geom_sf() + geom_sf(data = df, shape = 4, col = "red", size = 5)

ggplot2 way

like image 161
Fitzroy Hogsflesh Avatar answered Sep 21 '22 10:09

Fitzroy Hogsflesh