diff --git a/internal/server/search.go b/internal/server/search.go index c37cc975..a62f89ca 100644 --- a/internal/server/search.go +++ b/internal/server/search.go @@ -216,67 +216,6 @@ func (server *Server) cmdSearchArgs( switch ltyp { case "point": fallthrough - case "sector": - if s.clip { - err = errInvalidArgument("cannot clip with " + ltyp) - return - } - var slat, slon, smeters, sb1, sb2 string - if vs, slat, ok = tokenval(vs); !ok || slat == "" { - err = errInvalidNumberOfArguments - return - } - if vs, slon, ok = tokenval(vs); !ok || slon == "" { - err = errInvalidNumberOfArguments - return - } - if vs, smeters, ok = tokenval(vs); !ok || smeters == "" { - err = errInvalidNumberOfArguments - return - } - if vs, sb1, ok = tokenval(vs); !ok || sb1 == "" { - err = errInvalidNumberOfArguments - return - } - if vs, sb2, ok = tokenval(vs); !ok || sb2 == "" { - err = errInvalidNumberOfArguments - return - } - var lat, lon, meters, b1, b2 float64 - if lat, err = strconv.ParseFloat(slat, 64); err != nil { - err = errInvalidArgument(slat) - return - } - if lon, err = strconv.ParseFloat(slon, 64); err != nil { - err = errInvalidArgument(slon) - return - } - if meters, err = strconv.ParseFloat(smeters, 64); err != nil { - err = errInvalidArgument(smeters) - return - } - if b1, err = strconv.ParseFloat(sb1, 64); err != nil { - err = errInvalidArgument(sb1) - return - } - if b2, err = strconv.ParseFloat(sb2, 64); err != nil { - err = errInvalidArgument(sb2) - return - } - - if b1 == b2 { - err = fmt.Errorf("equal bearings (%s == %s), use CIRCLE instead", sb1, sb2) - return - } - - origin := sectr.Point{Lng: lon, Lat: lat} - sector := sectr.NewSector(origin, meters, b1, b2) - - s.obj, err = geojson.Parse(string(sector.JSON()), &server.geomParseOpts) - if err != nil { - return - } - case "circle": if s.clip { err = errInvalidArgument("cannot clip with " + ltyp) @@ -343,6 +282,66 @@ func (server *Server) cmdSearchArgs( if err != nil { return } + case "sector": + if s.clip { + err = errInvalidArgument("cannot clip with " + ltyp) + return + } + var slat, slon, smeters, sb1, sb2 string + if vs, slat, ok = tokenval(vs); !ok || slat == "" { + err = errInvalidNumberOfArguments + return + } + if vs, slon, ok = tokenval(vs); !ok || slon == "" { + err = errInvalidNumberOfArguments + return + } + if vs, smeters, ok = tokenval(vs); !ok || smeters == "" { + err = errInvalidNumberOfArguments + return + } + if vs, sb1, ok = tokenval(vs); !ok || sb1 == "" { + err = errInvalidNumberOfArguments + return + } + if vs, sb2, ok = tokenval(vs); !ok || sb2 == "" { + err = errInvalidNumberOfArguments + return + } + var lat, lon, meters, b1, b2 float64 + if lat, err = strconv.ParseFloat(slat, 64); err != nil { + err = errInvalidArgument(slat) + return + } + if lon, err = strconv.ParseFloat(slon, 64); err != nil { + err = errInvalidArgument(slon) + return + } + if meters, err = strconv.ParseFloat(smeters, 64); err != nil { + err = errInvalidArgument(smeters) + return + } + if b1, err = strconv.ParseFloat(sb1, 64); err != nil { + err = errInvalidArgument(sb1) + return + } + if b2, err = strconv.ParseFloat(sb2, 64); err != nil { + err = errInvalidArgument(sb2) + return + } + + if b1 == b2 { + err = fmt.Errorf("equal bearings (%s == %s), use CIRCLE instead", sb1, sb2) + return + } + + origin := sectr.Point{Lng: lon, Lat: lat} + sector := sectr.NewSector(origin, meters, b1, b2) + + s.obj, err = geojson.Parse(string(sector.JSON()), &server.geomParseOpts) + if err != nil { + return + } case "bounds", "hash", "tile", "quadkey": vs, s.obj, err = parseRectArea(ltyp, vs) if err != nil { @@ -442,8 +441,7 @@ func (server *Server) cmdSearchArgs( var nearbyTypes = []string{"point"} var withinOrIntersectsTypes = []string{ - "geo", "bounds", "hash", "tile", "quadkey", "get", "object", "circle", "sector", -} + "geo", "bounds", "hash", "tile", "quadkey", "get", "object", "circle", "sector"} func (server *Server) cmdNearby(msg *Message) (res resp.Value, err error) { start := time.Now() diff --git a/tests/keys_search_test.go b/tests/keys_search_test.go index 7853bc9c..f1c680aa 100644 --- a/tests/keys_search_test.go +++ b/tests/keys_search_test.go @@ -131,6 +131,7 @@ func keys_WITHIN_test(mc *mockServer) error { {"SET", "mykey", "point6", "POINT", -5, 5}, {"OK"}, {"SET", "mykey", "point7", "POINT", 33, 21}, {"OK"}, {"SET", "mykey", "poly8", "OBJECT", `{"type":"Polygon","coordinates":[[[-122.4408378,37.7341129],[-122.4408378,37.733],[-122.44,37.733],[-122.44,37.7341129],[-122.4408378,37.7341129]],[[-122.44060993194579,37.73345766902749],[-122.44044363498686,37.73345766902749],[-122.44044363498686,37.73355524732416],[-122.44060993194579,37.73355524732416],[-122.44060993194579,37.73345766902749]],[[-122.44060724973677,37.7336888869566],[-122.4402102828026,37.7336888869566],[-122.4402102828026,37.7339752567853],[-122.44060724973677,37.7339752567853],[-122.44060724973677,37.7336888869566]]]}`}, {"OK"}, + {"WITHIN", "mykey", "IDS", "SECTOR", "37.72999", "-122.44760", "1000", "0", "90"}, {"[0 [point1 point2 line3 poly4 multipoly5 poly8]]"}, {"WITHIN", "mykey", "IDS", "OBJECT", `{ "type": "Polygon",