def addBand(image): date = ee.Date(image.get('system:time_start')) yearOffset = date.difference(ee.Date(start), 'year') ndvi = image.normalizedDifference(['B4', 'B3']) return ee.Image(1).addBands(yearOffset).addBands(ndvi).toDouble()
import ee import time import pandas as pd from pull_MODIS import export_oneimage, appendBand_6 ee.Initialize() locations = pd.read_csv('locations_final_1.csv', header=None) county_region = ee.FeatureCollection( 'ft:1S4EB6319wWW2sWQDPhDvmSBIVrD3iEmCLYB7nMM') imgcoll = ee.ImageCollection('MODIS/MOD09A1') \ .filterBounds(ee.Geometry.Rectangle(-106.5, 50, -64, 23)) \ .filterDate('2002-12-31', '2016-8-4') img = imgcoll.iterate(appendBand_6) img = ee.Image(img) img_0 = ee.Image(ee.Number(-100)) img_16000 = ee.Image(ee.Number(16000)) img = img.min(img_16000) img = img.max(img_0) # img=ee.Image(ee.Number(100)) # img=ee.ImageCollection('LC8_L1T').mosaic() for loc1, loc2, lat, lon in locations.values: file_name = '{}_{}'.format(int(loc1), int(loc2)) # offset = 0.11 scale = 500
attr='Google Earth Engine', name=name, overlay=True, control=True).add_to(self) except: print("Could not display {}".format(name)) # Add EE drawing method to folium. folium.Map.add_ee_layer = add_ee_layer if __name__ == "__main__": # Load an ee.Image test_image = ee.Image('users/JohnBKilbride/umaine/Smooth/fitted_2018') # Set visualization parameters. vis_params = { 'min': 0, 'max': [1250, 6000, 1300], 'bands': ['B7', 'B4', 'B3'] } # Create a folium map object. my_map = folium.Map(location=[20, 0], zoom_start=3, height=500) # Add custom basemaps basemaps['Google Maps'].add_to(my_map) basemaps['Google Satellite Hybrid'].add_to(my_map)
def leesigma(image, KERNEL_SIZE): """ Implements the improved lee sigma filter to one image. It is implemented as described in, Lee, J.-S. Wen, J.-H. Ainsworth, T.L. Chen, K.-S. Chen, A.J. Improved sigma filter for speckle filtering of SAR imagery. IEEE Trans. Geosci. Remote Sens. 2009, 47, 202–213. Parameters ---------- image : ee.Image Image to be filtered KERNEL_SIZE : positive odd integer Neighbourhood window size Returns ------- ee.Image Filtered Image """ #parameters Tk = ee.Image.constant(7) #number of bright pixels in a 3x3 window sigma = 0.9 enl = 4 target_kernel = 3 bandNames = image.bandNames().remove('angle') #compute the 98 percentile intensity z98 = ee.Dictionary( image.select(bandNames).reduceRegion(reducer=ee.Reducer.percentile( [98]), geometry=image.geometry(), scale=10, maxPixels=1e13)).toImage() #select the strong scatterers to retain brightPixel = image.select(bandNames).gte(z98) K = brightPixel.reduceNeighborhood(ee.Reducer.countDistinctNonNull(), ee.Kernel.square(target_kernel / 2)) retainPixel = K.gte(Tk) #compute the a-priori mean within a 3x3 local window #original noise standard deviation since the data is 5 look eta = 1.0 / math.sqrt(enl) eta = ee.Image.constant(eta) #MMSE applied to estimate the apriori mean reducers = ee.Reducer.mean().combine( \ reducer2= ee.Reducer.variance(), \ sharedInputs= True ) stats = image.select(bandNames).reduceNeighborhood( \ reducer= reducers, \ kernel= ee.Kernel.square(target_kernel/2,'pixels'), \ optimization= 'window') meanBand = bandNames.map(lambda bandName: ee.String(bandName).cat('_mean')) varBand = bandNames.map( lambda bandName: ee.String(bandName).cat('_variance')) z_bar = stats.select(meanBand) varz = stats.select(varBand) oneImg = ee.Image.constant(1) varx = (varz.subtract(z_bar.abs().pow(2).multiply(eta.pow(2)))).divide( oneImg.add(eta.pow(2))) b = varx.divide(varz) xTilde = oneImg.subtract(b).multiply(z_bar.abs()).add( b.multiply(image.select(bandNames))) #step 3: compute the sigma range #Lookup table (J.S.Lee et al 2009) for range and eta values for intensity (only 4 look is shown here) LUT = ee.Dictionary({ 0.5: ee.Dictionary({ 'I1': 0.694, 'I2': 1.385, 'eta': 0.1921 }), 0.6: ee.Dictionary({ 'I1': 0.630, 'I2': 1.495, 'eta': 0.2348 }), 0.7: ee.Dictionary({ 'I1': 0.560, 'I2': 1.627, 'eta': 0.2825 }), 0.8: ee.Dictionary({ 'I1': 0.480, 'I2': 1.804, 'eta': 0.3354 }), 0.9: ee.Dictionary({ 'I1': 0.378, 'I2': 2.094, 'eta': 0.3991 }), 0.95: ee.Dictionary({ 'I1': 0.302, 'I2': 2.360, 'eta': 0.4391 }) }) #extract data from lookup sigmaImage = ee.Dictionary(LUT.get(str(sigma))).toImage() I1 = sigmaImage.select('I1') I2 = sigmaImage.select('I2') #new speckle sigma nEta = sigmaImage.select('eta') #establish the sigma ranges I1 = I1.multiply(xTilde) I2 = I2.multiply(xTilde) #step 3: apply MMSE filter for pixels in the sigma range #MMSE estimator mask = image.select(bandNames).gte(I1).Or(image.select(bandNames).lte(I2)) z = image.select(bandNames).updateMask(mask) stats = z.reduceNeighborhood( \ reducer= reducers, \ kernel= ee.Kernel.square(KERNEL_SIZE/2,'pixels'), \ optimization= 'window') z_bar = stats.select(meanBand) varz = stats.select(varBand) varx = (varz.subtract(z_bar.abs().pow(2).multiply(nEta.pow(2)))).divide( oneImg.add(nEta.pow(2))) b = varx.divide(varz) #if b is negative set it to zero new_b = b.where(b.lt(0), 0) xHat = oneImg.subtract(new_b).multiply(z_bar.abs()).add(new_b.multiply(z)) #remove the applied masks and merge the retained pixels and the filtered pixels xHat = image.select(bandNames).updateMask(retainPixel).unmask(xHat) output = ee.Image(xHat).rename(bandNames) return image.addBands(output, None, True)
# %% """ ## Create an interactive map The default basemap is `Google Satellite`. [Additional basemaps](https://github.com/giswqs/geemap/blob/master/geemap/geemap.py#L13) can be added using the `Map.add_basemap()` function. """ # %% Map = emap.Map(center=[40, -100], zoom=4) Map.add_basemap('ROADMAP') # Add Google Map Map # %% """ ## Add Earth Engine Python script """ # %% # Add Earth Engine dataset image = ee.Image('srtm90_v4') vis_params = {'min': 0, 'max': 3000} Map.addLayer(image, vis_params, "SRTM") Map.setCenter(0, 0, 2) # %% """ ## Display Earth Engine data layers """ # %% Map.addLayerControl() # This line is not needed for ipyleaflet-based Map. Map
def snow_mask(img: ee.Image) -> ee.Image: img = ee.Image(img) # TODO: assert that it is a surface reflectance image (otherwise no SCL band) no_snow = img.select('SCL').eq(11).Not() return img.updateMask(no_snow)
def medoidScore(collection, bands=None, discard_zeros=False, bandname='sumdist', normalize=True): """ Compute a score to reflect 'how far' is from the medoid. Same params as medoid() """ first_image = ee.Image(collection.first()) if not bands: bands = first_image.bandNames() # Create a unique id property called 'enumeration' enumerated = tools.imagecollection.enumerateProperty(collection) collist = enumerated.toList(enumerated.size()) def over_list(im): im = ee.Image(im) n = ee.Number(im.get('enumeration')) # Remove the current image from the collection filtered = tools.ee_list.removeIndex(collist, n) # Select bands for medoid to_process = im.select(bands) def over_collist(img): return ee.Image(img).select(bands) filtered = filtered.map(over_collist) # Compute the sum of the euclidean distance between the current image # and every image in the rest of the collection dist = algorithms.sumDistance(to_process, filtered, name=bandname, discard_zeros=discard_zeros) # Mask zero values if not normalize: # multiply by -1 to get the lowest value in the qualityMosaic dist = dist.multiply(-1) return im.addBands(dist) imlist = ee.List(collist.map(over_list)) medcol = ee.ImageCollection.fromImages(imlist) # Normalize result to be between 0 and 1 if normalize: min_sumdist = ee.Image(medcol.select(bandname).min())\ .rename('min_sumdist') max_sumdist = ee.Image(medcol.select(bandname).max()) \ .rename('max_sumdist') def to_normalize(img): sumdist = img.select(bandname) newband = ee.Image().expression('1-((val-min)/(max-min))', { 'val': sumdist, 'min': min_sumdist, 'max': max_sumdist }).rename(bandname) return tools.image.replace(img, bandname, newband) medcol = medcol.map(to_normalize) return medcol
def toNatural(img): return ee.Image(10.0).pow(img.select(0).divide(10.0))
def toDB(img): return ee.Image(img).log10().multiply(10.0)
This step creates an interactive map using [folium](https://github.com/python-visualization/folium). The default basemap is the OpenStreetMap. Additional basemaps can be added using the `Map.setOptions()` function. The optional basemaps can be `ROADMAP`, `SATELLITE`, `HYBRID`, `TERRAIN`, or `ESRI`. ''' # %% Map = folium.Map(location=[40, -100], zoom_start=4) Map.setOptions('HYBRID') # %% ''' ## Add Earth Engine Python script ''' # %% dataset = ee.Image('CSP/ERGo/1_0/Global/SRTM_landforms') landforms = dataset.select('constant') landformsVis = { 'min': 11.0, 'max': 42.0, 'palette': [ '141414', '383838', '808080', 'EBEB8F', 'F7D311', 'AA0000', 'D89382', 'DDC9C9', 'DCCDCE', '1C6330', '68AA63', 'B5C98E', 'E1F0E5', 'a975ba', '6f198c' ], } Map.setCenter(-105.58, 40.5498, 11) Map.addLayer(landforms, landformsVis, 'Landforms')
def logitTransform(img): return img.divide(ee.Image(1).subtract(img)).log()
# # Computes a simple reduction over a region of an image. A reduction # is any process that takes an arbitrary number of inputs (such as # all the pixels of an image in a given region) and computes one or # more fixed outputs. The result is a dictionary that contains the # computed values, which in this example is the maximum pixel value # in the region. # This example shows how to print the resulting dictionary to the # console, which is useful when developing and debugging your # scripts, but in a larger workflow you might instead use the # Dicitionary.get() function to extract the values you need from the # dictionary for use as inputs to other functions. # The input image to reduce, in this case an SRTM elevation map. image = ee.Image('CGIAR/SRTM90_V4') # The region to reduce within. poly = ee.Geometry.Rectangle([-109.05, 41, -102.05, 37]) # Reduce the image within the given region, using a reducer that # computes the max pixel value. We also specify the spatial # resolution at which to perform the computation, in this case 200 # meters. max = image.reduceRegion(**{ 'reducer': ee.Reducer.max(), 'geometry': poly, 'scale': 200 }) # Print the result (a Dictionary) to the console.
This step creates an interactive map using [folium](https://github.com/python-visualization/folium). The default basemap is the OpenStreetMap. Additional basemaps can be added using the `Map.setOptions()` function. The optional basemaps can be `ROADMAP`, `SATELLITE`, `HYBRID`, `TERRAIN`, or `ESRI`. ''' # %% Map = folium.Map(location=[40, -100], zoom_start=4) Map.setOptions('HYBRID') # %% ''' ## Add Earth Engine Python script ''' # %% dataset = ee.Image('USGS/NLCD/NLCD2016') landcover = ee.Image(dataset.select('landcover')) Map.setCenter(-95, 38, 5) Map.addLayer(landcover.randomVisualizer(), {}, 'Landcover') # %% ''' ## Display Earth Engine data layers ''' # %% Map.setControlVisibility(layerControl=True, fullscreenControl=True, latLngPopup=True)
def CreateTimeBand(img): year = ee.Date(img.get('system:time_start')).get('year').subtract(1991) return ee.Image(year).byte().addBands(img)
The optional basemaps can be `ROADMAP`, `SATELLITE`, `HYBRID`, `TERRAIN`, or `ESRI`. ''' # %% Map = folium.Map(location=[40, -100], zoom_start=4) Map.setOptions('HYBRID') # %% ''' ## Add Earth Engine Python script ''' # %% # Load an input Landsat 8 image. image = ee.Image('LANDSAT/LC08/C01/T1_TOA/LC08_186059_20130419') # Compute cloud score and reverse it such that the highest # weight (100) is for the least cloudy pixels. cloudWeight = ee.Image(100).subtract( ee.Algorithms.Landsat.simpleCloudScore(image).select(['cloud'])) # Compute NDVI and add the cloud weight band. ndvi = image.normalizedDifference(['B5', 'B4']).addBands(cloudWeight) # Define an arbitrary region in a cloudy area. region = ee.Geometry.Rectangle(9.9069, 0.5981, 10.5, 0.9757) # Use a mean reducer. reducer = ee.Reducer.mean()
def getTileLayerUrl(ee_image_object): map_id = ee.Image(ee_image_object).getMapId() tile_url_template = "https://earthengine.googleapis.com/map/{mapid}/{{z}}/{{x}}/{{y}}?token={token}" return tile_url_template.format(**map_id)
[23.86773700178469, -17.79920797603527], [23.537803835280783, -17.828298736637873], [23.539520449050315, -17.851829360100737]]) month = datetime.datetime.now().month collection = ee.ImageCollection('COPERNICUS/S1_GRD').filterBounds( bb).filterDate('2019-06-01', '2019-11-01').filter( ee.Filter.calendarRange(month, month, 'month')).select('VV', 'VH').median() input = collection training = input.sample(region, 10) clusterer = ee.Clusterer.wekaKMeans(2).train(training) result_raw = input.clip(bb).cluster(clusterer) result = result_raw.reproject("EPSG:4326", None, 50) sum_1 = ee.Number( ee.Image(1).clip(corridor).updateMask(result.eq(0)).reduceRegion( ee.Reducer.sum(), corridor, 50, "EPSG:4326").get('constant')) sum_2 = ee.Number( ee.Image(1).clip(corridor).updateMask(result.eq(1)).reduceRegion( ee.Reducer.sum(), corridor, 50, "EPSG:4326").get('constant')) cleared = sum_1.multiply(sum_1.lt(sum_2)).add(sum_2.multiply(sum_2.lt(sum_1))) area = ee.Number( ee.Image(1).reduceRegion(ee.Reducer.sum(), corridor, 50, "EPSG:4326").get('constant')) final = cleared.divide(area.divide(100)).round() print(final.getInfo())
def calcObs(img): # observation is img > 0 obs = img.gt(0) return ee.Image(obs).set('system:time_start', img.get('system:time_start'))
def over_collist(img): return ee.Image(img).select(bands)
def calcWater(img): water = img.select('water').eq(2) return ee.Image(water).set('system:time_start', img.get('system:time_start'))
# ============================= if __name__ == '__main__': ee.Initialize() ee.mapclient.centerMap(-117.3, 33.0, 9) ## Use my own study area - selected CA county subdivisions fc = ee.FeatureCollection('ft:1rY5cp4zTg5PtsjLPsmKPBTblFaJmzuXhxDfgmQGm','geometry') ee.mapclient.addToMap(fc, {'color': '0000FF'}) # test to see if it appears on the map ## A region of the image to train with - landcover classes including wildwire burned area train_fc = ee.FeatureCollection('ft:1nOsawIA_mWW-b0-OVHTlbU8Dzj4lIOZ3fdFL3dHB') ee.mapclient.addToMap(train_fc, {'color': '800080'}) # test to see if it appears on the map ## Input images are Landsat 7 after the 2007 December wildfire image1 = ee.Image('LE7_L1T_32DAY_RAW/20071219').select('B[1-5,7]') # bands B1-5 & 7 ## the following are for avarage fall value - not a good idear: too many clouds #image0 = (ee.ImageCollection('LC8_L1T_8DAY_TOA').filterDate(datetime.datetime(2013, 9, 1),datetime.datetime(2013, 10, 31))) #image1 = image0.median() ## limit the analysis area for the study area (dong nai region) only image2 = image1.clip(fc) ee.mapclient.addToMap(image2) # test to see if it appears on the map # print(image2.getInfo()) # to get the image info ## call classify = supervised classification function classified = classify(image2, train_fc) ee.mapclient.addToMap(classified) # test to see if it appears on the map ## visualize the classified according to values
import ee import geemap # Create a map centered at (lat, lon). Map = geemap.Map(center=[40, -100], zoom=4) # Load a Landsat 8 image, select the NIR band, threshold, display. image = ee.Image('LANDSAT/LC08/C01/T1_TOA/LC08_044034_20140318') \ .select(4).gt(0.2) Map.setCenter(-122.1899, 37.5010, 13) Map.addLayer(image, {}, 'NIR threshold') # Define a kernel. kernel = ee.Kernel.circle(**{'radius': 1}) # Perform an erosion followed by a dilation, display. opened = image \ .focal_min(**{'kernel': kernel, 'iterations': 2}) \ .focal_max(**{'kernel': kernel, 'iterations': 2}) Map.addLayer(opened, {}, 'opened') # Display the map. Map
def create(collection, region): """ Creates a Sentinel 1 time-scan. Args: collection: Sentinel 1 ee.ImageCollection with at least 'VV', 'VH' bands. region: The region to clip the mosaic to. Returns: A clipped ee.Image with the following bands: VV_min - Minimum VV; VV_mean - Mean VV; VV_median - Median VV; VV_max - Maximum VV; VV_stdDev - Standard deviation in VV; VV_CV - Coefficient of variation in VV; VH_min - Minimum VH; VH_mean - Mean VH; VH_median - Median VH; VH_max - Maximum VH; VH_stdDev - Standard deviation in VH; VH_CV - Coefficient of variation in VH; ratio_VV_median_VH_median - Ratio between VV_median and VH_median; NDCV - Normalized difference between VV_CV and VH_CV. """ reduced = collection \ .select(['VV', 'VH']) \ .map(lambda image: image.addBands( ee.Image(10).pow(image.divide(10)).rename(['VV_nat', 'VH_nat']) ) ) \ .reduce( ee.Reducer.mean() .combine(ee.Reducer.median(), '', True) .combine(ee.Reducer.stdDev(), '', True) .combine(ee.Reducer.minMax(), '', True) .combine(ee.Reducer.percentile([20, 80]), '', True) ) mosaic = reduced\ .addBands([ reduced.select('VV_median').subtract(reduced.select('VH_median')).rename(['ratio_VV_median_VH_median']) ])\ .addBands([ reduced.select('VV_stdDev').divide(reduced.select('VV_nat_mean')).log10().multiply(10).rename(['VV_CV']) ])\ .addBands([ reduced.select('VH_stdDev').divide(reduced.select('VH_nat_mean')).log10().multiply(10).rename(['VH_CV']) ]) mosaic = mosaic.addBands([ mosaic.normalizedDifference(['VV_CV', 'VH_CV']).rename('NDCV') ]) return mosaic \ .select([ 'VV_min', 'VV_mean', 'VV_median', 'VV_max', 'VV_stdDev', 'VV_CV', 'VH_min', 'VH_mean', 'VH_median', 'VH_max', 'VH_stdDev', 'VH_CV', 'ratio_VV_median_VH_median', 'NDCV' ]) \ .float() \ .clip(region)
def Radians(img): return img.toFloat().multiply(math.pi).divide(180) def Hillshade(az, ze, slope, aspect): """Compute hillshade for the given illumination az, el.""" azimuth = Radians(ee.Image(az)) zenith = Radians(ee.Image(ze)) # Hillshade = cos(Azimuth - Aspect) * sin(Slope) * sin(Zenith) + # cos(Zenith) * cos(Slope) return (azimuth.subtract(aspect).cos().multiply(slope.sin()).multiply( zenith.sin()).add(zenith.cos().multiply(slope.cos()))) terrain = ee.Algorithms.Terrain(ee.Image('srtm90_v4')) slope_img = Radians(terrain.select('slope')) aspect_img = Radians(terrain.select('aspect')) # Add 1 hillshade at az=0, el=60. Map.addLayer(Hillshade(0, 60, slope_img, aspect_img), {}, 'Hillshade') # %% """ ## Display Earth Engine data layers """ # %% Map.addLayerControl() # This line is not needed for ipyleaflet-based Map. Map
## Create an interactive map The default basemap is `Google Maps`. [Additional basemaps](https://github.com/giswqs/geemap/blob/master/geemap/basemaps.py) can be added using the `Map.add_basemap()` function. """ # %% Map = geemap.Map(center=[40, -100], zoom=4) Map # %% """ ## Add Earth Engine Python script """ # %% # Add Earth Engine dataset image = ee.Image('USGS/SRTMGL1_003') # Set visualization parameters. vis_params = { 'min': 0, 'max': 4000, 'palette': ['006633', 'E5FFCC', '662A00', 'D8D8D8', 'F5F5F5'] } # Print the elevation of Mount Everest. xy = ee.Geometry.Point([86.9250, 27.9881]) elev = image.sample(xy, 30).first().get('elevation').getInfo() print('Mount Everest elevation (m):', elev) # Add Earth Engine layers to Map Map.addLayer(image, vis_params, 'DEM')
# get target area sheds = ee.FeatureCollection("WWF/HydroSHEDS/v1/Basins/hybas_9") region = ee.Geometry.Point(REG) if glacier == '_Aru': region = ee.Geometry.Rectangle([82.26, 34.00, 82.36, 34.1]) target = sheds.filterBounds(region) ## 4. Get satellite images # import Landsat 7 & AW3D30 ls8 = ee.ImageCollection("LANDSAT/LC08/C01/T1_TOA")\ .filterBounds(target)\ .filterDate(StartTime,EndTime)\ .filter(ee.Filter.lte('CLOUD_COVER',50)) DEM = ee.Image("JAXA/ALOS/AW3D30/V2_2") print('Number of Images (Landsat 8): ', ls8.size().getInfo()) ## 5. Create water bodies, glaciers and debris cover masks # 1. Import Water Bodies from Global Surface Water WBmask = ee.Image("JRC/GSW1_2/GlobalSurfaceWater")\ .select("max_extent").unmask().focal_max(2).eq(0) # 2. Import Randolph Glacier Inventory rgi15 = ee.FeatureCollection("users/orie_sasaki/15_rgi60_SouthAsiaEast") localRIG15 = rgi15.filterBounds(target) rgi14 = ee.FeatureCollection("users/orie_sasaki/14_rgi60_SouthAsiaWest") localRIG14 = rgi14.filterBounds(target) rgi13 = ee.FeatureCollection("users/orie_sasaki/13_rgi60_CentralAsia") localRIG13 = rgi13.filterBounds(target)
Map.add_basemap('ROADMAP') # Add Google Map Map # %% """ ## Add Earth Engine Python script """ # %% # Add Earth Engine dataset HUC10 = ee.FeatureCollection("USGS/WBD/2017/HUC10") HUC08 = ee.FeatureCollection('USGS/WBD/2017/HUC08') roi = HUC08.filter(ee.Filter.eq('name', 'Pipestem')) Map.centerObject(roi, 10) Map.addLayer(ee.Image().paint(roi, 0, 3), {}, 'HUC08') # select polygons intersecting the roi roi2 = HUC10.filter( ee.Filter.contains(**{ 'leftValue': roi.geometry(), 'rightField': '.geo' })) Map.addLayer(ee.Image().paint(roi2, 0, 2), {'palette': 'blue'}, 'HUC10') # roi3 = HUC10.filter(ee.Filter.stringContains(**{'leftField': 'huc10', 'rightValue': '10160002'})) # # print(roi3) # Map.addLayer(roi3) # %% """
def toNatural(img): return ee.Image(10.0).pow(img.select('..').divide(10.0)).copyProperties( img, ['system:time_start'])
Map # %% """ ## Add Earth Engine Python script """ # %% # Add Earth Engine dataset Map.setCenter(-110, 40, 5) states = ee.FeatureCollection('TIGER/2018/States') # .filter(ee.Filter.eq('STUSPS', 'MN')) # // Turn the strings into numbers states = states.map(lambda f: f.set('STATEFP', ee.Number.parse(f.get('STATEFP')))) state_image = ee.Image().float().paint(states, 'STATEFP') visParams = { 'palette': ['purple', 'blue', 'green', 'yellow', 'orange', 'red'], 'min': 0, 'max': 50, 'opacity': 0.8, }; counties = ee.FeatureCollection('TIGER/2016/Counties') image = ee.Image().paint(states, 0, 2) Map.setCenter(-99.844, 37.649, 5) # Map.addLayer(image, {'palette': 'FF0000'}, 'TIGER/2018/States') Map.addLayer(image, visParams, 'TIGER/2016/States'); Map.addLayer(ee.Image().paint(counties, 0, 1), {}, 'TIGER/2016/Counties')
def __export_dataset_sample (self, sample, sample_num, num_exports): """ Function exports an individual sample to google drive as a TFRecord. These can be combined as a TensorFlow Dataset. Parameters ---------- sample : ee.Feature A feature containing the following propertioes: partition_id: a numerical ID indicating the sample's partition id_list: get sample_num : integer A number which is appended to the file path to identify a sample uniquely. num_exports: integer The total number of exports. This is used when printing info about the export status Returns ------- None. """ # Cast the sample sample = ee.Feature(sample) # Get the partition coordinates from the sample # partition_id = str(ee.Number(sample.get('partition_id')).toInt16().getInfo()) # Get all of the info needed for export sample_ids = ee.List(sample.get("id_list")).getInfo() model_set = ee.String(sample.get("model_set")).getInfo() if model_set == 'test': pass else: print(model_set) # Get rid of the leading characters introduced by previous processing steps sample_ids_trimmed = [] for sentinel_id in sample_ids: sample_ids_trimmed.append(sentinel_id) # Convert the IDs to images scenes = [] alert_date = None for i in range(0, len(sample_ids_trimmed)): # Get the id from the list of ids scene = ee.Image("COPERNICUS/S1_GRD/"+sample_ids_trimmed[i]) # Append the scene to the list scenes.append(scene) # Get the alert date if i == 0: alert_date = scene.date() # Convert the list of images to an ee.ImageCollection and select the correct bands scenes = ee.ImageCollection.fromImages(scenes) \ .select(self.output_bands) \ .sort('system:time_start') # Generate the features features = scenes.toBands().rename(self.model_feature_names) \ .unmask(-50) .unitScale(-50, 1) # Load the GLAD Alert for the scene print(alert_date.get('year').getInfo(), alert_date.get('month').getInfo(), alert_date.get('day').getInfo()) label = self.__retrieve_label(alert_date) # Stack the outputs labels_and_features = ee.Image.cat([features, label]).toFloat() # Run the sampling output = self.__sample_model_data(labels_and_features, sample.geometry()) # Create the export filename file_name = 'alert_record' + '_' + str(sample_num+1) # Export an image clip_geometry = sample.geometry().buffer(self.kernel_size / 2, ee.ErrorMargin(1, 'projected'), self.projection.atScale(10)) \ .bounds(ee.ErrorMargin(1, 'projected'), self.projection.atScale(10)) image_task = ee.batch.Export.image.toDrive( image = labels_and_features.clip(clip_geometry).toFloat(), description = 'Export-GLAD-Label-' + str(sample_num+1), folder = 'GLAD_TEST', fileNamePrefix = file_name, scale = 10, maxPixels = 1e13 ) image_task.start() # Initiate the export task = ee.batch.Export.table.toCloudStorage( collection = ee.FeatureCollection([output]), description = "Export-Mekong-Alert-" + str(sample_num), bucket = self.gcs_bucket, fileNamePrefix = self.gcs_export_folder + '/' + model_set + '/' + file_name, fileFormat = "TFRecord", selectors = labels_and_features.bandNames().getInfo() ) # Check if the number of output features is correct # Number should be: self.model_feature_names + 2 target_num_properties = len(self.model_feature_names) + 2 output_num_properties = output.propertyNames().length().getInfo() if target_num_properties == output_num_properties: print('Initating '+str(sample_num+1)+' of '+str(num_exports)) # task.start() else: print('Export for index ' + str(sample_num+1) + ' had too few features.') # Log the info in the exporter self.export_monitor.add_task('export_'+str(sample_num), task) return None