[GIS] Updating Fusion Table layers from csv files using python

csvgoogle-fusion-tablespython

I'm trying to use Python to take csv files and use them to update (or replace) tables in Fusion Tables. I found the "Python client library for Fusion Tables" on googles' site and downloaded them as well as the two required dependencies:

I've installed all three on python 2.7 like required and just can't seem to figure out how to actually get it to work. I've even tried to get their samples to work with no luck either. At this point let me say that I'm not a programmer and don't know python. However, I can grasp it enough (and look at and modify code) pretty well though and used python to write the scripts that export from SDE to shapefiles, then convert the shapefiles to KML using GDAL and then convert the KML to CSV using elementtree.

Does anybody know of a good resource to look at for help in doing what I'm trying to do? All I can find online is a few people saying they did it, with no explanation, and a ton of links to the google code (which has no documentation)

Thanks in advance,
Adam

Best Answer

In order to upload csv to FusionTables, you'll need to make sure you authenticate first. I see you've included the dependencies for OAuth, but if you're just trying to have a script do a simple upload, it'd be easiest to use ClientLogin (and you don't need the dependencies). A skeleton of the code should look something like this (adapted from the sample code in the python library you referenced):

from authorization.clientlogin import ClientLogin
from sql.sqlbuilder import SQL
import ftclient
from fileimport.fileimporter import CSVImporter

token = ClientLogin().authorize(username, password) #Replace with your username and password (or prompt for them would be more secure)
ft_client = ftclient.ClientLoginFTClient(token) #ft_client needs to be used from here on out to make any calls to the FusionTables SQL API

#If you want to CREATE a table from the csv...
tableid = int(CSVImporter(ft_client).importFile("data.csv")) #Replace with your csv data source (or prompt for the location would be less hard-coded)
print tableid #This will print out the ID of your new table, but the table should also show up in Google Docs for the username you used to authenticate.

If you want to add rows to an existing FusionTable, that'll be trickier. It would probably take parsing out the CSV with csv.DictReader or something and then iterating over that and uploading those rows using something like...

#update row using dictionary of values
print ft_client.query(SQL().update(tableid, cols={'strings': 'mystring3'}, row_id=rowid))
print ft_client.query(SQL().select(tableid))

...for each row.

Related Question