Ejemplo n.º 1
0
def transfer_files_to_user_drive(temp_file_name, user_email, user_id,
                                 file_name, oauth2object):

    APP_DRIVE_HELPER = drive.DriveHelper(settings.GOOGLE_OAUTH2_CREDENTIALS)
    files = APP_DRIVE_HELPER.GetExportedFiles(temp_file_name)
    # Grant the user write access to the file(s) in the app service account's Drive.
    for f in files:
        APP_DRIVE_HELPER.GrantAccess(f['id'], user_email)

    user_drive_helper = drive.DriveHelper(oauth2object)

    # Copy the file(s) into the user's Drive.
    if len(files) == 1:
        file_id = files[0]['id']
        copied_file_id = user_drive_helper.CopyFile(file_id, file_name)
        trailer = 'open?id=' + copied_file_id
    else:
        trailer = ''
        for f in files:
            # The titles of the files include the coordinates separated by a dash.
            coords = '-'.join(f['title'].split('-')[-2:])
            user_drive_helper.CopyFile(f['id'], file_name + '-' + coords)

    # Delete the file from the service account's Drive.
    for f in files:
        APP_DRIVE_HELPER.DeleteFile(f['id'])

    return 'https://drive.google.com/' + trailer
Ejemplo n.º 2
0
    def post(self):
        meta_file = self.build_meta_file()
        datastore(self.request.get('ip'), self.request.get('User-Agent'),
                  sum([len(label['points']) for label in self.classes]),
                  len(self.classes), True, json.dumps(self.data['region']))

        temp_file_prefix = str(uuid.uuid4())
        export_time = time.ctime()
        file_name = 'REMAP GeoTIFF Export ' + export_time + '.tif'

        classified = remap.get_classified_from_fc(self.train_fc,
                                                  self.predictors,
                                                  self.past).clip(self.region)

        task = ee.batch.Export.image(
            image=classified,
            description=file_name,
            config={
                'driveFileNamePrefix':
                temp_file_prefix,
                'maxPixels':
                1e10,
                'scale':
                config.EXPORT_TIFF_SCALE,
                'region':
                json.dumps([[x['lng'], x['lat']] for x in self.data['region']])
            })
        # Kangaroo Island CSV points
        # scale  30 took 1335.49342012 seconds (22 mins)
        # scale 100 took 177.377995968 seconds (3 mins)

        task.start()
        while task.active():
            time.sleep(10)

        state = task.status()['state']
        if state == ee.batch.Task.State.COMPLETED:
            files = APP_DRIVE_HELPER.GetExportedFiles(temp_file_prefix)
            credentials = oauth2client.client.OAuth2Credentials.from_json(
                self.request.get('credentials'))
            for f in files:
                APP_DRIVE_HELPER.GrantAccess(f['id'],
                                             self.request.get('email'))
            user_drive_helper = drive.DriveHelper(credentials)
            if len(files) > 0:
                folder = user_drive_helper.CreateFolder(
                    'REMAP Export Folder ' + export_time)
                user_drive_helper.CreateFile(
                    'REMAP metadata ' + export_time + '.csv', meta_file,
                    folder)
                for f in files:
                    try:  # will fail if the user doesn't have the right permissions
                        # just ignore it since it might have accidentally found someone else's file
                        user_drive_helper.CopyFile(f['id'], file_name, folder)
                        APP_DRIVE_HELPER.DeleteFile(f['id'])
                    except:
                        pass
            datastoreProgress(self.request.get('email'), 'COMPLETED')
        else:
            datastoreProgress(self.request.get('email'), 'ERROR')
Ejemplo n.º 3
0
def _GiveFilesToUser(temp_file_prefix, email, user_id, filename):
  """Moves the files with the prefix to the user's Drive folder.

  Copies and then deletes the source files from the app's Drive.

  Args:
    temp_file_prefix: The prefix of the temp files in the service
        account's Drive.
    email: The email address of the user to give the files to.
    user_id: The ID of the user to give the files to.
    filename: The name to give the files in the user's Drive.

  Returns:
    A link to the files in the user's Drive.
  """
  files = APP_DRIVE_HELPER.GetExportedFiles(temp_file_prefix)

  # Grant the user write access to the file(s) in the app service
  # account's Drive.
  for f in files:
    APP_DRIVE_HELPER.GrantAccess(f['id'], email)

  # Create a Drive helper to access the user's Google Drive.
  user_credentials = oauth2client.appengine.StorageByKeyName(
      oauth2client.appengine.CredentialsModel,
      user_id, 'credentials').get()
  user_drive_helper = drive.DriveHelper(user_credentials)

  # Copy the file(s) into the user's Drive.
  if len(files) == 1:
    file_id = files[0]['id']
    copied_file_id = user_drive_helper.CopyFile(file_id, filename)
    trailer = 'open?id=' + copied_file_id
  else:
    trailer = ''
    for f in files:
      # The titles of the files include the coordinates separated by a dash.
      coords = '-'.join(f['title'].split('-')[-2:])
      user_drive_helper.CopyFile(f['id'], filename + '-' + coords)

  # Delete the file from the service account's Drive.
  for f in files:
    APP_DRIVE_HELPER.DeleteFile(f['id'])

  return 'https://drive.google.com/' + trailer
Ejemplo n.º 4
0
# http://jinja.pocoo.org/docs/dev/
JINJA2_ENVIRONMENT = jinja2.Environment(loader=jinja2.FileSystemLoader(
    os.path.dirname(__file__)),
                                        autoescape=True,
                                        extensions=['jinja2.ext.autoescape'])

# Check https://developers.google.com/drive/scopes for all available scopes.
OAUTH_SCOPE = 'https://www.googleapis.com/auth/drive'

# The app's service account credentials (for Google Drive).
APP_CREDENTIALS = oauth2client.client.SignedJwtAssertionCredentials(
    config.EE_ACCOUNT,
    open(config.EE_PRIVATE_KEY_FILE, 'rb').read(), OAUTH_SCOPE)

# An authenticated Drive helper object for the app service account.
APP_DRIVE_HELPER = drive.DriveHelper(APP_CREDENTIALS)

# The decorator to trigger the user's Drive permissions request flow.
OAUTH_DECORATOR = oauth2client.appengine.OAuth2Decorator(
    client_id=config.OAUTH_CLIENT_ID,
    client_secret=config.OAUTH_CLIENT_SECRET,
    scope=OAUTH_SCOPE)

# The ImageCollection of night-time lights images.
IMAGE_COLLECTION_ID = 'NOAA/DMSP-OLS/NIGHTTIME_LIGHTS'

# The resolution of the exported images (meters per pixel).
EXPORT_RESOLUTION = 30

# The maximum number of pixels in an exported image.
EXPORT_MAX_PIXELS = 1e10