program-- / fipio

An R :package: for lightweight FIPS code information retrieval
https://fipio.justinsingh.me
Other
14 stars 1 forks source link

incorrect FIPS output when using coords_to_fips() on coordinates in Colorado #18

Open henhirsch opened 3 months ago

henhirsch commented 3 months ago

Hello,

First of all, thank you for developing this package. It's very helpful!

I believe I may have run into a bug when attempting to use coords_to_fips() to convert pairs of coordinates in Colorado to FIPS codes. Instead of getting the corresponding Colorado FIPS codes as my output, I get either a null value or Virginia FIPS codes (51059 and 51153). I'm attempting to use coords_to_fips() on the following pairs of coordinates: (39.705205, -104.760162) , (38.802568, -104.518953) , (38.824734, -104.704033) , and (38.735846, -104.787681).

I'd appreciate any clarity you can provide on this issue.

Thanks very much!

program-- commented 3 months ago

Hi, I suspect either (1) you may have flipped the coordinates, fipio::coords_to_fips() takes XY coordinates (which may be a mistake, since I specified in the documentation that coordinates should be in WGS84 😅), so instead of using the coordinates as is, you'd want to flip their positions when calling the function, i.e.

dplyr::tibble(
    lat = c(39.705205, 38.802568, 38.824734, 38.735846),
    lon = c(-104.760162, -104.518953, -104.704033, -104.787681)
) |>
    dplyr::mutate(
        yx = fipio::coords_to_fips(lat, lon),
        xy = fipio::coords_to_fips(lon, lat)
    )
#> # A tibble: 4 × 4
#>     lat   lon yx    xy     
#>   <dbl> <dbl> <chr> <chr>  
#> 1  39.7 -105. ""    ""     
#> 2  38.8 -105. ""    "08041"
#> 3  38.8 -105. ""    "08041"
#> 4  38.7 -105. ""    "08041"

Created on 2024-05-24 with reprex v2.1.0

or, (2) there's an issue with the fipio::coords_to_fips function, which is also possible since it's a very rudimentary function that I need to refactor in the near future (i.e. the first row in the above tibble, which is clearly in CO but doesn't provide the correct FIPS, which may be due to Denver Intl. breaking up the boundaries).

Hope that helps! I don't have an exact ETA on when fipio::coords_to_fips will be redone, but hopefully soon.

henhirsch commented 3 months ago

I did make sure to flip the order of the coordinates to XY beforehand, so I'm not sure that's the issue.

But your code does seem to produce the correct FIPS code, whereas mine does not. I may have made an error by converting my lat and lon values into vectors before running them through the coords_to_fips function.

I look forward to future updates. Thanks for your helpful response!