MCPcopy Index your code
hub / github.com/MongoEngine/mongoengine / _infer_geometry

Function _infer_geometry

mongoengine/queryset/transform.py:468–504  ·  view source on GitHub ↗

Helper method that tries to infer the $geometry shape for a given value.

(value)

Source from the content-addressed store, hash-verified

466
467
468def _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
507def _prepare_query_for_iterable(field, op, value):

Callers 1

_geo_operatorFunction · 0.85

Calls 1

InvalidQueryErrorClass · 0.90

Tested by

no test coverage detected