Using Fusion Tables as a Data Source
Fri, 09/20/2013 - 06:03 pm
Google’s Fusion Tables technology offers a number of advantages to map developers. Among these is the ability to load a “layer” of overlays with just a single line of code and to define symbology without writing any code. Let's walk through the usage of Fusion Tables as a data source for a Google Map with our familiar Jen and Barry's data. (If you don't have the Jen and Barry's data from GEOG 483, you can download it here.)
Obtaining a Google account
- If you don't already have one, sign up for a Google Account [http://accounts.google.com/].
- After creating and signing in to your account, navigate to the Google Fusion Tables [http://www.google.com/drive/apps.html#fusiontables] home page.
- Click on the Create link to get started.
Typically the easiest way to populate a Fusion Table is to import the data from an existing spreadsheet, delimited text file or KML file. It is also possible to create a new table from scratch.
Importing from a csv file
Point feature classes are convenient because it is possible to add fields to the attribute table in ArcMap, populate those fields with the coordinate values associated with the points, then export the feature class to a comma-delimited text file. That text file can then be imported into a Fusion Table. Let's follow those steps with the cities shapefile.
- Load the cities shapefile into ArcMap and open its attribute table.
- Add two new Double fields to the attribute table named X and Y.
- Right-click on the X field header and select Calculate Geometry.
- Choose X Coordinate of Point from the Property list and confirm that the values will be in the GCS/NAD27 coordinate system and in Decimal Degree units. Click OK.
- Repeat this step to populate the Y field.
- Open the cities layer's attribute table and select Export from the Table Options drop-down list (upper-left corner of the table).
- Select Text File from the Save as type drop-down list, give the file a logical name and click OK.
Note: Number formats applied in ArcMap don't appear to be "stick" when exporting; if this matters to you, may want to open the exported file in Excel, format there and import the Excel file to a fusion table.
- Open the text file in a text editor and verify that it was exported correctly.
- Back in your web browser, you should be seeing an Import new table dialog. Click the Browse button.
- Browse to the location of the exported file. Note that it is possible to specify the character that separates the values in the import file as a comma, tab, colon or some other character.
- Click the Next button to move to the next part of the import process.
- If satisfied that your file is being read correctly, click Next.
- The last step in the import process is to add/modify some properties of the new Fusion Table. For example, its name defaults to that of the import file, but can be changed to something else. When satisfied with these properties, click Finish to complete the import process.
Preparing your Fusion Table to be mapped
Once your ArcMap export file has been imported as a fusion table, you should confirm that the table is set to use the values in the X/Y columns to map the points.
- Select Edit > Change Columns.
- Highlight the X column to view its details on the right side of the dialog. The Two column location checkbox should be checked, the Latitude column should be set to Y and the Longitude column to X. (The X and Y columns were set the opposite by default when I went through this step.) Click Save when finished specifying the lat and long columns.
- Click on the Map of X tab to see your points on a map. You can return to the tabular view by selecting the Rows tab.
Note: If you don't see a map in this step, try clicking the red "plus" icon to the right of all the tabs and select Add map. You should see a map now.
- When you click on a point, an info window will present values from each column of the table by default. You can override this by clicking the dropdown arrow on the Map tab and selecting Change info window layout. The resulting dialog allows for two levels of customization. Under the Automatic tab, you can uncheck the columns that you’re not interested in seeing. Under the Custom tab, you can modify the HTML used to present the information.
- It is also possible to modify the symbology of the points by clicking the Change map styles from the same dropdown menu. Options include displaying all the points in the same style (Fixed tab), displaying them using a marker icon specified in one of the table’s columns (Column tab) and defining classes and using a different icon for each class (Buckets tab).
Try this: See if you can set up a 3-bucket classification based on the Population column with the following bucket definitions: 0-50000, 50000-100000, 100000+. Then try a 2-bucket classification based on the 1/0 values in the University column.
The final step in preparing your fusion table to be incorporated into a custom Google Map is to make it possible for anyone to view its contents. (It’s only visible to you by default.)
- Click on the Share button in the upper right corner of the page. In the resulting dialog, click the Change link under the Who has access heading. Note that it is also possible in this dialog to specify a list of people that you’d like to grant editing privileges.
- Select Public on the web and Save to finish setting the table's visibility.
- Click Done to dismiss the Sharing settings dialog.
With that, you are now ready to incorporate your Fusion Table data into a map.
Adding a FusionTableLayer to a Google Map
- With your Fusion Table open, go to File > About this table.
- In the resulting dialog, you should see a long alphanumeric ID. Highlight and copy this ID.
var lyr = new google.maps.FusionTablesLayer('xxxxxxx'); // Replace xxxxxxx with your table’s ID lyr.setMap(map); // where ‘map’ is a reference to the Map object in your code
In any case, let's talk about the FusionTablesLayerOptions object that appears in the documentation. This Options object offers a number of properties for configuring the layer. Most notable among these are:
1. query – used to retrieve a subset of records rather than the full fusion table
2. styles –used to override the style applied to the table through the point-and-click interface.
Importing KML into a Fusion Table
The method of adding coordinate values to the attribute table in ArcMap before exporting outlined above works great for point feature classes, but is not practical for line or polygon feature classes. For those geometry types, a better solution is to export the data in KML format. Let’s do that for the counties shapefile.
- Apply the desired symbology to the counties layer in ArcMap.
- Open ArcToolbox.
- Open the Layer to KML tool (under Conversion Tools > To KML).
- In the Layer to KML dialog, select the counties layer.
- Specify the desired name for the output file. Note that the tool outputs the data as compressed KML (in a .kmz file).
- If your layer relies on scale-dependent rendering, enter the scale at which you’d like it to be exported in the Layer Output Scale box. Otherwise, a value of 1 will work in this box.
- Skip the rest of the properties in the dialog (unless you have good reason not to) and click OK.
- As mentioned above, the output file is actually compressed to minimize its size. The Fusion Tables Import tool can only handle uncompressed KML, so you’ll need to unzip it. Depending on the setup of your computer, you may be able to unzip the .kmz file directly. If not, rename it, changing the .kmz suffix to .zip.
- After unzipping, you should be left with a file called doc.kml. I suggest you rename this file to something like counties.kml.
- Return to the Fusion Tables page referenced above, click Create, then Browse to your KML file.
- After selecting the KML file, you should see a preview indicating that three columns will be imported from the file: description, name and geometry. Click Next, then give the new Fusion Table a name and specify any other desired properties.
- Click Finish to complete the import.
- View the contents of the table and note that the description column contains a bit of HTML for each record and the geometry column simply lists the word kml to indicate that there is a block of KML stored in that cell.
- Select Map of Geometry to confirm that your data imported properly.
- You can now apply styling and add your Fusion Table to a Google Map using the methodology outlined above.
Adding a clickable sidebar
As with KmlLayers, the downside to FusionTablesLayers is that there is no way to work with the individual features that are displayed by the layer. The FusionTablesQuery object can be used only to specify which features from the table to display in the layer. There appears to be nothing built into the Fusion Tables API that enables querying records from a table and working with those records individually. However, it is still possible to build a map with a clickable sidebar using data stored in a Fusion Table. The solution involves sending a query that retrieves the desired data to Google's Visualization API.
The Visualization API is typically used for building dynamic charts, but it is useful in this situation because it can be used to obtain a DataTable object that encapsulates the data requested by a query. The DataTable class and other classes are documented in the Visualization API Reference [http://code.google.com/apis/chart/interactive/docs/reference.html#DataTable], which is formatted much like the Maps API Reference you've been consulting throughout the course.
Have a look at this example [http://www.personal.psu.edu/jed124/fusion_table_markers_w_sidebar.html] and view its source code to follow along with the discussion below:
- The example follows the same basic approach used earlier in the lesson to build a clickable sidebar from data stored in an XML file. The primary difference is in how the data are retrieved.
- Making use of the Visualization API requires the addition of two lines near the top of the document: a) the script element that sources the API at the URL http://www.google.com/jsapi, and b) the google.load statement. (The API includes a number of packages offering different bits of functionality. We're using the 'table' package. If you wanted to build charts, you would load the 'chart' package as well.)
- An SQL SELECT statement is used to retrieve information from a table that stores the cities from the Jen and Barry's scenario. Note the ORDER BY clause that causes the results to be returned in alphabetical order by city name.
- The query.send(getData)statement specifies that when a response comes back from the Viz API, it should be forwarded to the getData() function (defined just below the initialize() function).
- The API retrieves the data and passes a QueryResponse object to the getData() function. A reference to this object is stored in the response variable.
The DataTable object encapsulated within the QueryResponse is obtained using the getDataTable() method and stored in the dt variable.
- A for loop is used to step through each row in the DataTable. The upper bound of the loop is set by calling the DataTable's getNumberOfRows method.
- Within the loop, values from the current row are retrieved using the getValue() method. The first argument to getValue() specifies the desired row (in this case, i, which changes on each pass through the loop) and the second argument the column. Note that the column number is based on the columns retrieved by the query, not on the column's position in the parent table.
- Once the table values are obtained, the rest of the script functions just like the XML example.
This example works well when dealing with point data. Creating a clickable sidebar for line or polygon data in a Fusion Table is a much tougher nut to crack since those geometry types are typically stored in the table as KML. Those of you who are highly experienced programmers may want to consider having a go at this topic for your final project. I have some ideas on how to approach the problem that I would be happy to share.
With that, let's switch gears a bit and move on to the topic of Ajax, a programming paradigm that has revolutionized both web mapping and web publishing in general.