Git Product home page Git Product logo

aqueduct_cloudfunctions's Introduction

Google Cloud Function (getting started)

To create a Google Cloud Function we need a Google Cloud Project and gcloud SDK.

If we have already some projects we can check them by typing:

gcloud projects list

PROJECT_ID        NAME                     PROJECT_NUMBER
gef-ld-toolbox    gef-ld-toolbox           1080184168142
gfw-apis          Global Forest Watch API  872868960419
resource-watch    Resource Watch           312603932249
skydipper-196010  skydipper                230510979472
soc-platform      SOC Platform             345072612231

and select one by:

gcloud config set project gef-ld-toolbox

On the example folder we have set the 2 ways we work with cloud functions. Javascript and Python.

Python

You will need to add a main.py file with your function

import ee
import json
with open("privatekey.json") as keyfile:
    extracted_key_data = keyfile.read()
    credentials = ee.ServiceAccountCredentials(key_data=extracted_key_data)
ee.Initialize(credentials, use_cloud_api=True)
#service_account = '[email protected]'
#credentials = ee.ServiceAccountCredentials(service_account, 'privatekey.json')
#ee.Initialize(credentials)

def serialize_response(url):

    return {
        'download_url': url
    }

def download_image(request):
    # For more information about CORS and CORS preflight requests, see
    # https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request
    # for more information.
    # Set CORS headers for the main request
    headers = {
        'Access-Control-Allow-Origin': '*',
        'Content-Type': 'application/json'
    }
    # Set CORS headers for the preflight request
    if request.method == 'OPTIONS':
        # Allows GET requests from any origin with the Content-Type
        # header and caches preflight response for an 3600s
        headers.update({
            'Access-Control-Allow-Origin': '*',
            'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
            'Access-Control-Allow-Headers': 'Content-Type',
            'Access-Control-Max-Age': '3600'
        })

        return ('', 204, headers)

    

    request = request.get_json()
    if 'geometry' in request:
        polygon = ee.Geometry.Polygon(request['geometry'].get('features')[0].get('geometry').get('coordinates'))
        image = ee.Image(request['assetId']).clip(ee.Geometry(polygon))
    else:
        image = ee.Image(request['assetId'])
    url = image.getDownloadUrl()

    return (json.dumps(serialize_response(url)), 200, headers)

In the same directory include the privatekey.json with the service account keys and the requirements.txt file.

You can test it with the next lib pip install gcp-functions-emulator and then run: gcpfemu -m <path to module/file/main.py> -f <function_name>

Finally cd to that directory and deploy the cloud Function with the following command:

gcloud beta functions deploy <function Name> --runtime python37 --trigger-http

Note that the cloud function name (<function Name>) must match the name of the function we defined.

Javascript

You will need to add a index.js file with your function

const ee = require('@google/earthengine');
const PRIVATE_KEY = require('./privatekey.json');


const serialize = (originalData) => {
  return JSON.stringify({
    rows: [originalData]
  });
};

const interact_raster = (input) => {
  const reducer = {
    "reducer":ee.Reducer.first(), 
    "geometry":ee.Geometry.Point(input.point, 'EPSG:4326'), 
    "bestEffort":true, 
    "maxPixels":10e8, 
    "tileScale":10
  }
  return ee.Image(input.asset_id).reduceRegion(reducer);
};

exports.rasterInteraction = (req, res) => {
  const body = req.body
  res.set('Access-Control-Allow-Origin', '*');
  res.set('Content-Type', 'application/json');
  

  if (req.method === 'OPTIONS') {
   // Send response to OPTIONS requests
   res.set('Access-Control-Allow-Methods', 'POST, GET, OPTIONS');
   res.set('Access-Control-Allow-Headers', 'Content-Type');
   res.set('Access-Control-Max-Age', '3600');
   res.status(204).send('');
  }

  ee.data.authenticateViaPrivateKey(
    PRIVATE_KEY,
    () => {
      ee.initialize(null, null, () => {
        const result = interact_raster(body);
        result.evaluate((json) => res.status(200).send(serialize(json)));
      });
    }, 
    e => console.error(`Authentication error: ${e}`)
  );
};

In the same directory include the privatekey.json with the service account keys and the package.json file.

You can test it adding to your package.json file the next line

"devDependencies": {
    "@google-cloud/functions-framework": "^1.2.1"
  },
"scripts": {
    "start": "functions-framework --target=rasterInteraction"
  },

Doing a npm install and running the server with npm start and check http://localhost:8080/

To deploy it cd to that directory and deploy the cloud Function with the following command:

gcloud beta functions deploy <function Name> --runtime nodejs10 --trigger-http

Note that the cloud function name (<function Name>) must match the name of the function we defined.

aqueduct_cloudfunctions's People

Contributors

aagm avatar

Watchers

 avatar  avatar  avatar

Forkers

greenriver

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.