| 1 |
from abc import ABC |
| 2 |
from enum import Enum |
| 3 |
|
| 4 |
from beanie.odm.operators.find import BaseFindOperator |
| 5 |
|
| 6 |
|
| 7 |
class BaseFindGeospatialOperator(BaseFindOperator, ABC): ... |
| 8 |
|
| 9 |
|
| 10 |
class GeoIntersects(BaseFindGeospatialOperator): |
| 11 |
""" |
| 12 |
`$geoIntersects` query operator |
| 13 |
|
| 14 |
Example: |
| 15 |
|
| 16 |
```python |
| 17 |
class GeoObject(BaseModel): |
| 18 |
type: str = "Point" |
| 19 |
coordinates: Tuple[float, float] |
| 20 |
|
| 21 |
class Place(Document): |
| 22 |
geo: GeoObject |
| 23 |
|
| 24 |
class Collection: |
| 25 |
name = "places" |
| 26 |
indexes = [ |
| 27 |
[("geo", pymongo.GEOSPHERE)], # GEO index |
| 28 |
] |
| 29 |
|
| 30 |
GeoIntersects(Place.geo, "Polygon", [[0,0], [1,1], [3,3]]) |
| 31 |
``` |
| 32 |
|
| 33 |
Will return query object like |
| 34 |
|
| 35 |
```python |
| 36 |
{ |
| 37 |
"geo": { |
| 38 |
"$geoIntersects": { |
| 39 |
"$geometry": { |
| 40 |
"type": "Polygon", |
| 41 |
"coordinates": [[0,0], [1,1], [3,3]], |
| 42 |
} |
| 43 |
} |
| 44 |
} |
| 45 |
} |
| 46 |
``` |
| 47 |
|
| 48 |
MongoDB doc: |
| 49 |
<https://docs.mongodb.com/manual/reference/operator/query/geoIntersects/> |
| 50 |
""" |
| 51 |
|
| 52 |
def __init__(self, field, geo_type: str, coordinates: list[list[float]]): |
| 53 |
self.field = field |
| 54 |
self.geo_type = geo_type |
| 55 |
self.coordinates = coordinates |
| 56 |
|
| 57 |
@property |
| 58 |
def query(self): |
| 59 |
return { |
| 60 |
self.field: { |
| 61 |
"$geoIntersects": { |
| 62 |
"$geometry": { |
| 63 |
"type": self.geo_type, |
| 64 |
"coordinates": self.coordinates, |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
class GeoWithinTypes(str, Enum): |
| 72 |
Polygon = "Polygon" |
| 73 |
MultiPolygon = "MultiPolygon" |
| 74 |
|
| 75 |
|
| 76 |
class GeoWithin(BaseFindGeospatialOperator): |
| 77 |
""" |
| 78 |
`$geoWithin` query operator |
| 79 |
|
| 80 |
Example: |
| 81 |
|
| 82 |
```python |
| 83 |
class GeoObject(BaseModel): |
| 84 |
type: str = "Point" |
| 85 |
coordinates: Tuple[float, float] |
| 86 |
|
| 87 |
class Place(Document): |
| 88 |
geo: GeoObject |
| 89 |
|
| 90 |
class Collection: |
| 91 |
name = "places" |
| 92 |
indexes = [ |
| 93 |
[("geo", pymongo.GEOSPHERE)], # GEO index |
| 94 |
] |
| 95 |
|
| 96 |
GeoWithin(Place.geo, "Polygon", [[0,0], [1,1], [3,3]]) |
| 97 |
``` |
| 98 |
|
| 99 |
Will return query object like |
| 100 |
|
| 101 |
```python |
| 102 |
{ |
| 103 |
"geo": { |
| 104 |
"$geoWithin": { |
| 105 |
"$geometry": { |
| 106 |
"type": "Polygon", |
| 107 |
"coordinates": [[0,0], [1,1], [3,3]], |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
``` |
| 113 |
|
| 114 |
MongoDB doc: |
| 115 |
<https://docs.mongodb.com/manual/reference/operator/query/geoWithin/> |
| 116 |
""" |
| 117 |
|
| 118 |
def __init__( |
| 119 |
self, field, geo_type: GeoWithinTypes, coordinates: list[list[float]] |
| 120 |
): |
| 121 |
self.field = field |
| 122 |
self.geo_type = geo_type |
| 123 |
self.coordinates = coordinates |
| 124 |
|
| 125 |
@property |
| 126 |
def query(self): |
| 127 |
return { |
| 128 |
self.field: { |
| 129 |
"$geoWithin": { |
| 130 |
"$geometry": { |
| 131 |
"type": self.geo_type, |
| 132 |
"coordinates": self.coordinates, |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
class Box(BaseFindGeospatialOperator): |
| 140 |
""" |
| 141 |
`$box` query operator |
| 142 |
|
| 143 |
Example: |
| 144 |
|
| 145 |
```python |
| 146 |
class GeoObject(BaseModel): |
| 147 |
type: str = "Point" |
| 148 |
coordinates: Tuple[float, float] |
| 149 |
|
| 150 |
class Place(Document): |
| 151 |
geo: GeoObject |
| 152 |
|
| 153 |
class Collection: |
| 154 |
name = "places" |
| 155 |
indexes = [ |
| 156 |
[("geo", pymongo.GEOSPHERE)], # GEO index |
| 157 |
] |
| 158 |
|
| 159 |
Box(Place.geo, lower_left=[10,12], upper_right=[15,20]) |
| 160 |
``` |
| 161 |
|
| 162 |
Will return query object like |
| 163 |
|
| 164 |
```python |
| 165 |
{ |
| 166 |
"geo": { |
| 167 |
"$geoWithin": { |
| 168 |
"$box": [[10, 12], [15, 20]] |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
``` |
| 173 |
|
| 174 |
MongoDB doc: |
| 175 |
<https://docs.mongodb.com/manual/reference/operator/query/box/> |
| 176 |
""" |
| 177 |
|
| 178 |
def __init__( |
| 179 |
self, field, lower_left: list[float], upper_right: list[float] |
| 180 |
): |
| 181 |
self.field = field |
| 182 |
self.coordinates = [lower_left, upper_right] |
| 183 |
|
| 184 |
@property |
| 185 |
def query(self): |
| 186 |
return {self.field: {"$geoWithin": {"$box": self.coordinates}}} |
| 187 |
|
| 188 |
|
| 189 |
class Near(BaseFindGeospatialOperator): |
| 190 |
""" |
| 191 |
`$near` query operator |
| 192 |
|
| 193 |
Example: |
| 194 |
|
| 195 |
```python |
| 196 |
class GeoObject(BaseModel): |
| 197 |
type: str = "Point" |
| 198 |
coordinates: Tuple[float, float] |
| 199 |
|
| 200 |
class Place(Document): |
| 201 |
geo: GeoObject |
| 202 |
|
| 203 |
class Collection: |
| 204 |
name = "places" |
| 205 |
indexes = [ |
| 206 |
[("geo", pymongo.GEOSPHERE)], # GEO index |
| 207 |
] |
| 208 |
|
| 209 |
Near(Place.geo, 1.2345, 2.3456, min_distance=500) |
| 210 |
``` |
| 211 |
|
| 212 |
Will return query object like |
| 213 |
|
| 214 |
```python |
| 215 |
{ |
| 216 |
"geo": { |
| 217 |
"$near": { |
| 218 |
"$geometry": { |
| 219 |
"type": "Point", |
| 220 |
"coordinates": [1.2345, 2.3456], |
| 221 |
}, |
| 222 |
"$maxDistance": 500, |
| 223 |
} |
| 224 |
} |
| 225 |
} |
| 226 |
``` |
| 227 |
|
| 228 |
MongoDB doc: |
| 229 |
<https://docs.mongodb.com/manual/reference/operator/query/near/> |
| 230 |
""" |
| 231 |
|
| 232 |
operator = "$near" |
| 233 |
|
| 234 |
def __init__( |
| 235 |
self, |
| 236 |
field, |
| 237 |
longitude: float, |
| 238 |
latitude: float, |
| 239 |
max_distance: float | None = None, |
| 240 |
min_distance: float | None = None, |
| 241 |
): |
| 242 |
self.field = field |
| 243 |
self.longitude = longitude |
| 244 |
self.latitude = latitude |
| 245 |
self.max_distance = max_distance |
| 246 |
self.min_distance = min_distance |
| 247 |
|
| 248 |
@property |
| 249 |
def query(self): |
| 250 |
expression = { |
| 251 |
self.field: { |
| 252 |
self.operator: { |
| 253 |
"$geometry": { |
| 254 |
"type": "Point", |
| 255 |
"coordinates": [self.longitude, self.latitude], |
| 256 |
}, |
| 257 |
} |
| 258 |
} |
| 259 |
} |
| 260 |
if self.max_distance: |
| 261 |
expression[self.field][self.operator]["$maxDistance"] = ( |
| 262 |
self.max_distance |
| 263 |
) # type: ignore |
| 264 |
if self.min_distance: |
| 265 |
expression[self.field][self.operator]["$minDistance"] = ( |
| 266 |
self.min_distance |
| 267 |
) # type: ignore |
| 268 |
return expression |
| 269 |
|
| 270 |
|
| 271 |
class NearSphere(Near): |
| 272 |
""" |
| 273 |
`$nearSphere` query operator |
| 274 |
|
| 275 |
Example: |
| 276 |
|
| 277 |
```python |
| 278 |
class GeoObject(BaseModel): |
| 279 |
type: str = "Point" |
| 280 |
coordinates: Tuple[float, float] |
| 281 |
|
| 282 |
class Place(Document): |
| 283 |
geo: GeoObject |
| 284 |
|
| 285 |
class Collection: |
| 286 |
name = "places" |
| 287 |
indexes = [ |
| 288 |
[("geo", pymongo.GEOSPHERE)], # GEO index |
| 289 |
] |
| 290 |
|
| 291 |
NearSphere(Place.geo, 1.2345, 2.3456, min_distance=500) |
| 292 |
``` |
| 293 |
|
| 294 |
Will return query object like |
| 295 |
|
| 296 |
```python |
| 297 |
{ |
| 298 |
"geo": { |
| 299 |
"$nearSphere": { |
| 300 |
"$geometry": { |
| 301 |
"type": "Point", |
| 302 |
"coordinates": [1.2345, 2.3456], |
| 303 |
}, |
| 304 |
"$maxDistance": 500, |
| 305 |
} |
| 306 |
} |
| 307 |
} |
| 308 |
``` |
| 309 |
|
| 310 |
MongoDB doc: |
| 311 |
<https://docs.mongodb.com/manual/reference/operator/query/nearSphere/> |
| 312 |
""" |
| 313 |
|
| 314 |
operator = "$nearSphere" |