Python ee.String() Examples
The following are 30
code examples of ee.String().
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: filter_test.py From aqua-monitor with GNU Lesser General Public License v3.0 | 6 votes |
def testDate(self): """Verifies that date filters work.""" d1 = datetime.datetime.strptime('1/1/2000', '%m/%d/%Y') d2 = datetime.datetime.strptime('1/1/2001', '%m/%d/%Y') instant_range = ee.ApiFunction.call_('DateRange', d1, None) long_range = ee.ApiFunction.call_('DateRange', d1, d2) instant_filter = ee.Filter.date(d1) self.assertEquals(ee.ApiFunction.lookup('Filter.dateRangeContains'), instant_filter.func) self.assertEquals({'leftValue': instant_range, 'rightField': ee.String('system:time_start')}, instant_filter.args) long_filter = ee.Filter.date(d1, d2) self.assertEquals(ee.ApiFunction.lookup('Filter.dateRangeContains'), long_filter.func) self.assertEquals({'leftValue': long_range, 'rightField': ee.String('system:time_start')}, long_filter.args)
Example #2
Source File: imagecollection.py From gee_tools with MIT License | 6 votes |
def containsAllBands(collection, bands): """ Filter a collection with images cotaining all bands specified in parameter `bands` """ bands = ee.List(bands) # add bands as metadata collection = collection.map( lambda i: ee.Image(i).set('_BANDS_', ee.Image(i).bandNames())) band0 = ee.String(bands.get(0)) rest = ee.List(bands.slice(1)) filt0 = ee.Filter.listContains(leftField='_BANDS_', rightValue=band0) # Get filter def wrap(band, filt): band = ee.String(band) filt = ee.Filter(filt) newfilt = ee.Filter.listContains(leftField='_BANDS_', rightValue=band) return ee.Filter.And(filt, newfilt) filt = ee.Filter(rest.iterate(wrap, filt0)) return collection.filter(filt)
Example #3
Source File: imagecollection.py From gee_tools with MIT License | 6 votes |
def containsAnyBand(collection, bands): """ Filter a collection with images cotaining any of the bands specified in parameter `bands` """ bands = ee.List(bands) # add bands as metadata collection = collection.map( lambda i: ee.Image(i).set('_BANDS_', ee.Image(i).bandNames())) band0 = ee.String(bands.get(0)) rest = ee.List(bands.slice(1)) filt0 = ee.Filter.listContains(leftField='_BANDS_', rightValue=band0) # Get filter def wrap(band, filt): band = ee.String(band) filt = ee.Filter(filt) newfilt = ee.Filter.listContains(leftField='_BANDS_', rightValue=band) return ee.Filter.Or(filt, newfilt) filt = ee.Filter(rest.iterate(wrap, filt0)) return collection.filter(filt)
Example #4
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 #5
Source File: image.py From gee_tools with MIT License | 6 votes |
def minscale(image): """ Get the minimal scale of an Image, looking at all Image's bands. For example if: B1 = 30 B2 = 60 B3 = 10 the function will return 10 :return: the minimal scale :rtype: ee.Number """ bands = image.bandNames() first = image.select([ee.String(bands.get(0))]) ini = ee.Number(first.projection().nominalScale()) def wrap(name, i): i = ee.Number(i) scale = ee.Number(image.select([name]).projection().nominalScale()) condition = scale.lte(i) newscale = ee.Algorithms.If(condition, scale, i) return newscale return ee.Number(bands.slice(1).iterate(wrap, ini))
Example #6
Source File: string_test.py From aqua-monitor with GNU Lesser General Public License v3.0 | 6 votes |
def testString(self): """Verifies basic behavior of ee.String.""" bare_string = ee.String('foo') self.assertEquals('foo', bare_string.encode()) computed = ee.String('foo').cat('bar') self.assertTrue(isinstance(computed, ee.String)) self.assertEquals(ee.ApiFunction.lookup('String.cat'), computed.func) self.assertEquals({'string1': ee.String('foo'), 'string2': ee.String('bar')}, computed.args) # Casting a non-string ComputedObject. obj = ee.Number(1).add(1) s = ee.String(obj) self.assertTrue(isinstance(s, ee.String)) self.assertEquals(ee.ApiFunction.lookup('String'), s.func) self.assertEquals({'input': obj}, s.args)
Example #7
Source File: collection_test.py From aqua-monitor with GNU Lesser General Public License v3.0 | 6 votes |
def testSortAndLimit(self): """Verifies the behavior of the sort() and limit() methods.""" collection = ee.Collection(ee.Function(), {}) limited = collection.limit(10) self.assertEquals(ee.ApiFunction.lookup('Collection.limit'), limited.func) self.assertEquals( {'collection': collection, 'limit': 10}, limited.args) sorted_collection = collection.sort('bar', True) self.assertEquals( ee.ApiFunction.lookup('Collection.limit'), sorted_collection.func) self.assertEquals( {'collection': collection, 'key': ee.String('bar'), 'ascending': True}, sorted_collection.args) reverse_sorted_collection = collection.sort('bar', False) self.assertEquals( ee.ApiFunction.lookup('Collection.limit'), reverse_sorted_collection.func) self.assertEquals( {'collection': collection, 'key': ee.String('bar'), 'ascending': False}, reverse_sorted_collection.args)
Example #8
Source File: list_test.py From aqua-monitor with GNU Lesser General Public License v3.0 | 6 votes |
def testMapping(self): lst = ee.List(['foo', 'bar']) body = lambda s: ee.String(s).cat('bar') mapped = lst.map(body) self.assertTrue(isinstance(mapped, ee.List)) self.assertEquals(ee.ApiFunction.lookup('List.map'), mapped.func) self.assertEquals(lst, mapped.args['list']) # Need to do a serialized comparison for the function body because # variables returned from CustomFunction.variable() do not implement # __eq__. sig = { 'returns': 'Object', 'args': [{'name': '_MAPPING_VAR_0_0', 'type': 'Object'}] } expected_function = ee.CustomFunction(sig, body) self.assertEquals(expected_function.serialize(), mapped.args['baseAlgorithm'].serialize())
Example #9
Source File: date.py From gee_tools with MIT License | 6 votes |
def fromDOY(doy, year): """ Creat a ee.Date given a Day of Year and a Year """ def less10(doy): doy = doy.toInt() return ee.String('00').cat(ee.Number(doy).format()) def less100(doy): doy = doy.toInt() return ee.String('0').cat(ee.Number(doy).format()) doy = ee.Number(doy).add(1).toInt() year_str = ee.Number(year).format() doy_str = ee.Algorithms.If(doy.lt(10), less10(doy), ee.String(ee.Algorithms.If(doy.lt(100), less100(doy), doy.format()))) s = ee.String(doy_str).cat(year_str) return ee.Date.parse('DDDyyyy', s)
Example #10
Source File: string_test.py From earthengine with MIT License | 6 votes |
def testString(self): """Verifies basic behavior of ee.String.""" bare_string = ee.String('foo') self.assertEquals('foo', bare_string.encode()) computed = ee.String('foo').cat('bar') self.assertTrue(isinstance(computed, ee.String)) self.assertEquals(ee.ApiFunction.lookup('String.cat'), computed.func) self.assertEquals({'string1': ee.String('foo'), 'string2': ee.String('bar')}, computed.args) # Casting a non-string ComputedObject. obj = ee.Number(1).add(1) s = ee.String(obj) self.assertTrue(isinstance(s, ee.String)) self.assertEquals(ee.ApiFunction.lookup('String'), s.func) self.assertEquals({'input': obj}, s.args)
Example #11
Source File: collection_test.py From earthengine with MIT License | 6 votes |
def testSortAndLimit(self): """Verifies the behavior of the sort() and limit() methods.""" collection = ee.Collection(ee.Function(), {}) limited = collection.limit(10) self.assertEquals(ee.ApiFunction.lookup('Collection.limit'), limited.func) self.assertEquals( {'collection': collection, 'limit': 10}, limited.args) sorted_collection = collection.sort('bar', True) self.assertEquals( ee.ApiFunction.lookup('Collection.limit'), sorted_collection.func) self.assertEquals( {'collection': collection, 'key': ee.String('bar'), 'ascending': True}, sorted_collection.args) reverse_sorted_collection = collection.sort('bar', False) self.assertEquals( ee.ApiFunction.lookup('Collection.limit'), reverse_sorted_collection.func) self.assertEquals( {'collection': collection, 'key': ee.String('bar'), 'ascending': False}, reverse_sorted_collection.args)
Example #12
Source File: list_test.py From earthengine with MIT License | 6 votes |
def testMapping(self): lst = ee.List(['foo', 'bar']) body = lambda s: ee.String(s).cat('bar') mapped = lst.map(body) self.assertTrue(isinstance(mapped, ee.List)) self.assertEquals(ee.ApiFunction.lookup('List.map'), mapped.func) self.assertEquals(lst, mapped.args['list']) # Need to do a serialized comparison for the function body because # variables returned from CustomFunction.variable() do not implement # __eq__. sig = { 'returns': 'Object', 'args': [{'name': '_MAPPING_VAR_0_0', 'type': 'Object'}] } expected_function = ee.CustomFunction(sig, body) self.assertEquals(expected_function.serialize(), mapped.args['baseAlgorithm'].serialize())
Example #13
Source File: filter_test.py From earthengine with MIT License | 6 votes |
def testDate(self): """Verifies that date filters work.""" d1 = datetime.datetime.strptime('1/1/2000', '%m/%d/%Y') d2 = datetime.datetime.strptime('1/1/2001', '%m/%d/%Y') instant_range = ee.ApiFunction.call_('DateRange', d1, None) long_range = ee.ApiFunction.call_('DateRange', d1, d2) instant_filter = ee.Filter.date(d1) self.assertEquals(ee.ApiFunction.lookup('Filter.dateRangeContains'), instant_filter.func) self.assertEquals({'leftValue': instant_range, 'rightField': ee.String('system:time_start')}, instant_filter.args) long_filter = ee.Filter.date(d1, d2) self.assertEquals(ee.ApiFunction.lookup('Filter.dateRangeContains'), long_filter.func) self.assertEquals({'leftValue': long_range, 'rightField': ee.String('system:time_start')}, long_filter.args)
Example #14
Source File: image.py From gee_tools with MIT License | 5 votes |
def arrayNonZeros(image): """ Return an image array without zeros :param image: :return: """ def wrap(arr): binarr = arr.divide(arr) n = binarr.arrayReduce(ee.Reducer.sum(), [0]).multiply(-1) nimg = n.arrayProject([0]).arrayFlatten([['n']]).toInt() sorted = arr.arraySort() sliced = sorted.arraySlice(0, nimg) return sliced bands = image.bandNames() first = wrap(image.select([bands.get(0)])) rest = bands.slice(1) def overBands(band, i): band = ee.String(band) i = ee.Image(i) array = wrap(image.select([band])) return i.addBands(array) result1 = ee.Image(rest.iterate(overBands, first)) result2 = ee.Image(wrap(image)) return ee.Image(ee.Algorithms.If(bands.size(), result1, result2))
Example #15
Source File: image.py From gee_tools with MIT License | 5 votes |
def repeatBand(image, times=None, names=None, properties=None): """ Repeat one band. If the image parsed has more than one band, the first will be used """ band = ee.Image(image.select([0])) if times is not None: times = ee.Number(times) proxylist = ee.List.repeat(0, times.subtract(1)) def add(band, i): band = ee.Image(band) i = ee.Image(i) return i.addBands(band) proxyImg = proxylist.map(lambda n: band) repeated = ee.Image(proxyImg.iterate(add, band)) else: newNames = ee.List(names) firstName = ee.String(newNames.get(0)) rest = ee.List(newNames.slice(1)) def add(name, i): name = ee.String(name) i = ee.Image(i) return i.addBands(band.rename(name)) first = band.rename(firstName) repeated = ee.Image(rest.iterate(add, first)) if properties: repeated = repeated.setMulti(properties) return ee.Image(repeated)
Example #16
Source File: composite.py From gee_tools with MIT License | 5 votes |
def max(collection, band): """ Make a max composite using the specified band """ band = ee.String(band) first = collection.first() originalbands = first.bandNames() bands = originalbands.remove(band) bands = bands.insert(0, band) col = collection.map(lambda img: img.select(bands)) # change bands order comp = col.reduce(ee.Reducer.max(originalbands.size())) return comp.rename(bands).select(originalbands)
Example #17
Source File: image.py From gee_tools with MIT License | 5 votes |
def applyMask(image, mask, bands=None, negative=True): """ Apply a passed positive mask """ bands = bands or mask.bandNames() bands = ee.List(bands) def wrap(band, img): img = ee.Image(img) band = ee.String(band) m = mask.select(band) toapply = m.Not() if negative else m return img.updateMask(toapply) return ee.Image(bands.iterate(wrap, image))
Example #18
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 #19
Source File: image.py From gee_tools with MIT License | 5 votes |
def computeBits(image, start, end, newName): """ Compute the bits of an image :param start: start bit :type start: int :param end: end bit :type end: int :param newName: new name for the band :type newName: str :return: A function which single argument is the image and returns a single band image of the extracted bits, giving the band a new name :rtype: function """ pattern = ee.Number(0) start = ee.Number(start).toInt() end = ee.Number(end).toInt() newName = ee.String(newName) seq = ee.List.sequence(start, end) def toiterate(element, ini): ini = ee.Number(ini) bit = ee.Number(2).pow(ee.Number(element)) return ini.add(bit) patt = seq.iterate(toiterate, pattern) patt = ee.Number(patt).toInt() good_pix = image.select([0], [newName]).toInt() \ .bitwiseAnd(patt).rightShift(start) return good_pix.toInt()
Example #20
Source File: ee_list.py From gee_tools with MIT License | 5 votes |
def format(eelist): """ Convert a list to a string """ def wrap(el, ini): ini = ee.String(ini) strel = ee.Algorithms.String(el) return ini.cat(',').cat(strel) liststr = ee.String(eelist.iterate(wrap, '')) return liststr.replace('^,', '[').cat(']')
Example #21
Source File: string.py From gee_tools with MIT License | 5 votes |
def eq(string, to_compare): """ Compare two ee.String and return 1 if equal else 0 """ string = ee.String(string) to_compare = ee.String(to_compare) return string.compareTo(to_compare).Not()
Example #22
Source File: visualization.py From gee_tools with MIT License | 5 votes |
def stretch_percentile(image, region, bands=None, percentile=90, scale=None): """ Get mins and maxs values for stretching a visualization using percentiles """ # Calculate start and end percentiles startp = 50-(percentile/2) endp = 50+(percentile/2) if not bands: names = image.bandNames() bands = ee.List(ee.Algorithms.If( names.size().gte(3), names.slice(0,3), names.slice(0))) bands = bands.getInfo() image = image.select(bands) geom = region or image.geometry() params = dict(geometry=geom, bestEffort=True) if scale: params['scale'] = scale params['reducer'] = ee.Reducer.percentile([startp, endp]) percentiles = image.reduceRegion(**params) def minmax(band): minkey = ee.String(band).cat('_p').cat(ee.Number(startp).format()) maxkey = ee.String(band).cat('_p').cat(ee.Number(endp).format()) minv = ee.Number(percentiles.get(minkey)) maxv = ee.Number(percentiles.get(maxkey)) return ee.List([minv, maxv]) if len(bands) == 1: band = bands[0] values = minmax(band).getInfo() minv = values[0] maxv = values[1] else: values = ee.List(bands).map(minmax).getInfo() minv = [values[0][0], values[1][0], values[2][0]] maxv = [values[0][1], values[1][1], values[2][1]] return dict(bands=bands, min=minv, max=maxv)
Example #23
Source File: eigen_analysis.py From qgis-earthengine-examples with MIT License | 5 votes |
def getNewBandNames(prefix): seq = ee.List.sequence(1, bandNames.length()) return seq.map(lambda b: ee.String(prefix).cat(ee.Number(b).int().format())) # This function accepts mean centered imagery, a scale and # a region in which to perform the analysis. It returns the # Principal Components (PC) in the region as a new image.
Example #24
Source File: test_c_image.py From openet-ssebop-beta with Apache License 2.0 | 5 votes |
def test_Image_from_landsat_c1_sr_scaling(): """Test if Landsat SR images images are being scaled""" sr_img = ee.Image('LANDSAT/LC08/C01/T1_SR/LC08_044033_20170716') input_img = ee.Image.constant([100, 100, 100, 100, 100, 100, 3000.0, 322])\ .rename(['B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B10', 'pixel_qa'])\ .set({'SATELLITE': ee.String(sr_img.get('SATELLITE')), 'system:id': ee.String(sr_img.get('system:id')), 'system:index': ee.String(sr_img.get('system:index')), 'system:time_start': ee.Number(sr_img.get('system:time_start'))}) output = utils.constant_image_value( ssebop.Image.from_landsat_c1_sr(input_img).lst) # Won't be exact because of emissivity correction assert abs(output['lst'] - 300) <= 10 # @pytest.mark.parametrize( # 'image_id', # [ # 'LANDSAT/LC08/C01/T1_TOA/LC08_044033_20170716', # 'LANDSAT/LC08/C01/T1_SR/LC08_044033_20170716', # ] # ) # def test_Image_from_image_id(image_id): # """Test instantiating the class using the from_image_id method""" # output = utils.getinfo(ssebop.Image.from_image_id(image_id).ndvi) # assert output['properties']['system:index'] == image_id.split('/')[-1] # assert output['properties']['image_id'] == image_id
Example #25
Source File: string_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.String('one') b = ee.String('two') c = ee.String('one') self.assertEquals(a, a) self.assertNotEquals(a, b) self.assertEquals(a, c) self.assertNotEquals(b, c) self.assertNotEquals(hash(a), hash(b))
Example #26
Source File: scores.py From geebap with GNU General Public License v3.0 | 5 votes |
def map(self, collection, **kwargs): """ :return: :rtype: ee.Image """ name = self.name increment = self.increment reducer = self.process amount = self.dist outliers = self.apply(collection, bands=self.bands, reducer=reducer, amount=amount) pattern = self.bands_ee.map( lambda name: ee.String(name).cat('_outlier')) def wrap(img): out = img.select(pattern) # As apply method returns 1 for outliers and the score should be 0 # for it, turn it upside down out = out.Not() suma = tools.image.sumBands(out, name) final = suma.select(name) \ .multiply(ee.Image(increment)).rename(name) no_out = tools.image.removeBands(img, pattern) return no_out.addBands(final) return outliers.map(wrap)
Example #27
Source File: string_test.py From earthengine with MIT License | 5 votes |
def testInternals(self): """Test eq(), ne() and hash().""" a = ee.String('one') b = ee.String('two') c = ee.String('one') self.assertEquals(a, a) self.assertNotEquals(a, b) self.assertEquals(a, c) self.assertNotEquals(b, c) self.assertNotEquals(hash(a), hash(b))
Example #28
Source File: resolve_ecoregions.py From qgis-earthengine-examples with MIT License | 5 votes |
def set_color(f): c = ee.String(f.get('COLOR')).slice(1) return f \ .set('R', ee.Number.parse(c.slice(0, 2), 16)) \ .set('G', ee.Number.parse(c.slice(2, 4), 16)) \ .set('B', ee.Number.parse(c.slice(4, 6), 16))
Example #29
Source File: imagecollection.py From gee_tools with MIT License | 4 votes |
def getValues(collection, geometry, scale=None, reducer=None, id='system:index', properties=None, side='server', maxPixels=1e9): """ Return all values of all bands of an image collection in the specified geometry :param geometry: Point from where to get the info :type geometry: ee.Geometry :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 id: image property that will be the key in the result dict :type id: str :param properties: image properties that will be added to the resulting dict :type properties: list :param side: 'server' or 'client' side :type side: str :return: Values of all bands in the ponit :rtype: dict """ if reducer is None: reducer = ee.Reducer.mean() if not scale: scale = 1 else: scale = int(scale) if not properties: properties = [] properties = ee.List(properties) def listval(img, it): theid = ee.Algorithms.String(img.get(id)) values = img.reduceRegion(reducer, geometry, scale, maxPixels=maxPixels) values = ee.Dictionary(values) img_props = img.propertyNames() def add_properties(prop, ini): ini = ee.Dictionary(ini) condition = img_props.contains(prop) def true(): value = img.get(prop) return ini.set(prop, value) return ee.Algorithms.If(condition, true(), ini) with_prop = ee.Dictionary(properties.iterate(add_properties, values)) return ee.Dictionary(it).set(theid, with_prop) result = collection.iterate(listval, ee.Dictionary({})) result = ee.Dictionary(ee.Algorithms.If(collection.size().neq(0), result, {})) if side == 'server': return result elif side == 'client': return result.getInfo() else: raise ValueError("side parameter must be 'server' or 'client'")
Example #30
Source File: bap.py From geebap with GNU General Public License v3.0 | 4 votes |
def _set_properties(self, mosaic, year, col): """ Set some BAP common properties to the given mosaic """ # # USED IMAGES # used_images = self._used_images # for prop, value in used_images.items(): # mosaic = mosaic.set(prop, str(value)) # SCORES bap_scores = [] for score in self.scores: pattern = utils.object_init(score) bap_scores.append(pattern) mosaic = mosaic.set('BAP_SCORES', str(bap_scores)) # MASKS bap_masks = [] for mask in self.masks: pattern = utils.object_init(mask) bap_masks.append(pattern) mosaic = mosaic.set('BAP_MASKS', str(bap_masks)) # FILTERS bap_filters = [] for filter in self.filters: pattern = utils.object_init(filter) bap_filters.append(pattern) mosaic = mosaic.set('BAP_FILTERS', str(bap_filters)) # DATE date = self.time_start(year).millis() mosaic = mosaic.set('system:time_start', date) # BAP Version mosaic = mosaic.set('BAP_VERSION', __version__) # FOOTPRINT geom = tools.imagecollection.mergeGeometries(col) mosaic = mosaic.set('system:footprint', geom) # Seasons for year in self.year_range(year): # yearstr = ee.Number(year).format() daterange = self.season.add_year(year) start = daterange.start().format('yyyy-MM-dd') end = daterange.end().format('yyyy-MM-dd') string = start.cat(' to ').cat(end) # propname = ee.String('BAP_SEASON_').cat(yearstr) propname = ee.String('BAP_SEASON') mosaic = mosaic.set(propname, string) return mosaic