github.com/RevenueMonster/sqlike@v1.0.6/sql/expr/spatial.go (about)

     1  package expr
     2  
     3  import (
     4  	"github.com/RevenueMonster/sqlike/spatial"
     5  	"github.com/RevenueMonster/sqlike/sqlike/primitive"
     6  	"github.com/paulmach/orb"
     7  	"github.com/paulmach/orb/encoding/wkt"
     8  )
     9  
    10  //golint:ignore
    11  // ST_GeomFromText :
    12  func ST_GeomFromText(g interface{}, srid ...uint) (f spatial.Func) {
    13  	f.Type = spatial.SpatialTypeGeomFromText
    14  	switch vi := g.(type) {
    15  	case string:
    16  		f.Args = append(f.Args, primitive.Column{
    17  			Name: vi,
    18  		})
    19  	case orb.Geometry:
    20  		f.Args = append(f.Args, primitive.Value{
    21  			Raw: wkt.MarshalString(vi),
    22  		})
    23  	case primitive.Column:
    24  		f.Args = append(f.Args, vi)
    25  	default:
    26  		panic("unsupported data type for ST_GeomFromText")
    27  	}
    28  	if len(srid) > 0 {
    29  		f.Args = append(f.Args, primitive.Value{
    30  			Raw: srid[0],
    31  		})
    32  	}
    33  	return
    34  }
    35  
    36  //golint:ignore
    37  // ST_AsText :
    38  func ST_AsText(g interface{}) (f spatial.Func) {
    39  	f.Type = spatial.SpatialTypeAsText
    40  	switch vi := g.(type) {
    41  	case string:
    42  		f.Args = append(f.Args, primitive.Column{
    43  			Name: vi,
    44  		})
    45  	case orb.Geometry:
    46  		f.Args = append(f.Args, primitive.Value{
    47  			Raw: wkt.MarshalString(vi),
    48  		})
    49  	case primitive.Column:
    50  		f.Args = append(f.Args, vi)
    51  	default:
    52  		panic("unsupported data type for ST_AsText")
    53  	}
    54  	return
    55  }
    56  
    57  //golint:ignore
    58  // ST_IsValid :
    59  func ST_IsValid(g interface{}) (f spatial.Func) {
    60  	f.Type = spatial.SpatialTypeIsValid
    61  	switch vi := g.(type) {
    62  	case string:
    63  		f.Args = append(f.Args, primitive.Column{
    64  			Name: vi,
    65  		})
    66  	case orb.Geometry:
    67  		f.Args = append(f.Args, primitive.Value{
    68  			Raw: wkt.MarshalString(vi),
    69  		})
    70  	case primitive.Column:
    71  		f.Args = append(f.Args, vi)
    72  	default:
    73  		panic("unsupported data type for ST_IsValid")
    74  	}
    75  	return
    76  }
    77  
    78  //golint:ignore
    79  // column, value, ST_GeomFromText(column), ST_GeomFromText(value)
    80  // ST_Distance :
    81  func ST_Distance(g1, g2 interface{}, unit ...string) (f spatial.Func) {
    82  	f.Type = spatial.SpatialTypeDistance
    83  	for _, arg := range []interface{}{g1, g2} {
    84  		switch vi := arg.(type) {
    85  		case string:
    86  		case orb.Geometry:
    87  			f.Args = append(f.Args, primitive.Value{
    88  				Raw: vi,
    89  			})
    90  		case spatial.Func:
    91  			f.Args = append(f.Args, vi)
    92  		case primitive.Column:
    93  			f.Args = append(f.Args, vi)
    94  		default:
    95  			panic("unsupported data type for ST_Distance")
    96  		}
    97  	}
    98  	return
    99  }
   100  
   101  //golint:ignore
   102  // ST_Equals :
   103  func ST_Equals(g1, g2 interface{}) (f spatial.Func) {
   104  	f.Type = spatial.SpatialTypeEquals
   105  	for _, arg := range []interface{}{g1, g2} {
   106  		switch vi := arg.(type) {
   107  		case string:
   108  		case orb.Geometry:
   109  			f.Args = append(f.Args, primitive.Value{
   110  				Raw: vi,
   111  			})
   112  		case spatial.Func:
   113  			f.Args = append(f.Args, vi)
   114  		case primitive.Column:
   115  			f.Args = append(f.Args, vi)
   116  		default:
   117  			panic("unsupported data type for ST_Equals")
   118  		}
   119  	}
   120  	return
   121  }
   122  
   123  //golint:ignore
   124  // ST_Intersects :
   125  func ST_Intersects(g1, g2 interface{}) (f spatial.Func) {
   126  	f.Type = spatial.SpatialTypeIntersects
   127  	for _, arg := range []interface{}{g1, g2} {
   128  		switch vi := arg.(type) {
   129  		case string:
   130  		case orb.Geometry:
   131  			f.Args = append(f.Args, primitive.Value{
   132  				Raw: vi,
   133  			})
   134  		case spatial.Func:
   135  			f.Args = append(f.Args, vi)
   136  		case primitive.Column:
   137  			f.Args = append(f.Args, vi)
   138  		default:
   139  			panic("unsupported data type for ST_Intersects")
   140  		}
   141  	}
   142  	return
   143  }
   144  
   145  //golint:ignore
   146  // ST_Within :
   147  func ST_Within(g1, g2 interface{}) (f spatial.Func) {
   148  	f.Type = spatial.SpatialTypeWithin
   149  	for _, arg := range []interface{}{g1, g2} {
   150  		switch vi := arg.(type) {
   151  		case string:
   152  		case orb.Geometry:
   153  			f.Args = append(f.Args, primitive.Value{
   154  				Raw: vi,
   155  			})
   156  		case spatial.Func:
   157  			f.Args = append(f.Args, vi)
   158  		case primitive.Column:
   159  			f.Args = append(f.Args, vi)
   160  		default:
   161  			panic("unsupported data type for ST_Within")
   162  		}
   163  	}
   164  	return
   165  }