google / s2geometry

Computational geometry and spatial indexing on the sphere
http://s2geometry.io/
Apache License 2.0
2.29k stars 303 forks source link

How to build buffered region of a Polyline? #201

Closed jievince closed 3 years ago

jievince commented 3 years ago

Like what geography ST_Buffer(geography g1, float radius_of_buffer) does in postgis. For a Point, I could use S2Cap(S2Point, radius); For a Polygon, perhaps I could use S2ShapeIndexBufferedRegion(S2Polygon::index(), radius) (not sure); But for a Polyline, what could I do? image

jmr commented 3 years ago

There's no built-in support for that. You either need to walk the polyline and emit points for a polygon, or (perhaps, not sure if this will work) build it out of unions and intersections of S2Caps.

jievince commented 3 years ago

There's no built-in support for that. You either need to walk the polyline and emit points for a polygon, or (perhaps, not sure if this will work) build it out of unions and intersections of S2Caps.

Hi @jmr, my previous description may have been a bit misleading, I wasn't trying to get a buffer of a polyline, I was trying to get the cell covering of the buffer of the polyline and then index cellIDs stored in a KV database by the cell covering. I find a way to do it, is it right?

MutableS2ShapeIndex index;
index.Add(std::make_unique<S2Polyline::Shape>(polyLine)); // First, get the S2ShapeIndex of the polyline, just like what Polygon::index() does.
S2ShapeIndexBufferedRegion region(&index, radius); // Then, get the buffer expanded by a radius
coverer.GetCovering(region, &covering); // Finally, get the cell covering of the buffer.
eengle commented 3 years ago

Yes, that's correct. That performs index-accelerated distance tests against the underlying index of the line, so it should be fairly efficient as well.

jievince commented 3 years ago

Yes, that's correct. That performs index-accelerated distance tests against the underlying index of the line, so it should be fairly efficient as well.

Thank you very much