Using SQL Server spatial data with Spotfire
Last updated:
4:30pm Jan 19, 2017

 

SQL Server and other relational databases provide a means of storing and querying geometric and geographic data. This can include shapes, lines, points etc. 

This article will show how to bring data from SQL Server into Spotfire and display it on a Spotfire map. 

Additionally we will look at how to dynamically load data using Data-On-Demand. This feature is particularly useful when the data sets are too large to load permanently into memory, or are changing with time. 

This article was written using the Spotfire 6.0 release.

Step 1: Get some SQL Server spatial data

For the purpose of this exercise we will use some publicly available data sets. Navigate to the following site and download the States Table: http://uscdsql.codeplex.com/releases/view/26308. Once you have downloaded the zip file, extract the sql file. 

  1. Start the SQL Server management studio
  2. Login and navigate to the table list and navigate for the database where you want store the data
  3. Right-Click on the database entre and select “New Query”
  4. In the query window, copy and poste the entire contents of the file “duo.State.Table.sql”
  5. Hit the execute button. You will see series of messages saying “(1 row(s) affected)” 
  6. Click on the database and you will now see the new “duo.State” table
  7. Right-click the new “dbo.State” table and select “Select Top 1000 Rows”
  8. In the Results pane, click the “Spatial Results” tab and you will see the data displayed on a map

We cannot import this data directly into Spotfire as the geometry data uses SQL Servers internal representation. So we need to create a view that will expose the data as a WKB (Well Known Binary) representation which Spotfire can digest. 

For more information on WKB see: http://en.wikipedia.org/wiki/Well-known_binary
For more information on converting from SQL Server to WKB see: https://technet.microsoft.com/en-us/library/bb933881.aspx

  1. Right-click on the Views folder and select “New View…”
  2. When the “Add Table” dialog appears, click “Close”.
  3. Enter the following into the SQL pane: 
    SELECT StateID,
                  StateName,
                  geom.STAsBinary() AS Geometry
    FROM dbo.State
  4. Save the View as “VW_State_WKB”.
  5. Right-click on the newly created view and select “Select Top 1000 Rows”.

The results pane will show the content of the view. The will not be a “Spatial Results” tab.

Now we are ready to bring this data into Spotfire.

Step 2: Create an Information Link

We will use the Information Designer to setup an Information Link pointing to the VW_State_WKB view we just created. To keep things organized we will create a folder to store our Information Link. 

  1. Start Spotfire and open “Library Administration” from the Tools menu. 
  2. Create a new folder called “VW_State_WKB”. 
  3. Close the Library Administration dialog.
  4. Open the Information Designer from the Tools menu. 
  5. Select the “Data Sources” tab and expand the tree until the you can see the VW_State_KB view. 
  6. Right-click on the entry and select “Create Default Information Model...” 
  7. Select the folder you created in the previous step as the destination folder and click OK. The Information Link will be created. 
  8. Close the Information Designer. 
  9. Open the newly created Information Link from the Library. Switch the visualization to a Table if it is not the default view. You will notice that there is no geometry data.
  10. Before we proceed, we will need to tell Spotfire how to interpret the Geometry column correctly. Re-open the Information Designer
  11. Expand the Elements tree and locate the Geometry element of the VW_State_WKB view. 
  12. Double-click on the Geometry item to open the Column Element dialog and expand the Properties section. 
  13. Click the “Add...” button to add a new property. 
  14. Set the property name to “MapChart.ColumnTypeId” and the value to “Geometry”. 
  15. Click OK to save the new property. 
  16. Repeat the above process to create a property called “ContentType” with a value of “application/x-wkb”. 

At this point if you close and re-open the Information Link, you should now see the shapes of the States display in the Table Visualization.

Troubleshooting Note:  If the Table Visualization does not render the shapes of the States, you more than likely did not create the "view" from Step 1; therefore, you will need to change the "Geometry" Column Element -->Expression:  %1.STAsBinary()     [default is %1]

Now we can go ahead and display the data on the Map Chart.  Switch the visualization to Map Chart.

Step 3: Setup a Data-On-Demand query

Now we can use our foundational steps to create a Data-On-Demand example. 

  1. Create a new DXP file and open the VW_State_WKB Information Link. 
  2. Rename the Data Table to be “US States” and set the data to be Embedded. 
  3. Delete the Geometry Column from the data table. 
  4. Add a Table Visualization of the “US States” data. Remove the StateID column. Call the visualization “US States”. 
  5. Save the DXP file as “Spatial Data On Demand”. 
  6. Click on File > Add Data Table. On the dialog click Add > Information Link. 
  7. Select the VW_State_WKB Information Link. 
  8. Check the “Load on demand” option and click the “Settings...” button. 
  9. In the “On-Demand Settings” dialog, click on the “Define Input...” button and select the “StateID” column from the “US States” data table. 
  10. Click OK. 
  11. Repeat the previous steps for the “StateName” field this time selecting “StateName” from the “US States” data table. 
  12. Check the option to “Load automatically” and click OK and then OK again. 
  13. Spotfire will open a Map Chart on a new page. We need to do some further configuration so just go ahead and delete the entire page. 
  14. Add a Table Visualization below the US States table visualization. Point it to the VW_States_WKB data. 
  15. When you select an entry in the US States table visualization, the corresponding item will show up in the VW_State_WKB table visualization. 
  16. Add a Map Chart visualization and position it to the right with the two table visualizations stacked in the left pane, one above the other. 
  17. In the Map Chart options, remove the “US States” layer and add a Feature Layer using the VW_State_WKB data table. 
  18. In the Appearance tab, check the “Auto-zoom” option. 

Now when you select an entry in the US States table visualization, the corresponding item will show up in the VW_State_WKB table visualization and be highlighted on the Map Chart.