Title: | Obtain Suggested Coordinate Reference System Information for Spatial Data |
---|---|
Description: | Uses data from the 'EPSG' Registry to look up suitable coordinate reference system transformations for spatial datasets in R. Returns a data frame with 'CRS' codes that can be used for 'CRS' transformation and mapping projects. Please see the 'EPSG' Dataset Terms of Use at <https://epsg.org/terms-of-use.html> for more information. |
Authors: | Kyle Walker [aut, cre] |
Maintainer: | Kyle Walker <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.4 |
Built: | 2024-11-04 04:05:05 UTC |
Source: | https://github.com/walkerke/crsuggest |
Used by the crs_suggest()
function to look up suitable
coordinate reference systems for input spatial data. Terms of use are available
at https://epsg.org/terms-of-use.html.
data(crs_sf)
data(crs_sf)
An object of class sf
(inherits from tbl_df
, tbl
, data.frame
) with 6144 rows and 7 columns.
Dataset of area extent polygons that correspond to CRS codes
https://epsg.org/terms-of-use.html
Uses data from the EPSG Registry to look up suitable coordinate reference system transformations for spatial datasets in R. Returns a data frame with CRS codes that can be used for CRS transformation and mapping projects. Please see the EPSG Dataset Terms of Use at https://epsg.org/terms-of-use.html for more information.
Kyle Walker
This function will "guess" possible coordinate reference systems for spatial data that are
lacking a CRS definition. Input data, which must be of class "sf"
(or which can be converted to sf) might be objects
created from CSV files that use projected coordinates or objects created from shapefiles
loaded with sf::st_read()
that are missing .prj files. The function requires a
"target location" that the user knows to be within the general area of the input dataset. It
then identifies suitable coordinate reference systems for that area and "tests out"
those CRSs for the input data by analyzing the distance between the dataset and the known location
when that CRS is used. Those distances are returned by the function in a column dist_km
;
short distances represent better guesses for the CRS whereas longer distances suggest that
the CRS wouldn't work.
guess_crs(input, target_location, units = NULL, n_return = 10, input_sf = NULL)
guess_crs(input, target_location, units = NULL, n_return = 10, input_sf = NULL)
input |
A spatial dataset of class |
target_location |
A coordinate pair of form |
units |
If known, the units of your projected coordinate system (e.g. |
n_return |
The number of possible CRS choices to return; defaults to 10. A higher
number than that may include CRS options that are unlikely to work with
your data. Use the returned |
input_sf |
Deprecated; use |
A tibble of CRS guesses for your data, sorted in ascending order of distance between your target location and the input sf object's centroid when in that CRS.
## Not run: library(crsuggest) library(sf) # An example data frame of projected coordinates with no CRS information included locations <- data.frame( X = c(2312654.74514528, 2357493.02092003, 2398978.30047505, 2344378.47525209, 2475776.26735713, 2493751.94421798, 2456797.1698781, 2448392.13089886, 2319704.35367616, 2350119.25250331, 2449088.54659236, 2423774.3668849), Y = c(6966055.04531077, 6994256.06222144, 6951975.79788762, 6902972.35980149, 6918178.81070276, 6977643.56941746, 7053989.26343385, 7024543.36487243, 7015476.52061313, 6953350.28550116, 6945011.24615857, 6912284.16691977), id = 1:12 ) # Create an sf object but the CRS is not known locations_sf <- st_as_sf(locations, coords = c("X", "Y")) # Use `guess_crs()` to guess the CRS used for the coordinates along with a known coordinate # in the area of interest guesses <- guess_crs(locations_sf, target_location = c(-97.1071, 32.7356)) # Set the CRS of your data with the "best guess" st_crs(locations_sf) <- 6584 ## End(Not run)
## Not run: library(crsuggest) library(sf) # An example data frame of projected coordinates with no CRS information included locations <- data.frame( X = c(2312654.74514528, 2357493.02092003, 2398978.30047505, 2344378.47525209, 2475776.26735713, 2493751.94421798, 2456797.1698781, 2448392.13089886, 2319704.35367616, 2350119.25250331, 2449088.54659236, 2423774.3668849), Y = c(6966055.04531077, 6994256.06222144, 6951975.79788762, 6902972.35980149, 6918178.81070276, 6977643.56941746, 7053989.26343385, 7024543.36487243, 7015476.52061313, 6953350.28550116, 6945011.24615857, 6912284.16691977), id = 1:12 ) # Create an sf object but the CRS is not known locations_sf <- st_as_sf(locations, coords = c("X", "Y")) # Use `guess_crs()` to guess the CRS used for the coordinates along with a known coordinate # in the area of interest guesses <- guess_crs(locations_sf, target_location = c(-97.1071, 32.7356)) # Set the CRS of your data with the "best guess" st_crs(locations_sf) <- 6584 ## End(Not run)
This function takes an input spatial dataset as input and makes "suggestions" for suitable coordinate reference systems that could be used for CRS transformations in spatial analysis projects. The function works by analyzing the extent of the spatial dataset and comparing it to the area extents in the EPSG's coordinate reference system database. The "suggested" coordinate reference systems are determined by minimizing the Hausdorff distances between the CRS area extents and the input dataset, subject to user preferences (such as a geographic coordinate system ID or measurement units).
suggest_crs( input, type = "projected", limit = 10, gcs = NULL, units = NULL, drop_na = TRUE )
suggest_crs( input, type = "projected", limit = 10, gcs = NULL, units = NULL, drop_na = TRUE )
input |
A spatial dataset of class |
type |
The output CRS type; defaults to |
limit |
How many results to return; defaults to |
gcs |
(optional) The EPSG code for the corresponding geographic coordinate system of the results (e.g. |
units |
(optional) The measurement units of the coordinate systems in the returned results. Can be one of |
drop_na |
Whether or not to drop EPSG codes that do not appear in the PROJ database (and thus can't be used for CRS transformation). Defauts to |
A data frame with information about coordinate reference systems that could be suitably used for CRS transformation.
## Not run: library(tigris) library(crsuggest) # Get a dataset of Census tracts for Nassau County, NY nassau_tracts <- tracts("NY", "Nassau", cb = TRUE) # tigris datasets default to the NAD1983 GCS (EPSG code 4269) # What are some appropriate projected coordinate systems? suggest_crs(nassau_tracts) # Alternatively, we can require projections to have specific # geographic coordinate systems and/or units # For example, let's say we only want NAD83(HARN) (code 4152) # and we want the measurement units to be US feet suggest_crs(nassau_tracts, gcs = 4152, units = "us-ft") ## End(Not run)
## Not run: library(tigris) library(crsuggest) # Get a dataset of Census tracts for Nassau County, NY nassau_tracts <- tracts("NY", "Nassau", cb = TRUE) # tigris datasets default to the NAD1983 GCS (EPSG code 4269) # What are some appropriate projected coordinate systems? suggest_crs(nassau_tracts) # Alternatively, we can require projections to have specific # geographic coordinate systems and/or units # For example, let's say we only want NAD83(HARN) (code 4152) # and we want the measurement units to be US feet suggest_crs(nassau_tracts, gcs = 4152, units = "us-ft") ## End(Not run)
Return the EPSG code or proj4string syntax for the top-ranking projected coordinate reference system returned by suggest_crs()
. This function should be used with caution and is recommended for interactive work rather than in production data pipelines.
suggest_top_crs(input, units = NULL, inherit_gcs = TRUE, output = "epsg")
suggest_top_crs(input, units = NULL, inherit_gcs = TRUE, output = "epsg")
input |
An input spatial dataset of class |
units |
(optional) The measurement units used by the returned coordinate reference system. |
inherit_gcs |
if |
output |
one of |
The EPSG code or proj4string for the output coordinate reference system.
## Not run: # Let's say we are working with a demographic dataset from the US Census: library(tidycensus) library(ggplot2) library(sf) library(crsuggest) tx_income <- get_acs( geography = "county", variables = "B19013_001", state = "TX", geometry = TRUE ) # We can use `suggest_top_crs()` to return the EPSG code of the "top" suggested CRS # for statewide mapping of Texas tx_crs <- suggest_top_crs(tx_income) # The returned CRS is EPSG code 3083, NAD83 / Texas Centric Albers Equal Area. # This code can be used for visualization: ggplot(tx_income, aes(fill = estimate)) + geom_sf() + coord_sf(crs = tx_crs) # Alternatively, we can transform the CRS of our sf object directly: tx_projected <- st_transform(tx_income, tx_crs) ## End(Not run)
## Not run: # Let's say we are working with a demographic dataset from the US Census: library(tidycensus) library(ggplot2) library(sf) library(crsuggest) tx_income <- get_acs( geography = "county", variables = "B19013_001", state = "TX", geometry = TRUE ) # We can use `suggest_top_crs()` to return the EPSG code of the "top" suggested CRS # for statewide mapping of Texas tx_crs <- suggest_top_crs(tx_income) # The returned CRS is EPSG code 3083, NAD83 / Texas Centric Albers Equal Area. # This code can be used for visualization: ggplot(tx_income, aes(fill = estimate)) + geom_sf() + coord_sf(crs = tx_crs) # Alternatively, we can transform the CRS of our sf object directly: tx_projected <- st_transform(tx_income, tx_crs) ## End(Not run)
The crsuggest package makes coordinate reference systems suggestions that may not be perfect for your specific analytic use case. Use view_crs()
to quickly view the geographic extent of a given coordinate reference system (represented by its EPSG code) and assess whether that CRS makes sense for your data.
view_crs(crs)
view_crs(crs)
crs |
A character string representing the EPSG code of an input coordinate reference system, possibly returned by |
an object of class mapview
which uses the mapview package to preview the extent of a coordinate reference system.
## Not run: library(tigris) library(crsuggest) options(tigris_use_cache = TRUE) # Get a Census tract dataset from the tigris package tarrant_tracts <- tracts("TX", "Tarrant", cb = TRUE, year = 2021) # Suggest a CRS for your data target_crs <- suggest_top_crs(tarrant_tracts, units = "m", inherit_gcs = FALSE) # Preview the extent of the CRS view_crs(target_crs) ## End(Not run)
## Not run: library(tigris) library(crsuggest) options(tigris_use_cache = TRUE) # Get a Census tract dataset from the tigris package tarrant_tracts <- tracts("TX", "Tarrant", cb = TRUE, year = 2021) # Suggest a CRS for your data target_crs <- suggest_top_crs(tarrant_tracts, units = "m", inherit_gcs = FALSE) # Preview the extent of the CRS view_crs(target_crs) ## End(Not run)