| Title: | Miscellaneous Spatial Functions |
|---|---|
| Description: | Contains convenience functions for working with spatial data across multiple UTM zones, raster-vector operations common in the analysis of conflict data, and converting degrees, minutes, and seconds latitude and longitude coordinates to decimal degrees. |
| Authors: | Rob Williams [aut, cre] (ORCID: <https://orcid.org/0000-0001-9259-3883>) |
| Maintainer: | Rob Williams <[email protected]> |
| License: | GPL (>=3) |
| Version: | 0.1.2 |
| Built: | 2026-05-16 08:36:31 UTC |
| Source: | https://github.com/jayrobwilliams/rwmisc |
Convert latitudes and longitudes from degrees, minutes, and seconds to decimal degrees for conversion to spatial objects.
dms2dd(lon, lat)dms2dd(lon, lat)
lon |
a character vector of longitude coordinates in degrees, minutes,
and seconds; see |
lat |
a character vector of latitude coordinates in degrees, minutes,
and seconds; see |
lon and lat are expected to be in the format
"degrees° minutes' seconds" (direction)" where direction is optional. If
direction is not present, dms2dd will use negative signs (-) to
determine positioning of coordinates.
An matrix where n is the length of lon and lat.
ll <- data.frame(lon = c("-122° 19' 55\"", "71° 3' 32\" W"), lat = c("47° 36' 22\"", "42° 21' 36\" N"), stringsAsFactors = FALSE) dms2dd(ll[, 'lon'], ll[, 'lat'])ll <- data.frame(lon = c("-122° 19' 55\"", "71° 3' 32\" W"), lat = c("47° 36' 22\"", "42° 21' 36\" N"), stringsAsFactors = FALSE) dms2dd(ll[, 'lon'], ll[, 'lat'])
Extract one or more levels of administrative unit geometries from the GADM database in GeoPackage format
gadm.extract(input, output, countries = NULL, level = 0:5, ...)gadm.extract(input, output, countries = NULL, level = 0:5, ...)
input |
GeoPackage file to read from |
output |
name of file to save output to |
countries |
country or countries to limit results to, if |
level |
level(s) of administrative units 0:5 to extract; note not all levels are defined for all countries |
... |
additional arguments passed to |
This function is designed to extract subsets of the
Database of Global Administrative Areas (GADM). It uses
the version of the database in GeoPackage format that provides one layer for
each level of administrative division, available at
https://gadm.org/download_world.html.
The current version of this file is gadm36_levels.gpkg. It is intended
for programmatic and reproducible subsetting of the database without
requiring the user to individually download specific country data files.
## Not run: ## extract gadm.extract("gadm36_levels.gpkg", "Nordics.gpkg", c("Denmark", "Finland", "Iceland", "Norway", "Sweden"), level = 0:2) ## add layers 3 and 4, use delete_layer = TRUE to rewrite existing layers gadm.extract("gadm36_levels.gpkg", "Nordics.gpkg", c("Denmark", "Finland", "Iceland", "Norway", "Sweden"), level = 0:4, delete_layer = TRUE) ## End(Not run)## Not run: ## extract gadm.extract("gadm36_levels.gpkg", "Nordics.gpkg", c("Denmark", "Finland", "Iceland", "Norway", "Sweden"), level = 0:2) ## add layers 3 and 4, use delete_layer = TRUE to rewrite existing layers gadm.extract("gadm36_levels.gpkg", "Nordics.gpkg", c("Denmark", "Finland", "Iceland", "Norway", "Sweden"), level = 0:4, delete_layer = TRUE) ## End(Not run)
Weight raster cells by overlapping polygons to avoid over-counting when aggregating by polygons
overlap.weight(raster, polygons, count = FALSE, warn = TRUE)overlap.weight(raster, polygons, count = FALSE, warn = TRUE)
raster |
a RasterLayer object. |
polygons |
a SpatialPolygons, SpatialPolygonsDataFrame, or simple feature
collection with at least two features. The function will still work with only
one polygon, but values will be unchanged, and the result will be equivalent
to |
count |
a logical indicating whether to return a raster with the count of polygons intersecting each cell, or a raster with original values weighted by 1/number of intersecting polygons. |
warn |
include warnings? Most common is that the returned raster will be
an intersection of the raster and the polygons. Default |
This function takes a raster and a set of polygons as arguments.
It counts the number of polygons that intersect each raster cell. It can
return either a raster with the count of the number of intersecting polygons
as cell values or the original raster with cell values weighted by 1 / the
number of intersecting polygons (the default behavior). Cells that do not
intersect any polygons will receive a value of NA. If the extent of the
polygons is less than the extent of the raster, then the function will warn
that it is cropping the raster to the polygons' extent.
a RasterLayer object.
library(sf) library(raster) polys_t <- st_sfc(list(st_polygon(list(rbind(c(2,2), c(2,6), c(6,6), c(6,2), c(2, 2)))), st_polygon(list(rbind(c(8,8), c(4,8), c(4,4), c(8,4), c(8,8))))), crs = st_crs('OGC:CRS84')) raster_t <- raster(nrows = 10, ncols = 10, xmn = 0, xmx = 10, ymn = 0, ymx = 10, vals = 1:100, crs = CRS(st_crs(polys_t)$proj4string)) overlap.weight(raster_t, polys_t)library(sf) library(raster) polys_t <- st_sfc(list(st_polygon(list(rbind(c(2,2), c(2,6), c(6,6), c(6,2), c(2, 2)))), st_polygon(list(rbind(c(8,8), c(4,8), c(4,4), c(8,4), c(8,8))))), crs = st_crs('OGC:CRS84')) raster_t <- raster(nrows = 10, ncols = 10, xmn = 0, xmx = 10, ymn = 0, ymx = 10, vals = 1:100, crs = CRS(st_crs(polys_t)$proj4string)) overlap.weight(raster_t, polys_t)
Calculate the maximum or minimum possible distance from a point to the edge of a given polygon.
point.poly.dist(point, poly, max = TRUE, by_element = FALSE)point.poly.dist(point, poly, max = TRUE, by_element = FALSE)
point |
A simplefeatures object of class point. |
poly |
A simplefeatures object of class polygon or multipolygon. |
max |
Logical; return maximum or minimum distance? default |
by_element |
Logical; return total maximum or minimum, or for each input
point? default |
Maximum or minimum distance between a point and a polygon.
library(sf) polys <- st_sfc(st_polygon(list(rbind(c(0,0), c(0,1), c(1,1), c(1,0), c(0,0)))), crs = st_crs('OGC:CRS84')) points <- st_sfc(st_multipoint(rbind(c(.25, .5), c(.75, .5), c(.5, .5))), crs = st_crs('OGC:CRS84')) point.poly.dist(points, polys)library(sf) polys <- st_sfc(st_polygon(list(rbind(c(0,0), c(0,1), c(1,1), c(1,0), c(0,0)))), crs = st_crs('OGC:CRS84')) points <- st_sfc(st_multipoint(rbind(c(.25, .5), c(.75, .5), c(.5, .5))), crs = st_crs('OGC:CRS84')) point.poly.dist(points, polys)
Project an object in latitude/longitude to UTM.
projectUTM(x) ## S3 method for class 'sf' projectUTM(x) ## S3 method for class 'sfc' projectUTM(x) ## S3 method for class 'SpatialPointsDataFrame' projectUTM(x) ## S3 method for class 'SpatialPoints' projectUTM(x) ## S3 method for class 'SpatialPolygonsDataFrame' projectUTM(x) ## S3 method for class 'SpatialPolygons' projectUTM(x)projectUTM(x) ## S3 method for class 'sf' projectUTM(x) ## S3 method for class 'sfc' projectUTM(x) ## S3 method for class 'SpatialPointsDataFrame' projectUTM(x) ## S3 method for class 'SpatialPoints' projectUTM(x) ## S3 method for class 'SpatialPolygonsDataFrame' projectUTM(x) ## S3 method for class 'SpatialPolygons' projectUTM(x)
x |
An |
An sf or sp object projected to UTM CRS.
library(sf) nc <- st_read(system.file("shape/nc.shp", package="sf")) st_crs(projectUTM(nc))library(sf) nc <- st_read(system.file("shape/nc.shp", package="sf")) st_crs(projectUTM(nc))
A ggplot theme with no grid elements or gray background.
theme_rw()theme_rw()
A ggplot theme object.
ggplot2::ggplot(mtcars, ggplot2::aes(x = hp, y = mpg)) + ggplot2::geom_point() + theme_rw()ggplot2::ggplot(mtcars, ggplot2::aes(x = hp, y = mpg)) + ggplot2::geom_point() + theme_rw()
Functions for converting latitude-longitude data to UTM.
long2UTM(long) UTMzones(long) chooseUTM(long)long2UTM(long) UTMzones(long) chooseUTM(long)
long |
A vector of longitude values. |
UTM vector of zone numbers.
UTM vector of zone numbers.
UTM zone number.
long2UTM(c(-90, 0, 90)) UTMzones(c(-90, 90, 90)) chooseUTM(c(-90, -80, -70))long2UTM(c(-90, 0, 90)) UTMzones(c(-90, 90, 90)) chooseUTM(c(-90, -80, -70))