github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/function/spatial/st_perimeter.go (about) 1 // Copyright 2020-2022 Dolthub, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package spatial 16 17 import ( 18 "fmt" 19 "strings" 20 21 "github.com/dolthub/go-mysql-server/sql" 22 "github.com/dolthub/go-mysql-server/sql/expression" 23 "github.com/dolthub/go-mysql-server/sql/types" 24 ) 25 26 // Perimeter is a function that returns the Perimeter of a Polygon 27 // Not in MySQL, basing off: https://postgis.net/docs/ST_Perimeter.html 28 type Perimeter struct { 29 expression.NaryExpression 30 } 31 32 var _ sql.FunctionExpression = (*Perimeter)(nil) 33 var _ sql.CollationCoercible = (*Perimeter)(nil) 34 35 // NewSTLength creates a new STX expression. 36 func NewPerimeter(args ...sql.Expression) (sql.Expression, error) { 37 if len(args) != 1 && len(args) != 2 { 38 return nil, sql.ErrInvalidArgumentNumber.New("ST_PERIMETER", "1 or 2", len(args)) 39 } 40 return &Perimeter{expression.NaryExpression{ChildExpressions: args}}, nil 41 } 42 43 // FunctionName implements sql.FunctionExpression 44 func (p *Perimeter) FunctionName() string { 45 return "st_perimeter" 46 } 47 48 // Description implements sql.FunctionExpression 49 func (p *Perimeter) Description() string { 50 return "returns the perimeter of the given polygon. If given a second argument, will find perimeter projected on spheroid." 51 } 52 53 // Type implements the sql.Expression interface. 54 func (p *Perimeter) Type() sql.Type { 55 return types.Float64 56 } 57 58 // CollationCoercibility implements the interface sql.CollationCoercible. 59 func (*Perimeter) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) { 60 return sql.Collation_binary, 5 61 } 62 63 func (p *Perimeter) String() string { 64 var args = make([]string, len(p.ChildExpressions)) 65 for i, arg := range p.ChildExpressions { 66 args[i] = arg.String() 67 } 68 return fmt.Sprintf("%s(%s)", p.FunctionName(), strings.Join(args, ",")) 69 } 70 71 // WithChildren implements the Expression interface. 72 func (p *Perimeter) WithChildren(children ...sql.Expression) (sql.Expression, error) { 73 return NewPerimeter(children...) 74 } 75 76 // Eval implements the sql.Expression interface. 77 func (p *Perimeter) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) { 78 // Evaluate argument 79 v1, err := p.ChildExpressions[0].Eval(ctx, row) 80 if err != nil { 81 return nil, err 82 } 83 84 // Return nil if argument is nil 85 if v1 == nil { 86 return nil, nil 87 } 88 89 // Argument must be a polygon 90 poly, ok := v1.(types.Polygon) 91 if !ok { 92 return nil, sql.ErrInvalidArgument.New(p.FunctionName()) 93 } 94 95 // TODO: if SRID is not 0, find geodetic distance 96 // If just one argument, return length 97 if len(p.ChildExpressions) == 1 { 98 var perimeter float64 99 for _, l := range poly.Lines { 100 perimeter += calculateLength(l) 101 } 102 return perimeter, nil 103 } 104 105 // TODO: support perimeter along spheroid 106 return nil, sql.ErrUnsupportedFeature.New("st_perimeter on spheroid") 107 }