Creating Custom Map Regions

(Wil Gieseler) #1

Using a custom map region is easy in Looker. Here’s how:

Let’s say I have a table that looks like this:

Customer Neighborhood Customer Average Age
downtown 19.5292
eastside 54.4626
westside 34.9534

I’d like to map “Customer Neighborhood” to a particular geographic region so it can be visualized on a map.

Prepare Region Data

To do this, I need a data file that contains the geographic shapes of each region, and also contains metadata that associates the region with the data in my database.

Looker uses a format called TopoJSON to store this data in a compact way.

TopoJSON files can be easily created from many common shapefile formats, or you can draw one yourself with online tools.

Here’s instructions for finding and converting region data.

In my case, I’ve already got a TopoJSON file that provides this mapping.

Each of the regions in my TopoJSON file have a property called neighborhood that matches the values of the “Customer Neighborhood” field.

Upload Region Data

To import the region data into Looker, I can simply drag-and-drop my TopoJSON file into the project. This data will be committed alongside my LookML code and updated like any other code in your project.

Uploading region data is available starting in Looker 4.8.

Create the Map Layer

Now that my region data’s in the project, I need to create a map layer in my LookML model.

In the model file I can add a definition like this:

map_layer: my_neighborhood_layer {
  file: "neighborhoods.topojson"
  property_key: "neighborhood"

The file parameter references the name of the file in my project that contains the region data. The property_key is the property of the dataset that you’d like to expose within Looker.

If you’re not sure what the key is, just omit this property and Looker will attempt to select the appropriate key.

Pro Tip: You can also use a TopoJSON file that’s hosted elsewhere online by specifying url instead of file in the layer definition.

Associate the Map Layer with Data

The only thing left to do is associate the dimension in my view with the newly-created map layer.

dimension: neighborhood {
  sql: ${TABLE}.neighborhood ;;
  map_layer_name: my_neighborhood_layer

This tells Looker that the values of this dimension are associated with the property_key exposed by the map layer.

It also lets Looker know that it’s possible to display this data on a map.

Viewing the Map

Queries that uses the “Neighborhood” dimension can now be displayed on a map:

1 Like

Converting Shapefiles to TopoJSON
Using custom topojson maps in Looker
Creating Custom Maps Files from Shapefiles
Turn a shapefile into a custom topojson to visualize a map in Looker
Heat Map for Indian regions
Easier mapping with a collection of pre-defined map_layers