-
Notifications
You must be signed in to change notification settings - Fork 291
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
s2 for spherical geometry #502
Comments
How does the perfiormance loss of s2 compare to that of geosphere? |
geosphere does not provide intersects/intersection. |
|
I'm sure you've already looked into boost? |
Did you find any indication that it does spherical geometry? |
I didn't find spherical calculations in my old searches, but that might have changed or I have overlooked it. This issue mentions spherical area, so maybe there is something in there... |
I didn't do a thorough search through the documentation, but this seems to suggest that it does calculations on the sphere also here it mentions that area can be calculated on the sphere |
You're right - thanks! |
Boost.Geometry is included in bh |
I guess the big question is whether they do spherical intersections and unions of polygons. I couldn't find an example right off the bat. What do you think? |
In the sym_difference page it says that spherical is not yet supported. I couldn't find anything for intersection |
I tried the following example:
modified from their intersection example; it seemed to work OK, but I didn't compare to sf/s2. I couldn't find out well whether polygons on s2 are/can be indexed. |
Hi @barendgehrels, long time no see! We're evaluating adopting boost::geometry (b::g) for use in R package for simple features (here) and elsewhere. Work so far focused on GEOS and google's s2. A few short questions related to b::g:
Looks like great work! |
Hi @edzer nice to hear from you! Thanks for your interest in Boost.Geometry You probably mean as released (1.65)? We are currently busy with spherical geometry and geography. It is currently not me doing that, but Adam and Vissarion. Spherical segments can be intersected but the work is not all finished, so two spherical polygons cannot yet be intersected. However, that is to be expected. WKB is (actually already a long time) provided as an extension and never released officially (contrary to WKT which was there from the beginning). I will discuss when we will release this. About indexed geometries, @awulkiew does know all the details of this, can you comment? So not all finished. But it would be cool to see Boost.Geometry used in R! |
Hi @edzer @barendgehrels, All relational and set operations, For geographic geometries you'd have to pass the strategies explicitly into the algorithm but they are not documented for now. By default spherical strategies are used also for geographic geometries so the results are the same in both coordinate systems. Regarding the spatial indexing, I'm not sure what do you mean by "indexed geometries". As it is now the rtree requires the user to create envelopes, the spatial index, query the index for envelopes and then check corresponding geometries. Non-cartesian coordinate systems are supported by the rtree since it relies on relational operations of "simple" geometries like Point/Box or Box/Box. In order to use it with other geometries like Polygon you also need spherical envelope and relational operations for "more complex" geometries and as I mentioned above the support is there. |
Definitely sounds like we should have an eye on Boost.Geometry. I have been playing a bit with using indexed queries for intersections with the I guess spherical geometry and making an index on the sphere is just more complicated than in 2D... What kind of envelope and index is used in Boost.Geometry @awulkiew ? Is everything bounding boxes and R-trees in 3-dimensional Euclidean space? Or is there a special index on the sphere? |
@rubak In Boost.Geometry envelopes of 2D spherical geometries are 2D MBRs on the surface of the sphere being cartesian products in the spherical coordinates space, so they are not 3D cartesian (ECEF) boxes. They are MBRs having edges going along parallels and meridians, they are not polygons. Therefore in spherical case v.s. cartesian:
|
@awulkiew Thanks for clarifying this. If I understand you correctly:
|
@edzer Do I understand correctly that your points are not corresponding to my points? ;)
|
Ah, yes, so it seems b::g takes care of the dateline but otherwise is identical to the case where long/lat are taken as 2D? With spherical geometry, I had in mind what S2 does: assume that edges between all vertices are great circle segments. This would mean that |
@edzer Maybe your zero area example is just related to the comment by @awulkiew about a bug for polygons containing the pole? As I understand it b::g does spherical geometry like S2 with polygon sides defined by great circle segments, but for the R-tree index it uses (lon,lat)-boxes which are not spherical polygons. |
This is what I tried: #include <iostream>
#include <deque>
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/foreach.hpp>
namespace bg = boost::geometry;
int main()
{
typedef bg::model::polygon<bg::model::point<float, 2, bg::cs::spherical_equatorial<bg::degree> > > pol;
typedef bg::model::point<float, 2, bg::cs::spherical_equatorial<bg::degree> > point;
pol green, red;
boost::geometry::read_wkt("POLYGON((0 0,60 0,60 60,0 60,0 0))", green);
boost::geometry::read_wkt("POLYGON((30 61,31 61,31 62,30 61))", red);
std::deque<pol> output;
boost::geometry::intersection(green, red, output);
std::cout << boost::geometry::wkt(output[0]) << std::endl;
return 0;
} but I get completely lost in the error messages from g++ 5.4.0 and boost 1.65. |
In Boost.Geometry Boxes/MBRs/envelopes are not Polygons. Polygon's edges indeed are great-circle arcs however Box's edges are meridian and parallel (small-circle) arcs. For instance:
generates:
So these are Boxes with edges along parallels and meridians capable to contain the geometry. Unfortunately it seems that there is an error in spherical
generates:
which is wrong. The result should be the same as above. Regarding the compilation error, you're passing Regarding the question about the area of |
@awulkiew thanks for comments and explanations! |
Thanks for the explanation. I updated the example above, and made both rings counter clockwise. Still, the intersection I get is an empty polygon, which surprised me. |
Yes, the result should not be empty. For me the following program (tested Boost 1.65.0 and 1.65.1 with msvc-14 and mingw-w64 g++4.8):
generates:
Is it different for you? |
It seems like Boost.Geometry is probably the way to go for spherical calculations in |
@rubak I agree, although we haven't explored CGAL, which I believe now also comes as header templates. @awulkiew it turns out I compiled against boost 1.58 (ubuntu xenial); I had in mind 1.65 because that is the BH R package with boost headers, which R packages link against (I mostly use c++ for R packages, which pick up 1.65, barely "rare" as above). My apologies! @barendgehrels let me know when you want me to look into WKB I/O, and where I can find it. |
@edzer I didn't know that CGAL also is a header library now. That is very interesting. I have looked at it several times, but it has always been too big a system dependency for me. Maybe that drawback is over now. However, after a bit of searching it appears that the default way of installing CGAL even as header only uses CMAKE and linking to gmp, mpfr, and zlib is recommended, so there might be some work in making this work with the standard R package toolchain. Anyway, it's another interesting option for someone to look deeper into... |
@edzer I'm glad everything works as expected. Regarding WKB, all extensions has to be grabbed from Boost.Geometry develop branch. In this case from here: |
I moved the s2 stuff to a branch for now. I will look into the boost stuff deeper when I understand template programming better -- in sf we handle geometries at the geometry level, not at the level of their types. I can see the examples for this in the boost docs, I just can't read them, yet. |
OK @edzer , thanks for trying it out. Actually the intention of Boost.Geometry is that users do not need any knowlegde of the underlying templates or design. Apart from defining the type (maybe we could also avoid that), no templates are necessary for basic operations. But I understand, as soon as you get errors, it is often hard to figure out the real cause between all template noise. Especially if you use the brand new features from the library. Anyway, if you need more help, just ping. Regards, Barend |
If anyone's interested I too wanted to learn a bit more about Boost Geometry, so decided to start implementing it in R in my repo. |
Google developed a library for spherical geometry, s2, the C++ implementation of which is no longer maintained. Ege @rubak developed an R package s2 with an R interface to this library; the package contains the whole library, so has no new external dependency. Ege's UseR! talk in Brussels is found here. A nice explanation of s2, and in particular its ultra-clever indexing mechanism, is found here.
Over the weekend, I worked on a first prototype for interfacing sf with s2 (R code, tests). So far, we would get for a polar polygon wrong intersections and centroids:
With s2, we get correct values:
The question is how to go from here. Current issues:
contains
, but not the whole list of simple feature predicatesReactions welcome!
The text was updated successfully, but these errors were encountered: