Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sf version #27

Merged
merged 23 commits into from
Jul 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
b204b56
Start sf-version branch
MikkoVihtakari Jan 2, 2023
a9899c5
Add sf to qplot
MikkoVihtakari Feb 7, 2023
92a6e65
basemap to sf
MikkoVihtakari Jun 23, 2023
33f99bf
qmap to sf
MikkoVihtakari Jun 23, 2023
290e81b
data/limits and shapefiles bug
MikkoVihtakari Jun 23, 2023
fb657dc
Bug fixes to qmap
MikkoVihtakari Jun 23, 2023
2d4af66
dist2land to sf, fix RCM check warnings. Remove dependencies to sp an…
MikkoVihtakari Jun 26, 2023
012894c
A bug with reorder_layers and rotate = TRUE
MikkoVihtakari Jun 26, 2023
d60703b
Remove raster dependencies. raster_bathymetry to stars.
MikkoVihtakari Jun 27, 2023
7eee446
Adjust bathymetry functions
MikkoVihtakari Jun 28, 2023
64e2993
Tweaks to bathymetry functions
MikkoVihtakari Jun 29, 2023
0960189
Last tweak before breaking the package
MikkoVihtakari Jun 29, 2023
e317b9f
Add raster bathymertries and make ggOceanMapsData obsolete.
MikkoVihtakari Jun 30, 2023
354bc93
Bug fixes
MikkoVihtakari Jun 30, 2023
d0017f3
Update readme and description
MikkoVihtakari Jun 30, 2023
274f0b4
Typos in readme
MikkoVihtakari Jun 30, 2023
cd516f1
R CMD check fixes
MikkoVihtakari Jun 30, 2023
86bd165
Update documentation. Not finished yet...
MikkoVihtakari Jun 30, 2023
39439b8
Add support for user-defined bathymetry raster
MikkoVihtakari Jul 3, 2023
a565fa7
Bug fixes mostly
MikkoVihtakari Jul 4, 2023
3b6b58e
More bug fixes
MikkoVihtakari Jul 4, 2023
adb8f3a
Update webpage
MikkoVihtakari Jul 4, 2023
9f9faa2
Merge branch 'master' into sf-version
MikkoVihtakari Jul 4, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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