Defining territories on a map is a useful analysis and reporting method. Examples include visualizing customers near stores, or city residents near fire stations, schools, homeless shelters or hospitals.
Once a collection of locations has been defined, drawing a bounding polygon in Spotfire is straightforward using a TERR data function described herein.
In this example we use Census Tract locations near Boston, Massachusetts and quickly mark some of these in Spotfire using the lasso (Alt key, or option key on Mac):
We use a simple TERR data function to add a new Spotfire data table and a layer to the map chart. The data function is based on the R chull() function:
The first couple lines of the data function direct it to gracefully handle the situation where no points have been selected by returning a table with zero rows, yet containing the correct names and types of the expected output table.
Next, the convex hull is calculated with a simple call to the R chull() function using Longitude and Latitude as the x and y coordinates (ignoring the curvature of the earth for this small region). The result, “points.chull” is a vector of integer indices of the points that make up the convex hull for this region.
The section “Close the polygon” simply replicates one of these indices so that the resulting polygon will contain one repeated point, closing on itself.
We want to assemble and return a structured object that can be used by Spotfire to draw the polygon; the “Form the output table” section of the R script above does this. This constructs a table: latitude and longitude of the bounding points are found using the indices to extract the correct points in order. The drawOrder column is simply sequential integers that is used to correctly draw the polygon in Spotfire.
We insert the data function into this Spotfire file with the following settings:
• The Latitude and Longitude inputs are the Latitude and Longitude columns in the Census Tract file, limited by the marking;
• the data function is set to refresh automatically (whenever the marked points are changed).
These two settings together configure the data function to run interactively whenever the user changes the points marked, returning the table “polygonTable”. If the user clicks in a blank portion of the map (marking no points), the returned polygonTable has zero rows but still has the correct column names and types.
In Spotfire, we add a Marker Layer to the map using polygonTable with the following settings:
• Marker by (Row Number): This ensures that the duplicated point that closes the polygon will be rendered correctly.
• Line Connection: Order each line by the “drawOrder” column in the table.
Here is the resulting polygon in Spotfire:
In just a few clicks we’ve easily configured a highly interactive display to illustrate a territory based on a series of locations, here census tract centers. The bounding polygon is defined by connecting existing locations and is a simple and effective means for rapidly visualizing territories.
Further reading: Unleash the agility of R for the Enterprise (white paper)
Peter Shaw, Ph.D., Sr. Data Scientist, TIBCO Industry Analytics