Skip to content

Commit

Permalink
Merge pull request #27 from MikkoVihtakari/sf-version
Browse files Browse the repository at this point in the history
Sf version
  • Loading branch information
MikkoVihtakari committed Jul 4, 2023
2 parents 884a48f + 9f9faa2 commit d88ca86
Show file tree
Hide file tree
Showing 225 changed files with 4,515 additions and 2,729 deletions.
33 changes: 15 additions & 18 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
Package: ggOceanMaps
Type: Package
Title: Plot Data on Oceanographic Maps using 'ggplot2'
Version: 1.3.7
Date: 2022-11-24
Version: 2.0.0
Date: 2023-07-04
Authors@R: c(person("Mikko", "Vihtakari", email = "mikko.vihtakari@hi.no",
role = c("aut", "cre"),
comment = c(affiliation = "Institute of Marine Research",
ORCID = "0000-0003-0371-4319")),
person("Yves", "Reecht", role = "ctb",
comment = c(affiliation = "Institute of Marine Research",
ORCID = "0000-0003-3583-1843")),
person("Hadley", "Wickham", role = "ctb"),
person("Simon", "O'Hanlon", role = "ctb"),
person("Roger", "Bivand", role = "ctb")
person("Roger", "Bivand", role = "ctb"),
person("Hadley", "Wickham", role = "ctb")
)
URL: https://mikkovihtakari.github.io/ggOceanMaps/
BugReports: https://github.com/MikkoVihtakari/ggOceanMaps/issues
Expand All @@ -21,16 +17,17 @@ Description: Allows plotting data on bathymetric maps using 'ggplot2'. Plotting
for custom modifications. Data that contain geographic information from
anywhere around the globe can be plotted on maps generated by the basemap()
or qmap() functions using 'ggplot2' layers separated by the '+' operator. The
package uses spatial shapefiles stored in the 'ggOceanMapsData' package,
geospatial packages for R to manipulate, and the 'ggspatial' package to help
to plot these shapefiles. High-resolution shapefiles for detailed maps are
stored on GitHub and downloaded automatically when needed.
Depends: R (>= 3.5.0), ggplot2, ggspatial
Imports: sp, raster, sf, rgeos, methods, utils, stars, smoothr, units, dplyr, parallel
Suggests: ggOceanMapsData, cowplot, knitr, rmarkdown, scales, rgdal, ggnewscale
Additional_repositories: https://mikkovihtakari.github.io/drat
package uses spatial shape- ('sf') and raster ('stars') files, geospatial
packages for R to manipulate, and the 'ggplot2' package to plot these
files. The package ships with low-resolution spatial data files and
higher resolution files for detailed maps are stored in the
'ggOceanMapsLargeData' repository on GitHub and downloaded automatically
when needed.
Depends: R (>= 3.5.0), ggplot2
Imports: sf, stars, methods, utils, smoothr, units
Suggests: ggspatial, cowplot, knitr, rmarkdown, scales, ggnewscale
License: GPL-3
Encoding: UTF-8
RoxygenNote: 7.2.1
RoxygenNote: 7.2.3
LazyData: true

LazyDataCompression: xz
24 changes: 5 additions & 19 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ export(auto_limits)
export(basemap)
export(basemap_data)
export(clip_shapefile)
export(convert_crs)
export(dd_clip_boundary)
export(dd_to_deg)
export(define_bathy_style)
export(define_shapefiles)
export(deg_to_dd)
export(dist2land)
export(geonorge_bathymetry)
export(getCores)
export(guess_coordinate_columns)
export(is_decimal_limit)
export(load_map_data)
Expand All @@ -21,32 +21,18 @@ export(qmap)
export(quiet)
export(raster_bathymetry)
export(reorder_layers)
export(rotate_crs)
export(round_any)
export(select_element)
export(shapefile_list)
export(theme_map)
export(transform_coord)
export(vector_bathymetry)
import(ggplot2)
import(ggspatial)
import(raster)
import(rgeos)
import(sf)
import(sp)
importFrom(grDevices,chull)
importFrom(methods,"slot<-")
importFrom(methods,slot)
importFrom(parallel,detectCores)
importFrom(parallel,makeCluster)
importFrom(parallel,mclapply)
importFrom(parallel,parLapply)
importFrom(parallel,stopCluster)
importFrom(rgeos,createSPComment)
importFrom(rgeos,gDistance)
importFrom(rgeos,gIntersection)
importFrom(rgeos,gIntersects)
importFrom(rgeos,gSimplify)
importFrom(sf,st_sf)
importFrom(stars,read_stars)
importFrom(utils,download.file)
importFrom(utils,menu)
importFrom(utils,setTxtProgressBar)
importFrom(utils,txtProgressBar)
18 changes: 12 additions & 6 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
# ggOceanMaps 1.4 (development version)

