不存在简单的特征几何形状:返回带有表的 data.frame 或 tbl_df 问题

问题描述 投票:0回答:2

我的大学有一项任务,需要转换表格中的坐标。 因此,我有一张表,其中坐标类型错误,代码有效,并且这些坐标已成功转换。 一位朋友给我发送了一张具有正确坐标的新表格,但是当我尝试对其进行转换时,R studio 控制台向我显示了以下消息: 不存在简单的特征几何形状:返回 data.frame 或 tbl_df

我无法使用 st.transform 函数转换 data.frame 或 tbl.df

改造代码:

tabela1=st_read("table6.csv",
                  options=c("X_POSSIBLE_NAMES=Lon","Y_POSSIBLE_NAMES=Lat"),
                  crs=4326)
 tabela1_t=st_transform(tabela1, crs="+proj=merc +lon_0=0 +x_0=0 +y_0=0 +ellps=WGS84 +units=m")
 st_write(tabela1_t,"tacke1_trans.csv",layer_options="GEOMETRY=AS_XY")

当我运行 st_read 函数的第一个代码时,程序给我一条消息:不存在简单的几何图形,但表格是相同的。

有人可以告诉我为什么我的代码不起作用吗? 抱歉英语不好。 如果有人知道问题所在,我将非常感激。 再见,亚历山大。

我尝试更改表格的格式,但 R studio 仅导入了 .csv 格式。 我不知道下一步该做什么。

运行dput(head(tabela1))后的代码:

> dput(head(tabela1))
structure(list(Lon.lambda. = c("37.7", "-0.178", "30.25", "13.32757", 
"-3.69097", "12.52"), Lat.fi. = c("55.75", "51.48791", "59.91666", 
"52.51627", "40.44222", "41.88"), CITY_NAME = c("Moscow", "London", 
"Saint Petersburg", "Berlin", "Madrid", "Rome"), CNTRY_NAME = 
c("Russia", 
"U?", "Russia", "Germany", "Spain", "Italy"), N = c("6392773.83432", 
"6391248.57034", "6394182.05569", "6391622.66588", "6387139.33402", 
"6387672.51982"), m = c("1.77275", "1.60267", "1.98997", "1.63982", 
"1.3121", "1.3411"), p = c("3.14263", "2.56854", "3.95998", 
"2.68901", 
"1.72162", "1.79854"), Lon.rad = c("0.65799", "-0.00311", "0.52796", 
"0.23261", "-0.06442", "0.21852"), Lat.rad = c("0.97302", "0.89863", 
"1.04574", "0.91658", "0.70585", "0.73094"), M = c("6379156.04643", 
"6374591.09111", "6383372.63993", "6375710.51824", "6362303.41551", 
"6363896.88615")), row.names = c(NA, 6L), class = "data.frame")
r r-sf
2个回答
1
投票

您有几个选择:

  1. 要求制作

    table6.csv
    的人将其导出为“几何”文件(例如
    .shp
    .gpkg
    )。这将保留几何数据,因此可以直接读取
    sf::st_read()

  2. data.frame
    转换为 r

    中的空间对象
## After reading your data in to R you can create an `sf` object from `tablea1`
sf <- sf::st_as_sf(tablea1, coords = c("Lon.lambda.", "Lat.fi."))

## You need to set the coordinate reference system (CRS)
## THe `Lon.lambda.` and `Lat.fi` columns already look like 
## they're in CRS 4326, so there's no need to use `st_transform()`, you only need to set the CRS
sf::st_crs(sf) <- 4326 

此时您可以对数据做任何您想做的事情。但你可能不想

st_write(tabela1_t,"tacke1_trans.csv",layer_options="GEOMETRY=AS_XY")

因为您只是保存另一个 .csv 文件,所以您将丢失几何组件。


0
投票

从 dataframe 转换为 sf 格式 第1步:读取数据

tablea1 <- read.csv(tableai.csv)

第2步:可以自动转换为sf格式。

sfdata <- sf::st_as_sf(tablea1, coords = c("Lon.lambda.", "Lat.fi."), crs = st_crs(4326)
© www.soinside.com 2019 - 2024. All rights reserved.