1

I only extracted the first two rows from my database. It's in table format and I want to transform it into a geometric format (sf format precisely). I have very little knowledge in the manipulation of geometric data...

df <- structure(list(geo_shape = c("{\"type\": \"Polygon\", \"coordinates\": [[[5.338315871184333, 48.454965608365434], [5.352256779224244, 48.45748017019558], [5.356867659649554, 48.45861272223076], [5.370024462133918, 48.441016094245384], [5.405569613196442, 48.43914862965481], [5.415928061547558, 48.431728673093616], [5.439381211779697, 48.42679332254185], [5.444199380880549, 48.428035237878305], [5.445944000842286, 48.42484001047907], [5.455491195985311, 48.4225185412161], [5.464657397091936, 48.425352681093734], [5.468365530391375, 48.42368241532468], [5.47004819517617, 48.420929121711794], [5.465176866111291, 48.41999538465838], [5.447147987326301, 48.41497730281626], [5.409778594398087, 48.3926106292164], [5.408032022516386, 48.39578741775774], [5.40566901841934, 48.40233397230922], [5.408554486817462, 48.406010702893404], [5.381714974275032, 48.415130518910686], [5.364561043252318, 48.42722847189931], [5.365845831983447, 48.43053301922544], [5.360187985333197, 48.43584796495667], [5.331724869023629, 48.430857645862986], [5.326376374855521, 48.43651583877311], [5.330644859085079, 48.446143323969885], [5.326111768828245, 48.45222639592987], [5.334305718325071, 48.453937534132834], [5.338315871184333, 48.454965608365434]]]}", 
    "{\"type\": \"Polygon\", \"coordinates\": [[[3.505427731949752, 45.640237683009346], [3.50519896080469, 45.65034231639286], [3.495829620784017, 45.66222554163975], [3.504789423481917, 45.670209200943994], [3.507908911579412, 45.67282464175393], [3.5202167693257422, 45.674443403124194], [3.530494003723245, 45.6678398023463], [3.53381404087812, 45.66183576768132], [3.531416235839427, 45.65553458489732], [3.531074298770266, 45.652015234286026], [3.524797579564321, 45.6386902338926], [3.526413181690557, 45.63173983145512], [3.521344327827282, 45.63186248025302], [3.505427731949752, 45.640237683009346]]]}"
    ), ZipCode = c("52288", "63065"), Populaton = c(113, 156)), row.names = c(NA, 
    -2L), class = c("tbl_df", "tbl", "data.frame"))

Output:

# A tibble: 2 x 3
  geo_shape                                                                                                                              ZipCode Populaton
  <chr>                                                                                                                                  <chr>       <dbl>
1 "{\"type\": \"Polygon\", \"coordinates\": [[[5.338315871184333, 48.454965608365434], [5.352256779224244, 48.45748017019558], [5.35686~ 52288         113
2 "{\"type\": \"Polygon\", \"coordinates\": [[[3.505427731949752, 45.640237683009346], [3.50519896080469, 45.65034231639286], [3.495829~ 63065         156
william3031
  • 1,653
  • 1
  • 18
  • 39
Seydou GORO
  • 1,147
  • 7
  • 13
  • 1
    Thank you for the modification @william3031 – Seydou GORO Aug 20 '20 at 04:45
  • 1
    Your geometry data appears to be in geojson format. The [geojsonsf package](https://cran.r-project.org/web/packages/geojsonsf/vignettes/geojson-sf-conversions.html) should serve your purpose well, with something like `df$geo_shape <- geojson_sf(df$geo_shape)`, for example. – Z.Lin Aug 20 '20 at 05:04
  • @Z.Lin I have the same data in geojson format. But this one is imported from an excel format cause I do not know how to handle a geosjson file. But when I try your code, it doesn't work. I have this error message: ```Error: C stack usage 15924624 is too close to the limit``` – Seydou GORO Aug 20 '20 at 05:30
  • It worked for me. Is it an operating system problem? Perhaps name file length? https://stackoverflow.com/questions/14719349/error-c-stack-usage-is-too-close-to-the-limit – william3031 Aug 20 '20 at 05:59
  • @SeydouGORO try it on a few rows first? – Z.Lin Aug 20 '20 at 07:29
  • does `df$geo_shape <- sapply(df$geo_shape, geojson_sf)` change anything in that stack usage problem? – alex_jwb90 Aug 20 '20 at 08:53
  • @alex_jwb90 the problem still persists with your codes. ```Geometry set for 1 feature geometry type: POLYGON dimension: XY bbox: xmin: 5.326112 ymin: 48.39261 xmax: 5.470048 ymax: 48.45861 z_range: zmin: NA zmax: NA m_range: mmin: NA mmax: NA Error: C stack usage 15923392 is too close to the limit``` – Seydou GORO Aug 20 '20 at 09:04
  • @Z.Lin I tried it on only two rows, and the error remains – Seydou GORO Aug 20 '20 at 09:06
  • ah, I'm sorry, I think I should've suggested `df$geo_shape <- lapply(df$geo_shape, geojson_sf)`, since `sapply` tries to simplify the result. however, I assume this then wouldn't impact your stack problem. it does, however, work like a charm on the two rows you posted – alex_jwb90 Aug 20 '20 at 09:13
  • @alex_jwb90, ```lapply``` doesn't solve the problem and on only two rows. Could it be related to the capacity of my hard drive which has 10 gigabytes of memory left. If I import the json file instead of the excel file will it simplify things? – Seydou GORO Aug 20 '20 at 09:23
  • so the `lapply`solution does or does not work on two rows for you? if it does, can you scale up the number of rows and see when it fails? With `library(dplyr)`, try `df %>% head(100) %>% mutate(geo_shape = lapply(geo_shape, geojson_sf))` and try some other values in the `head` call until it explodes. (also, it shouldn't be a hard drive problem, the "stack usage" error message usually hints at some recursion-loop in the code..) – alex_jwb90 Aug 20 '20 at 09:32
  • This `C Stack` error is caused by the `print` function in `sf`, caused by differeng versions. `geojsonsf` v0.2.2 is designed to work with `sf` v0.9.0 [see here](https://github.com/SymbolixAU/geojsonsf/blob/master/R/onAttach.R) – SymbolixAU Aug 24 '20 at 04:54

0 Answers0