Skip to content

crsuggest::suggest_crs() hangs when units are specified and there is no match. #14

@ericrobskyhuntley

Description

@ericrobskyhuntley

Causes a problem because US State Plane crses are sometimes in "ft", sometimes in "us-ft". Could probably be resolved by allowing multiple candidate units in units parameter. For an example breaking case...

d1<- sf::st_polygon(
  list(matrix(
    c(-73.90909,41.19984,
      -73.81354,41.19984,
      -73.81354,41.12057,
      -73.90909,41.12057,
      -73.90909,41.19984
      ),
    ncol=2, 
    byrow=TRUE
    ))
) |>
  sf::st_sfc(crs=4326) |>
  sf::st_as_sf()

# Works
crsuggest::suggest_crs(d1, limit = 3, units = "us-ft") |>
  dplyr::select(crs_code, crs_name)

# A tibble: 3 × 2
#  crs_code crs_name                              
#  <chr>    <chr>                                 
# 1 6537     NAD83(2011) / New York East (ftUS)    
# 2 3626     NAD83(NSRS2007) / New York East (ftUS)
# 3 32015    NAD27 / New York East    

# Hangs
crsuggest::suggest_crs(d1, units = "ft")

# ...

But the following returns fine with either unit:

d2 <- sf::st_polygon(
  list(matrix(
    c(-83.79956,42.32397,
      -83.67580,42.32397,
      -83.67580,42.22268,
      -83.79956,42.22268,
      -83.79956,42.32397
    ),
    ncol=2, 
    byrow=TRUE
  ))
) |>
  sf::st_sfc(crs=4326) |>
  sf::st_as_sf()

# Works
crsuggest::suggest_crs(d2, limit = 3, units = "us-ft") |>
  dplyr::select(crs_code, crs_name)

# A tibble: 3 × 2
#  crs_code crs_name              
#  <chr>    <chr>                 
# 1 6202     NAD27 / Michigan South
# 2 5623     NAD27 / Michigan East 
# 3 32167    NAD83 / BLM 17N (ftUS)

# Works
crsuggest::suggest_crs(d2, limit = 3, units = "ft") |>
  dplyr::select(crs_code, crs_name)

# A tibble: 3 × 2
#  crs_code crs_name                             
#  <chr>    <chr>                                
# 1 6499     NAD83(2011) / Michigan South (ft)    
# 2 3593     NAD83(NSRS2007) / Michigan South (ft)
# 3 2898     NAD83(HARN) / Michigan South (ft) 

My guess is that this is the culprit (similar to #8):

crsuggest/R/suggest_crs.R

Lines 133 to 143 in 77f91d9

if (nrow(crs_sub) == 0) {
rows <- nrow(crs_sub)
bufdist <- -250
while (rows == 0) {
new_buf <- st_buffer(sf_poly, bufdist)
crs_sub <- crs_type[new_buf, ]
rows <- nrow(crs_sub)
bufdist <- bufdist / 2
}
}

There's a corresponding break on crsuggest::suggest_top_crs(), but because that seems to simply run crsuggest::suggest_crs(..., limit = 1, ...), fixing suggest_crs() would fix suggest_top_crs().

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions