Read-in weird list format and convert to sf
Closed this issue · 0 comments
Robinlovelace commented
Reproducible example:
library(sf)
#> Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
d = readRDS("~/Downloads/TRIPSsample.Rds")
class(d[[1]])
#> [1] "character"
d[[1]]
#> [1] "33830" "33837"
d[[2]] # all the data is here...
#> type
#> 33830 FeatureCollection
#> 33837 FeatureCollection
#> features
#> 33830 Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, 1549393829000, 1549393892000, 1549394083000, 1549394146000, 1549394210000, 1549394336000, 1549394399000, 1549394527000, 1549394664000, 1549394725000, 1549394782000, 1549394845000, 1549394971000, 1549395034000, 1549395097000, 1549395160000, 1549395223000, 1549395286000, 1549395323000, 1549395325000, 1549395386000, 1549395449000, 1549395512000, 1549395575000, 1549395638000, 1549395701000, 1549395764000, 1549395856000, 1549395889000, 1549395953000, 1549396016000, 1549396079000, 1549396142000, 1549396205000, 1549396268000, 1549396394000, 1549396457000, 1549396520000, 1549396583000, 1549396647000, 1549396710000, 1549396772000, 1549396835000, 1549396898000, 1549396962000, 1549397088000, 1549397151000, 1549397213000, 1549397276000, 1549397339000, 1549397403000, 1549397465000, 1549397529000, 1549397592000, 1549397654000, 1549397780000, 1549397844000, 1549397970000, 1549398115000, 1549398179000, 1549398242000, 1549398305000, 1549398367000, 1549398431000, 1549398494000, 1549398558000, 1549398665000, 1549398728000, 1549398791000, 1549398854000, 1549398916000, 1549398979000, 1549399093000, 1549399156000, 1549399219000, 1549399282000, 1549399346000, 1549399409000, 1549399472000, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, -9.161291, 38.724411, -9.161128, 38.724175, -9.161523, 38.724456, -9.162235, 38.725988, -9.162226, 38.727125, -9.162021, 38.72694, -9.160475, 38.72569, -9.154471, 38.724936, -9.150906, 38.725121, -9.150136, 38.725896, -9.147693, 38.728966, -9.144078, 38.729681, -9.138208, 38.73141, -9.134575, 38.731163, -9.13419, 38.733186, -9.132963, 38.735553, -9.132928, 38.73555, -9.133188, 38.735753, -9.133188, 38.735753, -9.132961, 38.735646, -9.132688, 38.735468, -9.132951, 38.73556, -9.13279, 38.735451, -9.133465, 38.736015, -9.13276, 38.735621, -9.132758, 38.735526, -9.13268, 38.735508, -9.132951, 38.735555, -9.132808, 38.735488, -9.13286, 38.735666, -9.132808, 38.735516, -9.132398, 38.735346, -9.132968, 38.735726, -9.133006, 38.735635, -9.132743, 38.735488, -9.13289, 38.735796, -9.132798, 38.73529, -9.132855, 38.735798, -9.132795, 38.735486, -9.132771, 38.735446, -9.132855, 38.735435, -9.132803, 38.73534, -9.13276, 38.735335, -9.132778, 38.735311, -9.13237, 38.735266, -9.13312, 38.73561, -9.132676, 38.735186, -9.13262, 38.735358, -9.13273, 38.73553, -9.132806, 38.735531, -9.132945, 38.735358, -9.132945, 38.735268, -9.132888, 38.735381, -9.133048, 38.735198, -9.132865, 38.73598, -9.132976, 38.735375, -9.133091, 38.735236, -9.132966, 38.735456, -9.132966, 38.735456, -9.132966, 38.735456, -9.133081, 38.735258, -9.132871, 38.73558, -9.132786, 38.735511, -9.132786, 38.735611, -9.133176, 38.73567, -9.132773, 38.73548, -9.132743, 38.736106, -9.133273, 38.734678, -9.132663, 38.73518, -9.132868, 38.735291, -9.132975, 38.735465, -9.132878, 38.735443, -9.133041, 38.735441, -9.132865, 38.735371, -9.132636, 38.73558, -9.132638, 38.735875, -9.133036, 38.735353, -9.133091, 38.735303, -9.133186, 38.735455
#> 33837 Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, Feature, 1549393811000, 1549393936000, 1549394063000, 1549394099000, 1549394111000, 1549394124000, 1549394187000, 1549394250000, 1549394273000, 1549394311000, 1549394374000, 1549394500000, 1549394694000, 1549394754000, 1549394817000, 1549394880000, 1549394943000, 1549395006000, 1549395069000, 1549395133000, 1549395195000, 1549395258000, 1549395267000, 1549395308000, 1549395375000, 1549395399000, 1549395461000, 1549395525000, 1549395588000, 1549395651000, 1549395656000, 1549395656000, 1549395719000, 1549395788000, 1549395851000, 1549395914000, 1549395977000, 1549396040000, 1549396104000, 1549396167000, 1549396229000, 1549396292000, 1549396397000, 1549396418000, 1549396481000, 1549396544000, 1549396617000, 1549396674000, 1549396743000, 1549396751000, 1549396859000, 1549396922000, 1549396985000, 1549397048000, 1549397111000, 1549397174000, 1549397237000, 1549397301000, 1549397364000, 1549397428000, 1549397490000, 1549397553000, 1549397618000, 1549397682000, 1549397745000, 1549397808000, 1549397872000, 1549397958000, 1549398001000, 1549398066000, 1549398130000, 1549398192000, 1549398256000, 1549398317000, 1549398380000, 1549398443000, 1549398507000, 1549398569000, 1549398632000, 1549398695000, 1549398758000, 1549398823000, 1549398884000, 1549398947000, 1549399010000, 1549399073000, 1549399137000, 1549399213000, 1549399275000, 1549399339000, 1549399402000, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, Point, -9.142151, 38.736323, -9.141861, 38.736798, -9.141823, 38.73535, -9.141823, 38.73535, -9.142536, 38.736198, -9.142591, 38.736221, -9.142671, 38.736233, -9.142751, 38.736235, -9.142751, 38.736235, -9.142705, 38.73626, -9.142671, 38.736238, -9.142695, 38.736275, -9.142675, 38.736253, -9.142621, 38.736255, -9.142603, 38.736275, -9.14264, 38.736255, -9.142613, 38.736253, -9.14264, 38.736181, -9.142616, 38.736256, -9.142623, 38.73623, -9.142615, 38.736216, -9.142575, 38.736256, -9.142556, 38.73624, -9.142556, 38.73624, -9.142556, 38.73624, -9.142641, 38.736293, -9.142608, 38.736261, -9.142616, 38.736251, -9.142573, 38.736291, -9.142633, 38.736221, -9.142633, 38.736221, -9.142648, 38.736215, -9.143483, 38.736651, -9.144363, 38.739641, -9.146151, 38.741515, -9.146841, 38.741233, -9.147258, 38.742781, -9.147865, 38.744413, -9.147958, 38.744805, -9.14847, 38.744878, -9.14771, 38.743513, -9.148411, 38.747156, -9.147908, 38.74479, -9.147663, 38.744456, -9.147483, 38.74456, -9.147008, 38.74256, -9.14932, 38.740946, -9.149371, 38.742638, -9.14919, 38.744111, -9.149301, 38.744693, -9.148485, 38.747263, -9.14749, 38.743993, -9.146511, 38.742263, -9.1445, 38.741911, -9.146423, 38.741526, -9.14604, 38.741595, -9.147011, 38.74229, -9.149915, 38.741053, -9.152793, 38.738848, -9.152106, 38.73607, -9.15015, 38.735401, -9.147803, 38.735066, -9.145475, 38.734733, -9.144841, 38.733266, -9.144561, 38.733376, -9.14503, 38.735153, -9.145655, 38.73609, -9.145803, 38.73711, -9.146598, 38.740501, -9.148458, 38.743025, -9.148105, 38.743191, -9.147953, 38.743346, -9.14764, 38.743563, -9.147911, 38.743405, -9.147981, 38.743355, -9.147938, 38.743415, -9.148001, 38.743341, -9.148598, 38.743163, -9.149945, 38.742676, -9.150211, 38.742636, -9.149691, 38.742716, -9.149518, 38.742668, -9.150115, 38.74262, -9.150153, 38.742678, -9.149668, 38.742616, -9.149853, 38.742626, -9.149838, 38.742661, -9.149863, 38.742705, -9.149156, 38.743285, -9.148181, 38.744135, -9.14789, 38.744378
r1 = d[[2]]$features[[1]]
class(r1)
#> [1] "data.frame"
lapply(r1, class)
#> $type
#> [1] "character"
#>
#> $properties
#> [1] "data.frame"
#>
#> $geometry
#> [1] "data.frame"
head(r1[[1]])
#> [1] "Feature" "Feature" "Feature" "Feature" "Feature" "Feature"
head(r1[[2]])
#> timestamp
#> 1 1.549394e+12
#> 2 1.549394e+12
#> 3 1.549394e+12
#> 4 1.549394e+12
#> 5 1.549394e+12
#> 6 1.549394e+12
head(r1[[3]])
#> type coordinates
#> 1 Point -9.161291, 38.724411
#> 2 Point -9.161128, 38.724175
#> 3 Point -9.161523, 38.724456
#> 4 Point -9.162235, 38.725988
#> 5 Point -9.162226, 38.727125
#> 6 Point -9.162021, 38.726940
r_linestring = st_linestring(t(sapply(r1$geometry$coordinates, as.numeric)))
plot(r_linestring)
get_linestrings_from_route = function(r1) {
st_linestring(t(sapply(r1$geometry$coordinates, as.numeric)))
}
d_to_sf = function(d) {
linestrings = lapply(d$route$features, get_linestrings_from_route)
linestrings_sfc = sf::st_sfc(linestrings, crs = 4326)
d_sf = sf::st_sf(d, geometry = linestrings_sfc)
d_sf
}
d_sf = d_to_sf(d)
plot(d_sf)
#> Error in plot.sf(x[, cname], main = cname, pal = pal, nbreaks = nbreaks, : plotting list-columns not supported
Created on 2020-12-23 by the reprex package (v0.3.0)