def get_global_geometry(test=False): """ Returns a global or samplle geometry. ------------------------------------------------------------------------------- please note that some python functions require you to modify the output. geometry.getInfo()['coordinates'] This will likely change in a future version of earthengine. Args: test (boolean) : toggle testing mode. Returns: geomtery (ee.geometry) : Server side geometry object. """ if test: geometry = ee.Geometry.Polygon(coords=[[-10.0, -10.0], [10, -10.0], [10, 10], [-10, 10]], proj=ee.Projection('EPSG:4326'), geodesic=False) else: geometry = ee.Geometry.Polygon(coords=[[-180.0, -90.0], [180, -90.0], [180, 90], [-180, 90]], proj=ee.Projection('EPSG:4326'), geodesic=False) return geometry
def agrupando(img): d = ee.Date(img.get("system:time_start")) cat = img.select("cat") connected = img.toInt().connectedComponents( ee.Kernel.plus(1), 8).reproject(ee.Projection('EPSG:4326'), None, 30) conn2 = connected.mask().select("labels") holes = conn2.gt(cat) islas = cat.gt(conn2) objetos = holes.add(islas) kernel = ee.Kernel.plus(1, "pixels", False) reducer = ee.Reducer.countDistinct() vecinos = (objetos.select(0).reduceNeighborhood( reducer, kernel, "kernel", False).reproject(ee.Projection('EPSG:4326'), None, 30)) objNot = objetos.eq(0) final = objNot.Not().reduceNeighborhood( ee.Reducer.max(), kernel).reproject(ee.Projection('EPSG:4326'), None, 30) final = final.set("system:time_start", d) return final
def init_projections(): global proj, latlon_proj # World Mercator projection proj = ee.Projection('PROJCS[WGS 84 / World Mercator", \ GEOGCS["WGS 84",\ DATUM["WGS_1984",\ SPHEROID["WGS 84",6378137,298.257223563,\ AUTHORITY["EPSG","7030"]],\ AUTHORITY["EPSG","6326"]],\ PRIMEM["Greenwich",0,\ AUTHORITY["EPSG","8901"]],\ UNIT["degree",0.0174532925199433,\ AUTHORITY["EPSG","9122"]],\ AUTHORITY["EPSG","4326"]],\ PROJECTION["Mercator_1SP"],\ PARAMETER["central_meridian",0],\ PARAMETER["scale_factor",1],\ PARAMETER["false_easting",0],\ PARAMETER["false_northing",0],\ UNIT["metre",1,\ AUTHORITY["EPSG","9001"]],\ AXIS["Easting",EAST],\ AXIS["Northing",NORTH],\ AUTHORITY["EPSG","3395"]]') latlon_proj = ee.Projection('GEOGCS["WGS 84",\ DATUM["WGS_1984",\ SPHEROID["WGS 84",6378137,298.257223563,\ AUTHORITY["EPSG","7030"]],\ AUTHORITY["EPSG","6326"]],\ PRIMEM["Greenwich",0,\ AUTHORITY["EPSG","8901"]],\ UNIT["degree",0.0174532925199433,\ AUTHORITY["EPSG","9122"]],\ AUTHORITY["EPSG","4326"]]')
def __init__(self, season, range=(0, 0), colgroup=None, scores=None, masks=None, filters=None, target_collection=None, brdf=False, harmonize=True, projection=None, **kwargs): self.range = range self.scores = scores self.masks = masks or () self.filters = filters or () self.season = season self.colgroup = colgroup self.brdf = brdf self.harmonize = harmonize self.projection = projection or ee.Projection('EPSG:3857') if target_collection is None: target_collection = collection.Landsat8SR() self.target_collection = target_collection self.score_name = kwargs.get('score_name', 'score') # Band names in case user needs different names self.bandname_col_id = kwargs.get('bandname_col_id', 'col_id') self.bandname_date = kwargs.get('bandname_date', 'date')
def _construct_data_stack(self): image_stack = ee_utils.preprocess_data(self.year).toBands() features = [feat['id'] for feat in image_stack.getInfo()['bands']] self.features = features + ['elevation', 'slope', 'constant'] self.shapefile_to_feature_collection = \ ee_utils.temporally_filter_features(self.mask_shapefiles, self.year) class_labels = ee_utils.create_class_labels( self.shapefile_to_feature_collection) # terrain = ee.Image("USGS/SRTMGL1_003").select('elevation') # slope = ee.Terrain.slope(terrain) # self.image_stack = ee.Image.cat([terrain, slope]).float() self.image_stack = ee.Image.cat([image_stack, class_labels]).float() list_ = ee.List.repeat(1, self.kernel_size) lists = ee.List.repeat(list_, self.kernel_size) kernel = ee.Kernel.fixed(self.kernel_size, self.kernel_size, lists) self.data_stack = self.image_stack.neighborhoodToArray(kernel) self.projection = ee.Projection('EPSG:5070') self.data_stack = self.data_stack.reproject(self.projection, None, 30) self.image_stack = self.image_stack.reproject(self.projection, None, 30)
def download_ee_image_layer(iface, name, imageid, bands, scale, proj, extent=None): if extent is None: image = ee.Image(imageid).select(bands) else: roi = ee.Geometry.Rectangle(extent, ee.Projection(proj), False) image = ee.Image(imageid).select(bands).clip(roi) task = ee.batch.Export.image.toDrive( image=image, description=name.replace('/', '_'), folder='qgis_gee_data_catalog', scale=int(scale), # coarse it to int crs=proj, maxPixels=1.0E13) task.start() status = task.status() taskid, state = status['id'], status['state'] if state == 'COMPLETED': iface.messageBar().pushMessage( f'Task id {taskid} is {state}. Please check GDrive') else: iface.messageBar().pushMessage( f'Task id {taskid} is {state}. Please wait and check GDrive')
def sum_raster(image): """ Global sum """ reducer = ee.Reducer.sum() #geometry = ee.Geometry.Polygon(coords=[[-10.0, -10.0], [10, -10.0], [10, 10], [-10,10]], proj= ee.Projection('EPSG:4326'),geodesic=False ) geometry = ee.Geometry.Polygon(coords=[[-180.0, -90.0], [180, -90.0], [180, 90], [-180, 90]], proj=ee.Projection('EPSG:4326'), geodesic=False) dictje = ee.Image(image).reduceRegion( reducer=reducer, geometry=geometry, #scale, crs=CRS, crsTransform=CRS_TRANSFORM_30S, #bestEffort, maxPixels=1e10, #tileScale) ) dictje = dictje.set("aq30_indicator", aq30_indicator) dictje = dictje.set("aq21_indicator", aq21_indicator) dictje = dictje.set("aq30_lower_bound", aq30_lower_bound) dictje = dictje.set("aq21_lower_bound", aq21_lower_bound) feature = ee.Feature(None, dictje) fc = ee.FeatureCollection([feature]) return fc
def get_features(shp): ''' converts shapefile to ee's feature collection :param shp: :return: feature collection ''' reader = shapefile.Reader(shp) fields = reader.fields[1:] field_names = [field[0] for field in fields] projection = get_epsg(shp) wgs84 = ee.Projection('EPSG:4326') features = [] for sr in reader.shapeRecords(): atr = dict(zip(field_names, sr.record)) geom = sr.shape.__geo_interface__ if projection == 4326: ee_geometry = ee.Geometry(geom, 'EPSG:4326') else: ee_geometry = ee.Geometry(geom, 'EPSG:' + projection)\ .transform(wgs84, 1) feat = ee.Feature(ee_geometry, atr) features.append(feat) return ee.FeatureCollection(features)
def get_checkerboard(image, imgband, updmask, viz, color1, color2): # Create a 0/1 checkerboard on a lon/lat grid: take the floor of lon and # lat, add them together, and take the low-order bit lonlat_checks = ee.Image.pixelLonLat().floor().toInt().reduce( ee.Reducer.sum()).bitwiseAnd(1) # Get the image projection from one of the bands imgproj = image.select([imgband]).projection() # Now replace the projection of the lat/lon checkboard (WGS84 by default) # with the desired projection. # TODO: it would be a good idea to understand difference between changeProj and reproject. imgchecks = lonlat_checks.changeProj(ee.Projection('EPSG:4326'), imgproj) # If requested copy the footprint of the image onto the checkerboard, # to avoid a global image. if updmask: imgchecks = imgchecks.updateMask(image.select([imgband]).mask()) if viz: imgchecks = imgchecks.visualize({ 'min': 0, 'max': 1, 'palette': [color1, color2] }) return imgchecks
def add_map_point(data, zoom, m, kml, name): # Make geojson from data gjson = ipyleaflet.GeoJSON(data=data, name=name) # Center map m.center = gjson.data['coordinates'][::-1] # Set map zoom m.zoom = zoom # Add geojson to map m.add_layer(gjson) # Make KML via earth engine functionality geo_point = ee.Geometry.Point( gjson.data['coordinates'][::-1]) # This may not work geo_fc = ee.FeatureCollection(geo_point) kmlstr = geo_fc.getDownloadURL("kml") # Update KML widget kml.value = "<a '_blank' rel='noopener noreferrer' href={}>KML Link</a>".format( kmlstr) # Add pixel bounds _bounds = utils.get_bounds(geo_point, ee.Projection("EPSG:4326")).getInfo() pixel_bounds = ipyleaflet.Polyline(locations=_bounds['coordinates'], name="Pixel") m.add_layer(pixel_bounds) return
def __init__(self, year, split='train', satellite='sentinel', fid=None, dataset='mt'): self.fid = fid self.year = year self.start, self.end = '{}-01-01'.format(year), '{}-12-31'.format(year) self.split = split self.dataset = dataset self.bounds = 'users/dgketchum/boundaries/{}'.format(dataset.upper()) self.grid = 'users/dgketchum/itype/{}_grid_{}'.format(dataset, year) self.irr_labels = 'users/dgketchum/itype/{}_itype_{}'.format( dataset, year) self.dryland_labels = 'users/dgketchum/itype/{}_dryland'.format( dataset) self.uncult_labels = 'users/dgketchum/itype/{}_uncultivated'.format( dataset) self.wetland_labels = 'users/dgketchum/itype/{}_wetlands'.format( dataset) self.projection = ee.Projection('EPSG:3857') self.satellite = satellite self.basename = os.path.basename(self.irr_labels) self.points_fc, self.features = None, None self.data_stack, self.image_stack = None, None self.gcs_bucket = GS_BUCKET self.kernel = KERNEL self.task = None
def testDynamicConstructorCasting(self): """Test the behavior of casting with dynamic classes.""" self.InitializeApi() result = ee.Geometry.Rectangle(1, 1, 2, 2).bounds(0, 'EPSG:4326') expected = (ee.Geometry.Polygon([[1, 2], [1, 1], [2, 1], [2, 2]]) .bounds(ee.ErrorMargin(0), ee.Projection('EPSG:4326'))) self.assertEqual(expected, result)
def main(): # Define the study area study_area = ee.Geometry.Polygon( [[[101.4915, 15.4685], [101.4915, 9.3992], [108.8084, 9.3992], [108.8084, 15.468]]], None, False) # Define the username username = "******" # Define the projection of the study area projection = ee.Projection('EPSG:32648') # Define the output location output_dir = "SERVIR/real_time_monitoring" # Generate the outputs labels = generate_glad_label(study_area, projection) # Export the data as an asset task = ee.batch.Export.image.toAsset(image=labels.toInt16(), description="Processed-Glad-Alerts", assetId='users/' + username + "/" + output_dir + '/glad_alerts_2019_to_2020', region=study_area.bounds(), scale=30, crs=projection, maxPixels=1e13) task.start() return None
def generate_voronoi_polygons(points, scale, aoi): """ Generates Voronoi polygons :param points: :param scale: :param aoi: :return: """ error = ee.ErrorMargin(1, 'projected') # proj = ee.Projection('EPSG:3857').atScale(scale) proj = ee.Projection('EPSG:4326').atScale(scale) distance = ee.Image(0).float().paint(points, 1) \ .fastDistanceTransform().sqrt().clip(aoi) \ .reproject(proj) concavity = distance.convolve(ee.Kernel.laplacian8()) \ .reproject(proj) concavity = concavity.multiply(distance) concavityTh = 0 edges = concavity.lt(concavityTh) # label connected components connected = edges.Not() \ .connectedComponents(ee.Kernel.circle(1), 256) \ .clip(aoi) \ .focal_max(scale * 3, 'circle', 'meters') \ .focal_min(scale * 3, 'circle', 'meters') \ .focal_mode(scale * 5, 'circle', 'meters') \ .reproject(proj) # fixing reduceToVectors() bug, remap to smaller int def fixOverflowError(i): hist = i.reduceRegion(ee.Reducer.frequencyHistogram(), aoi, scale) uniqueLabels = ee.Dictionary(ee.Dictionary(hist).get('labels')).keys() \ .map(lambda o: ee.Number.parse(o)) labels = ee.List.sequence(0, uniqueLabels.size().subtract(1)) return i.remap(uniqueLabels, labels).rename('labels').int() connected = fixOverflowError(connected).reproject(proj) polygons = connected.select('labels').reduceToVectors( **{ "scale": scale, "crs": proj, "geometry": aoi, "eightConnected": True, "labelProperty": 'labels', "tileScale": 4 }) # polygons = polygons.map(lambda o: o.snap(error, proj)) return {"polygons": polygons, "distance": distance}
def fromGeoJSON(filename=None, data=None, crs=None): """ Create a list of Features from a GeoJSON file. Return a python tuple with ee.Feature inside. This is due to failing when attempting to create a FeatureCollection (Broken Pipe ERROR) out of the list. You can try creating it yourself casting the result of this function to a ee.List or using it directly as a FeatureCollection argument. :param filename: the name of the file to load :type filename: str :param crs: a coordinate reference system in EPSG format. If not specified it will try to get it from the geoJSON, and if not there it will rise an error :type: crs: str :return: a tuple of features. """ if filename: with open(filename, 'r') as geoj: content = geoj.read() geodict = json.loads(content) else: geodict = data features = [] # Get crs from GeoJSON if not crs: filecrs = geodict.get('crs') if filecrs: name = filecrs.get('properties').get('name') splitcrs = name.split(':') cleancrs = [part for part in splitcrs if part] try: if cleancrs[-1] == 'CRS84': crs = 'EPSG:4326' elif cleancrs[-2] == 'EPSG': crs = '{}:{}'.format(cleancrs[-2], cleancrs[-1]) else: raise ValueError('{} not recognized'.format(name)) except IndexError: raise ValueError('{} not recognized'.format(name)) else: crs = 'EPSG:4326' for n, feat in enumerate(geodict.get('features')): properties = feat.get('properties') geom = feat.get('geometry') ty = geom.get('type') coords = geom.get('coordinates') if ty == 'GeometryCollection': ee_geom = utils.GEOMETRY_TYPES.get(ty)(geom, opt_proj=crs) else: if ty == 'Polygon': coords = utils.removeZ(coords) if utils.hasZ( coords) else coords ee_geom = utils.GEOMETRY_TYPES.get(ty)(coords, proj=ee.Projection(crs)) ee_feat = ee.feature.Feature(ee_geom, properties) features.append(ee_feat) return tuple(features)
def fromShapefile(filename, start=None, end=None): """ Convert an ESRI file (.shp and .dbf must be present) to a ee.FeatureCollection At the moment only works for shapes with less than 1000 records and doesn't handle complex shapes. :param filename: the name of the filename. If the shape is not in the same path than the script, specify a path instead. :type filename: str :param start: :return: the FeatureCollection :rtype: ee.FeatureCollection """ wgs84 = ee.Projection('EPSG:4326') # read the filename reader = shapefile.Reader(filename) fields = reader.fields[1:] field_names = [field[0] for field in fields] field_types = [field[1] for field in fields] types = dict(zip(field_names, field_types)) features = [] projection = get_projection(filename) # filter records with start and end start = start if start else 0 if not end: records = reader.shapeRecords() end = len(records) else: end = end + 1 if (end - start) > 1000: msg = "Can't process more than 1000 records at a time. Found {}" raise ValueError(msg.format(end - start)) for i in range(start, end): # atr = dict(zip(field_names, sr.record)) sr = reader.shapeRecord(i) atr = {} for fld, rec in zip(field_names, sr.record): fld_type = types[fld] if fld_type == 'D': value = ee.Date(rec.isoformat()).millis().getInfo() elif fld_type in ['C', 'N', 'F']: value = rec else: continue atr[fld] = value geom = sr.shape.__geo_interface__ geometry = ee.Geometry(geom, 'EPSG:' + projection) \ .transform(wgs84, 1) feat = ee.Feature(geometry, atr) features.append(feat) return ee.FeatureCollection(features)
def bufferRunMap(inputVals): filteredCatalog = filterCatalogSet() catalogDates = filteredCatalog.toList(10000) timeStart = band.get('system:time_start') projString = 'PROJCS["North_America_Equidistant_Conic",GEOGCS["GCS_North_American_1983",DATUM["D_North_American_1983",SPHEROID["GRS_1980",6378137,298.257222101]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]],PROJECTION["Equidistant_Conic"],PARAMETER["False_Easting",0],PARAMETER["False_Northing",0],PARAMETER["Central_Meridian",-96],PARAMETER["Standard_Parallel_1",20],PARAMETER["Standard_Parallel_2",60],PARAMETER["Latitude_Of_Origin",40],UNIT["Meter",1]]' proj = ee.Projection(projString) a = band.reduceRegion(ee.Reducer.mean(), inputVals.geometry(), 5, proj) inputVals2 = inputVals.set({str(timeStart.getInfo()): a}) return (inputVals2)
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
def tile_features(index): row = ee.Number.parse(ee.String(index).split('_').get(0)) col = ee.Number.parse(ee.String(index).split('_').get(1)) xmin = ee.Number(output_extent[0]).add(col.multiply(tile_size)) ymax = ee.Number(output_extent[3]).subtract(row.multiply(tile_size)) return ee.Feature( ee.Geometry.Rectangle( [xmin, ymax.subtract(tile_size), xmin.add(tile_size), ymax], ee.Projection(output_crs), False), {'index': row.format('%d').cat('_').cat(col.format('%d'))})
def geometryFromProj(projection, dimensions): coords = {} coords["xmin"] = projection["transform"][2] coords["xmax"] = projection["transform"][ 2] + dimensions["x"] * projection["transform"][0] coords["ymin"] = projection["transform"][ 5] + dimensions["y"] * projection["transform"][4] coords["ymax"] = projection["transform"][5] geometry = ee.Geometry.Polygon(coords=[[coords["xmin"], coords["ymin"]], [coords["xmax"], coords["ymin"]], [coords["xmax"], coords["ymax"]], [coords["xmin"], coords["ymax"]]], proj=ee.Projection('EPSG:4326'), geodesic=False) return geometry
def fromGeoJSON(filename, crs=None): """ Create a FeatureCollection from a GeoJSON file :param filename: :type filename: str :param crs: a coordinate reference system in EPSG format. If not specified it will try to get it from the geoJSON, and if not there it will rise an error :type: crs: str :return: """ with open(filename, 'r') as geojson: content = geojson.read() geodict = json.loads(content) features = [] # Get crs from GeoJSON if not crs: filecrs = geodict.get('crs') if filecrs: name = filecrs.get('properties').get('name') splitcrs = name.split(':') cleancrs = [part for part in splitcrs if part] try: if cleancrs[-1] == 'CRS84': crs = 'EPSG:4326' elif cleancrs[-2] == 'EPSG': crs = '{}:{}'.format(cleancrs[-2], cleancrs[-1]) else: raise ValueError('{} not recognized'.format(name)) except IndexError: raise ValueError('{} not recognized'.format(name)) else: crs = 'EPSG:4326' for n, feat in enumerate(geodict.get('features')): properties = feat.get('properties') geom = feat.get('geometry') ty = geom.get('type') coords = geom.get('coordinates') if ty == 'GeometryCollection': ee_geom = GEOMETRY_TYPES.get(ty)(geom, opt_proj=crs) else: ee_geom = GEOMETRY_TYPES.get(ty)(coords, proj=ee.Projection(crs)) ee_feat = ee.feature.Feature(ee_geom, properties) features.append(ee_feat) return ee.FeatureCollection(features)
def wrs2_bounds(ftr): crs = ee.String('EPSG:').cat( ee.Number(ee.Feature(ftr).get('EPSG')).format('%d')) extent = ee.Feature(ftr).geometry() \ .bounds(1, ee.Projection(crs)).coordinates().get(0) # extent = ee.Array(extent).transpose().toList() # extent = ee.List([ # ee.List(extent.get(0)).reduce(ee.Reducer.min()), # ee.List(extent.get(1)).reduce(ee.Reducer.min()), # ee.List(extent.get(0)).reduce(ee.Reducer.max()), # ee.List(extent.get(1)).reduce(ee.Reducer.max()) # ]) return ee.Feature(None, { 'crs': crs, 'extent': extent, 'wrs2_tile': ee.Feature(ftr).get(wrs2_tile_field)})
def getPlotBox(lnglat, chipSize): """create a polygon snapped to the image""" ps = 30 halfx = ps * chipSize / 2.0 #shifting the the center works for Javascript, but not when using Python API. cx = lnglat[0] #- 15 cy = lnglat[1] #+ 15 coords = [[[cx - halfx, cy - halfx + 30], [cx - halfx, cy + halfx], [cx + halfx - 30, cy + halfx], [cx + halfx - 30, cy - halfx + 30], [cx - halfx, cy - halfx + 30]]] # return crs, coords return ee.Geometry.Polygon(coords, ee.Projection(CRS, CRS_TRANSFORM).wkt(), False)
def main (): # Define the projection, the export location (google drive) projection = ee.Projection('EPSG:32648') forward_label_fuzz = 7 backward_label_fuzz = 7 kernel_size = 256 export_folder = 'SERVIR_alert_data' # Things that will become parametesr later sample_groups = ee.FeatureCollection("users/JohnBKilbride/SERVIR/real_time_monitoring/feature_groups") \ .map(id_list_to_string) \ .limit(10) num_features = sample_groups.size().getInfo() print('Num exports:', num_features) # Get the bounds of the area with study_area = sample_groups.geometry().bounds() # Load the GLAD Alerts glad_alerts = load_formatted_alerts(study_area) # Load the Sentinel 1 GRD dataset sentinel = ee.ImageCollection("COPERNICUS/S1_GRD") \ .filterBounds(study_area) \ .filterMetadata('orbitProperties_pass', 'equals', 'DESCENDING') \ .filterDate('2018-01-01', '2019-12-31') \ .select(['VV','VH']) # Load the kernel kernel = create_kernel(kernel_size) # Load the features feature_names = ["VV_1","VH_1","VV_2","VH_2","VV_3","VH_3"] # Loop over the features sample_group_list = sample_groups.toList(1e7) for i in range(0, num_features): # Get the feature feature = ee.Feature(sample_group_list.get(i)) # Run the sampling and export the feature export export_dataset_sample(feature, i, sentinel, glad_alerts, forward_label_fuzz, backward_label_fuzz, kernel, feature_names, export_folder) return None
def getConformProj(): wkt = """ PROJCS["World_Mollweide", GEOGCS["GCS_WGS_1984", DATUM["WGS_1984", SPHEROID["WGS_1984",6378137,298.257223563]], PRIMEM["Greenwich",0], UNIT["Degree",0.017453292519943295]], PROJECTION["Mollweide"], PARAMETER["False_Easting",0], PARAMETER["False_Northing",0], PARAMETER["Central_Meridian",0], UNIT["Meter",1], AUTHORITY["ESRI","54009"]]' """ return ee.Projection(wkt)
def output_image(image, outBands, project_id, model_name, version_name, scale): # Load the trained model and use it for prediction. model = ee.Model.fromAiPlatformPredictor( projectName=project_id, modelName=model_name, version=version_name, inputTileSize=[144, 144], inputOverlapSize=[8, 8], proj=ee.Projection('EPSG:4326').atScale(scale), fixInputProj=True, outputBands={ 'prediction': { 'type': ee.PixelType.float(), 'dimensions': 1, } }) prediction = model.predictImage(image.toArray()).arrayFlatten([outBands]) return prediction
def aggregateS2MCD15 (reducer,getImageCollectionRange,outputProjection,outputScale,feature,image): reducer = ee.Reducer(reducer) outputProjection = ee.Projection(outputProjection) outputScale = ee.Number(outputScale) grid = ee.Feature(feature) imahe = ee.Image(image) modisCollection = ee.ImageCollection("MODIS/006/MCD15A3H") ; return image.addBands(image.select('QC').not().rename('validFraction').updateMask(image.select('QC').mask())) \ # make a band with value=1 is QC is valid .unmask() \ # unmask all bands before aggregation , no data values are zero and adjusted later .addBands(getImageCollectionRange(modisCollection,image).first().select(['Lai','LaiStdDev']).multiply(0.1)) \ # get the MODIS LAI product containing the S2 image date and add after scaling to real units .addBands(getImageCollectionRange(modisCollection,image).first().select(['Fpar','FparStdDev']).multiply(0.01)) \ # get the MODIS Fpar product containing the S2 image date and add after scaling to real units .clip(feature.geometry()) \ # clip all layers to the grid feature .select(['estimateLAI','estimatefAPAR','Lai','LaiStdDev','Fpar','FparStdDev','LandCover','validFraction']) \ # only aggregate the product layers and the land cover and valid fraction .reduceResolution( \ reducer= ee.Reducer.mean(), \ maxPixels= 1024, \ # we can aggregate at most 1024 pixels or 32x32 fine resolution pixels (e.g. 640m outputScale) bestEffort= True \ # our estimate of mean will be statistical if the outputScale is to large )
def centerObject(feature, zoom=None): """ Centers the map view on a given object. https://developers.google.com/earth-engine/api_docs#mapcenterobject Uses: >>> from ee_plugin import Map >>> Map.centerObject(feature) """ if not hasattr(feature, 'geometry'): feature = ee.Feature(feature) if not zoom: # make sure our geometry is in geo rect = feature.geometry().transform(ee.Projection('EPSG:4326'), 1) # get coordinates coords = rect.bounds().getInfo()['coordinates'][0] xmin = coords[0][0] ymin = coords[0][1] xmax = coords[2][0] ymax = coords[2][1] # construct QGIS geometry rect = QgsRectangle(xmin, ymin, xmax, ymax) # transform rect to a crs used by current project crs_src = QgsCoordinateReferenceSystem('EPSG:4326') crs_dst = QgsCoordinateReferenceSystem(QgsProject.instance().crs()) geo2proj = QgsCoordinateTransform(crs_src, crs_dst, QgsProject.instance()) rect_proj = geo2proj.transform(rect) # center geometry iface.mapCanvas().zoomToFeatureExtent(rect_proj) else: # set map center to feature centroid at a specified zoom center = feature.geometry().centroid().coordinates().getInfo() setCenter(center[0], center[1], zoom)
def fromShapefile(filename): """ Convert an ESRI file (.shp and .dbf must be present) to a ee.FeatureCollection At the moment only works for shapes with less than 3000 records :param filename: the name of the filename. If the shape is not in the same path than the script, specify a path instead. :type filename: str :return: the FeatureCollection :rtype: ee.FeatureCollection """ wgs84 = ee.Projection('EPSG:4326') # read the filename reader = shapefile.Reader(filename) fields = reader.fields[1:] field_names = [field[0] for field in fields] field_types = [field[1] for field in fields] types = dict(zip(field_names, field_types)) features = [] for sr in reader.shapeRecords(): # atr = dict(zip(field_names, sr.record)) atr = {} for fld, rec in zip(field_names, sr.record): fld_type = types[fld] if fld_type == 'D': value = ee.Date(rec.isoformat()).millis().getInfo() elif fld_type in ['C', 'N', 'F']: value = rec else: continue atr[fld] = value geom = sr.shape.__geo_interface__ geometry = ee.Geometry(geom, 'EPSG:' + get_projection(filename)) \ .transform(wgs84, 1) feat = ee.Feature(geometry, atr) features.append(feat) return ee.FeatureCollection(features)
def search_ee_collection(collection: str, extent: list, proj: str, startdate: str, enddate: str, cloudfield: str, cloudcover: int, namefield: str, bands=None, limit=5): def get_info(image, lst): return ee.List(lst).add( ee.List([ image.get('system:id'), image.get(namefield), ee.Date(image.get('system:time_start')).format('Y-MM-dd') ])) first = ee.List([]) roi = ee.Geometry.Rectangle(extent, ee.Projection(proj), False) if cloudfield is None: images = ee.ImageCollection(collection).filterDate(startdate, enddate).filterBounds(roi) \ .limit(limit, 'system:time_start').iterate(get_info, first) elif collection == 'ASTER/AST_L1T_003': images = ee.ImageCollection(collection).filterDate(startdate, enddate).filterBounds(roi) \ .filter(ee.Filter.And(ee.Filter.lt(cloudfield, cloudcover), ee.Filter.listContains('ORIGINAL_BANDS_PRESENT', bands[0]), ee.Filter.listContains('ORIGINAL_BANDS_PRESENT', bands[1]), ee.Filter.listContains('ORIGINAL_BANDS_PRESENT', bands[2]))) \ .limit(limit, cloudfield).iterate(get_info, first) else: images = ee.ImageCollection(collection).filterDate(startdate, enddate).filterBounds(roi) \ .filter(ee.Filter.lt(cloudfield, cloudcover)) \ .limit(limit, cloudfield).iterate(get_info, first) return ee.List(images).getInfo() # ComputedObject need to coarse to List