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

internal function shares_border return NULL #138

Open
ggrittz opened this issue Feb 24, 2025 · 0 comments
Open

internal function shares_border return NULL #138

ggrittz opened this issue Feb 24, 2025 · 0 comments

Comments

@ggrittz
Copy link

ggrittz commented Feb 24, 2025

`

a = plantR:::shares_border(country = 'united kingdom', country2 = 'ireland')
a
NULL
`

The polis object is an integer of value 5L and poli_u of value 7L, which does not fit any of the conditions required to return the logical FALSE/TRUE.

polis <- nrow(v) + nrow(z)
  poli_u <- nrow(y)
  if (polis == poli_u) 
    return(FALSE)
  if (poli_u < polis) 
    return(TRUE)
}

The issue arises due to geographical units with multiple polygons in the plantR::world object, e.g., United Kingdom, Somalia, Canada, Argentina...and so on.

> v
Simple feature collection with 2 features and 2 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: 40.98105 ymin: -1.68325 xmax: 51.13387 ymax: 12.02464
Geodetic CRS:  WGS 84
    iso_a2    name                       geometry
12      SO somalia MULTIPOLYGON (((41.58513 -1...
171   <NA> somalia MULTIPOLYGON (((48.9482 11....

Due to that, I simplified the function to use sf::st_intersects instead of counting the number of polygons:

my_shares_border <- function(country1 = "ireland", country2 = "united kingdom") {
  world <- world
  w <- world

  if (!country1 %in% w$name) 
    country1 <- plantR::prepLoc(plantR::prepCountry(country1))
  if (!country2 %in% w$name) 
    country2 <- plantR::prepLoc(plantR::prepCountry(country2))
  if (!country1 %in% w$name | !country2 %in% w$name) 
    stop(paste0("Country name(s) '", c(country1, 
                                       country2)[!c(country1,
                                                    country2) %in% w$name], "' not found"))
  v <- w[w$name %in% c(country1), ]
  z <- w[w$name %in% c(country2), ]
  intersects <- sf::st_intersects(v, z, sparse = FALSE)[1, 1]
  return(intersects)
}
a = my_shares_border(country1 = 'united kingdom', country2 = 'ireland')
a

If wanted, it's possible to add a buffer to each country before using sf::st_intersects

@ggrittz ggrittz changed the title internal function share_borders return NULL internal function shares_border return NULL Feb 24, 2025
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

1 participant