Skip to content

Commit 58cc649

Browse files
authored
Merge pull request #267 from uwescience/253-chore-document-order-of-location-columns-in-crosswalkmd-and-doctrings-for-crosswalk_spatial
updated funcs and docs for crosswalk_spatial
2 parents 2168cd9 + 5afce55 commit 58cc649

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

code/crosswalk/crosswalk_geom.R

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ crosswalk_geom <- function(data, target, location_columns = NULL, extensive = FA
55
#'
66
#' @param data: point data (lat, lon) or a shapefile containing shapes (polygons, lines, etc.)
77
#' @param target: shapefile containing shapes (polygons, lines, etc.). This is for the target boundaries/geographic levels (assumes larger than the starting level I think).
8-
#' @param location_columns: required for points (e.g c("LATITUDE", "LONGITUDE") or c("lat", "lon") depending on data format), not required for shapefile data.
8+
#' @param location_columns: required for points (e.g c("LONGITUDE", "LATITUDE") or c("lon", "lat") depending on data name format), not required for shapefile data.
99
#' @param extensive: TRUE if data of interest is spatially extensive, (e.g population) or FALSE if spatially intensive (e.g population density)
1010
#' @param join_method: in the case of shapefile to shapefile (multipoint and polygons, not points) crosswalks, choose "max_area"
1111
#' or "areal-weighted" to inherit values of source data based on maximum intersection or a area-weighted average
1212
#' of all intersecting polygons. NULL in other cases, where the mean is taken by default.
1313
#' @output
1414
#' Returns a joined dataset on the target scale (a shapefile).
15+
#' @export
1516

1617
if (length(location_columns) == 2) {
1718

@@ -35,7 +36,7 @@ crosswalk_geom <- function(data, target, location_columns = NULL, extensive = FA
3536
data <- st_transform(data, crs = st_crs(target))
3637
data <- st_make_valid(data)
3738
# Perform the spatial join using areal aggregation extensive = TRUE (spatially extensive, e.g population) or extensive = FALSE (spatially intensive, e.g population density)
38-
joined <- st_interpolate_aw(data, target, extensive = extensive)
39+
joined <- st_interpolate_aw(data, target, extensive = extensive, na.rm = TRUE, keep_na = TRUE)
3940
}
4041

4142
return(joined)

code/crosswalk/crosswalk_raster.R

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ crosswalk_raster <- function(data, target, location_columns = NULL, extensive =
44
#'
55
#' @param data: point data (lat, lon) or a shapefile containing shapes (polygons, lines, etc.)
66
#' @param target: shapefile containing shapes (polygons, lines, etc.). This is for the target boundaries/geographic levels (assumes larger than the starting level I think).
7-
#' @param location_columns: required for points (e.g c("LATITUDE", "LONGITUDE") or c("lat", "lon") depending on data format), not required for shapefile data.
7+
#' @param location_columns: required for points (e.g c("LONGITUDE", "LATITUDE") or c("lon", "lat") longitude first, latitude second). Not required for shapefile data.
88
#' @param extensive: TRUE if data of interest is spatially extensive, (e.g population) or FALSE if spatially intensive (e.g population density)
99
#' @param join_method: in the case of shapefile to shapefile (multipoint and polygons, not points) crosswalks, choose "max_area"
1010
#' or "areal-weighted" to inherit values of source data based on maximum intersection or a area-weighted average
@@ -17,7 +17,7 @@ crosswalk_raster <- function(data, target, location_columns = NULL, extensive =
1717
#' Point data is maintained as points. User must rasterize resulting dataframe if they want the output of a point/raster join to be in raster form.
1818
#' Raster/shapefile combinations will output a shapefile, not a raster. User must rasterize polygon data if they want the output to be a raster.
1919
#' Raster/raster combinations take the mosaic mean of the two files. The output is a raster. Users must combine with another shapefile if they want a shapefile output.
20-
#'
20+
#' @export
2121

2222

2323
# Function for Raster/Raster
@@ -71,7 +71,7 @@ crosswalk_raster <- function(data, target, location_columns = NULL, extensive =
7171
polygonized_raster <- st_make_valid(data)
7272

7373
#take the areal-weighted mean/sum of the polygonized raster and shapefile polygons
74-
new_shapefile <- st_interpolate_aw(polygonized_raster, shapefile, extensive = extensive)
74+
new_shapefile <- st_interpolate_aw(polygonized_raster, shapefile, extensive = extensive, na.rm = TRUE, keep_na = TRUE)
7575
return(new_shapefile)
7676
}
7777

code/crosswalk/crosswalk_spatial.R

+10-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ crosswalk_spatial <- function(data, target, location_columns = NULL, extensive =
1313
#' @param data: data on the source geographic scale. Can be a shapefile, raster, or in the case of point data, a table with lat/lon columns.
1414
#' point data (lat, lon) or a shapefile containing shapes (polygons, lines, etc.). For point data crosswalking to a target polygon geometry, data is maintained on the point scale and assigned to a geometry (requires users to perform aggregation of choice outside the function
1515
#' @param target: shapefile containing shapes (polygons, lines, etc.). This is for the target boundaries/geographic levels.
16-
#' @param location_columns: only required for points (e.g c("LATITUDE", "LONGITUDE") or c("lat", "lon") depending on data format), not required for shapefile or raster data.
16+
#' @param location_columns: only required for points (e.g ("LONGITUDE", "LATITUDE") or c("lon", "lat"), longitude first and latitude second). Not required for shapefile or raster data.
1717
#' @param extensive: TRUE if data of interest is spatially extensive, (e.g population, function = SUM) or FALSE if spatially intensive (e.g population density, function = MEAN).
1818
#' @param join_method: in the case of shapefile to shapefile (multipoint and polygons, not points) crosswalks, choose "max_area"
1919
#' or "areal_weighted" to inherit values of source data based on maximum intersection assignment or a area-weighted average/sum
@@ -22,12 +22,20 @@ crosswalk_spatial <- function(data, target, location_columns = NULL, extensive =
2222
#' Returns a shapefile on the target scale (except raster/raster combinations, which output a stacked raster). Please check that your data is correctly aggregated and adjust input parameters as needed.
2323
#' @Notes
2424
#' Relies on functions crosswalk_geom.R (data, target, location_columns, extensive, join_method) and crosswalk_raster.R (data, target, location_columns, extensive).
25+
#'
2526
#' This function will automatically detect geometry columns associated with shapefiles.
27+
#'
28+
#' Please be mindful of NA values prior to performing spatial crosswalks/joins. When taking areal-weighted averages and sums, all target geometries will be maintained even
29+
#' if the resulting interpolation is NA. Prior to calculating the interpolation, NA values in the source data will be removed so as to not cause calculations to return NA. You will need to check your NAs and handle them in the way you see appropriate before using the crosswalked data output by this function.
30+
#'
31+
#' When assigning points to shapes, all points will be returned with their associated shapes (left join where points are maintained). When assigning point values to a raster, you will end up with a polygonized raster with appended values for points contained by the raster squares.
32+
#'
33+
#'
2634
#' Notes on using raster data in combination with other data:
2735
#' Point data is set on the raster scale but maintains its original values. NA values for polygonized rasters not containing a point. Will double count polygons containing multiple points.
2836
#' Raster/shapefile combinations will output a shapefile, not a raster. User must rasterize polygon data if they want the output to be a raster.
2937
#' Raster/raster combinations stack the two files to be a stacked raster object. The output is a stacked raster. Users must combine with another shapefile if they want a shapefile output.
30-
#'
38+
#' @export
3139

3240
require(sf)
3341
require(raster)

0 commit comments

Comments
 (0)