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

Extract simple features-like interface into a C++ only API #165

Merged
merged 88 commits into from
May 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
88 commits
Select commit Hold shift + click to select a range
fd1645b
separate existing c utility into one folder
paleolimbot Jan 30, 2022
6a47443
start on rewriting geography abstraction
paleolimbot Jan 31, 2022
80334a0
scaffold some new geography bits in
paleolimbot Feb 4, 2022
a716f5e
check with C API def
paleolimbot Feb 4, 2022
323a516
in theory, construct objects
paleolimbot Feb 4, 2022
439bfa9
add the "status" object
paleolimbot Feb 5, 2022
ced79eb
rename 'capi' to 'geography'
paleolimbot Feb 5, 2022
d4e3033
implement accessors
paleolimbot Feb 8, 2022
631bd1e
don't make the new geography conditional on comnpile
paleolimbot Feb 11, 2022
1f3e493
actually use new geography accessor API
paleolimbot Feb 11, 2022
0562ca7
use overloads for geog-specific methods
paleolimbot Feb 12, 2022
6c0307f
try to fix R 3.6 on Windows
paleolimbot Feb 12, 2022
8b4f656
a few more operators
paleolimbot Feb 12, 2022
46c3d50
move distance and max_distance
paleolimbot Feb 13, 2022
843764e
implement s2_boundary in new api
paleolimbot Feb 14, 2022
5e79b0a
slightly better boundary logic
paleolimbot Feb 14, 2022
2e9909e
fix R 3.6 for windows
paleolimbot Feb 14, 2022
826cc6e
undo kludge that cause segfault
paleolimbot Feb 14, 2022
2b9e709
remove non-functional stub
paleolimbot Feb 14, 2022
6a98cf7
make s2_dimension() cheaper
paleolimbot Feb 17, 2022
ed0506e
add s2_centroid() to new API
paleolimbot Feb 17, 2022
f9f0642
s2_centroid_agg() use new s2_centroid()
paleolimbot Feb 17, 2022
5978ca2
move s2_closest_point() and s2_clearance_line_between() to new api
paleolimbot Feb 17, 2022
8dc5272
use new API for boolean operations
paleolimbot Feb 21, 2022
263d126
remove old boolean operation code
paleolimbot Feb 21, 2022
db1a68b
move rebuild to new api
paleolimbot Feb 22, 2022
4801922
move rebuild_agg to new api
paleolimbot Feb 22, 2022
1f99873
remove one more reference to the old rebuildGeography
paleolimbot Feb 22, 2022
04a8de3
remove a few more references
paleolimbot Feb 23, 2022
ce9e556
centroid aggregator
paleolimbot Feb 24, 2022
f966edf
prepare for more aggregators
paleolimbot Mar 5, 2022
ccc1733
prepare s2_geography_from_layers
paleolimbot Mar 5, 2022
9becb38
allow builder to rebuild bits
paleolimbot Mar 5, 2022
4ed1fde
enable building polygon as a fallback
paleolimbot Mar 5, 2022
033b49e
separate aggregate rebuild logic
paleolimbot Mar 5, 2022
2d63704
coverage union agg logic
paleolimbot Mar 5, 2022
26d34aa
sketch union_agg solution
paleolimbot Mar 6, 2022
ee63077
use and test new aggregator
paleolimbot Mar 6, 2022
5f5add8
migrate s2_point_on_surface to new api
paleolimbot Mar 6, 2022
3906ba6
move s2_find_validation_error() to new API
paleolimbot Mar 9, 2022
f36e518
move unary union to new API
paleolimbot Mar 9, 2022
eea70c3
shuffle accessors that return a geography to their own file
paleolimbot Mar 11, 2022
f47c55f
swap out convex hull query
paleolimbot Mar 11, 2022
dc22028
more flexible s2_project_normalized(), sketch s2_interpolate_normaliz…
paleolimbot Mar 17, 2022
d43d258
use new api for interpolate_normalized
paleolimbot Mar 17, 2022
f29bc24
use new API for buffer_cells()
paleolimbot Mar 17, 2022
afbdd0c
remove unused headers
paleolimbot Mar 17, 2022
1fcd5c4
bounds use new geographies
paleolimbot Mar 18, 2022
636ce8b
coverings
paleolimbot Mar 18, 2022
9374f15
add basic indexer
paleolimbot Mar 20, 2022
75b3e1e
more using new API for matrix
paleolimbot Mar 21, 2022
a8e5743
remove old index infrastructure, use new index for matrix predicates
paleolimbot Mar 21, 2022
a2a9005
add preeicates
paleolimbot Mar 25, 2022
13d7000
fix touches
paleolimbot Mar 25, 2022
98b0862
actually use new s2_intersects_box()
paleolimbot Mar 27, 2022
69690ca
use new predicates in s2-matrix
paleolimbot Mar 27, 2022
a6e36d2
wrap regions instead of copying data
paleolimbot Mar 27, 2022
b605e14
explain the S2RegionWrapper
paleolimbot Mar 27, 2022
0ad573d
remove mutable shape index accessor
paleolimbot Mar 29, 2022
934b1c8
move ownership of index set outside index
paleolimbot Mar 29, 2022
f34774e
start on new constructors
paleolimbot Apr 3, 2022
34cbb87
maybe fix Windows/R 3.6 error
paleolimbot Apr 4, 2022
a22fd04
fix wiring for wk handler
paleolimbot Apr 4, 2022
f71ba7f
basics of the constructor work
paleolimbot Apr 8, 2022
72fc88c
valid output for collections
paleolimbot Apr 9, 2022
ad63656
small progress towards builder working
paleolimbot Apr 9, 2022
ae789f5
maybe fix linestring construction
paleolimbot Apr 9, 2022
7dbc2b6
better linestrings maybe but still segfaulting
paleolimbot Apr 10, 2022
6c2fd45
fix export of truly empty (multi)linestring
paleolimbot Apr 10, 2022
f1bcd09
add the example wkt as package data
paleolimbot Apr 12, 2022
5daae43
using the new constructor for wkt
paleolimbot Apr 12, 2022
7326d3f
use writer for make_polygon
paleolimbot Apr 13, 2022
5301092
superceed previous s2-constructors-formatters
paleolimbot Apr 13, 2022
67c1c29
remove previous builder implementation
paleolimbot Apr 13, 2022
6846652
fix C++ errors on gcc
paleolimbot Apr 13, 2022
84f4a26
fix constructors
paleolimbot Apr 13, 2022
47f37bf
remove superceeded methods
paleolimbot Apr 13, 2022
fe05448
fix include
paleolimbot Apr 14, 2022
b4f2070
include fewer headers, fix compile and test
paleolimbot Apr 14, 2022
201c0bb
wk_handle method for geography
paleolimbot Apr 17, 2022
731f313
actually use new exporters
paleolimbot Apr 17, 2022
553acc5
simplify Geography class and use it
paleolimbot Apr 21, 2022
b93cc48
fix point constructor
paleolimbot Apr 21, 2022
e7cb711
S2GeographyOwningPoint -> PointGeography
paleolimbot Apr 21, 2022
8176023
more renaming to saner names
paleolimbot Apr 21, 2022
1b4137b
more renaming
paleolimbot Apr 21, 2022
7c8e602
rename index
paleolimbot Apr 21, 2022
3d33c13
stop pretending that there will be a C API
paleolimbot Apr 21, 2022
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
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ S3method(str,s2_cell_union)
S3method(str,s2_xptr)
S3method(unique,s2_cell)
S3method(unlist,s2_cell_union)
S3method(wk_handle,s2_geography)
export(as_s2_cell)
export(as_s2_cell_union)
export(as_s2_geography)
Expand Down Expand Up @@ -202,4 +203,5 @@ importFrom(Rcpp,sourceCpp)
importFrom(utils,str)
importFrom(wk,as_wkb)
importFrom(wk,as_wkt)
importFrom(wk,wk_handle)
useDynLib(s2, .registration = TRUE)
32 changes: 0 additions & 32 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -253,42 +253,10 @@ cpp_s2_cell_common_ancestor_level_agg <- function(cellId) {
.Call(`_s2_cpp_s2_cell_common_ancestor_level_agg`, cellId)
}

cpp_s2_geog_point <- function(x, y) {
.Call(`_s2_cpp_s2_geog_point`, x, y)
}

cpp_s2_make_line <- function(x, y, featureId) {
.Call(`_s2_cpp_s2_make_line`, x, y, featureId)
}

cpp_s2_make_polygon <- function(x, y, featureId, ringId, oriented, check) {
.Call(`_s2_cpp_s2_make_polygon`, x, y, featureId, ringId, oriented, check)
}

s2_geography_from_wkb <- function(wkb, oriented, check) {
.Call(`_s2_s2_geography_from_wkb`, wkb, oriented, check)
}

s2_geography_from_wkt <- function(wkt, oriented, check) {
.Call(`_s2_s2_geography_from_wkt`, wkt, oriented, check)
}

s2_geography_full <- function(x) {
.Call(`_s2_s2_geography_full`, x)
}

s2_geography_to_wkt <- function(s2_geography, precision, trim) {
.Call(`_s2_s2_geography_to_wkt`, s2_geography, precision, trim)
}

s2_geography_to_wkb <- function(s2_geography, endian) {
.Call(`_s2_s2_geography_to_wkb`, s2_geography, endian)
}

s2_geography_format <- function(s2_geography, maxCoords, precision, trim) {
.Call(`_s2_s2_geography_format`, s2_geography, maxCoords, precision, trim)
}

s2_lnglat_from_numeric <- function(lng, lat) {
.Call(`_s2_s2_lnglat_from_numeric`, lng, lat)
}
Expand Down
7 changes: 7 additions & 0 deletions R/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,10 @@ s2_data_cities <- function(name = NULL) {

as_s2_geography(wkb)
}

#' Example Geometries
#'
#' These geometries are toy examples useful for testing various coordinate
#' shuffling operations in the s2 package.
#'
"s2_data_example_wkt"
69 changes: 37 additions & 32 deletions R/s2-constructors-formatters.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,72 +70,77 @@
#' s2_as_binary(geog)
#'
s2_geog_point <- function(longitude, latitude) {
recycled <- recycle_common(longitude, latitude)
new_s2_xptr(cpp_s2_geog_point(recycled[[1]], recycled[[2]]), "s2_geography")
wk::wk_handle(wk::xy(longitude, latitude), s2_geography_writer())
}

#' @rdname s2_geog_point
#' @export
s2_make_line <- function(longitude, latitude, feature_id = 1L) {
recycled <- recycle_common(longitude, latitude, feature_id)
new_s2_xptr(cpp_s2_make_line(recycled[[1]], recycled[[2]], featureId = recycled[[3]]), "s2_geography")
wk::wk_handle(
wk::xy(longitude, latitude),
wk::wk_linestring_filter(
s2_geography_writer(),
feature_id = as.integer(feature_id)
)
)
}

#' @rdname s2_geog_point
#' @export
s2_make_polygon <- function(longitude, latitude, feature_id = 1L, ring_id = 1L,
oriented = FALSE, check = TRUE) {
recycled <- recycle_common(longitude, latitude, feature_id, ring_id)
new_s2_xptr(
cpp_s2_make_polygon(
recycled[[1]], recycled[[2]],
featureId = recycled[[3]],
ringId = recycled[[4]],
oriented = oriented,
check = check
),
"s2_geography"
wk::wk_handle(
wk::xy(longitude, latitude),
wk::wk_polygon_filter(
s2_geography_writer(oriented = oriented, check = check),
feature_id = as.integer(feature_id),
ring_id = as.integer(ring_id)
)
)
}

#' @rdname s2_geog_point
#' @export
s2_geog_from_text <- function(wkt_string, oriented = FALSE, check = TRUE) {
attributes(wkt_string) <- NULL
wk::validate_wk_wkt(wk::new_wk_wkt(wkt_string))
new_s2_xptr(
s2_geography_from_wkt(
wkt_string,
oriented = oriented,
check = check
),
"s2_geography"
wkt <- wk::new_wk_wkt(wkt_string, geodesic = TRUE)
wk::validate_wk_wkt(wkt)

wk::wk_handle(
wkt,
s2_geography_writer(oriented = oriented, check = check)
)
}

#' @rdname s2_geog_point
#' @export
s2_geog_from_wkb <- function(wkb_bytes, oriented = FALSE, check = TRUE) {
attributes(wkb_bytes) <- NULL
wk::validate_wk_wkb(wk::new_wk_wkb(wkb_bytes))
new_s2_xptr(
s2_geography_from_wkb(
wkb_bytes,
oriented = oriented,
check = check
),
"s2_geography"
wkb <- wk::new_wk_wkb(wkb_bytes)
wk::validate_wk_wkb(wkb)
wk::wk_handle(
wkb,
s2_geography_writer(oriented = oriented, check = check)
)
}

#' @rdname s2_geog_point
#' @export
s2_as_text <- function(x, precision = 16, trim = TRUE) {
s2_geography_to_wkt(as_s2_geography(x), precision = precision, trim = trim)
wkt <- wk::wk_handle(
as_s2_geography(x),
wk::wkt_writer(precision = precision, trim = trim)
)

attributes(wkt) <- NULL
wkt
}

#' @rdname s2_geog_point
#' @export
s2_as_binary <- function(x, endian = wk::wk_platform_endian()) {
structure(s2_geography_to_wkb(as_s2_geography(x), endian = endian), class = "blob")
structure(
wk::wk_handle(as_s2_geography(x), wk::wkb_writer(endian = endian)),
class = "blob"
)
}
49 changes: 19 additions & 30 deletions R/s2-geography.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ as_s2_geography.s2_geography <- function(x, ...) {
#' @export
as_s2_geography.s2_lnglat <- function(x, ...) {
df <- data_frame_from_s2_lnglat(x)
new_s2_xptr(cpp_s2_geog_point(df[[1]], df[[2]]), "s2_geography")
s2_geog_point(df[[1]], df[[2]])
}

#' @rdname as_s2_geography
Expand Down Expand Up @@ -76,28 +76,22 @@ as_s2_geography.wk_wkb <- function(x, ..., oriented = FALSE, check = TRUE) {
}
}

new_s2_xptr(
s2_geography_from_wkb(x, oriented = oriented, check = check),
"s2_geography"
wk::wk_handle(
x,
s2_geography_writer(oriented = oriented, check = check)
)
}

#' @rdname as_s2_geography
#' @export
as_s2_geography.WKB <- function(x, ..., oriented = FALSE, check = TRUE) {
new_s2_xptr(
s2_geography_from_wkb(x, oriented = oriented, check = check),
"s2_geography"
)
s2_geog_from_wkb(x, oriented = oriented, check = check)
}

#' @rdname as_s2_geography
#' @export
as_s2_geography.blob <- function(x, ..., oriented = FALSE, check = TRUE) {
new_s2_xptr(
s2_geography_from_wkb(x, oriented = oriented, check = check),
"s2_geography"
)
s2_geog_from_wkb(x, oriented = oriented, check = check)
}

#' @rdname as_s2_geography
Expand All @@ -119,19 +113,16 @@ as_s2_geography.wk_wkt <- function(x, ..., oriented = FALSE, check = TRUE) {
}
}

new_s2_xptr(
s2_geography_from_wkt(x, oriented = oriented, check = check),
"s2_geography"
wk::wk_handle(
x,
s2_geography_writer(oriented = oriented, check = check)
)
}

#' @rdname as_s2_geography
#' @export
as_s2_geography.character <- function(x, ..., oriented = FALSE, check = TRUE) {
new_s2_xptr(
s2_geography_from_wkt(x, oriented = oriented, check = check),
"s2_geography"
)
s2_geog_from_text(x, oriented = oriented, check = check)
}

#' @rdname as_s2_geography
Expand All @@ -145,22 +136,20 @@ as_s2_geography.logical <- function(x, ...) {
#' @rdname as_s2_geography
#' @export
as_wkb.s2_geography <- function(x, ...) {
wk::new_wk_wkb(
s2_geography_to_wkb(x, wk::wk_platform_endian()),
crs = wk::wk_crs_longlat("WGS84"),
geodesic = TRUE
)
wkb <- wk::wk_handle(x, wk::wkb_writer())
wk::wk_is_geodesic(wkb) <- TRUE
wk::wk_crs(wkb) <- wk::wk_crs_longlat()
wkb
}

#' @importFrom wk as_wkt
#' @rdname as_s2_geography
#' @export
as_wkt.s2_geography <- function(x, ...) {
wk::new_wk_wkt(
s2_geography_to_wkt(x, precision = 16, trim = TRUE),
crs = wk::wk_crs_longlat(),
geodesic = TRUE
)
wkt <- wk::wk_handle(x, wk::wkt_writer())
wk::wk_is_geodesic(wkt) <- TRUE
wk::wk_crs(wkt) <- wk::wk_crs_longlat()
wkt
}


Expand All @@ -180,7 +169,7 @@ as_wkt.s2_geography <- function(x, ...) {

#' @export
format.s2_geography <- function(x, ..., max_coords = 5, precision = 9, trim = TRUE) {
paste0("<", s2_geography_format(x, max_coords, precision, trim), ">")
wk::wk_format(x, precision = precision, max_coords = max_coords, trim = trim)
}

# this is what gets called by the RStudio viewer, for which
Expand Down
14 changes: 12 additions & 2 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,18 @@ stop_problems <- function(feature_id, problem, header) {

expect_wkt_equal <- function(x, y, precision = 16) {
testthat::expect_equal(
s2_geography_to_wkt(as_s2_geography(x), precision = precision, trim = TRUE),
s2_geography_to_wkt(as_s2_geography(y), precision = precision, trim = TRUE)
wk::wk_format(
as_s2_geography(x),
precision = precision,
trim = TRUE,
max_coords = .Machine$integer.max
),
wk::wk_format(
as_s2_geography(y),
precision = precision,
trim = TRUE,
max_coords = .Machine$integer.max
)
)
}

Expand Down
17 changes: 17 additions & 0 deletions R/wk-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,23 @@ s2_projection_filter <- function(handler, projection = s2_projection_plate_carre
)
}

#' @importFrom wk wk_handle
#' @export
wk_handle.s2_geography <- function(geog, handler, ...) {
.Call(c_s2_handle_geography, geog, wk::as_wk_handler(handler))
}

s2_geography_writer <- function(oriented = FALSE, check = TRUE) {
wk::new_wk_handler(
.Call(
c_s2_geography_writer_new,
as.logical(oriented)[1],
as.logical(check)[1]
),
"s2_geography_writer"
)
}

#' @rdname s2_unprojection_filter
#' @export
s2_projection_plate_carree <- function() {
Expand Down
Loading