Skip to content

Commit

Permalink
improve trim-osmdata for #253
Browse files Browse the repository at this point in the history
  • Loading branch information
mpadge committed Jan 3, 2022
1 parent 276911d commit 8e55083
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 28 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: osmdata
Title: Import 'OpenStreetMap' Data as Simple Features or Spatial Objects
Version: 0.1.8.010
Version: 0.1.8.011
Authors@R: c(
person("Mark", "Padgham", , "[email protected]", role = c("aut", "cre")),
person("Bob", "Rudis", role = "aut"),
Expand Down Expand Up @@ -37,6 +37,7 @@ Imports:
magrittr,
methods,
Rcpp (>= 0.12.4),
reproj,
rvest,
sp,
tibble,
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ Major changes:
*around* a defined location; thanks to @barryrowlingson via #199 and
@maellecoursonnais via #238

Minor changes:

- New dependency on `reproj` package, so that `trim_osmdata()` can be applied
to re-projected coordinates.

0.1.8
===================

Expand Down
12 changes: 12 additions & 0 deletions R/trim-osmdata.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ trim_osmdata_sfp <- function (dat, bb_poly, exclude = TRUE) {

if (nrow (bb_poly) > 1) {

srcproj <- .lonlat() #"+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
crs <- .sph_merc() # "+proj=merc +a=6378137 +b=6378137"
bb_poly <- reproj::reproj (bb_poly, target = crs, source = srcproj) [, 1:2]

dat <- trim_to_poly_pts (dat, bb_poly, exclude = exclude) %>%
trim_to_poly (bb_poly = bb_poly, exclude = exclude) %>%
trim_to_poly_multi (bb_poly = bb_poly, exclude = exclude)
Expand Down Expand Up @@ -132,7 +136,11 @@ trim_to_poly_pts <- function (dat, bb_poly, exclude = TRUE) {

if (is (dat$osm_points, "sf")) {

srcproj <- .lonlat() #"+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
crs <- .sph_merc() # "+proj=merc +a=6378137 +b=6378137"

g <- do.call (rbind, dat$osm_points$geometry)
g <- reproj::reproj (g, target = crs, source = srcproj)
indx <- sp::point.in.polygon (g [, 1], g [, 2],
bb_poly [, 1], bb_poly [, 2])
if (exclude)
Expand Down Expand Up @@ -160,10 +168,14 @@ trim_to_poly_pts <- function (dat, bb_poly, exclude = TRUE) {
#' @noRd
get_trim_indx <- function (g, bb, exclude) {

srcproj <- .lonlat() #"+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
crs <- .sph_merc() # "+proj=merc +a=6378137 +b=6378137"

indx <- lapply (g, function (i) {

if (is.list (i)) # polygons
i <- i [[1]]
i <- reproj::reproj (as.matrix (i), target = crs, source = srcproj)
inp <- sp::point.in.polygon (i [, 1], i [, 2],
bb [, 1], bb [, 2])
if ((exclude & all (inp > 0)) |
Expand Down
43 changes: 18 additions & 25 deletions codemeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
"codeRepository": "https://github.com/ropensci/osmdata/",
"issueTracker": "https://github.com/ropensci/osmdata/issues",
"license": "https://spdx.org/licenses/GPL-3.0",
"version": "0.1.8.010",
"version": "0.1.8.11",
"programmingLanguage": {
"@type": "ComputerLanguage",
"name": "R",
"url": "https://r-project.org"
},
"runtimePlatform": "R version 4.1.1 (2021-08-10)",
"runtimePlatform": "R version 4.1.2 (2021-11-01)",
"provider": {
"@id": "https://cran.r-project.org",
"@type": "Organization",
Expand Down Expand Up @@ -272,6 +272,18 @@
},
"sameAs": "https://CRAN.R-project.org/package=Rcpp"
},
{
"@type": "SoftwareApplication",
"identifier": "reproj",
"name": "reproj",
"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=reproj"
},
{
"@type": "SoftwareApplication",
"identifier": "rvest",
Expand Down Expand Up @@ -332,28 +344,12 @@
],
"applicationCategory": "DataAccess",
"isPartOf": "https://ropensci.org",
"keywords": [
"open0street0map",
"openstreetmap",
"overpass0API",
"OSM",
"overpass-api",
"r",
"cpp",
"rstats",
"osm",
"osm-data",
"r-package",
"peer-reviewed"
],
"contIntegration": [
"https://github.com/ropensci/osmdata/actions?query=workflow%3AR-CMD-check",
"https://codecov.io/gh/ropensci/osmdata"
],
"keywords": ["open0street0map", "openstreetmap", "overpass0API", "OSM", "overpass-api", "r", "cpp", "rstats", "osm", "osm-data", "r-package", "peer-reviewed"],
"contIntegration": ["https://github.com/ropensci/osmdata/actions?query=workflow%3AR-CMD-check", "https://codecov.io/gh/ropensci/osmdata"],
"developmentStatus": "https://www.repostatus.org/#active",
"releaseNotes": "https://github.com/ropensci/osmdata/blob/master/NEWS.md",
"readme": "https://github.com/ropensci/osmdata/blob/main/README.md",
"fileSize": "7782.91KB",
"fileSize": "7784.406KB",
"citation": [
{
"@type": "ScholarlyArticle",
Expand Down Expand Up @@ -390,10 +386,7 @@
"issueNumber": "14",
"datePublished": "2017",
"isPartOf": {
"@type": [
"PublicationVolume",
"Periodical"
],
"@type": ["PublicationVolume", "Periodical"],
"volumeNumber": "2",
"name": "The Journal of Open Source Software"
}
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-trim.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ test_that ("trim_osm_data", {
expect_equal (nrow (x1$edge), 0)
expect_equal (nrow (x1$vertex), 0)

bb <- list (cbind (c (0, 0),
bb <- list (rbind (c (0, 0),
c (0, 1),
c (1, 1),
c (1, 0),
c (0, 0)),
cbind (c (2, 3),
rbind (c (2, 3),
c (2, 4),
c (3, 4),
c (3, 3),
Expand Down

0 comments on commit 8e55083

Please sign in to comment.