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

rm sp for #61 #66

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: osmplotr
Title: Bespoke Images of 'OpenStreetMap' Data
Version: 0.3.5.004
Version: 0.3.5.009
Authors@R:
c(person(given = "Mark",
family = "Padgham",
Expand Down Expand Up @@ -33,6 +33,7 @@ Imports:
methods,
osmdata,
sf,
sfheaders,
sp,
spatstat (>= 2.0-0),
spatstat.explore,
Expand Down
100 changes: 23 additions & 77 deletions R/add-osm-groups.R
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,18 @@ add_osm_groups <- function (map, obj, groups, cols, bg, make_hull = FALSE,
check_obj_arg (obj)
groups <- check_groups_arg (groups)
if (length (groups) == 1) {

colmat <- FALSE
if (missing (bg)) {

message (paste0 (
"Plotting one group only makes sense with bg;",
" defaulting to gray40"
))
bg <- "gray40"
if (inherits (groups, "sf")) {
groups <- groups$geometry
} else {
colmat <- FALSE
if (missing (bg)) {

message (paste0 (
"Plotting one group only makes sense with bg;",
" defaulting to gray40"
))
bg <- "gray40"
}
}
}
# ---------- colmat
Expand Down Expand Up @@ -240,7 +243,7 @@ add_osm_groups <- function (map, obj, groups, cols, bg, make_hull = FALSE,

xyflat <- cbind_membs_xy (membs, xy)

if (!missing (bg)) {
if (!is.null (bg)) {
cols <- c (cols, bg)
}
lon <- lat <- id <- NULL # suppress "no visible binding" error
Expand Down Expand Up @@ -286,8 +289,9 @@ check_groups_arg <- function (groups) {

if (is (groups, "Spatial")) {
groups <- de_spatial_points (groups)
} else if (!inherits (groups, "sf")) {
groups <- list (groups)
}
groups <- list (groups)
}

return (groups)
Expand Down Expand Up @@ -388,67 +392,6 @@ group_colours_colourmat <- function (cols, groups, rotate) {
return (list ("cols" = cols, "cmat" = cmat))
}

#' identify groups which are holes in other groups
#'
#' @note This is not currently used, but the code is ready to implement in this
#' form.
#'
#' @noRd
groups_are_holes <- function (groups) {

holes <- rep (FALSE, length (groups))
group_pairs <- combn (length (groups), 2)
for (i in seq_len (ncol (group_pairs))) {

n1 <- length (groups [[group_pairs [1, i]]]) # nolint
n2 <- length (groups [[group_pairs [2, i]]]) # nolint
if (n1 > 2 && n2 > 2) { # otherwise can't be a hole

x1 <- sp::coordinates (groups [[group_pairs [1, i]]]) [, 1] # nolint
y1 <- sp::coordinates (groups [[group_pairs [1, i]]]) [, 2] # nolint
indx <- which (!duplicated (cbind (x1, y1)))
x1 <- x1 [indx]
y1 <- y1 [indx]
xy1 <- spatstat.geom::ppp (x1, y1,
xrange = range (x1), yrange = range (y1)
)
ch1 <- spatstat.geom::convexhull (xy1)
bdry1 <- cbind (ch1$bdry [[1]]$x, ch1$bdry [[1]]$y)
x2 <- sp::coordinates (groups [[group_pairs [2, i]]]) [, 1] # nolint
y2 <- sp::coordinates (groups [[group_pairs [2, i]]]) [, 2] # nolint
indx <- which (!duplicated (cbind (x2, y2)))
x2 <- x2 [indx]
y2 <- y2 [indx]
xy2 <- spatstat.geom::ppp (x2, y2,
xrange = range (x2), yrange = range (y2)
)
ch2 <- spatstat.geom::convexhull (xy2)
bdry2 <- cbind (ch2$bdry [[1]]$x, ch2$bdry [[1]]$y)

indx <- sapply (bdry1, function (x) {
sp::point.in.polygon (
bdry2 [, 1], bdry2 [, 2],
bdry1 [, 1], bdry1 [, 2]
)
})
if (all (indx == 1)) {
holes [group_pairs [1, i]] <- TRUE
}
indx <- sapply (bdry2, function (x) {
sp::point.in.polygon (
bdry1 [, 1], bdry1 [, 2],
bdry2 [, 1], bdry2 [, 2]
)
})
if (all (indx == 1)) {
holes [group_pairs [2, i]] <- TRUE
}
}
}

return (holes)
}

#' Trim coordinates of obj to be plotted down to coordinates of map
#'
#' @note This has to be modified for points!
Expand Down Expand Up @@ -557,15 +500,14 @@ group_centroids_bdrys <- function (groups, make_hull, cols,
bdry [, 1], bdry [, 2]
)
indx <- which (indx > 0) # see below for point.in.polygon values
grp_centroids [[i]] <- obj_trim$xy_mn [indx, ]
} else {

grp_centroids [[i]] <- bdry
# indx closest point to bdry
d <- sqrt ((obj_trim$xmn - bdry [1])^2 +
(obj_trim$ymn - bdry [2])^2)
indx <- which.min (d)
d <- sqrt ((obj_trim$xy_mn [, 1] - bdry [1])^2 +
(obj_trim$xy_mn [, 2] - bdry [2])^2)
indx <- unname (which.min (d))
}
grp_centroids [[i]] <- obj_trim$xy_mn [indx, ]

boundaries [[i]] <- bdry

Expand Down Expand Up @@ -883,6 +825,10 @@ map_plus_hulls <- function (map, border_width = 1, groups, xyflat, cols) {
}
}
bdry <- data.frame (do.call (rbind, bdry))
if (nrow (bdry) == 0) {
return (map)
}

names (bdry) <- c ("id", "x", "y")

aes <- ggplot2::aes (x = x, y = y, group = id)
Expand Down
19 changes: 10 additions & 9 deletions R/add-osm-surface.R
Original file line number Diff line number Diff line change
Expand Up @@ -290,16 +290,17 @@ list2df_with_data <- function (map, obj, obj_type, xy_mn, dat, bg,
}
if (!missing (bg)) {

xyh <- spatstat.geom::ppp (xyz$x, xyz$y,
xrange = range (xyz$x),
yrange = range (xyz$y)
xy <- cbind (as.numeric (xyz$x), as.numeric (xyz$y)) |>
sfheaders::sf_point () |>
sf::st_sf (crs = 4326) |>
reproj_equal_area ()
bdry <- sf::st_convex_hull (sf::st_combine (xy))
indx <- vapply (
sf::st_within (xy, bdry), function (i) {
ifelse (length (i) == 0, 0L, i [1])
},
integer (1L)
)
ch <- spatstat.geom::convexhull (xyh)
bdry <- cbind (ch$bdry [[1]]$x, ch$bdry [[1]]$y)

indx <- apply (xy_mn, 1, function (x) {
sp::point.in.polygon (x [1], x [2], bdry [, 1], bdry [, 2])
})
# indx = 0 for outside polygon
}

Expand Down
4 changes: 1 addition & 3 deletions R/colour-mat.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@
#' x <- bbox [1, 1] + runif (ngroups) * diff (bbox [1, ])
#' y <- bbox [2, 1] + runif (ngroups) * diff (bbox [2, ])
#' groups <- cbind (x, y)
#' groups <- apply (groups, 1, function (i) {
#' sp::SpatialPoints (matrix (i, nrow = 1, ncol = 2))
#' })
#' groups <- sfheaders::sf_point (groups)
#' # plot a basemap and add groups
#' map <- osm_basemap (bbox = bbox, bg = "gray20")
#' map <- add_osm_groups (map,
Expand Down
24 changes: 18 additions & 6 deletions codemeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
"codeRepository": "https://github.com/ropensci/osmplotr",
"issueTracker": "https://github.com/ropensci/osmplotr/issues",
"license": "https://spdx.org/licenses/GPL-3.0",
"version": "0.3.5.004",
"version": "0.3.5.009",
"programmingLanguage": {
"@type": "ComputerLanguage",
"name": "R",
"url": "https://r-project.org"
},
"runtimePlatform": "R version 4.3.3 (2024-02-29)",
"runtimePlatform": "R version 4.4.1 (2024-06-14)",
"author": [
{
"@type": "Person",
Expand Down Expand Up @@ -219,6 +219,18 @@
"sameAs": "https://CRAN.R-project.org/package=sf"
},
"9": {
"@type": "SoftwareApplication",
"identifier": "sfheaders",
"name": "sfheaders",
"provider": {
"@id": "https://cran.r-project.org",
"@type": "Organization",
"name": "Comprehensive R Archive Network (CRAN)",
"url": "https://cran.r-project.org"
},
"sameAs": "https://CRAN.R-project.org/package=sfheaders"
},
"10": {
"@type": "SoftwareApplication",
"identifier": "sp",
"name": "sp",
Expand All @@ -230,7 +242,7 @@
},
"sameAs": "https://CRAN.R-project.org/package=sp"
},
"10": {
"11": {
"@type": "SoftwareApplication",
"identifier": "spatstat",
"name": "spatstat",
Expand All @@ -243,7 +255,7 @@
},
"sameAs": "https://CRAN.R-project.org/package=spatstat"
},
"11": {
"12": {
"@type": "SoftwareApplication",
"identifier": "spatstat.explore",
"name": "spatstat.explore",
Expand All @@ -255,7 +267,7 @@
},
"sameAs": "https://CRAN.R-project.org/package=spatstat.explore"
},
"12": {
"13": {
"@type": "SoftwareApplication",
"identifier": "spatstat.geom",
"name": "spatstat.geom",
Expand All @@ -269,7 +281,7 @@
},
"SystemRequirements": {}
},
"fileSize": "12101.377KB",
"fileSize": "1515.651KB",
"releaseNotes": "https://github.com/ropensci/osmplotr/blob/master/NEWS.md",
"readme": "https://github.com/ropensci/osmplotr/blob/main/README.md",
"contIntegration": [
Expand Down
4 changes: 1 addition & 3 deletions man/colour_mat.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 4 additions & 7 deletions tests/testthat/test-print-map.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,22 @@ test_that ("print_osm_map", {
map <- osm_basemap (bbox = bbox, bg = "gray20")
map <- add_osm_objects (map, dat_B)

# exts <- c ("eps", "ps", "tex", "pdf", "svg", "png",
# "jpg", "jpeg", "bmp", "tiff")
# svg fails on travis osx with r: release
exts <- c (
"eps", "ps", "tex", "pdf", "png",
"eps", "ps", "tex", "pdf", "svg", "png",
"jpg", "jpeg", "bmp", "tiff"
)
) [-3] # text generates a warning from grDevices::pictex
for (e in exts) {

fname <- paste0 ("map.", e)
# expect_silent (
expect_silent (
print_osm_map (
map,
width = 5,
height = 4,
filename = fname,
units = "in"
)
# )
)
expect_true (fname %in% list.files ())
}
if (test_all) {
Expand Down
Loading