Exemple #1
0
def convert(feature):
    res = ee.Feature(None,feature.toDictionary())
    return(res)
Exemple #2
0
def getWRS(feature):
    return ee.Feature(feature).get('PR')
Exemple #3
0
#!/usr/bin/env python
"""Create and render a feature collection from polygons."""

import ee
from ee_plugin import Map

Map.setCenter(-107, 41, 6)

fc = ee.FeatureCollection([
    ee.Feature(
        ee.Geometry.Polygon([[-109.05, 41], [-109.05, 37], [-102.05, 37],
                             [-102.05, 41]]), {
                                 'name': 'Colorado',
                                 'fill': 1
                             }),
    ee.Feature(
        ee.Geometry.Polygon([[-114.05, 37.0], [-109.05, 37.0], [-109.05, 41.0],
                             [-111.05, 41.0], [-111.05, 42.0],
                             [-114.05, 42.0]]), {
                                 'name': 'Utah',
                                 'fill': 2
                             })
])

# Fill, then outline the polygons into a blank image.
image1 = ee.Image(0).mask(0).toByte()
image2 = image1.paint(fc, 'fill')  # Get color from property named 'fill'
image3 = image2.paint(fc, 3, 5)  # Outline using color 3, width 5.

Map.addLayer(image3, {
    'palette': ['000000', 'FF0000', '00FF00', '0000FF'],
Exemple #4
0
def shapelyToEEFeature(row):
    properties = row.drop(["geometry"]).to_dict()
    geoJSONfeature = geojson.Feature(geometry=row["geometry"],
                                     properties=properties)
    return ee.Feature(geoJSONfeature)
Exemple #5
0
 def _reduce_region(image):
     stat_dict = image.reduceRegion(ee.Reducer.mean(), region, scale);
     return ee.Feature(None, stat_dict)
Exemple #6
0
def makefeature(data):
    ''' for exporting as CSV to Drive '''
    return ee.Feature(None, {'data': data})
    def _BuildDataSet(self, sampling_factor, normalize):

        estimation_set = self.img_est.sample(region=self.region,
                                             factor=sampling_factor,
                                             seed=self.seed)

        prediction_set = self.img_pred.sample(region=self.region,
                                              factor=sampling_factor,
                                              seed=self.seed)
        # Add weights
        estimation_set_size = ee.Number(estimation_set.size())
        prediction_set_size = ee.Number(prediction_set.size())
        peso_estimacion = ee.Number(self.beta).divide(estimation_set_size)
        peso_prediccion = ee.Number(1 - self.beta).divide(prediction_set_size)

        bands_modeling_estimation_input_weight = list(
            self.bands_modeling_estimation_input)
        bmp = list(self.bands_modeling_prediction)
        bmp.append("weight")
        bme = list(self.bands_modeling_estimation)
        bme.append("weight")

        estimation_set = estimation_set.map(
            lambda ft: ee.Feature(ft).set("weight", peso_estimacion))
        prediction_set = prediction_set.map(
            lambda ft: ee.Feature(ft).set("weight", peso_prediccion))
        bands_modeling_estimation_input_weight.append("weight")

        self.estimation_set = estimation_set
        self.prediction_set = prediction_set
        if (self.beta > 0) and (self.cc_image < CC_IMAGE_TOP):
            self.datos = estimation_set.merge(prediction_set.select(bmp, bme))
        else:
            logger.info("Using only prediction")
            self.datos = prediction_set.select(bmp, bme)
        if normalize:
            self.datos, self.inputs_mean, self.inputs_std = normalization.ComputeNormalizationFeatureCollection(
                self.datos,
                self.bands_modeling_estimation_input,
                only_center_data=False,
                weight="weight")
            self.datos, self.outputs_mean, self.outputs_std = normalization.ComputeNormalizationFeatureCollection(
                self.datos,
                self.bands_modeling_estimation_output,
                only_center_data=True,
                weight="weight")

            self.inputs_mean = self.inputs_mean.toArray(
                self.bands_modeling_estimation_input)
            self.inputs_std = self.inputs_std.toArray(
                self.bands_modeling_estimation_input)
            self.outputs_mean = self.outputs_mean.toArray(
                self.bands_modeling_estimation_output)

            #if "B10" in self.bands_modeling_estimation_output:
            #    self.datos.select("B10").divide(100)
            #if "B11" in self.bands_modeling_estimation_output:
            #    output_dataset["B11"] /= 100

        self.inputs = self.datos.select(bands_modeling_estimation_input_weight)
        self.outputs = self.datos.select(self.bands_modeling_estimation_output)

        return
def getGeom(image):
    return ee.Feature(image.geometry().centroid(), {'foo': 1})
Exemple #9
0
    def getMinMaxCoord(feature):
        listCoords = ee.Array.cat(feature.geometry().coordinates(), 1)
        xCoords = listCoords.slice(1, 0, 1)
        yCoords = listCoords.slice(1, 1, 2)
        return feature.set({'xMin':xCoords.reduce('min', [0]).get([0,0]),\
                            'yMin':yCoords.reduce('min', [0]).get([0,0])})

    #Map the area getting function over the FeatureCollection.
    chunks = chunks.map(getMinMaxCoord)

    ## for loop over each chunk
    chunklist = chunks.toList(chunks.size())

    start = time.time()
    for ic in range(chunks.size().getInfo()):
        ROI = ee.Feature(chunklist.get(ic))
        print('\nProcessing SSC_ann_' + write_day + '_chunk' + str(ic + 1) +
              '.asc')

        #Print the first feature from the collection with the added property.
        xMin = ROI.get('xMin').getInfo()
        #~ #print('xmin:',xMin)
        yMin = ROI.get('yMin').getInfo()
        #~ #print('ymin:',yMin)

        # write to ascii

        xllcorner = xMin
        yllcorner = yMin
        cellsize = 0.00179  #for 200m     (0.00091 for 100m,for 30m use 0.000277778)
        nodata = -9999.0
    bands = [k[1:] for k in config.desired_bands[sat]]
    bands = ''.join(bands)

    # name = '_'.join([spacecraft, date,  tile, bands, cc])
    name = '_'.join([spacecraft, date, tile, cc])

    return name


basic.cwa(ee.Initialize)

crs_descriptor = basic.CRS(crs=config.crs, scale=config.scale)

if config.geometry is not None:
    fc = ee.FeatureCollection(config.geometry)
    config.geometry = ee.Feature(fc.iterate(uni, ee.Feature(fc.first())))
    config.geometry = config.geometry.geometry()
    if config.geometry_buff is not None:
        config.geometry = config.geometry.buffer(config.geometry_buff)

if config.qa:
    print 'Exporting QA as band 4'
    config.desired_bands = {
        i: config.desired_bands[i] + ['BQA']
        for i in config.desired_bands
    }

tasks = {}

collections = {
    '8': 'LANDSAT/LC08/C01/T1_SR',
def sampling(sample):
    lat = sample.get('latitude')
    lon = sample.get('longitude')
    ch4 = sample.get('ch4')
    return ee.Feature(ee.Geometry.Point([lon, lat]), {'ch4': ch4})
Exemple #12
0
def export_oneimage(img,location,name,scale,crs):
    task = ee.batch.Export.image(img, name, {
      'driveFolder':location,
      'driveFileNamePrefix':name,
      'outputBucket': location,
      'outputPrefix': name,
      'scale':scale,
      'crs':crs
    })
    task.start()
    while task.status()['state'] == 'RUNNING':
    print 'Running...'
    # Perhaps task.cancel() at some point.
    time.sleep(10)
    print 'Done.', task.status()




def appendBand(current, previous):
    # Rename the band
    previous=ee.Image(previous)
    current = current.select([0,1,2,3,4,5,6])
    # Append it to the result (Note: only return current item on first element/iteration)
    accum = ee.Algorithms.If(ee.Algorithms.IsEqual(previous,None), current, previous.addBands(ee.Image(current)))
    # Return the accumulation
    return accum


fusion_table_dict = {
#    'ET_admin2': ("users/tkenned97/ET_Admin2_simp_2k",'2001-1-1','2016','ADMIN2'),
#     'TZ_admin1': ("users/tkenned97/TZ_Admin1_simp_2k", '2000-2-18','2015','ADMIN1'),
#     'ZM_admin2': ("users/tkenned97/ZM_Admin2_simp_2k",'2000-2-18','2017','ADMIN2'),
#     'NG_admin1': ("users/tkenned97/NG_Admin1_simp_2k",'2000-2-18','2017','ADMIN1'),
#     'MW_admin2': ("users/tkenned97/MW_Admin2_simp_2k",'2000-2-18','2017','ADMIN2'),
#     'KE_admin2': ("users/tkenned97/KE_Admin2_simp_2k",'2000-2-18','2016','ADMIN2')
      'SU_admin2': ('users/akkstuff/sudan_admin2', '2013-1-1', '2018', 'ADM2_EN')
}





for country_key,val in fusion_table_dict.items():
    fid, start_date, end_date, admin_type = val
    region = ee.FeatureCollection(fid)
    imgcoll = ee.ImageCollection('MODIS/006/MOD09A1').filterDate(start_date, end_date + '-12-31').sort('system:time_start').filterBounds(ee.Geometry.Rectangle(-22, 38, 60, -38))

    img=imgcoll.iterate(appendBand)
    img=ee.Image(img)

    img_0=ee.Image(ee.Number(0))
    img_5000=ee.Image(ee.Number(5000))

    img=img.min(img_5000)
    img=img.max(img_0)

    scale  = 500
    crs='EPSG:4326'

    raw_feature_data = list(region.toList(9999999).getInfo())
    feature_names = []
    for x in raw_feature_data:
        feature_names.append(x['properties'][admin_type])


    for name in feature_names:
        feature = region.filterMetadata(admin_type, 'equals', name)

        if "'" in name:
            name = name.replace("'","")
        if "/" in name:
            name = name.replace("/","")

        feature = ee.Feature(feature.first())
        export_oneimage(img.clip(feature), 'africa-yield', country_key + "_" + name, scale, crs)
    def export(self,
               folder_name,
               data_type,
               coordinate_system='EPSG:4326',
               scale=500,
               export_limit=None,
               min_img_val=None,
               max_img_val=None,
               major_states_only=True,
               check_if_done=False,
               download_folder=None):
        """Export an Image Collection from Earth Engine to Google Drive

        Parameters
        ----------
            folder_name: str
                The name of the folder to export the images to in
                Google Drive. If the folder is not there, this process
                creates it
            data_type: str {'image', 'mask', 'temperature'}
                The type of data we are collecting. This tells us which bands to collect.
            coordinate_system: str, default='EPSG:4326'
                The coordinate system in which to export the data
            scale: int, default=500
                The pixel resolution, as determined by the output.
                https://developers.google.com/earth-engine/scale
            export_limit: int or None, default=None
                If not none, limits the number of files exported to the value
                passed.
            min_img_val = int or None:
                A minimum value to clip the band values to
            max_img_val: int or None
                A maximum value to clip the band values to
            major_states_only: boolean, default=True
                Whether to only use the 11 states responsible for 75 % of national soybean
                production, as is done in the paper
            check_if_done: boolean, default=False
                If true, will check download_folder for any .tif files which have already been
                downloaded, and won't export them again. This effectively allows for
                checkpointing, and prevents all files from having to be downloaded at once.
            download_folder: None or pathlib Path, default=None
                Which folder to check for downloaded files, if check_if_done=True. If None, looks
                in data/folder_name
        """
        if check_if_done:
            if download_folder is None:
                download_folder = Path('data') / folder_name
                already_downloaded = get_tif_files(download_folder)

        imgcoll = ee.ImageCollection(self.collection_id) \
            .filterBounds(ee.Geometry.Rectangle(-106.5, 50, -64, 23)) \
            .filterDate('2002-12-31', '2016-8-4')

        datatype_to_func = {
            'image': _append_im_band,
            'mask': _append_mask_band,
            'temperature': _append_temp_band,
        }

        img = imgcoll.iterate(datatype_to_func[data_type])
        img = ee.Image(img)

        # "clip" the values of the bands
        if min_img_val is not None:
            # passing en ee.Number creates a constant image
            img_min = ee.Image(ee.Number(min_img_val))
            img = img.min(img_min)
        if max_img_val is not None:
            img_max = ee.Image(ee.Number(max_img_val))
            img = img.max(img_max)

        # note that the county regions are pulled from Google's Fusion tables. This calls a merge
        # of county geometry and census data:
        # https://fusiontables.google.com/data?docid=1S4EB6319wWW2sWQDPhDvmSBIVrD3iEmCLYB7nMM#rows:id=1

        region = ee.FeatureCollection('TIGER/2018/Counties')

        # turn the strings into numbers, see
        # https://developers.google.com/earth-engine/datasets/catalog/TIGER_2018_Counties
        def state_to_int(feature):
            return feature.set('COUNTYFP',
                               ee.Number.parse(feature.get('COUNTYFP')))

        region = region.map(state_to_int)

        count = 0

        for state_id, county_id in np.unique(
                self.locations[['State ANSI', 'County ANSI']].values, axis=0):
            if major_states_only:
                if int(state_id) not in MAJOR_STATES:
                    print(f'Skipping state id {int(state_id)}')
                    continue

            fname = '{}_{}'.format(int(state_id), int(county_id))

            if check_if_done:
                if f'{fname}.tif' in already_downloaded:
                    print(f'{fname}.tif already downloaded! Skipping')
                    continue

            file_region = region.filterMetadata('COUNTYFP', 'equals',
                                                int(county_id))
            file_region = ee.Feature(file_region.first())
            processed_img = img.clip(file_region)
            file_region = None
            while True:
                try:
                    self._export_one_image(processed_img, folder_name, fname,
                                           file_region, scale,
                                           coordinate_system)
                except (ee.ee_exception.EEException, ssl.SSLEOFError):
                    print(
                        f'Retrying State {int(state_id)}, County {int(county_id)}'
                    )
                    time.sleep(10)
                    continue
                break

            count += 1
            if export_limit:
                if count >= export_limit:
                    print('Reached export limit! Stopping')
                    break
        print(f'Finished Exporting {count} files!')
Exemple #14
0
        'collection': l8.filterDate('2018-01-01', '2018-12-31'),
        'asFloat': True
    })

# Use these bands for prediction.
bands = ['B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B10', 'B11']

# Manually created polygons.
forest1 = ee.Geometry.Rectangle(-63.0187, -9.3958, -62.9793, -9.3443)
forest2 = ee.Geometry.Rectangle(-62.8145, -9.206, -62.7688, -9.1735)
nonForest1 = ee.Geometry.Rectangle(-62.8161, -9.5001, -62.7921, -9.4486)
nonForest2 = ee.Geometry.Rectangle(-62.6788, -9.044, -62.6459, -8.9986)

# Make a FeatureCollection from the hand-made geometries.
polygons = ee.FeatureCollection([
    ee.Feature(nonForest1, {'class': 0}),
    ee.Feature(nonForest2, {'class': 0}),
    ee.Feature(forest1, {'class': 1}),
    ee.Feature(forest2, {'class': 1}),
])

# Get the values for all pixels in each polygon in the training.
training = image.sampleRegions(
    **{
        # Get the sample from the polygons FeatureCollection.
        'collection': polygons,
        # Keep this list of properties from the polygons.
        'properties': ['class'],
        # Set the scale to get Landsat pixels in the polygons.
        'scale': 30
    })
Map

# %%
"""
## Add Earth Engine Python script 
"""

# %%
# Add Earth Engine dataset
# Feature buffer example.
# Display the area within 2 kilometers of San Francisco BART stations.

# Instantiate a FeatureCollection of BART locations in Downtown San Francisco
# (points).
stations = [
    ee.Feature(ee.Geometry.Point(-122.42, 37.77),
               {'name': '16th St. Mission (16TH)'}),
    ee.Feature(ee.Geometry.Point(-122.42, 37.75),
               {'name': '24th St. Mission (24TH)'}),
    ee.Feature(ee.Geometry.Point(-122.41, 37.78),
               {'name': 'Civic Center/UN Plaza (CIVC)'})
]
bartStations = ee.FeatureCollection(stations)

# Map a function over the collection to buffer each feature.


def func_dky(f):
    return f.buffer(2000, 100)
    # Note that the errorMargin is set to 100.

Exemple #16
0
bio = ee.Image('WORLDCLIM/V1/BIO')
srtm = ee.Image('CGIAR/SRTM90_V4')

bio01 = bio.select('bio01')
bio12 = bio.select('bio12')

# downscale env data
bio01 = downscale(bio01).unmask(0)
bio12 = downscale(bio12).unmask(0)
srtm = downscale(srtm).unmask(0)

## merge env data
envdata = adddata(srtm)

#get centroid polygon
tile_point = ee.Feature(ee.Geometry.Point(geom.centroid().coordinates()))

##create kernel
size = args.kernelsize
weights = ee.List.repeat(ee.List.repeat(1, size), size)
kernel = ee.Kernel.fixed(size, size, weights)

##Sample pixels in the ImageCollection at these random points
values = kernelnb(envdata)

task = ee.batch.Export.table.toCloudStorage(
    collection=values,
    description=name,
    folder=None,
    fileNamePrefix=args.folder + '/raw/' + str(size) + '/env_data/' + name,
    fileFormat='TFRecord',
Exemple #17
0
 def _reduce_region(image):
     """Spatial aggregation function for a single image and a polygon feature"""
     stat_dict = image.reduceRegion(fun, geometry, 30);
     # FEature needs to be rebuilt because the backend doesn't accept to map
     # functions that return dictionaries
     return ee.Feature(None, stat_dict)
Exemple #18
0
 def contructXGrid(j):
     j = ee.Number(j)
     box = ee.Feature(ee.Geometry.Rectangle(j,i,j.add(gridSize),i.add(gridSize)))
     out = ee.Algorithms.If(geom.contains(box.geometry()),box,null)
     return ee.Feature(out)
Exemple #19
0
def MultiPointFromQuery(query, geocoder="nominatim", **kwargs):
    """Constructs an ee.Feature describing a point from a query submitted to a geodocer using the geopy package. This returns all pairs of coordinates retrieved by the query.
    The properties of the feature collection correspond to the raw properties retrieved by the locations of the query.

    Tip
    ----------
    Check more info about constructors in the :ref:`User Guide<Constructors>`.

    Parameters
    ----------
    query : str
        Address, query or structured query to geocode.
    geocoder : str, default = 'nominatim'
        Geocoder to use. Please visit https://geopy.readthedocs.io/ for more info.
    **kwargs :
        Keywords arguments for geolocator.geocode(). The user_agent argument is mandatory (this argument can be set as user_agent = 'my-gee-username' or
        user_agent = 'my-gee-app-name'). Please visit https://geopy.readthedocs.io/ for more info.

    Returns
    -------
    ee.FeatureCollection
        Feature Collection with point geometries from the specified query.

    Examples
    --------
    >>> import ee, eemont
    >>> ee.Authenticate()
    >>> ee.Initialize()
    >>> ee.FeatureCollection.MultiPointFromQuery('Río Amazonas',user_agent = 'my-gee-eemont-query').getInfo()
    {'type': 'FeatureCollection',
     'columns': {'boundingbox': 'List<String>',
      'class': 'String',
      'display_name': 'String',
      'importance': 'Float',
      'lat': 'String',
      'licence': 'String',
      'lon': 'String',
      'osm_id': 'Integer',
      'osm_type': 'String',
      'place_id': 'Integer',
      'system:index': 'String',
      'type': 'String'},
     'features': [{'type': 'Feature',
       'geometry': {'type': 'Point', 'coordinates': [-57.4801276, -2.3740229]},
       'id': '0',
       'properties': {'boundingbox': ['-4.4421898',
         '0.7065296',
         '-73.4501259',
         '-49.2759133'],
        'class': 'waterway',
        'display_name': 'Rio Amazonas, Região Norte, 69100-143, Brasil',
        'importance': 0.4,
        'lat': '-2.3740229',
        'licence': 'Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright',
        'lon': '-57.4801276',
        'osm_id': 2295651,
        'osm_type': 'relation',
        'place_id': 258650987,
        'type': 'river'}},
      {'type': 'Feature',
       'geometry': {'type': 'Point',
        'coordinates': [-70.04978704421745, -4.10958645]},
       'id': '1',
       'properties': {'boundingbox': ['-4.2647706',
         '-3.9548576',
         '-70.1817875',
         '-69.9440055'],
        'class': 'natural',
        'display_name': 'Río Amazonas, Ramón Castilla, Mariscal Ramón Castilla, Loreto, Perú',
        'importance': 0.39999999999999997,
        'lat': '-4.10958645',
        'licence': 'Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright',
        'lon': '-70.04978704421745',
        'osm_id': 8495385,
        'osm_type': 'relation',
        'place_id': 297654614,
        'type': 'water'}},
      ...]}
    """
    locations = _retrieve_location(query, geocoder, False, **kwargs)

    features = []

    for location in locations:
        geometry = ee.Geometry.Point([location.longitude, location.latitude])
        feature = ee.Feature(geometry, location.raw)
        features.append(feature)

    return ee.FeatureCollection(features)
Exemple #20
0
def bmaxOtsu(collection,target_data,region,
             smoothing=100,
             qualityBand=None,
             reductionScale=90,
             initThresh=0,
             reverse=False,
             gridSize=0.1,
             bmaxThresh=0.75,
             maxBoxes=100,
             seed=7):

    def constuctGrid(i):
        def contructXGrid(j):
            j = ee.Number(j)
            box = ee.Feature(ee.Geometry.Rectangle(j,i,j.add(gridSize),i.add(gridSize)))
            out = ee.Algorithms.If(geom.contains(box.geometry()),box,null)
            return ee.Feature(out)
        i = ee.Number(i)
        out = ee.List.sequence(west,east.subtract(gridSize),gridSize).map(constuctXGrid)
      return out


    def calcBmax(feature):
        segment = target.clip(feature)
        initial = segment.lt(initThresh)
        p1 = ee.Number(initial.reduceRegion(
            reducer= ee.Reducer.mean(),
            geometry= feature.geometry(),
            bestEffort= True,
            scale= reductionScale,
        ).get(histBand))
        p2 = ee.Number(1).subtract(p1)

        m = segment.updateMask(initial).rename('m1').addBands(
            segment.updateMask(initial.not()).rename('m2')
        )

        mReduced = m.reduceRegion(
            reducer= ee.Reducer.mean(),
            geometry= feature.geometry(),
            bestEffort= True,
            scale= reductionScale,
        )

        m1 = ee.Number(mReduced.get('m1'))
        m2 = ee.Number(mReduced.get('m2'))

        m1 = ee.Number(ee.Algorithms.If(m1,m1,globalLow))
        m2 = ee.Number(ee.Algorithms.If(m2,m2,globalHigh))

        sigmab = p1.multiply(p2.multiply(m1.subtract(m2).pow(2)))
        sigmat = ee.Number(segment.reduceRegion(
            reducer= ee.Reducer.variance(),
            geometry= feature.geometry(),
            bestEffort= True,
            scale= reductionScale,
        ).get(histBand))
        bmax = sigmab.divide(sigmat)
        return feature.set({'bmax':bmax})


    tDate = ee.Date(target_date)
    targetColl = collection.filterDate(tDate,tDate.advance(1,'day'))

    if qualityBand == None:
        histBand = ee.String(target.bandNames().get(0))
        target = targetColl.mosaic()\
            .select(histBand)
    else:
        histBand = ee.String(qualityBand)
        target = targetColl.qualityMosaic(qualityBand)\
            .select(histBand)

    searchRegion = ee.Feature(ee.List(targeColl.map(geeutilsgetGeom).toList(1)).get(0))

    theoretical = target.reduceRegion(
        reducer= ee.Reducer.percentile([10,90]),
        geometry= searchRegion.geometry(),
        bestEffort= True,
        scale: 5000
    )
    globalLow = theoretical.get(histBand.cat('_p10'))
    globalHigh = theoretical.get(histBand.cat('_p90'))

    geom = searchRegion.geometry()
    bounds = geom.bounds()
    coords = ee.List(bounds.coordinates().get(0))
    gridSize = ee.Number(gridSize)

    west = ee.Number(ee.List(coords.get(0)).get(0))
    south = ee.Number(ee.List(coords.get(0)).get(1))
    east = ee.Number(ee.List(coords.get(2)).get(0))
    north = ee.Number(ee.List(coords.get(2)).get(1))

    west = west.subtract(west.mod(gridSize))
    south = south.subtract(south.mod(gridSize))
    east = east.add(gridSize.subtract(east.mod(gridSize)))
    north = north.add(gridSize.subtract(north.mod(gridSize)))

    grid = ee.FeatureCollection(
      ee.List.sequence(south,north.subtract(gridSize),gridSize).map(constuctGrid).flatten()
    )

    bmaxes = grid.map(calcBmax).filter(ee.Filter.gt('bmax',bmaxThresh)).randomColumn('random',seed)

    nBoxes = ee.Number(bmax.size())
    randomThresh = maxBoxes.divide(nBoxes)
    selection = bmaxes.filter(ee.Filter.lt('random',randomThresh))

    histogram =  histogram_image.reduceRegion(ee.Reducer.histogram(255, 2)\
                                .combine('mean', None, True)\
                                .combine('variance', None,True),selection,reductionScale,bestEffort=True,
                                tileScale=16)

    threshold = geeutils.otsu_function(histogram.get(histBand.cat('_histogram')))

    water = target.gt(threshold).clip(geeutils.LAND.geometry())

    return water.rename('water')
Exemple #21
0
                                        [180, 90], [-180, 90]],
                                proj=ee.Projection('EPSG:4326'),
                                geodesic=True)

