-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
dealing with nested json columns after reading with geojsonsf #97
Comments
please see this issue - I initially decided to follow {sf}'s lead and convert nested properties in to strings. And I'm not sure if an {sf} object can handle nested / list columns. A work-around is something like
|
Dave,
Thanks for getting back to me - I really appreciate it.
I figured out the issue this afternoon - the file I was reading had an odd format on a couple of the fields so they didn’t read in correctly. When I read the file with geojsonsf it put the data into a column with curly { brackets but not square [ brackets. Not having the square [ bracket kept all of the things I tried initially from working as they were not recognized as JSON format. I pasted a leading and trailing square bracket on to each observation and was able to feed that through tidyjson gather and spread to get the data into columns and then join it back on the original data frame. I don’t think it is necessarily an issue with geojsonsf, but just in case you want to see what I was working with I did I will include my code below
Thanks again for following up!
Frank Baumgardner
…---------------------------------
library(tidyjson)
library(tidyr)
library(dplyr)
library(geojsonsf)
library(sf)
link <- 'https://tiles.skimap.org/geojson/ski_areas.geojson'
S0 <- geojson_sf(link)
st_crs(S0)
#make a copy to save the inital file
S1 <- S0
#location's format needs brackets to process
S1$location2 <- ifelse(is.na(S1$location), NA, paste0('[', S1$location,']'))
#gather won't work if you keep the geometry
S2 <- st_drop_geometry(S1)
#Select the id column along with the json columns we need to parse
S2 <- S2 %>%
select(id, location2, statistics2)
#you can't gather na's so drop them - we will join on id later
S3.loc <- S2 %>%
filter(!is.na(S2$location2)) %>%
select(id, location2)
#Gather & Spread
S3.loc$json <- gather_array(S3.loc$location2)
S4.loc <- S3.loc$json %>% spread_all
S5.loc <- as.data.frame(S4.loc)
S6.loc <- cbind(S3.loc, S5.loc)
S7.loc <- S6.loc[-c(2:7)]
On Jan 3, 2023, at 3:11 PM, Dave ***@***.***> wrote:
please see this issue <#17> - I initially decided to follow {sf}'s lead and convert nested properties in to strings. And I'm not sure if an {sf} object can handle nested / list columns.
A work-around is something like
lst <- jsonify::from_json(link)
coords <- lst$features$geometry$coordinates
sfc_points <- sfheaders::sf_point(matrix(unlist(coords), ncol = 2, byrow = T))
lst$geometry <- sfc_points
—
Reply to this email directly, view it on GitHub <#97 (comment)>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/AZN7WNICK2WQ5N43O7IF4HDWQSBW7ANCNFSM6AAAAAATQATZKM>.
You are receiving this because you authored the thread.
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm working with a geojson file and reading it with geojsonsf. The file is importing successfully, but there are still nested json columns in the data frame. I'm not sure this is an issue with the geojsonsf package, but wanted to see if the team had any suggestions on how to unnest the additional columns.
The text was updated successfully, but these errors were encountered: