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

S3 proj_trans() Generic #56

Merged
merged 16 commits into from
Apr 17, 2024
Merged

S3 proj_trans() Generic #56

merged 16 commits into from
Apr 17, 2024

Conversation

anthonynorth
Copy link
Collaborator

@anthonynorth anthonynorth commented Mar 22, 2024

Re-implements proj_trans(), leveraging wk::wk_handle() and proj_trans_create(). This allows for inputs to take any form that wk::wk_handle() supports, including:

  • Coordinate matrices
  • Data frames with coordinates
  • {wk} geometry vectors: xy, wkt, wkb, rct,
  • {sf} geometry vectors
  • {geos} geometry vectors
  • {geoarrow} geometry vectors
  • {s2} geography vectors (although this makes little sense)
  • A data frame containing a geometry/geography vector from wk, sf, geos, geoarrow, s2

For coordinate matrices / data frames, there's an approximate 15-20% compute overhead compared with previous implementation. Benchmark:

library(PROJ)

xy <- wk::xy(
  runif(1e6, -180, 180),
  runif(1e6, -90, 90),
  "OGC:CRS84"
)

coords <- as.matrix(xy)
sfc <- sf::st_as_sfc(xy)

bench::mark(
  .Call(PROJ:::C_proj_trans_xy, wk::xy_x(xy), wk::xy_y(xy), "OGC:CRS84", "EPSG:3857"),
  .Call(PROJ:::C_proj_trans_list, unclass(xy), "OGC:CRS84", "EPSG:3857"),
  proj_trans(xy, "EPSG:3857"),
  sf::sf_project("OGC:CRS84", "EPSG:3857", coords),
  proj_trans(sfc, "EPSG:3857"),
  sf::st_transform(sfc, "EPSG:3857"),
  iterations = 5,
  check = FALSE
)
#> Warning: Some expressions had a GC in every iteration; so filtering is
#> disabled.
#> # A tibble: 6 × 6
#>   expression                           min   median `itr/sec` mem_alloc `gc/sec`
#>   <bch:expr>                      <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
#> 1 ".Call(PROJ:::C_proj_trans_xy,… 167.98ms 168.92ms     5.92     15.3MB    0
#> 2 ".Call(PROJ:::C_proj_trans_lis…  163.6ms 164.08ms     6.04     15.3MB    1.21
#> 3 "proj_trans(xy, \"EPSG:3857\")" 213.58ms 213.74ms     4.64     15.4MB    0.929
#> 4 "sf::sf_project(\"OGC:CRS84\",… 164.49ms 168.31ms     5.95     15.3MB    1.19
#> 5 "proj_trans(sfc, \"EPSG:3857\"… 598.56ms 652.47ms     1.31     30.8MB    1.31
#> 6 "sf::st_transform(sfc, \"EPSG:…    3.03s    3.38s     0.293   114.8MB    0.820

Created on 2024-03-22 with reprex v2.1.0

Closes #49

Breaking changes

  • Z and T coordinate vectors are always supplied in the x parameter
  • Renamed parameters: target -> target_crs, source -> source_crs
  • source_crs defaults to the input crs if available, falling back to OGC:CRS84
  • If coordinate matrices are named, expects names "x", "y" and optionally "z" and/or "m" columns. Previously names were ignored
  • Coordinate data frames expect names "x", "y" and optionally "z" and/or "m" columns
  • Output coordinate matrix or data frame will always contain names "x", "y" and optionally "z" and/or "m" depending on use_z and use_m parameters

@anthonynorth
Copy link
Collaborator Author

Failing tests are related to paleolimbot/wk#217 which hasn't yet landed on CRAN.

@mdsumner
Copy link
Member

I'll check some more tomorrow, I think this is fine to merge and I will probably just do that after I do my "usage" checks for usual tricks.

@anthonynorth
Copy link
Collaborator Author

I haven't yet removed the existing C_proj_trans_xy() and C_proj_trans_list() functions. Useful for a benchmark, but wasn't sure what you want to do with them. We can refactor these to avoid taking any perf hit for the matrix/dataframe usage if it's important.

I haven't attempted to replicate this behaviour in the {wk} transformer. I think it's doable though

PROJ/src/C_proj_trans.c

Lines 78 to 81 in 7f4c4e7

if (r) {
Rprintf("Error detected, some values Inf (error code: %i)\n\n", r);
Rprintf("' %s\n\n '", proj_errno_string(r));
}

Copy link
Member

@mdsumner mdsumner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm!

@mdsumner
Copy link
Member

any reason not to merge this in? I'm keen to do that and move on, I can spend some time here now

@anthonynorth
Copy link
Collaborator Author

Sorry for the delay, I've been unable to get back to this. I'll drop the superseded c functions and then we can merge.

We could put in a guard for the failing tests also since they're dependent on wk > 0.9.1 which isn't on cran.

@anthonynorth anthonynorth marked this pull request as ready for review April 17, 2024 02:31
@anthonynorth anthonynorth merged commit dc0c3de into hypertidy:main Apr 17, 2024
6 of 7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

replace proj_trans with wk::xy impl.
2 participants