Skip to content

Fix for linestrings enclosed by polygon #9

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

Merged
merged 1 commit into from
Aug 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 13 additions & 24 deletions geojson/geojson_s2_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@ func polylineIntersectsPoint(pls []*s2.Polyline,

func polylineIntersectsPolygons(pls []*s2.Polyline,
s2pgns []*s2.Polygon) bool {
// Early exit if the polygon contains any of the line's vertices.
for _, pl := range pls {
for i := 0; i < pl.NumEdges(); i++ {
edge := pl.Edge(i)
for _, s2pgn := range s2pgns {
if s2pgn.IntersectsCell(s2.CellFromPoint(edge.V0)) ||
s2pgn.IntersectsCell(s2.CellFromPoint(edge.V1)) {
return true
}
}
}
}

for _, pl := range pls {
for _, s2pgn := range s2pgns {
for i := 0; i < pl.NumEdges(); i++ {
Expand Down Expand Up @@ -75,30 +88,6 @@ func geometryCollectionIntersectsShape(gc *GeometryCollection,
return false
}

func polygonsIntersectsLinestrings(s2pgn *s2.Polygon,
pls []*s2.Polyline) bool {
for _, pl := range pls {
for i := 0; i < pl.NumEdges(); i++ {
edge := pl.Edge(i)
a := []float64{edge.V0.X, edge.V0.Y}
b := []float64{edge.V1.X, edge.V1.Y}

for j := 0; j < s2pgn.NumEdges(); j++ {
edgeB := s2pgn.Edge(j)

c := []float64{edgeB.V0.X, edgeB.V0.Y}
d := []float64{edgeB.V1.X, edgeB.V1.Y}

if doIntersect(a, b, c, d) {
return true
}
}
}
}

return false
}

func polygonsContainsLineStrings(s2pgns []*s2.Polygon,
pls []*s2.Polyline) bool {
linesWithIn := make(map[int]struct{})
Expand Down
6 changes: 3 additions & 3 deletions geojson/geojson_shapes_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -1232,8 +1232,8 @@ func checkPolygonIntersectsShape(s2pgn *s2.Polygon, shapeIn,
// check if the other shape is a linestring.
if ls, ok := other.(*LineString); ok {

if polygonsIntersectsLinestrings(s2pgn,
[]*s2.Polyline{ls.pl}) {
if polylineIntersectsPolygons([]*s2.Polyline{ls.pl},
[]*s2.Polygon{s2pgn}) {
return true, nil
}

Expand All @@ -1243,7 +1243,7 @@ func checkPolygonIntersectsShape(s2pgn *s2.Polygon, shapeIn,
// check if the other shape is a multilinestring.
if mls, ok := other.(*MultiLineString); ok {

if polygonsIntersectsLinestrings(s2pgn, mls.pls) {
if polylineIntersectsPolygons(mls.pls, []*s2.Polygon{s2pgn}) {
return true, nil
}

Expand Down
6 changes: 6 additions & 0 deletions geojson/geojson_shapes_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,12 @@ var GlueBytes = []byte("##")
// can be used later while filering the doc values.
func NewGeometryCollection(coordinates [][][][][]float64,
typs []string) (index.GeoJSON, []byte, error) {
if typs == nil {
return nil, nil, fmt.Errorf("nil type information")
}
if len(typs) < len(coordinates) {
return nil, nil, fmt.Errorf("missing type information for some shapes")
}
shapes := make([]index.GeoJSON, 0, len(coordinates))
for i, vertices := range coordinates {
s, _, err := NewGeoJsonShape(vertices, typs[i])
Expand Down