element.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/usr/bin/env python
  2. """Base class for Image, Feature and Collection.
  3. This class is never intended to be instantiated by the user.
  4. """
  5. # Using lowercase function naming to match the JavaScript names.
  6. # pylint: disable=g-bad-name
  7. from . import apifunction
  8. from . import computedobject
  9. from . import ee_exception
  10. class Element(computedobject.ComputedObject):
  11. """Base class for ImageCollection and FeatureCollection."""
  12. _initialized = False
  13. def __init__(self, func, args, opt_varName=None):
  14. """Constructs a collection by initializing its ComputedObject."""
  15. super(Element, self).__init__(func, args, opt_varName)
  16. @classmethod
  17. def initialize(cls):
  18. """Imports API functions to this class."""
  19. if not cls._initialized:
  20. apifunction.ApiFunction.importApi(cls, 'Element', 'Element')
  21. cls._initialized = True
  22. @classmethod
  23. def reset(cls):
  24. """Removes imported API functions from this class."""
  25. apifunction.ApiFunction.clearApi(cls)
  26. cls._initialized = False
  27. @staticmethod
  28. def name():
  29. return 'Element'
  30. def set(self, *args):
  31. """Overrides one or more metadata properties of an Element.
  32. Args:
  33. *args: Either a dictionary of properties, or a vararg sequence of
  34. properties, e.g. key1, value1, key2, value2, ...
  35. Returns:
  36. The element with the specified properties overridden.
  37. """
  38. if len(args) == 1:
  39. properties = args[0]
  40. # If this is a keyword call, unwrap it.
  41. if (isinstance(properties, dict) and
  42. (len(properties) == 1 and 'properties' in properties) and
  43. isinstance(properties['properties'],
  44. (dict, computedobject.ComputedObject))):
  45. # Looks like a call with keyword parameters. Extract them.
  46. properties = properties['properties']
  47. if isinstance(properties, dict):
  48. # Still a plain object. Extract its keys. Setting the keys separately
  49. # allows filter propagation.
  50. result = self
  51. for key, value in properties.items():
  52. result = apifunction.ApiFunction.call_(
  53. 'Element.set', result, key, value)
  54. elif (isinstance(properties, computedobject.ComputedObject) and
  55. apifunction.ApiFunction.lookupInternal('Element.setMulti')):
  56. # A computed dictionary. Can't set each key separately.
  57. result = apifunction.ApiFunction.call_(
  58. 'Element.setMulti', self, properties)
  59. else:
  60. raise ee_exception.EEException(
  61. 'When Element.set() is passed one argument, '
  62. 'it must be a dictionary.')
  63. else:
  64. # Interpret as key1, value1, key2, value2, ...
  65. if len(args) % 2 != 0:
  66. raise ee_exception.EEException(
  67. 'When Element.set() is passed multiple arguments, there '
  68. 'must be an even number of them.')
  69. result = self
  70. for i in range(0, len(args), 2):
  71. key = args[i]
  72. value = args[i + 1]
  73. result = apifunction.ApiFunction.call_(
  74. 'Element.set', result, key, value)
  75. # Manually cast the result to an image.
  76. return self._cast(result)