# ggOceanMaps 2.0

* Full [sf](https://r-spatial.github.io/sf/) integration. Old GIS packages for R and ggspatial dependencies removed. Since this change required rewriting of most functions, new bugs have almost certainly been introduced.
* Bathymetry system redesigned (see [this](https://mikkovihtakari.github.io/ggOceanMaps/articles/new-features.html))
* Decimal degree maps can now be plotted across the antimeridian.
* Added spatial data to ggOceanMaps making the ggOceanMapsData package not needed any longer.
* `dist2land()` now uses great circle distances on a spherical Earth ([s2](https://r-spatial.github.io/s2/)) by default and should be able to calculate distances to land anywhere around the globe.
* qmap points turned red. Addressed a long-standing issue with `shapefiles` and `shape` getting mixed.
* Fixed a bug with shapefiles argument shortcut.
* Fixed a bug in ices_data
* Add sf support for clip_shapefile
* Add sf support for shapefiles (converts to sp, needs to be refined)
* Fix a bug with expanded limits in decimal degree projection
* Fix a bug where shapefile_list("all") would retunr multiple rows per shapefile name.
* Added sf support for clip_shapefile
* Added sf support for shapefiles (converts to sp, needs to be refined)
* Fixed a bug with expanded limits in decimal degree projection
* Fixed a bug where shapefile_list("all") would return multiple rows per shapefile name.

# ggOceanMaps 1.3.4

Expand Down
65 changes: 20 additions & 45 deletions R/auto_limits.R
Original file line number Diff line number Diff line change
@@ -1,53 +1,33 @@
#' @title Automatic limits for basemap
#' @description Find limits for a \code{\link{basemap}} from a data frame.
#' @param data Data frame containing data for which the limits should be calculated.
#' @param data Data frame or a spatial object containing data for which the limits should be calculated.
#' @param proj.in Original \code{\link[sf:st_crs]{CRS}} projection. Must be defined as character argument.
#' @param proj.out Resulting map projection. See \code{\link{transform_coord}}.
#' @param lon,lat Names of longitude and latitude columns in \code{data} as character or integer index. If \code{NULL}, the column names are \link[=guess_coordinate_columns]{guessed}.
#' @param expand.factor Expansion factor for map limits. Set to \code{NULL} to ignore.
#' @param rotate Logical indicating whether the limits should be rotated to point towards the pole relative to mid-longitude limit.
#' @param verbose Logical indicating whether information about the projection and guessed column names should be returned as message. Set to \code{FALSE} to make the function silent.
#' @param output.sf Logical indicating whether an \code{\link[sf:st_polygon]{sf}} (\code{TRUE}) or \code{sp} (\code{FALSE}) polygon should be returned.
#' @details This is an internal function, which is automatically run by the \code{\link{basemap}} function.
#' @return A list of limits and projections in \code{proj.in} and \code{proj.out} formats.
#' @keywords internal
#' @author Mikko Vihtakari
#' @family customize shapefiles
#' @examples
#' if(requireNamespace("ggOceanMapsData")) {
#' auto_limits(data = expand.grid(lon = c(-120, 180, 120),
#' lat = c(60, 60, 80)))
#' }
#' @export

# data = expand.grid(lon = c(-120, 180, 120), lat = c(60, 60, 80)); lon = NULL; lat = NULL; proj.in = 4326; proj.out = NULL; expand.factor = NULL; verbose = FALSE; output.sf = FALSE
auto_limits <- function(data, lon = NULL, lat = NULL, proj.in = 4326, proj.out = NULL, expand.factor = NULL, verbose = FALSE, output.sf = FALSE) {
# lon = NULL; lat = NULL; proj.in = 4326; proj.out = NULL; expand.factor = NULL; verbose = FALSE
auto_limits <- function(data, lon = NULL, lat = NULL, proj.in = 4326, proj.out = NULL, expand.factor = NULL, verbose = FALSE) {

# Get limits from spatial polygons ####
# Get coordinates from spatial objects

if(any(inherits(data, c("SpatialPolygonsDataFrame", "SpatialPolygons")))) {
proj.in <- raster::crs(data)

# if(!sf::st_is_longlat(proj.in)) {
# data <- sp::spTransform(data, sp::CRS(convert_crs(4326)))
# proj.in <- convert_crs(4326)
# message("The data argument is a spatial polygons object, which is not given as decimal degrees. Converted to decimal degrees.")
# }

data <- suppressMessages(ggplot2::fortify(data)[c("long", "lat")])
names(data) <- c("lon", "lat")
}

# Get limits from sf objects

if(any(inherits(data, "sf"))) {
proj.in <- sf::st_crs(data)

tmp <- sf::st_bbox(data)
if(any(inherits(data, c("sf", "sfc", "SpatialPolygonsDataFrame", "SpatialPolygons")))) {
tmp <- sf::st_bbox(sf::st_transform(sf::st_as_sfc(sf::st_bbox(data)), proj.in))

data <- expand.grid(data.frame(
lon = tmp[c(1,3)],
lat = tmp[c(2,4)])
lon = tmp[c("xmin", "xmax")],
lat = tmp[c("ymin", "ymax")])
)
}

Expand Down Expand Up @@ -125,18 +105,15 @@ auto_limits <- function(data, lon = NULL, lat = NULL, proj.in = 4326, proj.out =

}

# Projected boundaries
projLims <- stats::setNames(projLims, c("xmin", "xmax", "ymin", "ymax"))

# Projected boundaries

## Old sp way, to be removed when the sf way is tested
# projBound <- sp::Polygon(matrix(c(projLims[1], projLims[3], projLims[1], projLims[4], projLims[2], projLims[4], projLims[2], projLims[3], projLims[1], projLims[3]), ncol = 2, byrow = TRUE))
# projBound <- sp::SpatialPolygons(list(sp::Polygons(list(projBound), ID = "clip_boundary")),
# proj4string = if(class(proj.crs) == "CRS") {proj.crs} else {sp::CRS(proj.crs)})
# tmp <- as.data.frame(t(sp::bbox(projBound)))
# projBoundNodes <- sp::SpatialPoints(expand.grid(lon = tmp$x, lat = tmp$y),
# proj4string = if(class(proj.crs) == "CRS") {proj.crs} else {sp::CRS(convert_crs(proj.crs))})
projBound <- sf::st_polygon(
list(matrix(c(projLims[1], projLims[3], projLims[1], projLims[4], projLims[2],
projLims[4], projLims[2], projLims[3], projLims[1], projLims[3]),
ncol = 2, byrow = TRUE)))

projBound <- sf::st_polygon(list(matrix(c(projLims[1], projLims[3], projLims[1], projLims[4], projLims[2], projLims[4], projLims[2], projLims[3], projLims[1], projLims[3]), ncol = 2, byrow = TRUE)))
projBound <- sf::st_sfc(projBound, crs = sf::st_crs(proj.crs))

tmp <- sf::st_bbox(projBound)
Expand All @@ -148,20 +125,18 @@ auto_limits <- function(data, lon = NULL, lat = NULL, proj.in = 4326, proj.out =

decBoundNodes <- sf::st_transform(projBoundNodes, 4326)

# decBoundNodes <- sp::spTransform(projBoundNodes, sp::CRS(convert_crs(4326))) # proj.in

if(!identical(sign(projLims[3]), sign(projLims[4]))) { # Spans across the pole
decLims <- unname(sf::st_bbox(decBoundNodes)[c(1,3,2,4)]) # old: c(raster::extent(decBoundNodes)[1:3], 90)
decLims <- c(unname(sf::st_bbox(decBoundNodes)[c(1,3,2)]), 90) # old: c(raster::extent(decBoundNodes)[1:3], 90)
decLims <- c(decLims[1:3], sign(decLims[4]) * 90)
} else if(sign(decLims[1]) != sign(decLims[2]) & decLims[1] < decLims[2]) { # Antimeridian correction
decLims <- unname(sf::st_bbox(decBoundNodes)[c(1,3,2,4)]) # old: c(raster::extent(decBoundNodes)[1:4])
}

names(decLims) <- c("xmin", "xmax", "ymin", "ymax")

# Return

if(output.sf) {
list(ddLimits = decLims, projLimits = projLims, projBound = projBound, proj.in = attributes(x)$proj.in, proj.out = proj.out)
} else {
list(ddLimits = decLims, projLimits = projLims, projBound = sf::as_Spatial(projBound), proj.in = attributes(x)$proj.in, proj.out = proj.out)
}
list(ddLimits = decLims, projLimits = projLims, projBound = projBound,
proj.in = attributes(x)$proj.in, proj.out = proj.out)

}
Loading

0 comments on commit d88ca86

Please sign in to comment.