Esempio n. 1
0
  def testCallAndApply(self):
    """Verifies library initialization."""

    # Use a custom set of known functions.
    def MockSend(path, params, unused_method=None, unused_raw=None):
      if path == '/algorithms':
        return {
            'fakeFunction': {
                'type': 'Algorithm',
                'args': [
                    {'name': 'image1', 'type': 'Image'},
                    {'name': 'image2', 'type': 'Image'}
                ],
                'returns': 'Image'
            },
            'Image.constant': apitestcase.BUILTIN_FUNCTIONS['Image.constant']
        }
      else:
        raise Exception('Unexpected API call to %s with %s' % (path, params))
    ee.data.send_ = MockSend

    ee.Initialize()
    image1 = ee.Image(1)
    image2 = ee.Image(2)
    expected = ee.Image(ee.ComputedObject(
        ee.ApiFunction.lookup('fakeFunction'),
        {'image1': image1, 'image2': image2}))

    applied_with_images = ee.apply(
        'fakeFunction', {'image1': image1, 'image2': image2})
    self.assertEquals(expected, applied_with_images)

    applied_with_numbers = ee.apply('fakeFunction', {'image1': 1, 'image2': 2})
    self.assertEquals(expected, applied_with_numbers)

    called_with_numbers = ee.call('fakeFunction', 1, 2)
    self.assertEquals(expected, called_with_numbers)

    # Test call and apply() with a custom function.
    sig = {'returns': 'Image', 'args': [{'name': 'foo', 'type': 'Image'}]}
    func = ee.CustomFunction(sig, lambda foo: ee.call('fakeFunction', 42, foo))
    expected_custom_function_call = ee.Image(
        ee.ComputedObject(func, {'foo': ee.Image(13)}))
    self.assertEquals(expected_custom_function_call, ee.call(func, 13))
    self.assertEquals(expected_custom_function_call,
                      ee.apply(func, {'foo': 13}))

    # Test None promotion.
    called_with_null = ee.call('fakeFunction', None, 1)
    self.assertEquals(None, called_with_null.args['image1'])
Esempio n. 2
0
  def testCallAndApply(self):
    """Verifies library initialization."""

    # Use a custom set of known functions.
    def MockSend(path, params, unused_method=None, unused_raw=None):
      if path == '/algorithms':
        return {
            'fakeFunction': {
                'type': 'Algorithm',
                'args': [
                    {'name': 'image1', 'type': 'Image'},
                    {'name': 'image2', 'type': 'Image'}
                ],
                'returns': 'Image'
            },
            'Image.constant': apitestcase.BUILTIN_FUNCTIONS['Image.constant']
        }
      else:
        raise Exception('Unexpected API call to %s with %s' % (path, params))
    ee.data.send_ = MockSend

    ee.Initialize(None)
    image1 = ee.Image(1)
    image2 = ee.Image(2)
    expected = ee.Image(ee.ComputedObject(
        ee.ApiFunction.lookup('fakeFunction'),
        {'image1': image1, 'image2': image2}))

    applied_with_images = ee.apply(
        'fakeFunction', {'image1': image1, 'image2': image2})
    self.assertEquals(expected, applied_with_images)

    applied_with_numbers = ee.apply('fakeFunction', {'image1': 1, 'image2': 2})
    self.assertEquals(expected, applied_with_numbers)

    called_with_numbers = ee.call('fakeFunction', 1, 2)
    self.assertEquals(expected, called_with_numbers)

    # Test call and apply() with a custom function.
    sig = {'returns': 'Image', 'args': [{'name': 'foo', 'type': 'Image'}]}
    func = ee.CustomFunction(sig, lambda foo: ee.call('fakeFunction', 42, foo))
    expected_custom_function_call = ee.Image(
        ee.ComputedObject(func, {'foo': ee.Image(13)}))
    self.assertEquals(expected_custom_function_call, ee.call(func, 13))
    self.assertEquals(expected_custom_function_call,
                      ee.apply(func, {'foo': 13}))

    # Test None promotion.
    called_with_null = ee.call('fakeFunction', None, 1)
    self.assertEquals(None, called_with_null.args['image1'])
def earth_engine_classifier(domain, b, classifier_name, extra_args={}):
    '''Apply EE classifier tool using a ground truth image.'''
    
    # Training requires a training image plus either ground truth or training features.
    training_domain = None
    #if domain.training_domain:
    training_domain = domain.training_domain
    #elif domain.unflooded_domain:
    #training_domain = domain.unflooded_domain
    if not training_domain:
        raise Exception('Cannot run classifier algorithm without a training domain!')

    training_image  = _create_learning_image(training_domain, compute_modis_indices(training_domain))
    if training_domain.training_features:
        args = {
                'training_features' : training_domain.training_features,
                'training_property' : 'classification',
                #'crs'               : 'EPSG:32736',
                #'crs_transform'     : [0.8,0,733605.2,0,-0.8,8117589.2]
                "crs": "EPSG:4326", # TODO: What to use here???
                "crs_transform": [8.9831528411952135e-05, 0, -180, 0, -8.9831528411952135e-05, 90],
               }
    elif training_domain.ground_truth:
        args = {
                'training_image'    : training_domain.ground_truth,
                'training_band'     : "b1",
                'training_region'   : training_domain.bounds
               }
    else: # Use the permanent water mask
        args = {
                'training_image'    : get_permanent_water_mask(),
                'training_band'     : "b1",
                'training_region'   : training_domain.bounds
               }
    common_args = {
                   'image'             : training_image,
                   'subsampling'       : 0.2, # TODO: Reduce this on failure?
                   'max_classification': 2,
                   'classifier_mode' : 'classification',
                   'classifier_name'   : classifier_name
                  }
    args.update(common_args)
    args.update(extra_args)
    classifier = ee.apply("TrainClassifier", args)  # Call the EE classifier
    classified = _create_learning_image(domain, b).classify(classifier).select(['classification'], ['b1'])
    
    
    # For high resolution Skybox images, apply an additional filter step to clean up speckles.
    try:
        try: # The Skybox data can be in one of two names
            skyboxSensor = domain.skybox
        except:
            skyboxSensor = domain.skybox_nir
        classified = classified.focal_min(13, 'circle', 'meters').focal_max(13, 'circle', 'meters')
    except:
        pass
    
    return classified;
Esempio n. 4
0
def __learning_threshold(domain, algorithm):
    
    training_domain = None
    if domain.training_domain:
        training_domain = domain.training_domain
    elif domain.unflooded_domain:
        training_domain = domain.unflooded_domain
    if not training_domain:
        raise Exception('Cannot use learning algorithms without a training image defined by the domain!')
    classifier = ee.apply('TrainClassifier', {'image': training_domain.get_radar().image,
                            'subsampling'       : 0.07,
                            'training_image'    : training_domain.ground_truth,
                            'training_band'     : 'b1',
                            'training_region'   : training_domain.bounds,
                            'max_classification': 2,
                            'classifier_name'   : algorithm})
    classified = ee.call('ClassifyImage', domain.get_radar().image, classifier).select(['classification'], ['b1']);
    return classified;
def getTexture(img):
    square = ee.apply('Kernel.square', {'radius': 1.0, 'normalize': True})
    return img.unitScale(0, 1).multiply(255).toByte().glcmTexture(
        1, square, True).select(['VH_var', 'VH_ent', 'VH_contrast'])