# In[ ]:

# In[22]:

geometry3 = ee.Geometry.Polygon(coords=[[-170.0, -80.0], [170, -80.0],
                                        [170, 80], [-170, 80]],
                                proj=ee.Projection('EPSG:4326'),
                                geodesic=True)

# In[23]:

feature = ee.Feature(geometry3, {})

# In[24]:

geoJSON = feature.geometry().getInfo()

# In[25]:

print(geoJSON)

# In[18]:

geoJSON["geodesic"] = False

# In[19]:
Exemple #22
0
 def spatialSelect(feature):
     test = ee.Algorithms.If(geom.contains(feature.geometry()), feature,
                             None)
     return ee.Feature(test)
Exemple #23
0
def noGeometryEEFeature(row):
    properties = row.to_dict()
    return ee.Feature(None, properties)
def get_region(feature):

    coll = LS_COLL.filterBounds(ee.Feature(feature).geometry())
    ncoll = coll.size().getInfo()

    # feature to be extracted
    feat = ee.Algorithms.If(
        ee.Number(BUFFER_DIST).gt(0),
        ee.Feature(feature).buffer(BUFFER_DIST).bounds(), ee.Feature(feature))

    feat_dict = feat.getInfo()
    feat_props = feat_dict['properties']

    pt_list = list()

    if ncoll > 0:

        # list of properties of each image in the collection as dictionary
        coll_dicts = get_coll_dict(coll).getInfo()

        # extract pixel values from the collection
        # the output is a list or lists with the first row as column names
        temp_list = coll.getRegion(ee.Feature(feat).geometry(),
                                   SCALE).getInfo()
        n = len(temp_list)

        elem_names = temp_list[0]
        id_column = elem_names.index('id')

        for j in range(1, n):
            pt_dict = dict()
            for k in range(0, len(elem_names)):
                pt_dict[elem_names[k]] = temp_list[j][k]
                id = temp_list[j][id_column]

                scene_prop_list = list(coll_dict for coll_dict in coll_dicts
                                       if coll_dict['id'] == id)
                scene_prop = scene_prop_list[0]

                for prop in scene_properties:
                    pt_dict[prop] = scene_prop[prop]

                for prop in feat_properties:
                    pt_dict[prop] = feat_props[prop]

            pt_list.append(pt_dict)

    else:
        pt_dict = dict()
        pt_dict['id'] = NULL_VALUE
        pt_dict['time'] = NULL_VALUE

        for prop in feat_properties:
            pt_dict[prop] = feat_props[prop]

        for prop in scene_properties:
            pt_dict[prop] = NULL_VALUE

        for band in bands:
            pt_dict[band] = NULL_VALUE

        pt_list.append(pt_dict)

    return pt_list
