Skip to content
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

OGR Error when trying to convert polygons into sf objects #155

Open
adejumoridwan opened this issue Dec 4, 2024 · 2 comments
Open

OGR Error when trying to convert polygons into sf objects #155

adejumoridwan opened this issue Dec 4, 2024 · 2 comments

Comments

@adejumoridwan
Copy link

I am trying to build a choropleth map using the data result from the following code.

library(sf)
libarary(rdhs)

result <- dhs_data(
    indicatorIds = c("CN_NUTS_C_HA2"), 
    countryIds = c("GH", "NG"),
    returnGeometry = TRUE
) 
result

But I am getting the following error when trying to convert the variable Coordinates polygon into sf objects.

> data_sf <- st_as_sf(a, wkt = "Coordinates", crs = 4326)
OGR: Corrupt data
Error: OGR error

Further investigation revealed that the Coordinates variable is being truncated. It is supposed to end with a )). I compared it with the online API and found out that some values where missing.

> substr(result$Coordinates[1], nchar(result$Coordinates[1]) - 5, nchar(result$Coordinates[1]))
[1] "4.5868"

Please I would kindly appreciate you guys help on this.

Session Info
> devtools::session_info()
─ Session info ─────────────────────────────────────────────────────────
 setting  value
 version  R version 4.4.1 (2024-06-14 ucrt)
 os       Windows 10 x64 (build 19045)
 system   x86_64, mingw32
 ui       RStudio
 language (EN)
 collate  English_United States.utf8
 ctype    English_United States.utf8
 tz       Africa/Lagos
 date     2024-12-04
 rstudio  2024.04.2+764 Chocolate Cosmos (desktop)
 pandoc   NAPackages ─────────────────────────────────────────────────────────────
 package     * version    date (UTC) lib source
 cachem        1.1.0      2024-05-16 [1] CRAN (R 4.4.1)
 class         7.3-22     2023-05-03 [2] CRAN (R 4.4.1)
 classInt      0.4-10     2023-09-05 [1] CRAN (R 4.4.1)
 cli           3.6.3      2024-06-21 [1] CRAN (R 4.4.1)
 colorspace    2.1-1      2024-07-26 [1] CRAN (R 4.4.1)
 crosstalk     1.2.1      2023-11-23 [1] CRAN (R 4.4.1)
 curl          5.2.2      2024-08-26 [1] CRAN (R 4.4.1)
 data.table  * 1.16.0     2024-08-27 [1] CRAN (R 4.4.1)
 DBI           1.2.3      2024-06-02 [1] CRAN (R 4.4.1)
 devtools      2.4.5      2022-10-11 [1] CRAN (R 4.4.1)
 digest        0.6.37     2024-08-19 [1] CRAN (R 4.4.1)
 dplyr       * 1.1.4      2023-11-17 [1] CRAN (R 4.4.1)
 e1071         1.7-16     2024-09-16 [1] CRAN (R 4.4.1)
 ellipsis      0.3.2      2021-04-29 [1] CRAN (R 4.4.1)
 fansi         1.0.6      2023-12-08 [1] CRAN (R 4.4.1)
 fastmap       1.2.0      2024-05-15 [1] CRAN (R 4.4.1)
 forcats     * 1.0.0      2023-01-29 [1] CRAN (R 4.4.1)
 fs            1.6.4      2024-04-25 [1] CRAN (R 4.4.1)
 generics      0.1.3      2022-07-05 [1] CRAN (R 4.4.1)
 getPass       0.2-4      2023-12-10 [1] CRAN (R 4.4.1)
 ggplot2     * 3.5.1      2024-04-23 [1] CRAN (R 4.4.1)
 glue          1.8.0      2024-09-30 [1] CRAN (R 4.4.1)
 gridExtra     2.3        2017-09-09 [1] CRAN (R 4.4.1)
 gt          * 0.11.1     2024-10-04 [1] CRAN (R 4.4.2)
 gtable        0.3.6      2024-10-25 [1] CRAN (R 4.4.1)
 haven       * 2.5.4      2023-11-30 [1] CRAN (R 4.4.1)
 hms           1.1.3      2023-03-21 [1] CRAN (R 4.4.1)
 htmltools     0.5.8.1    2024-04-04 [1] CRAN (R 4.4.1)
 htmlwidgets   1.6.4      2023-12-06 [1] CRAN (R 4.4.1)
 httpuv        1.6.15     2024-03-26 [1] CRAN (R 4.4.1)
 httr          1.4.7      2023-08-15 [1] CRAN (R 4.4.1)
 jsonlite      1.8.8      2023-12-04 [1] CRAN (R 4.4.1)
 KernSmooth    2.23-24    2024-05-17 [2] CRAN (R 4.4.1)
 knitr         1.48       2024-07-07 [1] CRAN (R 4.4.1)
 later         1.3.2      2023-12-06 [1] CRAN (R 4.4.1)
 lattice       0.22-6     2024-03-20 [2] CRAN (R 4.4.1)
 lazyeval      0.2.2      2019-03-15 [1] CRAN (R 4.4.1)
 leaflet     * 2.2.2      2024-03-26 [1] CRAN (R 4.4.2)
 lifecycle     1.0.4      2023-11-07 [1] CRAN (R 4.4.1)
 lubridate   * 1.9.3      2023-09-27 [1] CRAN (R 4.4.1)
 magrittr      2.0.3      2022-03-30 [1] CRAN (R 4.4.1)
 Matrix      * 1.7-0      2024-04-26 [2] CRAN (R 4.4.1)
 memoise       2.0.1      2021-11-26 [1] CRAN (R 4.4.1)
 mime          0.12       2021-09-28 [1] CRAN (R 4.4.0)
 miniUI        0.1.1.1    2018-05-18 [1] CRAN (R 4.4.1)
 mitools       2.4        2019-04-26 [1] CRAN (R 4.4.1)
 munsell       0.5.1      2024-04-01 [1] CRAN (R 4.4.1)
 pillar        1.9.0      2023-03-22 [1] CRAN (R 4.4.1)
 pivottabler * 1.5.5      2023-10-01 [1] CRAN (R 4.4.2)
 pkgbuild      1.4.4      2024-03-17 [1] CRAN (R 4.4.1)
 pkgconfig     2.0.3      2019-09-22 [1] CRAN (R 4.4.1)
 pkgload       1.4.0      2024-06-28 [1] CRAN (R 4.4.1)
 plotly      * 4.10.4     2024-01-13 [1] CRAN (R 4.4.2)
 profvis       0.4.0      2024-09-20 [1] CRAN (R 4.4.1)
 promises      1.3.0      2024-04-05 [1] CRAN (R 4.4.1)
 proxy         0.4-27     2022-06-09 [1] CRAN (R 4.4.1)
 purrr       * 1.0.2      2023-08-10 [1] CRAN (R 4.4.1)
 R6            2.5.1      2021-08-19 [1] CRAN (R 4.4.1)
 rappdirs      0.3.3      2021-01-31 [1] CRAN (R 4.4.1)
 Rcpp          1.0.13     2024-07-17 [1] CRAN (R 4.4.1)
 rdhs        * 0.8.1      2023-11-21 [1] CRAN (R 4.4.1)
 readr       * 2.1.5      2024-01-10 [1] CRAN (R 4.4.1)
 remotes       2.5.0      2024-03-17 [1] CRAN (R 4.4.1)
 rlang         1.1.4      2024-06-04 [1] CRAN (R 4.4.1)
 rstudioapi    0.16.0     2024-03-24 [1] CRAN (R 4.4.1)
 scales        1.3.0      2023-11-28 [1] CRAN (R 4.4.1)
 sessioninfo   1.2.2      2021-12-06 [1] CRAN (R 4.4.1)
 sf          * 1.0-19     2024-11-05 [1] CRAN (R 4.4.2)
 shiny         1.9.1      2024-08-01 [1] CRAN (R 4.4.1)
 storr         1.2.5      2020-12-01 [1] CRAN (R 4.4.1)
 stringi       1.8.4      2024-05-06 [1] CRAN (R 4.4.0)
 stringr     * 1.5.1      2023-11-14 [1] CRAN (R 4.4.1)
 survey      * 4.4-2      2024-03-20 [1] CRAN (R 4.4.1)
 survival    * 3.6-4      2024-04-24 [2] CRAN (R 4.4.1)
 tibble      * 3.2.1      2023-03-20 [1] CRAN (R 4.4.1)
 tidyr       * 1.3.1      2024-01-24 [1] CRAN (R 4.4.1)
 tidyselect    1.2.1      2024-03-11 [1] CRAN (R 4.4.1)
 tidyverse   * 2.0.0      2023-02-22 [1] CRAN (R 4.4.1)
 timechange    0.3.0      2024-01-18 [1] CRAN (R 4.4.1)
 tzdb          0.4.0      2023-05-12 [1] CRAN (R 4.4.1)
 units         0.8-5      2023-11-28 [1] CRAN (R 4.4.1)
 urbnmapr    * 0.0.0.9002 2024-12-03 [1] Github (UrbanInstitute/urbnmapr@ef9f448)
 urlchecker    1.0.1      2021-11-30 [1] CRAN (R 4.4.1)
 usethis       3.0.0      2024-07-29 [1] CRAN (R 4.4.1)
 utf8          1.2.4      2023-10-22 [1] CRAN (R 4.4.1)
 vctrs         0.6.5      2023-12-01 [1] CRAN (R 4.4.1)
 viridis     * 0.6.5      2024-01-29 [1] CRAN (R 4.4.1)
 viridisLite * 0.4.2      2023-05-02 [1] CRAN (R 4.4.1)
 withr         3.0.2      2024-10-28 [1] CRAN (R 4.4.1)
 xfun          0.47       2024-08-17 [1] CRAN (R 4.4.1)
 xml2          1.3.6      2023-12-04 [1] CRAN (R 4.4.1)
 xtable        1.8-4      2019-04-21 [1] CRAN (R 4.4.1)

 [1] C:/Users/abu aisha/AppData/Local/R/win-library/4.4
 [2] C:/Program Files/R/R-4.4.1/library

────────────────────────────────────────────────────────────────────────
@jeffeaton
Copy link
Collaborator

Hi @adejumoridwan -- many thanks for submitting this. I am getting a slightly different result from you from the last line, but same general issue:

> substr(result$Coordinates[1], nchar(result$Coordinates[1]) - 5, nchar(result$Coordinates[1]))
[1] "3, -1."

I think the Coordinates column would ideally be returning a list. I'm getting a very long string, but truncated at 32,000 characters for each item:

> nchar(result$Coordinates)
 [1] 32000 32000 32000 32000 32000 32000 32000 32000 32000 32000

I'm not sure what the underlying issue is here and if/how it's solvable; will try to look into it more when I have time.

But a potential workaround is to separately download the DHS region boundaries using the rdhs::download_boundaries() function.

Here is an example. Also, I noticed that the dhs_data() call was simply national data, so no subnational data. I added breakdown = "subnational" such that it it returns for multiple subnational locations.

library(sf)
library(rdhs)
library(dplyr)
library(ggplot2)


result <- dhs_data(
    indicatorIds = c("CN_NUTS_C_HA2"), 
    countryIds = c("GH", "NG"),
    breakdown="subnational",
    returnGeometry = FALSE
)

geo_raw <- lapply(unique(result$SurveyId), \(x) download_boundaries(surveyId = x))

geo <- lapply(geo_raw, bind_rows) %>%
  bind_rows()

result_sf <- result %>%
  left_join(select(geo, REG_ID, geometry), by = c("RegionId" = "REG_ID"))

result_sf <- st_as_sf(result_sf)

result_sf %>%
  ggplot(aes(fill = Value)) +
  geom_sf() +
  facet_wrap(~SurveyId) +
  theme_bw()

image

@adejumoridwan
Copy link
Author

Thanks @jeffeaton for the workaround. I appreciate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants