Helper method that tries to infer the $geometry shape for a given value.
(value)
| 466 | |
| 467 | |
| 468 | def _infer_geometry(value): |
| 469 | """Helper method that tries to infer the $geometry shape for a |
| 470 | given value. |
| 471 | """ |
| 472 | if isinstance(value, dict): |
| 473 | if "$geometry" in value: |
| 474 | return value |
| 475 | elif "coordinates" in value and "type" in value: |
| 476 | return {"$geometry": value} |
| 477 | raise InvalidQueryError( |
| 478 | "Invalid $geometry dictionary should have type and coordinates keys" |
| 479 | ) |
| 480 | elif isinstance(value, (list, set)): |
| 481 | # TODO: shouldn't we test value[0][0][0][0] to see if it is MultiPolygon? |
| 482 | |
| 483 | try: |
| 484 | value[0][0][0] |
| 485 | return {"$geometry": {"type": "Polygon", "coordinates": value}} |
| 486 | except (TypeError, IndexError): |
| 487 | pass |
| 488 | |
| 489 | try: |
| 490 | value[0][0] |
| 491 | return {"$geometry": {"type": "LineString", "coordinates": value}} |
| 492 | except (TypeError, IndexError): |
| 493 | pass |
| 494 | |
| 495 | try: |
| 496 | value[0] |
| 497 | return {"$geometry": {"type": "Point", "coordinates": value}} |
| 498 | except (TypeError, IndexError): |
| 499 | pass |
| 500 | |
| 501 | raise InvalidQueryError( |
| 502 | "Invalid $geometry data. Can be either a " |
| 503 | "dictionary or (nested) lists of coordinate(s)" |
| 504 | ) |
| 505 | |
| 506 | |
| 507 | def _prepare_query_for_iterable(field, op, value): |
no test coverage detected