From e7feb0d50403754afcf07b099707d6baf30260c8 Mon Sep 17 00:00:00 2001 From: MikkoVihtakari Date: Wed, 8 Nov 2023 12:45:46 +0100 Subject: [PATCH] Error in st_cast.POINT(X[[i]], ...): cannot create MULTILINESTRING from POINT fix --- DESCRIPTION | 4 +-- R/basemap.R | 2 +- R/basemap_data.R | 75 +++++++++++++++--------------------------------- R/map_cmd.R | 2 +- 4 files changed, 27 insertions(+), 56 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index f5923c2..1f30203 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Package: ggOceanMaps Type: Package Title: Plot Data on Oceanographic Maps using 'ggplot2' -Version: 2.1.7 -Date: 2023-11-02 +Version: 2.1.8 +Date: 2023-11-08 Authors@R: c(person("Mikko", "Vihtakari", email = "mikko.vihtakari@hi.no", role = c("aut", "cre"), comment = c(affiliation = "Institute of Marine Research", diff --git a/R/basemap.R b/R/basemap.R index aecdafc..f9d8ea7 100644 --- a/R/basemap.R +++ b/R/basemap.R @@ -193,7 +193,7 @@ ## Test parameters # limits = c(160, -160, 60, 80); bathymetry = TRUE -# x = NULL; limits = NULL; data = NULL; shapefiles = NULL; crs = NULL; bathymetry = FALSE; glaciers = FALSE; rotate = FALSE; legends = TRUE; legend.position = "right"; lon.interval = NULL; lat.interval = NULL; bathy.style = NULL; downsample = 0; bathy.border.col = NA; bathy.size = 0.1; bathy.alpha = 1; land.col = "grey60"; land.border.col = "black"; land.size = 0.1; gla.col = "grey95"; gla.border.col = "black"; gla.size = 0.1; grid.col = "grey70"; grid.size = 0.1; base_size = 11; projection.grid = FALSE; verbose = TRUE +# x = NULL; limits = NULL; data = NULL; shapefiles = NULL; crs = NULL; bathymetry = FALSE; glaciers = FALSE; rotate = FALSE; legends = TRUE; legend.position = "right"; lon.interval = NULL; lat.interval = NULL; bathy.style = NULL; downsample = 0; bathy.border.col = NA; bathy.size = 0.1; bathy.alpha = 1; land.col = "grey60"; land.border.col = "black"; land.size = 0.1; gla.col = "grey95"; gla.border.col = "black"; gla.size = 0.1; grid.col = "grey70"; grid.size = 0.1; base_size = 11; projection.grid = FALSE; expand.factor = 1; verbose = TRUE basemap <- function(x = NULL, limits = NULL, data = NULL, shapefiles = NULL, crs = NULL, bathymetry = FALSE, glaciers = FALSE, rotate = FALSE, legends = TRUE, legend.position = "right", lon.interval = NULL, lat.interval = NULL, bathy.style = NULL, downsample = 0, bathy.border.col = NA, bathy.size = 0.1, bathy.alpha = 1, land.col = "grey60", land.border.col = "black", land.size = 0.1, gla.col = "grey95", gla.border.col = "black", gla.size = 0.1, grid.col = "grey70", grid.size = 0.1, base_size = 11, projection.grid = FALSE, expand.factor = 1, verbose = FALSE) { diff --git a/R/basemap_data.R b/R/basemap_data.R index d6f46cc..b5a5813 100644 --- a/R/basemap_data.R +++ b/R/basemap_data.R @@ -543,9 +543,9 @@ basemap_data_define_shapefiles <- function(limits = NULL, data = NULL, shapefile sf::st_bbox( sf::st_buffer( clip_shape, dist = (expand.factor-1)*sqrt(sf::st_area(clip_shape)) + ) ) ) - ) } } else if(case %in% c("limits_proj", "data_proj")) { ## Projected limits/data #### @@ -784,61 +784,29 @@ basemap_data_crop <- function(x, bathymetry = FALSE, glaciers = FALSE, crs = NUL basemap_define_grid_lines <- function(x, lon.interval = NULL, lat.interval = NULL) { - ## A quick fix. Improve later - - # if(!is.null(x$clipLimits)) { - # if(abs(x$clipLimits$ddLimits[4]) != 90) { - # tmp <- sf::st_bbox(sf::st_transform(x$clipLimits$projBound, 4326)) - # x$clipLimits$ddLimits <- tmp[c("xmin", "xmax", "ymin", "ymax")] - # } - # } - ## Define intervals if not specified if(is.null(lat.interval)) { - if(x$polarMap) { latDist <- 90 - abs(x$decLimits) - } else { - limits <- sf::st_bbox(sf::st_transform(x$limit_shape, 4326))[c("xmin", "xmax", "ymin", "ymax")] - latDist <- abs(diff(round(limits)[3:4])) + lat.interval <- + ifelse(latDist >= 30, 10, + ifelse(latDist >= 15, 5, + ifelse(latDist >= 10, 4, + ifelse(latDist >= 6, 3, + ifelse(latDist > 4, 2, 1) + )))) + } else { + lat.breaks <- ggplot2::waiver() } - lat.interval <- - ifelse(latDist >= 30, 10, - ifelse(latDist >= 15, 5, - ifelse(latDist >= 10, 4, - ifelse(latDist >= 6, 3, - ifelse(latDist > 4, 2, 1) - )))) } if(is.null(lon.interval)) { - if(x$polarMap) { lon.interval <- 45 } else { - limits <- sf::st_bbox(sf::st_transform(x$limit_shape, 4326))[c("xmin", "xmax", "ymin", "ymax")] - - if(diff(limits[1:2]) == 360) { - lonDist <- 360 - } else { - tmp <- dd_to_deg(round(x$decLimits)[1:2]) - - if(tmp[1] > tmp[2]) { - lonDist <- 360 - tmp[1] + tmp[2] - } else { - lonDist <- tmp[2] - tmp[1] - } - } - - lon.interval <- - ifelse(lonDist > 180, 45, - ifelse(lonDist > 90, 30, - ifelse(lonDist >= 40, 10, - ifelse(lonDist > 10, 5, - ifelse(lonDist > 4, 2, 1) - )))) + lon.breaks <- ggplot2::waiver() } } @@ -886,16 +854,19 @@ basemap_define_grid_lines <- function(x, lon.interval = NULL, lat.interval = NUL } else { - limits <- sf::st_bbox(sf::st_transform(x$limit_shape, 4326))[c("xmin", "xmax", "ymin", "ymax")] - - minLat <- min(limits[3:4]) - maxLat <- max(limits[3:4]) + if(!is.null(lat.interval)) { + limits <- sf::st_bbox(sf::st_transform(x$limit_shape, 4326))[c("xmin", "xmax", "ymin", "ymax")] + minLat <- min(limits[3:4]) + minLat <- ifelse(minLat < 0, -90, round_any(minLat, 10, floor)) + maxLat <- max(limits[3:4]) + maxLat <- ifelse(maxLat > 0, 90, round_any(maxLat, 10, ceiling)) + lat.breaks <- seq(minLat, maxLat, lat.interval) + } - minLat <- ifelse(minLat < 0, -90, round_any(minLat, 10, floor)) - maxLat <- ifelse(maxLat > 0, 90, round_any(maxLat, 10, ceiling)) + if(!is.null(lon.interval)) { + lon.breaks <- unique(c(seq(0, 180, lon.interval), seq(-180, 0, lon.interval))) + } - lat.breaks <- seq(minLat, maxLat, lat.interval) - lon.breaks <- unique(c(seq(0, 180, lon.interval), seq(-180, 0, lon.interval))) mapGrid <- list(lon.breaks = lon.breaks, lat.breaks = lat.breaks) } @@ -905,4 +876,4 @@ basemap_define_grid_lines <- function(x, lon.interval = NULL, lat.interval = NUL limit_shape = x$limit_shape, map_limits = x$map_limits, crs = x$crs, mapGrid = mapGrid) -} +} \ No newline at end of file diff --git a/R/map_cmd.R b/R/map_cmd.R index f73d53c..c124690 100644 --- a/R/map_cmd.R +++ b/R/map_cmd.R @@ -142,7 +142,7 @@ map_cmd <- function(command, alternative = FALSE) { ggplot2::scale_y_continuous(breaks = X$map.grid$lat.breaks, expand = c(0,0.1)) + ggplot2::scale_x_continuous(breaks = X$map.grid$lon.breaks, expand = c(0,0.1)) + ggplot2::labs(y = "Latitude (decimal degrees)", x = "Longitude (decimal degrees)") + - ggplot2::coord_sf(xlim = X$map.limits[1:2], ylim = X$map.limits[3:4], expand = FALSE, + ggplot2::coord_sf(xlim = X$map.limits[1:2], ylim = X$map.limits[3:4], crs = sf::st_crs(X$proj), default = TRUE) + theme_map(base_size = base_size, grid.col = grid.col, grid.size = grid.size) + ggplot2::theme(legend.position = legend.position,