import ee 
import geemap

# Create a map centered at (lat, lon).
Map = geemap.Map(center=[40, -100], zoom=4)

# Make a list of Features.
features = [
  ee.Feature(ee.Geometry.Rectangle(30.01, 59.80, 30.59, 60.15), {'name': 'Voronoi'}),
  ee.Feature(ee.Geometry.Point(-73.96, 40.781), {'name': 'Thiessen'}),
  ee.Feature(ee.Geometry.Point(6.4806, 50.8012), {'name': 'Dirichlet'})
]

# Create a FeatureCollection from the list and print it.
fromList = ee.FeatureCollection(features)
print(fromList.getInfo())

# Create a FeatureCollection from a single geometry and print it.
fromGeom = ee.FeatureCollection(ee.Geometry.Point(16.37, 48.225))
print(fromGeom.getInfo())

# Display the map.
Map
def reclass_list_to_fc(dictionary):
    return ee.Feature(None, dictionary)
Exemple #27
0
 def over_list(p):
     p = ee.List(p)
     point = ee.Geometry.Point(p).buffer(buffer).bounds()
     return ee.Feature(point)
Exemple #28
0
def dict_to_feature(dictje):
    return ee.Feature(None,dictje)
## Add Earth Engine Python script 
"""


# %%
# Add Earth Engine dataset
# This function creates a new property that is the sum of two existing properties.
def addField(feature):
    sum = ee.Number(feature.get('property1')).add(feature.get('property2'))
    return feature.set({'sum': sum})


# Create a FeatureCollection from a list of Features.
features = ee.FeatureCollection([
    ee.Feature(ee.Geometry.Point(-122.4536, 37.7403), {
        'property1': 100,
        'property2': 100
    }),
    ee.Feature(ee.Geometry.Point(-118.2294, 34.039), {
        'property1': 200,
        'property2': 300
    }),
])

# Map the function over the collection.
featureCollection = features.map(addField)

# Print the entire FeatureCollection.
print(featureCollection.getInfo())

# Print a selected property of one Feature.
print(featureCollection.first().get('sum').getInfo())
Exemple #30
0
def ee_get_s2_data(AOIs, req_params, qi_threshold=0, qi_filter=s2_filter1):
    """Get S2 data (level L2A, bottom of atmosphere data) from GEE.

    Parameters
    ----------
    AOIs : list or AOI instance
        List of AOI instances or single AOI instance. If multiple AOIs
        proviveded the computation in GEE server is parallellized.
        If too many areas with long time range is provided, user might
        hit GEE memory limits. Then you should call this function
        sequentally to all AOIs. AOIs should have qi attribute computed first.
    req_params : S2RequestParams instance
        S2RequestParams instance with request details.
    qi_threshold : float
        Threshold value to filter images based on used qi filter.
        qi filter holds labels of classes whose percentages within the AOI
        is summed. If the sum is larger then the qi_threhold, data will not be
        retrieved for that date/image. The default is 1, meaning all data is
        retrieved.
    qi_filter : list
        List of strings with class labels (of unwanted classes) used to compute qi value,
        see qi_threhold. The default is s2_filter1 = ['NODATA',
              'SATURATED_DEFECTIVE',
              'CLOUD_SHADOW',
              'UNCLASSIFIED',
              'CLOUD_MEDIUM_PROBA',
              'CLOUD_HIGH_PROBA',
              'THIN_CIRRUS',
              'SNOW_ICE'].

    Returns
    -------
    Nothing:
        Computes data attribute for the given AOI instances.

    """
    datestart = req_params.datestart
    dateend = req_params.dateend
    bands = req_params.bands
    # if single AOI instance, make a list
    if isinstance(AOIs, AOI):
        AOIs = list([AOIs])

    features = []
    for a in AOIs:
        filtered_qi = filter_s2_qi_dataframe(a.qi, qi_threshold, qi_filter)
        if len(filtered_qi) == 0:
            print("No observations to retrieve for area %s" % a.name)
            continue

        if a.tile is None:
            min_tile = min(filtered_qi["tileid"].values)
            filtered_qi = filtered_qi[filtered_qi["tileid"] == min_tile]
            a.tile = min_tile
        else:
            filtered_qi = filtered_qi[filtered_qi["tileid"] == a.tile]

        full_assetids = "COPERNICUS/S2_SR/" + filtered_qi["assetid"]
        image_list = [ee.Image(asset_id) for asset_id in full_assetids]
        crs = filtered_qi["projection"].values[0]["crs"]
        if a.geometry_type == "Polygon":
            feature = ee.Feature(
                ee.Geometry.Polygon(a.coordinate_list),
                {
                    "name": a.name,
                    "image_list": image_list
                },
            )
        else:
            feature = ee.Feature(
                ee.Geometry.Point(a.coordinate_list[0][0],
                                  a.coordinate_list[0][1]),
                {
                    "name": a.name,
                    "image_list": image_list
                },
            )

        features.append(feature)

    if len(features) == 0:
        print("No data to be retrieved!")
        return None

    feature_collection = ee.FeatureCollection(features)

    def ee_get_s2_data_feature(feature):
        geom = feature.geometry(0.01, crs)
        image_collection = (
            ee.ImageCollection.fromImages(
                feature.get("image_list")).filterBounds(geom).filterDate(
                    datestart, dateend)
            #      .select(bands + ["SCL"])
        )
        image_collection = image_collection.map(calc_ndvi(
            nir="B8A", red="B4")).select(bands + ["SCL", "NDVI"])

        def ee_get_s2_data_image(img):
            # img = img.clip(geom)
            productid = img.get("PRODUCT_ID")
            assetid = img.id()
            tileid = img.get("MGRS_TILE")
            system_index = img.get("system:index")
            proj = img.select(bands[0]).projection()
            sun_azimuth = img.get("MEAN_SOLAR_AZIMUTH_ANGLE")
            sun_zenith = img.get("MEAN_SOLAR_ZENITH_ANGLE")
            view_azimuth = (ee.Array([
                img.get("MEAN_INCIDENCE_AZIMUTH_ANGLE_%s" % b) for b in bands
            ]).reduce(ee.Reducer.mean(), [0]).get([0]))
            view_zenith = (ee.Array([
                img.get("MEAN_INCIDENCE_ZENITH_ANGLE_%s" % b) for b in bands
            ]).reduce(ee.Reducer.mean(), [0]).get([0]))

            img = img.resample("bilinear").reproject(crs=crs,
                                                     scale=req_params.scale)

            # get the lat lon and add the ndvi
            image_grid = ee.Image.pixelCoordinates(
                ee.Projection(crs)).addBands(
                    [img.select(b) for b in bands + ["SCL", "NDVI"]])

            # apply reducer to list
            image_grid = image_grid.reduceRegion(
                reducer=ee.Reducer.toList(),
                geometry=geom,
                maxPixels=1e8,
                scale=req_params.scale,
            )

            # get data into arrays
            x_coords = ee.Array(image_grid.get("x"))
            y_coords = ee.Array(image_grid.get("y"))
            band_data = {b: ee.Array(image_grid.get("%s" % b)) for b in bands}

            scl_data = ee.Array(image_grid.get("SCL"))
            ndvi_data = ee.Array(image_grid.get("NDVI"))

            # perform LAI et al. computation possibly here!

            tmpfeature = (ee.Feature(ee.Geometry.Point([0, 0])).set(
                "productid", productid).set("system_index", system_index).set(
                    "assetid",
                    assetid).set("tileid", tileid).set("projection", proj).set(
                        "sun_zenith",
                        sun_zenith).set("sun_azimuth", sun_azimuth).set(
                            "view_zenith",
                            view_zenith).set("view_azimuth", view_azimuth).set(
                                "x_coords",
                                x_coords).set("y_coords", y_coords).set(
                                    "SCL",
                                    scl_data).set("NDVI",
                                                  ndvi_data).set(band_data))
            return tmpfeature

        s2_data_feature = image_collection.map(ee_get_s2_data_image)

        return (feature.set(
            "productid", s2_data_feature.aggregate_array("productid")).set(
                "system_index",
                s2_data_feature.aggregate_array("system_index")).set(
                    "assetid", s2_data_feature.aggregate_array("assetid")).set(
                        "tileid",
                        s2_data_feature.aggregate_array("tileid")).set(
                            "projection",
                            s2_data_feature.aggregate_array("projection")).set(
                                "sun_zenith",
                                s2_data_feature.aggregate_array("sun_zenith")).
                set("sun_azimuth",
                    s2_data_feature.aggregate_array("sun_azimuth")).set(
                        "view_zenith",
                        s2_data_feature.aggregate_array("view_zenith")).set(
                            "view_azimuth",
                            s2_data_feature.aggregate_array("view_azimuth")).
                set("x_coords",
                    s2_data_feature.aggregate_array("x_coords")).set(
                        "y_coords",
                        s2_data_feature.aggregate_array("y_coords")).set(
                            "SCL", s2_data_feature.aggregate_array("SCL")).set(
                                "NDVI",
                                s2_data_feature.aggregate_array("NDVI")).set({
                                    b: s2_data_feature.aggregate_array(b)
                                    for b in bands
                                }))

    s2_data_feature_collection = feature_collection.map(
        ee_get_s2_data_feature).getInfo()

    s2_data = s2_feature_collection_to_dataframes(s2_data_feature_collection)

    for a in AOIs:
        name = a.name
        a.data = s2_data[name]