Skip to content

Commit

Permalink
[issue-56] Quick fix for line clipping issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
gdey committed Nov 1, 2017
1 parent dcc5fa0 commit 8ffa295
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
27 changes: 25 additions & 2 deletions maths/clip/clip.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"fmt"

colour "github.com/logrusorgru/aurora"
"github.com/terranodo/tegola/maths/validate"
)

/*
Expand Down Expand Up @@ -343,6 +342,30 @@ func CleanLineString(sub []float64) [][]float64 {
retsub[0] = mainsub
return retsub
}
func validateCleanLinestring(g []float64) (l []float64, err error) {

var ptsMap = make(map[maths.Pt][]int)
var pts []maths.Pt
i := 0
for x, y := 0, 1; y < len(g); x, y = x+2, y+2 {

p := maths.Pt{g[x], g[y]}
ptsMap[p] = append(ptsMap[p], i)
pts = append(pts, p)
i++
}

for i := 0; i < len(pts); i++ {
pt := pts[i]
fpts := ptsMap[pt]
l = append(l, pt.X, pt.Y)
if len(fpts) > 1 {
// we will need to skip a bunch of points.
i = fpts[len(fpts)-1]
}
}
return l, nil
}

// linestring does the majority of the clipping of the subject array to the square bounds defined by the rMinPt and rMaxPt.
func linestring(sub []float64, rMinPt, rMaxPt maths.Pt) (clippedSubjects [][]float64, err error) {
Expand Down Expand Up @@ -418,7 +441,7 @@ func linestring(sub []float64, rMinPt, rMaxPt maths.Pt) (clippedSubjects [][]flo
})

// Must have at least 3 points for it to be a valid runstring. (3 *2 = 6)
s, err = validate.CleanLinestring(s)
s, err = validateCleanLinestring(s)
if err != nil {
return nil, err
}
Expand Down
15 changes: 15 additions & 0 deletions maths/validate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/terranodo/tegola"
"github.com/terranodo/tegola/basic"
"github.com/terranodo/tegola/maths"
"github.com/terranodo/tegola/maths/clip"
"github.com/terranodo/tegola/maths/hitmap"
"github.com/terranodo/tegola/maths/makevalid"
)
Expand Down Expand Up @@ -83,6 +84,20 @@ func CleanGeometry(ctx context.Context, g tegola.Geometry, extent float64) (geo
return makePolygonValid(ctx, hm, extent, gg)
case tegola.MultiPolygon:
return makePolygonValid(ctx, hm, extent, gg.Polygons()...)
case tegola.MultiLine:
var ml basic.MultiLine
lns := gg.Lines()
for i := range lns {
nls, err := clip.LineString(lns[i], maths.Pt{-8, -8}, maths.Pt{4104, 4104}, 0)
if err != nil {
return ml, err
}
ml = append(ml, nls...)
}
return ml, nil
case tegola.LineString:
nls, err := clip.LineString(gg, maths.Pt{-8, -8}, maths.Pt{4104, 4104}, 0)
return basic.MultiLine(nls), err
}
return g, nil
}

0 comments on commit 8ffa295

Please sign in to comment.