I’ve got data currently within csv, with a column called "journeyroute." This column has the following data [truncated due to size]:
{"type": "FeatureCollection", "features": [{"type": "Feature", "geometry": {"type": "Point", "coordinates": [-4.095772, 50.409393]}, "properties": {"name": "start"}}, {"type": "Feature", "geometry": null, "properties": {"name": "end"}}, {"type": "Feature", "geometry": {"type": "LineString", "coordinates": [[-4.095772, 50.409393], [-4.095781, 50.409397], [-4.095792, 50.409401], [-4.095965, 50.40971], [-4.096064, 50.410069], [-4.09597, 50.410397]]}, "properties": {"distance": 4027.4, "name": "Raw", "times": [1690900467000, 1690900520000, 1690900522000, 1690900539000, 1690900550000, 1690900569000], "duration": 4923.0}}]}
There are 5,000 rows of data. What I’m trying to do is extract out the LineString data to use within R, but I’m getting stuck. Can anyone help please?
I’ve tried converting to JSON and then unnesting, but comes up with an error (code adapted from other answers using Google Earth Engine):
new_df <- df %>%
mutate(geo = map(Journey.Route, ~ jsonlite::fromJSON(.))) %>%
as.data.frame() %>%
unnest(geo) %>%
filter(geo != "FeatureCollection") %>%
mutate(coord = rep(c("x", "y"))) %>%
pivot_wider(names_from = coord, values_from = coordinates)
Error in `mutate()`:
ℹ In argument: `coord = rep(c("x", "y"))`.
Caused by error:
! `coord` must be size 5000 or 1, not 2.
Run `rlang::last_trace()` to see where the error occurred.
Expecting a sf geometry column of LineString coordinates.
2
Answers
I suggest you don’t convert the
geo
to a frame, since it’s a rather nested list. A quick function that extracts the"LineString"
component:With this, testing on a singleton, we see
If your frame is well structured, then we can do something like:
As we are dealing with GeoJSON string, it can be parsed with
sf::st_read()
or perhaps withgejsonsf::geojson_sfc()
for some performance boost (~ 2x).Rowwise grouping to access one row at a time; keeping only
LINESTRING
geometries (presumably one per FeatureCollection, as in provided sample).Benchmark results and resulting
sf
object:Created on 2023-08-04 with reprex v2.0.2