Skip to content

Commit d9ebb09

Browse files
committed
Updating LineString for multiple points.
The linestring could contain two or more points as per the geoJSON standard and hence updating the utility methods to handle that case.
1 parent 0850958 commit d9ebb09

File tree

2 files changed

+32
-26
lines changed

2 files changed

+32
-26
lines changed

geojson/geojson_s2_util.go

+9-8
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,13 @@ func geometryCollectionIntersectsShape(gc *GeometryCollection,
7878
func polygonsIntersectsLinestrings(s2pgn *s2.Polygon,
7979
pls []*s2.Polyline) bool {
8080
for _, pl := range pls {
81-
if len(*pl) == 2 {
82-
a := []float64{(*pl)[0].X, (*pl)[0].Y}
83-
b := []float64{(*pl)[1].X, (*pl)[1].Y}
81+
for i := 0; i < pl.NumEdges(); i++ {
82+
edge := pl.Edge(i)
83+
a := []float64{edge.V0.X, edge.V0.Y}
84+
b := []float64{edge.V1.X, edge.V1.Y}
8485

85-
for i := 0; i < s2pgn.NumEdges(); i++ {
86-
edgeB := s2pgn.Edge(i)
86+
for j := 0; j < s2pgn.NumEdges(); j++ {
87+
edgeB := s2pgn.Edge(j)
8788

8889
c := []float64{edgeB.V0.X, edgeB.V0.Y}
8990
d := []float64{edgeB.V1.X, edgeB.V1.Y}
@@ -103,9 +104,9 @@ func polygonsContainsLineStrings(s2pgns []*s2.Polygon,
103104
linesWithIn := make(map[int]struct{})
104105
nextLine:
105106
for lineIndex, pl := range pls {
106-
if len(*pl) == 2 {
107-
start := (*pl)[0]
108-
end := (*pl)[1]
107+
for i := 0; i < len(*pl)-1; i++ {
108+
start := (*pl)[i]
109+
end := (*pl)[i+1]
109110

110111
// check whether both the end vertices are inside the polygon.
111112
for _, s2pgn := range s2pgns {

geojson/geojson_shapes_impl.go

+23-18
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,10 @@ func NewGeoJsonLinestring(points [][]float64) index.GeoJSON {
256256

257257
func (ls *LineString) init() {
258258
if ls.pl == nil {
259-
latlngs := make([]s2.LatLng, 2)
260-
latlngs[0] = s2.LatLngFromDegrees(ls.Vertices[0][1], ls.Vertices[0][0])
261-
latlngs[1] = s2.LatLngFromDegrees(ls.Vertices[1][1], ls.Vertices[1][0])
259+
latlngs := make([]s2.LatLng, len(ls.Vertices))
260+
for i, vertex := range ls.Vertices {
261+
latlngs[i] = s2.LatLngFromDegrees(vertex[1], vertex[0])
262+
}
262263
ls.pl = s2.PolylineFromLatLngs(latlngs)
263264
}
264265
}
@@ -1088,9 +1089,11 @@ func checkLineStringsIntersectsShape(pls []*s2.Polyline, shapeIn,
10881089
if c, ok := other.(*Circle); ok {
10891090
centre := c.s2cap.Center()
10901091
for _, pl := range pls {
1091-
edge := pl.Edge(0)
1092-
distance := s2.DistanceFromSegment(centre, edge.V0, edge.V1)
1093-
return distance <= c.s2cap.Radius(), nil
1092+
for i := 0; i < pl.NumEdges(); i++ {
1093+
edge := pl.Edge(i)
1094+
distance := s2.DistanceFromSegment(centre, edge.V0, edge.V1)
1095+
return distance <= c.s2cap.Radius(), nil
1096+
}
10941097
}
10951098

10961099
return false, nil
@@ -1099,18 +1102,20 @@ func checkLineStringsIntersectsShape(pls []*s2.Polyline, shapeIn,
10991102
// check if the other shape is a envelope.
11001103
if e, ok := other.(*Envelope); ok {
11011104
for _, pl := range pls {
1102-
edge := pl.Edge(0)
1103-
latlng1 := s2.LatLngFromPoint(edge.V0)
1104-
latlng2 := s2.LatLngFromPoint(edge.V1)
1105-
a := []float64{latlng1.Lng.Degrees(), latlng1.Lat.Degrees()}
1106-
b := []float64{latlng2.Lng.Degrees(), latlng2.Lat.Degrees()}
1107-
for j := 0; j < 4; j++ {
1108-
v1 := e.r.Vertex(j)
1109-
v2 := e.r.Vertex((j + 1) % 4)
1110-
c := []float64{v1.Lng.Degrees(), v1.Lat.Degrees()}
1111-
d := []float64{v2.Lng.Degrees(), v2.Lat.Degrees()}
1112-
if doIntersect(a, b, c, d) {
1113-
return true, nil
1105+
for i := 0; i < pl.NumEdges(); i++ {
1106+
edge := pl.Edge(i)
1107+
latlng1 := s2.LatLngFromPoint(edge.V0)
1108+
latlng2 := s2.LatLngFromPoint(edge.V1)
1109+
a := []float64{latlng1.Lng.Degrees(), latlng1.Lat.Degrees()}
1110+
b := []float64{latlng2.Lng.Degrees(), latlng2.Lat.Degrees()}
1111+
for j := 0; j < 4; j++ {
1112+
v1 := e.r.Vertex(j)
1113+
v2 := e.r.Vertex((j + 1) % 4)
1114+
c := []float64{v1.Lng.Degrees(), v1.Lat.Degrees()}
1115+
d := []float64{v2.Lng.Degrees(), v2.Lat.Degrees()}
1116+
if doIntersect(a, b, c, d) {
1117+
return true, nil
1118+
}
11141119
}
11151120
}
11161121
}

0 commit comments

Comments
 (0)