# polyline clipping

61 views

### Michał Hubert

Sep 5, 2020, 2:45:11 PM9/5/20
to s2geometry-io
Hi,

I am wondering if there is any easier way to clip geometry in relation to cells' boundaries at given level.
So far I am doing it this way (to get intersection point between line segment) and cell:

p1 := s2.LatLngFromDegrees(11,21)
p2 := s2.LatLngFromDegrees(31,41)
coo := make([]s2.LatLng, 2)
coo[0] = p1
coo[1] = p2

cellid := s2.CellFromLatLng(p1).ID().Parent(3) //get some cell
cell := s2.CellFromCellID(cellid)
var po1 s2.Point = cell.Vertex(1)
var po2 s2.Point = cell.Vertex(2)
beg := s2.PointFromLatLng(p1)
end := s2.PointFromLatLng(p2)
res := s2.Intersection(po1, po2, beg, end)
inter := s2.LatLngFromPoint(res)
lat := inter.Lat.Degrees()
lon := inter.Lng.Degrees()
fmt.Println(lat,lon) //intersection

### Michał Hubert

Sep 6, 2020, 5:05:59 PM9/6/20
to Mark Heath, s2geometry-io
Hi Mark,

On 6 Sep 2020, at 07:49, Mark Heath <silic...@gmail.com> wrote:

This is the code I use to produce a polygon from a cell.

S2Cell cell;
S2Loop cellLoop = new S2Loop(cell);
return new S2Polygon(cellLoop);

Nice. Thanks !

I then perform an intersection between this and the target polygon, hopefully you can do something similar with a line segment.

I'm also not 100% sure what your code is doing but I am concerned that you are only getting 2 cell vertices.  Cells are not square and need more than 2 points to correctly define them.  Also a cell, no matter how small, occupies 2d space on the sphere (even if it's 2mm^2 area). So any intersection with it and a line will result in a sub section of that line, not a point, so not quite sure what your logic is.

You are right. My code is just illustration how I am doing clipping now. Agree that normally I shall consider all four sides of cells and iterate over polyline segments to compute intersections points.
My code was about finding intersection point.

Thanks