briatte / briatte/selection-bv

`mapvotr` alternative to Etalab shapes, using aggregated file

Open
#3 3 comments 0 reactions 0 assignees View on GitHub
bug data
Dominant language
R
Stars
2
Forks
0
PR merge metrics
No merged PRs in 30d

Description

- https://github.com/inseeFrLab/mapvotr
- https://github.com/cedricr/bureau-vote-insee
- https://www.data.gouv.fr/datasets/proposition-de-contours-des-bureaux-de-vote-selon-la-methode-de-linsee

```r
sf::st_read("~/Downloads/contours-bureaux-vote.json") %>%
dplyr::filter(codeCommune== "59350") %>%
dplyr::mutate(numeroBureauVote = codeBureauVote,
codeBureauVote = str_c(codeCommune, numeroBureauVote,
sep = "_")) %>%
readr::write_rds("sorties/contours-Lille-BV.rds")
```

The result is close enough to the Etalab version.

```r
# missing CRS, so add it
ggplot(read_rds("sorties/contours-Lille-BV.rds") %>%
st_set_crs("WGS84")) +
geom_sf(fill = "grey", color = "red", size = 2) +
# saved with CRS 2154
geom_sf(data = read_rds("sorties/contours-Lille-BV-Etalab.rds"),
fill = NA, color = "black", size = 1) +
theme_minimal()

ggsave("insee-v-etalab.png", width = 10, height = 10)
```

Image

# Problem

Won't work with `sfReapportion` straight out of the box, due to missing CRS in `bv_fdc`. If I save `iris_fdc` without reprojecting to 2154 like the code currently does (so, keeping the `RGF93` CRS provided by IGN), I have to pass this to get somewhere, but get an error anyway:

```r
sfReapportion::sfReapportion(st_transform(iris_fdc, "WGS84"),
st_set_crs(bv_fdc, "WGS84"), iris_base,
"CODE_IRIS", "codeBureauVote", "IRIS")
```
```
Error in wk_handle.wk_wkb(wkb, s2_geography_writer(oriented = oriented, :
Loop 0 is not valid: Edge 46 is degenerate (duplicate vertex)
```

This won't work either:

```r
sfReapportion::sfReapportion(st_transform(iris_fdc, 2154),
st_set_crs(bv_fdc, "WGS84") %>%
st_transform(2154), iris_base,
"CODE_IRIS", "codeBureauVote", "IRIS")
```
```
Error during wrapup: scan() expected 'a real', got '7059098.4432252664.'
Error: no more error handlers available (recursive errors?); invoking 'abort' restart
Error during wrapup: TopologyException: side location conflict at 707261.52056810772 7059098.4432252664. This can occur if the input geometry is invalid.
Error: no more error handlers available (recursive errors?); invoking 'abort' restart
```

That same error also comes up when I try the same procedure with `{areal}`, even though the objects pass `areal::ar_validate`:

```r
library(areal)
library(testthat)

# reproject to planar CRS (Lambert 93) and add variables
src <- sf::st_transform(iris_fdc, 2154) %>%
dplyr::left_join(iris_base, by = c("CODE_IRIS" = "IRIS"))
src_vars <- names(iris_base)[ -1 ]
trg <- sf::st_transform(st_set_crs(bv_fdc, "WGS84"), 2154)

stopifnot(areal::ar_validate(src, trg, varList = src_vars))
# areal::ar_validate(src, trg, varList = src_vars, verbose = TRUE)

with_areal <- areal::aw_interpolate(select(trg, -id), tid = "codeBureauVote",
source = src, sid = "CODE_IRIS",
extensive = src_vars,
weight = "total", output = "tibble") %>%
dplyr::select(codeBureauVote, dplyr::all_of(src_vars)) %>%
dplyr::arrange(codeBureauVote) %>%
as.data.frame()

testthat::expect_equal(base_soc, with_areal, tolerance = 1e-4)
```
```
> with_areal <- areal::aw_interpolate(select(trg, -id), tid = "codeBureauVote" .... [TRUNCATED]
Error in `map2()`:
ℹ In index: 1.
ℹ With name: Actifs.
Caused by error in `scan()`:
! scan() expected 'a real', got '7059098.4432252664.'
Run `rlang::last_trace()` to see where the error occurred.
Error in `map2()`:
ℹ In index: 1.
ℹ With name: Actifs.
Caused by error:
! TopologyException: side location conflict at 707261.52056810772 7059098.4432252664. This can occur if the input geometry is invalid.
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reproducing the sfReapportion and areal examples in the issue, then inspect the CRS and geometry validity of bv_fdc, iris_fdc, and their transformed versions. Compare the resulting errors with the ar_validate output; done means the aggregated bureau shapes work with the shown workflow and the resulting values can be checked against base_soc.

Written by the indexing model from the issue text.

Assessment

Tech stack
r
Domain
data
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.