gopkg.in/rethinkdb/rethinkdb-go.v6@v6.2.2/query_geospatial.go (about) 1 package rethinkdb 2 3 import ( 4 p "gopkg.in/rethinkdb/rethinkdb-go.v6/ql2" 5 ) 6 7 // CircleOpts contains the optional arguments for the Circle term. 8 type CircleOpts struct { 9 NumVertices interface{} `rethinkdb:"num_vertices,omitempty"` 10 GeoSystem interface{} `rethinkdb:"geo_system,omitempty"` 11 Unit interface{} `rethinkdb:"unit,omitempty"` 12 Fill interface{} `rethinkdb:"fill,omitempty"` 13 } 14 15 func (o CircleOpts) toMap() map[string]interface{} { 16 return optArgsToMap(o) 17 } 18 19 // Circle constructs a circular line or polygon. A circle in RethinkDB is 20 // a polygon or line approximating a circle of a given radius around a given 21 // center, consisting of a specified number of vertices (default 32). 22 func Circle(point, radius interface{}, optArgs ...CircleOpts) Term { 23 opts := map[string]interface{}{} 24 if len(optArgs) >= 1 { 25 opts = optArgs[0].toMap() 26 } 27 28 return constructRootTerm("Circle", p.Term_CIRCLE, []interface{}{point, radius}, opts) 29 } 30 31 // DistanceOpts contains the optional arguments for the Distance term. 32 type DistanceOpts struct { 33 GeoSystem interface{} `rethinkdb:"geo_system,omitempty"` 34 Unit interface{} `rethinkdb:"unit,omitempty"` 35 } 36 37 func (o DistanceOpts) toMap() map[string]interface{} { 38 return optArgsToMap(o) 39 } 40 41 // Distance calculates the Haversine distance between two points. At least one 42 // of the geometry objects specified must be a point. 43 func (t Term) Distance(point interface{}, optArgs ...DistanceOpts) Term { 44 opts := map[string]interface{}{} 45 if len(optArgs) >= 1 { 46 opts = optArgs[0].toMap() 47 } 48 49 return constructMethodTerm(t, "Distance", p.Term_DISTANCE, []interface{}{point}, opts) 50 } 51 52 // Distance calculates the Haversine distance between two points. At least one 53 // of the geometry objects specified must be a point. 54 func Distance(point1, point2 interface{}, optArgs ...DistanceOpts) Term { 55 opts := map[string]interface{}{} 56 if len(optArgs) >= 1 { 57 opts = optArgs[0].toMap() 58 } 59 60 return constructRootTerm("Distance", p.Term_DISTANCE, []interface{}{point1, point2}, opts) 61 } 62 63 // Fill converts a Line object into a Polygon object. If the last point does not 64 // specify the same coordinates as the first point, polygon will close the 65 // polygon by connecting them 66 func (t Term) Fill() Term { 67 return constructMethodTerm(t, "Fill", p.Term_FILL, []interface{}{}, map[string]interface{}{}) 68 } 69 70 // GeoJSON converts a GeoJSON object to a ReQL geometry object. 71 func GeoJSON(args ...interface{}) Term { 72 return constructRootTerm("GeoJSON", p.Term_GEOJSON, args, map[string]interface{}{}) 73 } 74 75 // ToGeoJSON converts a ReQL geometry object to a GeoJSON object. 76 func (t Term) ToGeoJSON(args ...interface{}) Term { 77 return constructMethodTerm(t, "ToGeoJSON", p.Term_TO_GEOJSON, args, map[string]interface{}{}) 78 } 79 80 // GetIntersectingOpts contains the optional arguments for the GetIntersecting term. 81 type GetIntersectingOpts struct { 82 Index interface{} `rethinkdb:"index,omitempty"` 83 } 84 85 func (o GetIntersectingOpts) toMap() map[string]interface{} { 86 return optArgsToMap(o) 87 } 88 89 // GetIntersecting gets all documents where the given geometry object intersects 90 // the geometry object of the requested geospatial index. 91 func (t Term) GetIntersecting(args interface{}, optArgs ...GetIntersectingOpts) Term { 92 opts := map[string]interface{}{} 93 if len(optArgs) >= 1 { 94 opts = optArgs[0].toMap() 95 } 96 97 return constructMethodTerm(t, "GetIntersecting", p.Term_GET_INTERSECTING, []interface{}{args}, opts) 98 } 99 100 // GetNearestOpts contains the optional arguments for the GetNearest term. 101 type GetNearestOpts struct { 102 Index interface{} `rethinkdb:"index,omitempty"` 103 MaxResults interface{} `rethinkdb:"max_results,omitempty"` 104 MaxDist interface{} `rethinkdb:"max_dist,omitempty"` 105 Unit interface{} `rethinkdb:"unit,omitempty"` 106 GeoSystem interface{} `rethinkdb:"geo_system,omitempty"` 107 } 108 109 func (o GetNearestOpts) toMap() map[string]interface{} { 110 return optArgsToMap(o) 111 } 112 113 // GetNearest gets all documents where the specified geospatial index is within a 114 // certain distance of the specified point (default 100 kilometers). 115 func (t Term) GetNearest(point interface{}, optArgs ...GetNearestOpts) Term { 116 opts := map[string]interface{}{} 117 if len(optArgs) >= 1 { 118 opts = optArgs[0].toMap() 119 } 120 121 return constructMethodTerm(t, "GetNearest", p.Term_GET_NEAREST, []interface{}{point}, opts) 122 } 123 124 // Includes tests whether a geometry object is completely contained within another. 125 // When applied to a sequence of geometry objects, includes acts as a filter, 126 // returning a sequence of objects from the sequence that include the argument. 127 func (t Term) Includes(args ...interface{}) Term { 128 return constructMethodTerm(t, "Includes", p.Term_INCLUDES, args, map[string]interface{}{}) 129 } 130 131 // Intersects tests whether two geometry objects intersect with one another. 132 // When applied to a sequence of geometry objects, intersects acts as a filter, 133 // returning a sequence of objects from the sequence that intersect with the 134 // argument. 135 func (t Term) Intersects(args ...interface{}) Term { 136 return constructMethodTerm(t, "Intersects", p.Term_INTERSECTS, args, map[string]interface{}{}) 137 } 138 139 // Line constructs a geometry object of type Line. The line can be specified in 140 // one of two ways: 141 // - Two or more two-item arrays, specifying longitude and latitude numbers of 142 // the line's vertices; 143 // - Two or more Point objects specifying the line's vertices. 144 func Line(args ...interface{}) Term { 145 return constructRootTerm("Line", p.Term_LINE, args, map[string]interface{}{}) 146 } 147 148 // Point constructs a geometry object of type Point. The point is specified by 149 // two floating point numbers, the longitude (−180 to 180) and latitude 150 // (−90 to 90) of the point on a perfect sphere. 151 func Point(lon, lat interface{}) Term { 152 return constructRootTerm("Point", p.Term_POINT, []interface{}{lon, lat}, map[string]interface{}{}) 153 } 154 155 // Polygon constructs a geometry object of type Polygon. The Polygon can be 156 // specified in one of two ways: 157 // - Three or more two-item arrays, specifying longitude and latitude numbers of the polygon's vertices; 158 // - Three or more Point objects specifying the polygon's vertices. 159 func Polygon(args ...interface{}) Term { 160 return constructRootTerm("Polygon", p.Term_POLYGON, args, map[string]interface{}{}) 161 } 162 163 // PolygonSub "punches a hole" out of the parent polygon using the polygon passed 164 // to the function. 165 // polygon1.PolygonSub(polygon2) -> polygon 166 // In the example above polygon2 must be completely contained within polygon1 167 // and must have no holes itself (it must not be the output of polygon_sub itself). 168 func (t Term) PolygonSub(args ...interface{}) Term { 169 return constructMethodTerm(t, "PolygonSub", p.Term_POLYGON_SUB, args, map[string]interface{}{}) 170 }