Skip to content

Commit 31354f7

Browse files
Merge pull request #509 from e-kotov/street-net-extent-jar
Street net extent function, with jar (attempt 2)
2 parents 32d0b97 + f226787 commit 31354f7

File tree

9 files changed

+235
-1
lines changed

9 files changed

+235
-1
lines changed

java-r5rcore/src/org/ipea/r5r/R5RCore.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
import java.util.concurrent.ExecutionException;
3232
import java.util.concurrent.ForkJoinPool;
3333

34+
import org.locationtech.jts.geom.Envelope;
35+
36+
3437
public class R5RCore {
3538

3639
public static final String R5_VERSION = System.getProperty("R5_VER", "7+, ERROR getting exact version");
@@ -760,4 +763,15 @@ public void abort() {
760763
public String identify(){
761764
return "I am an R5R core!";
762765
}
763-
}
766+
767+
/** Get the geographic envelope (bounding box) of the transport network's street layer as a primitive array. @return A double array with the coordinates in the order: [minX, maxX, minY, maxY]. */
768+
public double[] getNetworkEnvelopeAsArray() {
769+
Envelope envelope = this.routingProperties.getTransportNetworkBase().getEnvelope();
770+
return new double[]{
771+
envelope.getMinX(),
772+
envelope.getMaxX(),
773+
envelope.getMinY(),
774+
envelope.getMaxY()
775+
};
776+
}
777+
}

r-package/NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export(read_fare_structure)
1616
export(setup_fare_structure)
1717
export(setup_r5)
1818
export(stop_r5)
19+
export(street_network_bbox)
1920
export(street_network_to_sf)
2021
export(transit_network_to_sf)
2122
export(travel_time_matrix)

r-package/R/street_network_bbox.R

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#' Extract the geographic bounding box of the transport network
2+
#'
3+
#' Extracts the geographic bounding box of the street network layer from a
4+
#' routable transport network built with [build_network()]). It is a fast and
5+
#' memory-efficient alternative to `sf::st_bbox(street_network_to_sf(r5r_net))`.
6+
#'
7+
#' @template r5r_network
8+
#' @template r5r_core
9+
#' @param output A character string specifying the desired output format. One of
10+
#' `"polygon"` (the default), `"bbox"`, or `"vector"`.
11+
#'
12+
#' @return By default (`output = "polygon"`), an `sf` object with a single `POLYGON`
13+
#' geometry. If `output = "bbox"`, an `sf` `bbox` object. If `output = "vector"`,
14+
#' a named numeric vector with `xmin`, `ymin`, `xmax`, `ymax` coordinates.
15+
#' All outputs use the WGS84 coordinate reference system (EPSG: 4326).
16+
#'
17+
#' @family network functions
18+
#'
19+
#' @examplesIf identical(tolower(Sys.getenv("NOT_CRAN")), "true")
20+
#' library(r5r)
21+
#' library(sf)
22+
#'
23+
#' data_path <- system.file("extdata/poa", package = "r5r")
24+
#' r5r_net <- build_network(data_path)
25+
#'
26+
#' # Get the network's bounding box as an sf polygon (default)
27+
#' poly <- street_network_bbox(r5r_net)
28+
#' plot(poly)
29+
#'
30+
#' # Get an sf bbox object (order is xmin, ymin, xmax, ymax)
31+
#' box <- street_network_bbox(r5r_net, output = "bbox")
32+
#' box
33+
#'
34+
#' # Get a simple named vector (order now also xmin, ymin, xmax, ymax)
35+
#' vec <- street_network_bbox(r5r_net, output = "vector")
36+
#' vec
37+
#'
38+
#' stop_r5(r5r_net)
39+
#' @export
40+
street_network_bbox <- function(
41+
r5r_network,
42+
output = c("polygon", "bbox", "vector"),
43+
r5r_core = deprecated()
44+
) {
45+
# deprecating r5r_core --------------------------------------
46+
if (lifecycle::is_present(r5r_core)) {
47+
cli::cli_warn(c(
48+
"!" = "The `r5r_core` argument is deprecated as of r5r v2.3.0.",
49+
"i" = "Please use the `r5r_network` argument instead."
50+
))
51+
r5r_network <- r5r_core
52+
}
53+
54+
# --- Input validation ---
55+
checkmate::assert_class(r5r_network, "r5r_network")
56+
output <- match.arg(output)
57+
58+
# --- Call the optimized Java method ---
59+
coords_vec <- rJava::.jcall(
60+
r5r_network@jcore,
61+
"[D", # Signature for "returns an array of doubles"
62+
"getNetworkEnvelopeAsArray"
63+
)
64+
65+
# --- Assign names based on the documented order from the Java method ---
66+
names(coords_vec) <- c("xmin", "xmax", "ymin", "ymax")
67+
68+
# --- Reorder to match sf::st_bbox standard ---
69+
coords_vec <- coords_vec[c("xmin", "ymin", "xmax", "ymax")]
70+
71+
# --- Format the output as requested ---
72+
if (output == "vector") {
73+
return(coords_vec)
74+
}
75+
76+
bbox <- sf::st_bbox(coords_vec, crs = 4326) # WGS 84
77+
78+
if (output == "bbox") {
79+
return(bbox)
80+
}
81+
82+
if (output == "polygon") {
83+
return(sf::st_as_sfc(bbox))
84+
}
85+
}

