Backend: Increase search radius for S2 cell IDs

Signed-off-by: Michael Mayer <michael@liquidbytes.net>
This commit is contained in:
Michael Mayer 2020-01-15 12:30:50 +01:00
parent 317ef92c42
commit e78080b3a6
3 changed files with 30 additions and 7 deletions

View file

@ -51,10 +51,10 @@ func (s *Repo) Geo(f form.GeoSearch) (results []GeoResult, err error) {
}
if f.S2 != "" {
s2Min, s2Max := s2.Range(f.S2, 1)
s2Min, s2Max := s2.Range(f.S2, 5)
q = q.Where("photos.location_id BETWEEN ? AND ?", s2Min, s2Max)
} else if f.Olc != "" {
s2Min, s2Max := s2.Range(pluscode.S2(f.Olc), 1)
s2Min, s2Max := s2.Range(pluscode.S2(f.Olc), 5)
q = q.Where("photos.location_id BETWEEN ? AND ?", s2Min, s2Max)
} else {
// Inaccurate distance search, but probably 'good enough' for now

View file

@ -71,7 +71,10 @@ func Range(token string, levelUp int) (min, max string) {
return min, max
}
parent := c.Parent(c.Level() - levelUp)
// See https://s2geometry.io/resources/s2cell_statistics.html
lvl := c.Level()
return parent.Prev().ToToken(), parent.Next().ToToken()
parent := c.Parent(lvl - levelUp)
return parent.Prev().ChildBeginAtLevel(lvl).ToToken(), parent.Next().ChildBeginAtLevel(lvl).ToToken()
}

View file

@ -121,10 +121,30 @@ func TestIsZero(t *testing.T) {
}
func TestRange(t *testing.T) {
t.Run("valid", func(t *testing.T) {
t.Run("valid_1", func(t *testing.T) {
min, max := Range("4799e370ca54c8b9", 1)
assert.Equal(t, "4799e370ca54c8b4", min)
assert.Equal(t, "4799e370ca54c8c4", max)
assert.Equal(t, "4799e370ca54c8b1", min)
assert.Equal(t, "4799e370ca54c8c1", max)
})
t.Run("valid_2", func(t *testing.T) {
min, max := Range("4799e370ca54c8b9", 2)
assert.Equal(t, "4799e370ca54c881", min)
assert.Equal(t, "4799e370ca54c8c1", max)
})
t.Run("valid_3", func(t *testing.T) {
min, max := Range("4799e370ca54c8b9", 3)
assert.Equal(t, "4799e370ca54c801", min)
assert.Equal(t, "4799e370ca54c901", max)
})
t.Run("valid_4", func(t *testing.T) {
min, max := Range("4799e370ca54c8b9", 4)
assert.Equal(t, "4799e370ca54c601", min)
assert.Equal(t, "4799e370ca54ca01", max)
})
t.Run("valid_5", func(t *testing.T) {
min, max := Range("4799e370ca54c8b9", 5)
assert.Equal(t, "4799e370ca54c001", min)
assert.Equal(t, "4799e370ca54d001", max)
})
t.Run("invalid", func(t *testing.T) {
min, max := Range("4799e370ca5q", 1)