Reputation: 11
I hava a task for my college in which I need to transform coordinates from a table. So I had one table which had bad typed coordinates and the code worked and those coordinates got succesfully transformed. A friend sent me a new table which had the right coordinates,but when I tried to transform it the R studio console got me this message: no simple feature geometries present: returning a data.frame or tbl_df
I can't transform the data.frame or tbl.df with st.transform function
The code for transformation:
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")
when I run the first code for st_read function the program gives me the message that no simple geometries are present,but the tables are the same.
Can someone give me the ansfer for why my code doesn't work? Sorry for bad english. I would be very thankful if someone knows what is the problem. Bye, Aleksandar.
I tried changing the format of the table but R studio only imported the .csv format. I don't know what to do next.
The code after running 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")
Upvotes: 1
Views: 1114
Reputation: 31
Convert from dataframe to sf format Step 1: Read data
tablea1 <- read.csv(tableai.csv)
Step 2: You can automatically convert to sf format.
sfdata <- sf::st_as_sf(
tablea1,
coords = c("Lon.lambda.", "Lat.fi."), crs = st_crs(4326)
)
Upvotes: 0
Reputation: 8343
You have a couple of options:
Ask whoever made table6.csv
to export it as a "geometry" file (e.g. .shp
, .gpkg
). This will maintain the geometry data, and therefore can be read directly by sf::st_read()
Convert your data.frame
to a spatial object in 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
At this point you can do whatever you want with the data. But you probably don't want to
st_write(tabela1_t,"tacke1_trans.csv",layer_options="GEOMETRY=AS_XY")
Because you'll just be saving another .csv file, so you'll loose the geometry component.
Upvotes: 2