r-package/inst/jar/r5r.jar

157 Bytes
Binary file not shown.

r-package/man/find_snap.Rd

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

r-package/man/street_network_bbox.Rd

Lines changed: 62 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

r-package/man/street_network_to_sf.Rd

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

r-package/man/transit_network_to_sf.Rd

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
context("Testing street_network_bbox")
2+
3+
testthat::skip_on_cran()
4+
5+
test_that("street_network_bbox returns an sf polygon by default", {
6+
bbox_poly <- street_network_bbox(r5r_network)
7+
8+
# Check class and type
9+
expect_s3_class(bbox_poly, "sfc")
10+
expect_s3_class(bbox_poly, "sfc_POLYGON")
11+
expect_equal(
12+
as.character(sf::st_geometry_type(bbox_poly, by_geometry = FALSE)),
13+
"POLYGON"
14+
)
15+
16+
# Check properties
17+
expect_equal(length(bbox_poly), 1)
18+
expect_equal(sf::st_crs(bbox_poly), sf::st_crs(4326))
19+
})
20+
21+
test_that("street_network_bbox returns a bbox object when output = 'bbox'", {
22+
bbox_obj <- street_network_bbox(r5r_network, output = "bbox")
23+
24+
# Check class and properties
25+
expect_s3_class(bbox_obj, "bbox")
26+
expect_true(is.numeric(bbox_obj))
27+
expect_length(bbox_obj, 4)
28+
expect_named(bbox_obj, c("xmin", "ymin", "xmax", "ymax"))
29+
})
30+
31+
test_that("street_network_bbox returns a named vector when output = 'vector'", {
32+
bbox_vec <- street_network_bbox(r5r_network, output = "vector")
33+
34+
# Check class and properties
35+
expect_true(is.numeric(bbox_vec))
36+
expect_false(inherits(bbox_vec, "bbox"))
37+
expect_length(bbox_vec, 4)
38+
expect_named(bbox_vec, c("xmin", "ymin", "xmax", "ymax"))
39+
})
40+
41+
test_that("bbox and vector outputs are consistent in value and order", {
42+
bbox_obj <- street_network_bbox(r5r_network, output = "bbox")
43+
bbox_vec <- street_network_bbox(r5r_network, output = "vector")
44+
45+
# Check that the numeric values are equal, ignoring names and attributes.
46+
expect_equal(as.numeric(bbox_obj), unclass(unname(bbox_vec)))
47+
48+
# Check that the names are identical.
49+
expect_identical(names(bbox_obj), names(bbox_vec))
50+
})
51+
52+
test_that("street_network_bbox gives deprecation warning for r5r_core", {
53+
# Check that the warning is thrown
54+
bbox_poly <- expect_warning(
55+
street_network_bbox(r5r_core = r5r_network),
56+
regexp = "The `r5r_core` argument is deprecated"
57+
)
58+
59+
# Check that the function still returns the correct output
60+
expect_s3_class(bbox_poly, "sfc")
61+
expect_s3_class(bbox_poly, "sfc_POLYGON")
62+
})
63+
64+
test_that("street_network_bbox throws an error for invalid output argument", {
65+
expect_error(
66+
street_network_bbox(r5r_network, output = "invalid_option"),
67+
regexp = "'arg' should be one of"
68+
)
69+
})

0 commit comments

Comments
 (0)