Python ee.Dictionary() Examples
The following are 30
code examples of ee.Dictionary().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
ee
, or try the search function
.
Example #1
Source File: dictionary_test.py From aqua-monitor with GNU Lesser General Public License v3.0 | 6 votes |
def testDictionary(self): """Verifies basic behavior of ee.Dictionary.""" src = {'a': 1, 'b': 2, 'c': 'three'} dictionary = ee.Dictionary(src) self.assertEquals({'type': 'Dictionary', 'value': src}, ee.Serializer(False)._encode(dictionary)) f = ee.Feature(None, {'properties': src}) computed = ee.Dictionary(f.get('properties')) self.assertTrue(isinstance(computed, ee.Dictionary)) # The 4 types of arguments we expect cons = (ee.Dictionary(src), ee.Dictionary(f.get('properties')), ee.Dictionary(), ee.Dictionary(('one', 1))) for d in cons: self.assertTrue(isinstance(d, ee.ComputedObject))
Example #2
Source File: app.py From earthengine with MIT License | 6 votes |
def iterate(image1,image2,niter,first): # simulated iteration of MAD for debugging # result = iterate(image1,image2,niter,first) for i in range(1,niter+1): result = ee.Dictionary(imad(i,first)) allrhos = ee.List(result.get('allrhos')) chi2 = ee.Image(result.get('chi2')) MAD = ee.Image(result.get('MAD')) first = ee.Dictionary({'image':image1.addBands(image2), 'allrhos':allrhos, 'chi2':chi2, 'MAD':MAD}) return result #------------------ # helper functions #------------------
Example #3
Source File: ee_list.py From gee_tools with MIT License | 6 votes |
def getFromDict(eelist, values): """ Get a list of Dict's values from a list object. Keys must be unique :param values: dict to get the values for list's keys :type values: ee.Dictionary :return: a list of values :rtype: ee.List """ values = ee.Dictionary(values) if isinstance(values, dict) else values empty = ee.List([]) def wrap(el, first): f = ee.List(first) cond = values.contains(el) return ee.Algorithms.If(cond, f.add(values.get(el)), f) values = ee.List(eelist.iterate(wrap, empty)) return values
Example #4
Source File: dictionary_test.py From earthengine with MIT License | 6 votes |
def testDictionary(self): """Verifies basic behavior of ee.Dictionary.""" src = {'a': 1, 'b': 2, 'c': 'three'} dictionary = ee.Dictionary(src) self.assertEquals({'type': 'Dictionary', 'value': src}, ee.Serializer(False)._encode(dictionary)) f = ee.Feature(None, {'properties': src}) computed = ee.Dictionary(f.get('properties')) self.assertTrue(isinstance(computed, ee.Dictionary)) # The 4 types of arguments we expect cons = (ee.Dictionary(src), ee.Dictionary(f.get('properties')), ee.Dictionary(), ee.Dictionary(('one', 1))) for d in cons: self.assertTrue(isinstance(d, ee.ComputedObject))
Example #5
Source File: eeWishart.py From earthengine with MIT License | 6 votes |
def filter_j(current,prev): P = ee.Image(current) prev = ee.Dictionary(prev) ell = ee.Number(prev.get('ell')) cmap = ee.Image(prev.get('cmap')) smap = ee.Image(prev.get('smap')) fmap = ee.Image(prev.get('fmap')) bmap = ee.Image(prev.get('bmap')) threshold = ee.Image(prev.get('threshold')) j = ee.Number(prev.get('j')) cmapj = cmap.multiply(0).add(ell.add(j).subtract(1)) cmap1 = cmap.multiply(0).add(1) tst = P.gt(threshold).And(cmap.eq(ell.subtract(1))) cmap = cmap.where(tst,cmapj) fmap = fmap.where(tst,fmap.add(1)) smap = ee.Algorithms.If(ell.eq(1),smap.where(tst,cmapj),smap) idx = ell.add(j).subtract(2) tmp = bmap.select(idx) bname = bmap.bandNames().get(idx) tmp = tmp.where(tst,cmap1) tmp = tmp.rename([bname]) bmap = bmap.addBands(tmp,[bname],True) return ee.Dictionary({'ell':ell,'j':j.add(1),'threshold':threshold,'cmap':cmap,'smap':smap,'fmap':fmap,'bmap':bmap})
Example #6
Source File: image.py From gee_tools with MIT License | 6 votes |
def _add_suffix_prefix(image, value, option, bands=None): """ Internal function to handle addPrefix and addSuffix """ if bands: bands = ee.List(bands) addon = ee.String(value) allbands = image.bandNames() bands_ = ee.List(ee.Algorithms.If(bands, bands, allbands)) def over_bands(band, first): all = ee.List(first) options = ee.Dictionary({ 'suffix': ee.String(band).cat(addon), 'prefix': addon.cat(ee.String(band)) }) return all.replace(band, ee.String(options.get(option))) newbands = bands_.iterate(over_bands, allbands) newbands = ee.List(newbands) return image.select(allbands, newbands)
Example #7
Source File: eeWishart.py From earthengine with MIT License | 6 votes |
def omnibus(imList,significance=0.0001,median=False): '''return change maps for sequential omnibus change algorithm''' imList = ee.List(imList).map(multbyenl) p = ee.Image(imList.get(0)).bandNames().length() k = imList.length() # pre-calculate p-value array ells = ee.List.sequence(1,k.subtract(1)) first = ee.Dictionary({'k':k,'p':p,'median':median,'imList':imList,'pv_arr':ee.List([])}) pv_arr = ee.List(ee.Dictionary(ells.iterate(ells_iter,first)).get('pv_arr')) # filter p-values to generate cmap, smap, fmap and bmap cmap = ee.Image(imList.get(0)).select(0).multiply(0.0) smap = ee.Image(imList.get(0)).select(0).multiply(0.0) fmap = ee.Image(imList.get(0)).select(0).multiply(0.0) bmap = ee.Image.constant(ee.List.repeat(0,k.subtract(1))) threshold = ee.Image.constant(1-significance) first = ee.Dictionary({'ell':1,'threshold':threshold,'cmap':cmap,'smap':smap,'fmap':fmap,'bmap':bmap}) return ee.Dictionary(pv_arr.iterate(filter_ell,first))
Example #8
Source File: image.py From gee_tools with MIT License | 5 votes |
def makeName(img, pattern, date_pattern=None, extra=None): """ Make a name with the given pattern. The pattern must contain the propeties to replace between curly braces. There are 2 special words: * 'system_date': replace with the date of the image formatted with `date_pattern`, which defaults to 'yyyyMMdd' * 'id' or 'ID': the image id. If None, it'll be replaced with 'id' Pattern example (supposing each image has a property called `city`): 'image from {city} on {system_date}' You can add extra parameters using keyword `extra` """ img = ee.Image(img) props = img.toDictionary() props = ee.Dictionary(ee.Algorithms.If( img.id(), props.set('id', img.id()).set('ID', img.id()), props)) props = ee.Dictionary(ee.Algorithms.If( img.propertyNames().contains('system:time_start'), props.set('system_date', img.date().format(date_pattern)), props)) if extra: extra = ee.Dictionary(extra) props = props.combine(extra) name = string.format(pattern, props) return name
Example #9
Source File: element_test.py From aqua-monitor with GNU Lesser General Public License v3.0 | 5 votes |
def testSet(self): """Verifies Element.set() keyword argument interpretation.""" image = ee.Image(1) # Constant dictionary. def AssertProperties(expected, image): properties = {} while image.func == ee.ApiFunction.lookup('Element.set'): key = image.args['key'] if not isinstance(key, six.string_types): key = key.encode() properties[key] = image.args['value'] image = image.args['object'] self.assertEquals(ee.Image(1), image) self.assertEquals(expected, properties) AssertProperties({'foo': 'bar'}, image.set({'foo': 'bar'})) AssertProperties({'foo': 'bar'}, image.set({'properties': {'foo': 'bar'}})) AssertProperties({'properties': 5}, image.set({'properties': 5})) AssertProperties({'properties': {'foo': 'bar'}, 'baz': 'quux'}, image.set({'properties': {'foo': 'bar'}, 'baz': 'quux'})) AssertProperties({'foo': 'bar', 'baz': 'quux'}, image.set('foo', 'bar', 'baz', 'quux')) # Computed dictionary. computed_arg = ee.ComputedObject(None, None, 'foo') def CheckMultiProperties(result): self.assertEquals(ee.ApiFunction.lookup('Element.setMulti'), result.func) self.assertEquals( {'object': image, 'properties': ee.Dictionary(computed_arg)}, result.args) CheckMultiProperties(image.set(computed_arg)) CheckMultiProperties(image.set({'properties': computed_arg}))
Example #10
Source File: utils.py From gee_tools with MIT License | 5 votes |
def reduceRegionsPandas(data, index='system:index', add_coordinates=False, duplicate_index=False): """ Transform data coming from Image.reduceRegions to a pandas dataframe :param data: data coming from Image.reduceRegions :type data: ee.Dictionary or dict :param index: the index of the dataframe :param add_coordinates: if True adds the coordinates to the dataframe :param duplicate_index: if True adds the index data to the dataframe too :return: a pandas dataframe :rtype: pd.DataFrame """ if not isinstance(data, dict): if add_coordinates: def addCentroid(feat): feat = ee.Feature(feat) centroid = feat.centroid().geometry() coords = ee.List(centroid.coordinates()) return feat.set('longitude', ee.Number(coords.get(0)), 'latitude', ee.Number(coords.get(1))) data = data.map(addCentroid) data = data.getInfo() features = data['features'] d, indexes = [], [] for feature in features: nf = deepcopy(feature) props = nf['properties'] if not duplicate_index: props.pop(index) if index in props else props d.append(props) if index == 'system:index': indexes.append(feature['id']) else: indexes.append(feature['properties'][index]) return pd.DataFrame(d, indexes)
Example #11
Source File: utils.py From gee_tools with MIT License | 5 votes |
def makeName(img, pattern, date_pattern=None, extra=None): """ Make a name with the given pattern. The pattern must contain the propeties to replace between curly braces. There are 2 special words: * 'system_date': replace with the date of the image formatted with `date_pattern`, which defaults to 'yyyyMMdd' * 'id' or 'ID': the image id. If None, it'll be replaced with 'id' Pattern example (supposing each image has a property called `city`): 'image from {city} on {system_date}' You can add extra parameters using keyword `extra` """ img = ee.Image(img) props = img.toDictionary() props = ee.Dictionary(ee.Algorithms.If( img.id(), props.set('id', img.id()).set('ID', img.id()), props)) props = ee.Dictionary(ee.Algorithms.If( img.propertyNames().contains('system:time_start'), props.set('system_date', img.date().format(date_pattern)), props)) if extra: extra = ee.Dictionary(extra) props = props.combine(extra) name = string.format(pattern, props) return name
Example #12
Source File: cloud_mask.py From gee_tools with MIT License | 5 votes |
def decodeBitsEE(bit_reader, qa_band): """ :param bit_reader: the bit reader :type bit_reader: BitReader :param qa_band: name of the band that holds the bit information :type qa_band: str :return: a function to map over a collection. The function adds all categories masks as new bands """ options = ee.Dictionary(bit_reader.info) categories = ee.List(bit_reader.all_categories) def wrap(image): def eachcat(cat, ini): ini = ee.Image(ini) qa = ini.select(qa_band) # get data for category data = ee.Dictionary(options.get(cat)) lshift = ee.Number(data.get('lshift')) length = ee.Number(data.get('bit_length')) decoded = ee.Number(data.get('shifted')) # move = places to move bits right and left back move = lshift.add(length) # move bits right and left rest = qa.rightShift(move).leftShift(move) # subtract the rest norest = qa.subtract(rest) # right shift to compare with decoded data to_compare = norest.rightShift(lshift) ## Image # compare if is equal, return 0 if not equal, 1 if equal mask = to_compare.eq(decoded) # rename to the name of the category qa_mask = mask.select([0], [cat]) return ini.addBands(qa_mask) return ee.Image(categories.iterate(eachcat, image)) return wrap
Example #13
Source File: image.py From gee_tools with MIT License | 5 votes |
def getValue(image, point, scale=None, side="server"): """ Return the value of all bands of the image in the specified point :param img: Image to get the info from :type img: ee.Image :param point: Point from where to get the info :type point: ee.Geometry.Point :param scale: The scale to use in the reducer. It defaults to 10 due to the minimum scale available in EE (Sentinel 10m) :type scale: int :param side: 'server' or 'client' side :type side: str :return: Values of all bands in the ponit :rtype: ee.Dictionary or dict """ if scale: scale = int(scale) else: scale = 1 type = point.getInfo()["type"] if type != "Point": raise ValueError("Point must be ee.Geometry.Point") result = image.reduceRegion(ee.Reducer.first(), point, scale) if side == 'server': return result elif side == 'client': return result.getInfo() else: raise ValueError("side parameter must be 'server' or 'client'")
Example #14
Source File: image.py From gee_tools with MIT License | 5 votes |
def renamePattern(image, pattern, bands=None): """ Rename the bands of the parsed image with the given pattern :param image: :param pattern: the special keyword `{band}` will be replaced with the actual band name. Spaces will be replaced with underscore. It also will be trimmed :param bands: the bands to rename. If None it'll rename all the bands :return: """ allbands = image.bandNames() pattern = ee.String(pattern) selected = image.select(bands) if bands else image pattern = pattern.trim().split(' ').join('_') bands_to_replace = selected.bandNames() def wrap(name): condition = pattern.index('{band}').gt(0) return ee.String(ee.Algorithms.If( condition, pattern.replace('{band}', ee.String(name)), ee.String(name))) newbands = bands_to_replace.map(wrap) new_allbands = ee_list.replaceDict( allbands, ee.Dictionary.fromLists(bands_to_replace, newbands)) return image.select(allbands, new_allbands)
Example #15
Source File: 3_water_class_transition.py From qgis-earthengine-examples with MIT License | 5 votes |
def createFeature(transition_class_stats): transition_class_stats = ee.Dictionary(transition_class_stats) class_number = transition_class_stats.get('transition_class_value') result = { 'transition_class_number': class_number, 'transition_class_''name': lookup_names.get(class_number), 'transition_class_''palette': lookup_palette.get(class_number), 'area_m2': transition_class_stats.get('sum') } return ee.Feature({}, result) # Creates a feature without a geometry. # Create a JSON dictionary that defines piechart colors based on the # transition class palette. # https:#developers.google.com/chart/interactive/docs/gallery/piechart
Example #16
Source File: dictionary.py From gee_tools with MIT License | 5 votes |
def extractList(dict, list): """ Extract values from a list of keys """ empty = ee.List([]) list = ee.List(list) dict = ee.Dictionary(dict) def iteration(el, first): f = ee.List(first) cond = dict.contains(el) return ee.Algorithms.If(cond, f.add(dict.get(el)), f) values = ee.List(list.iterate(iteration, empty)) return values
Example #17
Source File: dictionary.py From gee_tools with MIT License | 5 votes |
def sort(dictionary): """ Sort a dictionary. Can be a `dict` or a `ee.Dictionary` :param dictionary: the dictionary to sort :type dictionary: dict or ee.Dictionary :rtype: OrderedDict or ee.Dictionary """ if isinstance(dictionary, dict): sorted = OrderedDict() keys = list(dictionary.keys()) keys.sort() for key in keys: sorted[key] = dictionary[key] return sorted elif isinstance(dictionary, ee.Dictionary): keys = dictionary.keys() ordered = keys.sort() def iteration(key, first): new = ee.Dictionary(first) val = dictionary.get(key) return new.set(key, val) return ee.Dictionary(ordered.iterate(iteration, ee.Dictionary())) else: return dictionary
Example #18
Source File: ee_list.py From gee_tools with MIT License | 5 votes |
def replaceDict(eelist, to_replace): """ Replace many elements of a Earth Engine List object using a dictionary **EXAMPLE** .. code:: python list = ee.List(["one", "two", "three", 4]) newlist = replace_many(list, {"one": 1, 4:"four"}) print newlist.getInfo() >> [1, "two", "three", "four"] :param to_replace: values to replace :type to_replace: dict :return: list with replaced values :rtype: ee.List """ eelist = ee.List(eelist) to_replace = ee.Dictionary(to_replace) keys = to_replace.keys() def wrap(el): # Convert to String elstr = ee.Algorithms.String(el) condition = ee.List(keys).indexOf(elstr) return ee.Algorithms.If(condition.neq(-1), to_replace.get(elstr), el) return eelist.map(wrap)
Example #19
Source File: bitreader.py From gee_tools with MIT License | 5 votes |
def decodeImage(self, image, qa_band): """ Get an Image with one band per category in the Bit Reader :param bit_reader: the bit reader :type bit_reader: BitReader :param qa_band: name of the band that holds the bit information :type qa_band: str :return: the image with the decode bands added """ options = ee.Dictionary(self.info) categories = ee.List(self.all_categories) def eachcat(cat, ini): ini = ee.Image(ini) qa = ini.select(qa_band) # get data for category data = ee.Dictionary(options.get(cat)) lshift = ee.Number(data.get('lshift')) length = ee.Number(data.get('bit_length')) decoded = ee.Number(data.get('shifted')) # move = places to move bits right and left back move = lshift.add(length) # move bits right and left rest = qa.rightShift(move).leftShift(move) # subtract the rest norest = qa.subtract(rest) # right shift to compare with decoded data to_compare = norest.rightShift(lshift) ## Image # compare if is equal, return 0 if not equal, 1 if equal mask = to_compare.eq(decoded) # rename to the name of the category qa_mask = mask.select([0], [cat]) return ini.addBands(qa_mask) return ee.Image(categories.iterate(eachcat, image)).select(categories)
Example #20
Source File: test_ee_list.py From gee_tools with MIT License | 5 votes |
def test_get_from_dict(): test_list = ee.List(['a', 'c']) test_dict = ee.Dictionary({'a':1, 'b':5, 'c':8}) values = ee_list.getFromDict(test_list, test_dict) # assert assert_equal(values, [1, 8])
Example #21
Source File: global_power_plant_database.py From qgis-earthengine-examples with MIT License | 5 votes |
def addStyle(pt): size = ee.Number(pt.get('capacitymw')).sqrt().divide(10).add(2) color = fuelColor.get(pt.get('fuel1')) return pt.set('styleProperty', ee.Dictionary({'pointSize': size, 'color': color})) # Make a FeatureCollection out of the power plant data table
Example #22
Source File: app.py From earthengine with MIT License | 5 votes |
def anglestats(current,prev): ''' get dictionary of incident angle statistics of current image and append to list ''' prev = ee.Dictionary(prev) rect = ee.Geometry(prev.get('rect')) stats = ee.List(prev.get('stats')) current = ee.Image(current).select('angle') meana = current.reduceRegion(ee.Reducer.mean(),geometry=rect,maxPixels= 1e9).get('angle') mina = current.reduceRegion(ee.Reducer.min(),geometry=rect,maxPixels= 1e9).get('angle') maxa = current.reduceRegion(ee.Reducer.max(),geometry=rect,maxPixels= 1e9).get('angle') stats = stats.add(ee.Dictionary({'mina':mina,'meana':meana,'maxa':maxa})) return ee.Dictionary({'stats':stats,'rect':rect})
Example #23
Source File: ee_requests.py From ee-atmcorr-timeseries with Apache License 2.0 | 5 votes |
def get(): altitude = AtmcorrInput.elevation.reduceRegion(\ reducer = ee.Reducer.mean(),\ geometry = TimeSeries.geom.centroid()\ ) return ee.Dictionary({ 'solar_z':mission_s.solar_z(TimeSeries.image, TimeSeries.mission), 'h2o':Atmospheric.water(TimeSeries.geom,TimeSeries.date), 'o3':Atmospheric.ozone(TimeSeries.geom,TimeSeries.date), 'aot':Atmospheric.aerosol(TimeSeries.geom,TimeSeries.date), 'alt':altitude.get('be75'), 'doy':TimeSeries.day_of_year })
Example #24
Source File: eeMad.py From earthengine with MIT License | 5 votes |
def imad(current,prev): done = ee.Number(ee.Dictionary(prev).get('done')) return ee.Algorithms.If(done,prev,imad1(current,prev))
Example #25
Source File: app.py From earthengine with MIT License | 5 votes |
def clipList(current,prev): ''' clip a list of images ''' imlist = ee.List(ee.Dictionary(prev).get('imlist')) rect = ee.Dictionary(prev).get('rect') imlist = imlist.add(ee.Image(current).clip(rect)) return ee.Dictionary({'imlist':imlist,'rect':rect})
Example #26
Source File: dictionary_test.py From earthengine with MIT License | 5 votes |
def testInternals(self): """Test eq(), ne() and hash().""" a = ee.Dictionary({'one': 1}) b = ee.Dictionary({'two': 2}) c = ee.Dictionary({'one': 1}) self.assertEquals(a, a) self.assertNotEquals(a, b) self.assertEquals(a, c) self.assertNotEquals(b, c) self.assertNotEquals(hash(a), hash(b))
Example #27
Source File: eeMad.py From earthengine with MIT License | 5 votes |
def radcal(current,prev): ''' iterator function for orthogonal regression and interactive radiometric normalization ''' k = ee.Number(current) prev = ee.Dictionary(prev) # image is concatenation of reference and target image = ee.Image(prev.get('image')) ncmask = ee.Image(prev.get('ncmask')) nbands = ee.Number(prev.get('nbands')) rect = ee.Geometry(prev.get('rect')) coeffs = ee.List(prev.get('coeffs')) normalized = ee.Image(prev.get('normalized')) scale = image.select(0).projection().nominalScale() # orthoregress reference onto target image1 = image.clip(rect).select(k.add(nbands),k).updateMask(ncmask).rename(['x','y']) means = image1.reduceRegion(ee.Reducer.mean(), scale=scale, maxPixels=1e9) \ .toArray()\ .project([0]) Xm = means.get([0]) Ym = means.get([1]) S = ee.Array(image1.toArray() \ .reduceRegion(ee.Reducer.covariance(), geometry=rect, scale=scale, maxPixels=1e9) \ .get('array')) # Pearson correlation R = S.get([0,1]).divide(S.get([0,0]).multiply(S.get([1,1])).sqrt()) eivs = S.eigen() e1 = eivs.get([0,1]) e2 = eivs.get([0,2]) # slope and intercept b = e2.divide(e1) a = Ym.subtract(b.multiply(Xm)) coeffs = coeffs.add(ee.List([b,a,R])) # normalize kth band in target normalized = normalized.addBands(image.select(k.add(nbands)).multiply(b).add(a)) return ee.Dictionary({'image':image,'ncmask':ncmask,'nbands':nbands,'rect':rect,'coeffs':coeffs,'normalized':normalized})
Example #28
Source File: dictionary_test.py From aqua-monitor with GNU Lesser General Public License v3.0 | 5 votes |
def testInternals(self): """Test eq(), ne() and hash().""" a = ee.Dictionary({'one': 1}) b = ee.Dictionary({'two': 2}) c = ee.Dictionary({'one': 1}) self.assertEquals(a, a) self.assertNotEquals(a, b) self.assertEquals(a, c) self.assertNotEquals(b, c) self.assertNotEquals(hash(a), hash(b))
Example #29
Source File: app_sav.py From earthengine with MIT License | 5 votes |
def clipList(current,prev): imlist = ee.List(ee.Dictionary(prev).get('imlist')) rect = ee.Dictionary(prev).get('rect') imlist = imlist.add(ee.Image(current).clip(rect)) return ee.Dictionary({'imlist':imlist,'rect':rect})
Example #30
Source File: eeWishart.py From earthengine with MIT License | 5 votes |
def filter_ell(current,prev): pvs = ee.List(current) prev = ee.Dictionary(prev) ell = ee.Number(prev.get('ell')) threshold = ee.Image(prev.get('threshold')) cmap = prev.get('cmap') smap = prev.get('smap') fmap = prev.get('fmap') bmap = prev.get('bmap') first = ee.Dictionary({'ell':ell,'j':1, 'threshold':threshold,'cmap':cmap,'smap':smap,'fmap':fmap,'bmap':bmap}) result = ee.Dictionary(ee.List(pvs).iterate(filter_j,first)) return ee.Dictionary({'ell':ell.add(1),'threshold':threshold,'cmap':result.get('cmap'), 'smap':result.get('smap'), 'fmap':result.get('fmap'), 'bmap':result.get('bmap')})