Skip to contents

Try to figure out if user provided latitude / longitude as vectors, data.frame, file, or interactively pick file.

Usage

latlon_from_anything(
  anything,
  lon_if_used,
  interactiveprompt = TRUE,
  invalid_msg_table = FALSE,
  set_invalid_to_na = TRUE
)

Arguments

anything

If missing and interactive mode in RStudio, prompts user for file. Otherwise, this can be a filename (csv or xlsx, with path), or data.frame/ data.table/ matrix, or vector of longitudes (in which case y must be the latitudes). File or data.frame/data.table/matrix must have columns called lat and lon, or names that can be inferred to be that by latlon_infer()

lon_if_used

If anything parameter is a vector of longitudes, lon_if_used must be the latitudes. Ignored otherwise.

interactiveprompt

If TRUE (default) and in interactive mode not running shiny, will prompt user for file if "anything" is missing.

invalid_msg_table

Set to TRUE to add columns "valid" and "invalid_msg" to output

set_invalid_to_na

used by latlon_df_clean()

Value

A data.frame that has at least columns lon and lat (and others if they were in anything), and a logical column called "valid"

Details

Also see closely related function sitepoints_from_any().

This function relies on

read_csv_or_xl() and

latlon_df_clean() which in turn uses latlon_infer() latlon_as.numeric() latlon_is.valid()

A draft function read_and_clean_points() would a more general way to get points,

but is still work in progress... it is similar to latlon_from_anything()

except it also uses these functions:

latlon_from_regid(), latlon_from_programid()

and could eventually use _from_naics() etc.

Even more generally, FIPS and shapefile inputs could be read through a single wrapper function at some point.

See also

sitepoints_from_any() which is like this but also adds ejam_uniq_id column, and see read_csv_or_xl() and latlon_df_clean()

Examples

 latlon_from_anything(testpoints_10)
#>         lat        lon sitenumber        sitename
#> 1  37.64122 -122.41065          1  Example Site 1
#> 2  43.92249  -72.66370          2  Example Site 2
#> 3  40.69351  -73.98002          3  Example Site 3
#> 4  45.29520  -92.98184          4  Example Site 4
#> 5  39.43730  -74.72370          5  Example Site 5
#> 6  45.67487  -94.81534          6  Example Site 6
#> 7  33.14865 -117.19895          7  Example Site 7
#> 8  34.01176  -80.95414          8  Example Site 8
#> 9  43.01026  -89.45952          9  Example Site 9
#> 10 40.74625  -74.25955         10 Example Site 10
 latlon_from_anything(testpoints_10$lat, testpoints_10$lon)
#>         lat        lon
#> 1  37.64122 -122.41065
#> 2  43.92249  -72.66370
#> 3  40.69351  -73.98002
#> 4  45.29520  -92.98184
#> 5  39.43730  -74.72370
#> 6  45.67487  -94.81534
#> 7  33.14865 -117.19895
#> 8  34.01176  -80.95414
#> 9  43.01026  -89.45952
#> 10 40.74625  -74.25955
 pts = c("33,-100", "32,-101")
 latlon_from_anything(pts)
#>   lat  lon
#> 1  33 -100
#> 2  32 -101
 pts = data.frame(Longitude = testpoints_10$lon, Latitude = testpoints_10$lat)
 latlon_from_anything(pts)
#> Replaced column names that were inferred to be and therefore renamed as the lat and/or lon columns!
#>           lon      lat
#> 1  -122.41065 37.64122
#> 2   -72.66370 43.92249
#> 3   -73.98002 40.69351
#> 4   -92.98184 45.29520
#> 5   -74.72370 39.43730
#> 6   -94.81534 45.67487
#> 7  -117.19895 33.14865
#> 8   -80.95414 34.01176
#> 9   -89.45952 43.01026
#> 10  -74.25955 40.74625
 pts = data.table(Lat = testpoints_10$lat, Long = testpoints_10$lon)
#> Error in data.table(Lat = testpoints_10$lat, Long = testpoints_10$lon): could not find function "data.table"
 latlon_from_anything(pts)
#> Replaced column names that were inferred to be and therefore renamed as the lat and/or lon columns!
#>           lon      lat
#> 1  -122.41065 37.64122
#> 2   -72.66370 43.92249
#> 3   -73.98002 40.69351
#> 4   -92.98184 45.29520
#> 5   -74.72370 39.43730
#> 6   -94.81534 45.67487
#> 7  -117.19895 33.14865
#> 8   -80.95414 34.01176
#> 9   -89.45952 43.01026
#> 10  -74.25955 40.74625
 if (FALSE) { # \dontrun{
 if (interactive()) {
   pts <- latlon_from_anything()
 }} # }
 if (FALSE) { # \dontrun{
 pts = system.file("testdata/latlon/testpoints_10.xlsx", package = "EJAM")
 latlon_from_anything(pts)
 } # }