如何将数据框变成简单的特征数据框? [英] how to make a data frame into a simple features data frame?

查看:38
本文介绍了如何将数据框变成简单的特征数据框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表,其中包含给定坐标参考系统中的位置参考和 x 和 y 坐标.我想把它变成一个简单的特征数据框.我该如何创建?

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?

我认为可能是:

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

但这会导致错误:

mutate_impl(.data, dots) 中的错误:列 feature 的长度必须为 1(组大小),而不是 2

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 :).

我也想试试:

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.

我正在尝试做的另一个问题是我有一个包含 EPSG 列的数据框.我需要为每个地方创建这个简单的特征数据框,并将它们组合成一个更大的简单特征数据框.

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.

推荐答案

UPDATE@Franz Plumpton 的答案是单个 epsg 的正确解决方案.仅当 data.frame 的每一行都有不同的 epsg 时,我才需要下面的回答.否则,这将是重复的(如上面@Henrik 所指出的).

UPDATE The answer from @Franz Plumpton is the correct correct solution with a single epsg. My answer below is only necessary when each row of a data.frame has a different epsg. Otherwise, this would be a duplicate (as pointed out by @Henrik above).

library(sf)
library(tibble)

df <- data_frame(place = c("London", "Kalamazoo"), 
           lat = c(51.5074, 396088), lon = c(0.1278, 5452158),
           epsg = c(4326, 32610))

l <- lapply(unique(df$place), function(x){
  df <- df[df$place == x,]
  epsg <- df$epsg[1]
  df  <-  st_as_sf(df, coords = c('lon', 'lat'), crs = epsg)
}) 

然后您可以将所有内容转换为相同的 epsg 并合并为一个 data.frame:

you could then transform all to the same epsg and combine into a single data.frame:

do.call(rbind, lapply(l, function(x) x <- st_transform(x, 4326)))

这篇关于如何将数据框变成简单的特征数据框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