-
Notifications
You must be signed in to change notification settings - Fork 42
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
switch to 'sf' for projection and polygon calculations? #1629
Comments
I handled the |
A checklist for
|
I second this work (obviously 😛 ). Installing |
Thanks for checking into sf availability. I think sf will get more popular as time goes on. (If you've not read the article on it, I recommend it (first item in the 'blogs' part of their webpage).) |
library(oce)
data(coastlineWorld)
options("oce:test_sf" = 1) # increases time from 1.8s to 2.1s
print(system.time(
cl <- subset(coastlineWorld, -80<lon & lon<-50 & 30<lat & lat<60)
))
if (!interactive()) png("sf_20191229.png")
plot(cl, clon=-65, clat=45, span=6000)
rect(-80, 30, -50, 60, bg="transparent", border="red")
if (!interactive()) dev.off() |
I think I've done the first phase at each relevant spot, in the "sf" branch commit 99fb05e. The test code at https://github.com/dankelley/oce-issues/tree/master/16xx/1629 runs without producing errors and warnings, which means that the "old" scheme and the "new" (sf-based) scheme produce identical results. These tests are only performed if I am now ready for phase 2, in which the tests are done even without setting that option. That way, anyone using the
|
The vignette now builds with the "sf" branch, with the following status indicators.
|
I just noticed that A lot of the time is in determining where to place axis labels. This might seem like a simple thing, but it isn't, because lon and lat lines might intersect either atlas. The way I handle this (say for the lower axis) is to use This is with |
Sigh. One step forward, two steps back. |
I have an idea for another way to get the calculation done, without calling |
This might be better in a f2f, but is this comment relevant here: |
As noted in our f2f earlier today, I have another way of sidestepping the slowness in finding locations for longitude and latitude labels: record intersections of longitude and latitude lines with the edges of the plot region. A proof of concept, showing which library(sf)
x <- seq(0, 5, 0.1)
y <- sin(x*pi*2/2)
curve <- sf::st_linestring(cbind(x, y))
ax <- sf::st_linestring(cbind(c(0,5), c(-0.5,-0.5)))
I <- sf::st_intersection(curve, ax)
par(mar=c(3,3,1,1), mgp=c(1.8,0.8,0))
plot(x, y)
plot(ax, add=TRUE, col=2)
plot(I, add=TRUE, col=3, pch=20) |
My testing shows this is pretty simple, e.g. "sf" branch commit 58b7a1e with library(oce)
data(coastlineWorld)
par(mar=c(2,2,1,1))
mapPlot(coastlineWorld, longitudelim=c(-100,0),latitudelim=c(10,70),projection="+proj=robin", col='gray',debug=2) yields as below, so we're getting the intersections right, so now it's a matter of hooking this up with |
I've coded Note that As http://dankelley.github.io/r/2020/01/04/oce-proj.html there is an update to a previous blog entry demonstrating map projections. Since things are looking good for a possible conversion to |
This issue has been automatically marked as stale because no comments have been made in a month. Unless comments are made in the next week (or the 'pinned' label is applied), this issue will be closed automatically in a week. |
I removed the "pinned" tag, because I have ignored this issue for far too long. We have enough features for a new CRAN coming fairly soon, and it would make sense to have this sf work merged into "develop" before then. |
As might be expected for a branch that has been ignored for so long, there are some merge conflicts. Merging "develop" into "sf" gives
And
|
Below are some tests, which are also present in This test suite shows no problems with "sf" branch commit bf87ce6 library(oce)
data(coastlineWorld)
par(mar=c(2,2,1,1))
options(warn=3)
mapPlot(coastlineWorld, projection="+proj=moll", col="gray")
mapPlot(coastlineWorld, projection="+proj=ortho", col="gray")
mapPlot(coastlineCut(coastlineWorld,-100), longitudelim=c(-130,-55), latitudelim=c(35,60),
projection="+proj=lcc +lat_0=30 +lat_1=60 +lon_0=-100", col="gray")
mapPlot(coastlineCut(coastlineWorld, -135), longitudelim=c(-130, 50), latitudelim=c(70, 110),
projection="+proj=stere +lat_0=90 +lon_0=-135", col="gray") |
VOTING: other oce developers are asked to object to the plan outlined here before noon, next Tuesday. I have rebuilt my previous blog items that went through tests of the available map projections within This has uncovered four projections that ought to be removed from oce, for reasons explained in the Executive Summary of http://dankelley.github.io/r/2020/08/02/oce-proj.html, which reads
My plan is to remove those projections. Note that PLAN I plan to remove |
I see that the oce docs mentioned the And that reminds me -- the docs need to stop referring to PROJ.4, because now it's just called PROJ (with a stroke through the O), not PROJ.6 (although that would have made some sense, I suppose). |
+1 from me! |
I did the changes described in #1629 (comment) (a few comments up) in the "sf" branch commit 18c4007 and several commits before that. (I accidentally made a bunch of other fixes in "sf" that I should have made in "develop" ... this was the usual fixing of broken/changed links that seems to be housekeeping that's needed about once per month.) My plan is to live on "sf" for the remainder of the week, and then to merge it into "develop" on Saturday morning. That way, any user who lives on "develop" will become part of the testing team. I imagine there will be some problems, e.g. I had to relax some of my tests that "sf" and "rgdal" match, by only demanding a 1-metre agreement in x-y space. (Granted, that is basically total agreement in an ocean-mapping context, but I am being very careful here because once we transition fully to "sf", it will be hard to transition back to "rgdal".) Maybe I'll start an issue for a CRAN release... |
This issue has been automatically marked as stale because no comments have been made in two weeks. Unless comments are made in the next week (or the 'pinned' label is applied), this issue will be closed automatically in a week. |
I've merged "sf" into "develop" (commit 874a6c3) without difficulties. It builds fine locally, and the following blog posts also suggest that it's OK: |
The travis-CI build failed (after under 2 minutes) but that was just because it could not set up the test machine, since |
I plan to use this code for a week, and after that I'll start commenting-out the existing |
I've been busy with other things, and have not followed my "a week" plan. However, I just tried to do Presumably, over the next while, rgdal will get to a state in which it can cleanly install on macos in the way R users expect (without doing system-level work). Still, this is a reminder that oce users will benefit from dropping oce dependence on rgdal. configure: rgdal: 1.5-17
checking for /usr/bin/svnversion... yes
svnversion: error: The subversion command line tools are no longer provided by Xcode.
configure: svn revision: 1070
checking for gdal-config... no
no
configure: error: gdal-config not found or not executable.
ERROR: configuration failed for package ‘rgdal’
* removing ‘/Library/Frameworks/R.framework/Versions/4.0/Resources/library/rgdal’
* restoring previous ‘/Library/Frameworks/R.framework/Versions/4.0/Resources/library/rgdal’
The downloaded source packages are in
‘/private/var/folders/8b/l4h64m1j22v5pb7vj049ff140000gn/T/Rtmpi7lK2q/downloaded_packages’
Warning message:
In install.packages(update[instlib == l, "Package"], l, repos = repos, :
installation of package ‘rgdal’ had non-zero exit status |
I made a little video (https://youtu.be/3VFRsZPzNFw) about my plans to work on this over the solstice holiday. Partly, this is an experiment to see whether videos are useful. I suspect the answer is 'no' for people reading issues (because a well-written comment can be examined in less time than it takes to watch a video), but 'yes' for people solving issues (because the time to create is about the same, plus there is an advantage to 'thinking out loud'). |
In my video I was noting that I want to check whether most users will have an |
Checklist (mutable, code not yet commited)
|
I strongly recommend that @paleolimbot get involved with this, given his experience with sf and other geo-computations. If you are wanting the help and additional eyes in code, of course. |
Right now, I'm mainly uncommenting code. I'm mainly done, but won't push to GH for a day or two. None of this is anything like a wholesale conversion of code. It's just a matter of switching a function call (and removing a lot of fluff that was needed to get rgdal to work across all platforms). |
Happy to test when it hits the develop branch! The In case you're curious and don't already know, the 1-meter ish difference between sf and rgdal might be because North America moved since 1983 and PROJ 6 was the first version that took into account the datum shift between NAD83 and WGS84 and rgdal must not be taking this into account. |
@paleolimbot I won't need eyes on the changes I'm making today -- it's just code cleanup. What we've had for the past year is that all calculations were done once by Wow, libproj -- that's a flash from the past. I wonder when it will get to CRAN? The place where oce could really use your help (in the new year, perhaps by Q2 if you'll have time) is with what we call the UHL (ugly horizontal line) problem. This is what happens when tracing a path that crosses the 'edge of the earth'. We have some ugly kludges to try to fix that, but I suspect that the real solution is to do proper calculations of edge-of-earth intersection, as I think you have in |
(Some is removed, some is commented-out for a while as a placeholder.) See #1629
This was fixed in commit e70e266 so I'll close the issue now (finally). |
In an f2f meeting yesterday, @richardsc and I agreed that it is worth seeing whether oce can rely on the
sf
package for map projection calculations. I've been kicking this idea around for a while now (see e.g. #1599 and #1604).The goal is to future-proof
oce
against changes that ar coming torgdal
. That package is presently used for map-projection calculations, with thergdal::project()
function. This is a problem, because that function is destined for deprecation and later deletion.I am under the impression that the proposed alternative to
rgdal::project()
isrgdal::spTransform()
, but that is not a solution foroce
because it cannot handle unprojectable points, returning nothing if any point is unprojectable. (An example of an unprojectable point would be one that is just on the "edge of the world" in a view: does it go on one side or the other? This problem comes up in drawing grid lines, for example.)A helpful comment on r-sig-geo-request@r-project.org led me to consider using
st::sf_project()
, and my initial tests (see #1599) show that this has promise.I also want to switch some other bits in the code, where I do point-within-polygon calculations with
sp::point.in.polygon()
and also where I do intersection calculations with e.g.raster::intersect()
. I think all of these things can be done withsf
, and I would prefer to rely on just one single package,sf
, instead of thesp
/raster
/rgdal
triplet. (Relying on multiple packages imposes a cost of learning how to navigate the docs for each of those packages, which is a developer/debugger cost. There are other, more general, problems with excessive package dependence.)I have started doing this work, in a new branch called
sf
that will be unstable for a while, as I fiddle. My approach, so far, is to start by reproducing the older method and usingall.equal()
tests to see if the results are the same. If not, I issue awarning()
.Checklist for point-in-polygon calculations
R/argo.R:532: keep <- 1==sp::point.in.polygon(lon, lat, lonp, latp)
R/map.R:788: erase <- 1==sp::point.in.polygon(xc, yc,
R/section.R:681: keep <- 1==sp::point.in.polygon(lon, lat, lonp, latp)
Checklist for map projection
map.R:57: XY <- unname(rgdal::project(xy, proj=proj, inv=inv))
map.R:67: XY <- unname(rgdal::project(xy, proj=proj, inv=inv, legacy=legacy, allowNAs_if_not_legacy=TRUE))
map.R:73: XY <- unname(rgdal::project(xy=xy, proj=proj, inv=inv, legacy=legacy))
Checklist for polygon extent
coastline.R:222: box <- as(raster::extent(W, E, S, N), "SpatialPolygons")
Checklist for polygon intersection
coastline.R:242: i <- raster::intersect(box, C)
Checklist of sp:: calls (checked when
sf
equivalents are added; comment-strings ignored)keep <- 1==sp::point.in.polygon(lon, lat, lonp, latp)
A <- sp::Polygon(cbind(lon, lat))
B <- sp::Polygons(list(A), "A")
C <- sp::SpatialPolygons(list(B))
erase <- 1==sp::point.in.polygon(xc, yc,
keep <- 1==sp::point.in.polygon(lon, lat, lonp, latp)
section.R:2953:(will not change, since this is an add-on kriging method)input_data=sp::SpatialPointsDataFrame(xy, data.frame(F)),
section.R:2954:(will not change, since this is an add-on kriging method)new_data=sp::SpatialPoints(expand.grid(xg/xr, yg/yr)))
The text was updated successfully, but these errors were encountered: