Skip to content

Instantly share code, notes, and snippets.

@ttresslar
Created August 23, 2019 09:51
Show Gist options
  • Save ttresslar/30c014cb08c2bc52bd30a987cfc567cd to your computer and use it in GitHub Desktop.
Save ttresslar/30c014cb08c2bc52bd30a987cfc567cd to your computer and use it in GitHub Desktop.
#This is designed to be used with the google sheets python api
#Please see the "https://developers.google.com/sheets/api/quickstart/python" Google Python API Quickstart and follow the steps listed there.
#You will need to authorize the API and download a file called "credentials.json" for the account you wish to use
#load the required packages/libraries/etc
from __future__ import print_function
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from qgis.PyQt.QtCore import QVariant
import numpy as np
# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly']
# The ID and range of a sample spreadsheet.
SAMPLE_SPREADSHEET_ID = 'Enter Your Sheet ID here'
SAMPLE_RANGE_NAME = 'Enter the range that you want to pull from here'
gpsx= "enter the index number of the column where the 'x' data is"
gpsy "enter the index number of the column where the 'y' data is"
def getDataFromTable():
"""Shows basic usage of the Sheets API.
Prints values from a sample spreadsheet.
"""
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server()
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('sheets', 'v4', credentials=creds)
# Call the Sheets API
sheet = service.spreadsheets()
result = sheet.values().get(spreadsheetId=SAMPLE_SPREADSHEET_ID,
range=SAMPLE_RANGE_NAME).execute()
values = result.get('values', [])
return values
data = getDataFromTable()
headers = data.pop(0)
#now we can start the qgis-y stuff
vl = QgsVectorLayer("Point", "temp", "memory")
pr = vl.dataProvider()
for attrb in headers:
pr.addAttributes([QgsField(attrb, QVariant.String)])
vl.updateFields()
for datum in data:
if len(datum) == len(headers)-1:
f = QgsFeature()
f.setGeometry(QgsGeometry.fromPointXY(QgsPointXY(float(datum[gpsx]),float(datum[gpsy]))))
f.setAttributes(datum)
pr.addFeature(f)
vl.updateExtents()
QgsProject.instance().addMapLayer(vl)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment