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

sauutmhj  于 2023-01-28  发布在  其他
关注(0)|答案(1)|浏览(122)

我有一个任务,我需要从一个表转换坐标。所以我有一个表,其中有错误的输入坐标和代码工作,这些坐标得到了成功的转换。一个朋友给我一个新的表,其中有正确的坐标,但当我试图转换它的R studio控制台给我这个消息:不存在简单的特征几何形状:返回 Dataframe 或tbl_df
我无法用st.transform函数转换数据.frame或表.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函数的第一个代码时,程序给了我一条消息:没有简单的几何图形,但是表格是相同的。
有人能给予我为什么我的代码不能工作吗?对不起,英语不好。如果有人知道是什么问题,我将非常感激。再见, Alexandria 。
我试着改变表格的格式,但是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")
qxsslcnc

qxsslcnc1#

您有几个选择:
1.让制作table6.csv的人将其导出为“几何”文件(例如.shp.gpkg)。这将保留几何数据,因此sf::st_read()可以直接读取
1.将您的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文件,所以您将丢失几何体组件。

相关问题