Hey! This content applies only to previous CARTO products

Please check if it's relevant to your use case. On October 2021 we released a new version of our platform.
You can learn more and read the latest documentation at docs.carto.com

Questions  /  Building Maps  /  Cartography

How can I inset a map feature?

Learn how to inset geometry with PostGIS.

Why do I need an inset?

When you are creating a map, in some cases your areas of interest may be separated by large areas that are not relevant to your map’s purpose. That can force you to use a default zoom level that does not show your preferred level of information.

For example if we want to show all 50 states in the USA, we need to be at this zoom level to include Alaska and Hawaii:

All states in Map View at zoom 3

In traditional print maps you could render Alaska and Hawaii closer to the continental USA using an inset. Map features can also be inset in digital maps.

How can I create an inset?

We can do this in CARTO using a SQL query with PostGIS functions.

ST_TransScale lets us move map geometry and simultaneously increase or decrease it’s size. The first parameter is the geometry we want to modify.

In the next two parameters we specify a number of units to offset the geometry along its East/West axis and its North/South axis, just like in the ST_Translate function. According to PostGIS documentation, units are based on the units defined in spatial reference SRID for this geometry.

In the last two parameters we pick numbers to use as a scale factor along the East/West and North/South axes, the same way they get defined in the ST_Scale function.

ST_Transform lets us define projections. If we use the same projection for all states, Alaska and Hawaii can look distorted. For example here’s how they look after being translated in a map that uses the Albers projection for North America:

Albers North American Projection

Tip: When you change the projection of your Builder map’s data layer, it will not match the default basemaps. Instead you can choose a solid color for your map background.

Instead we can use the Alaska Albers projection for Alaska, and the Hawaii Albers Equal Area Conic projection for Hawaii:

Inset with mixed projections

Here’s the SQL query we used to create the map above:

SELECT 
  CASE 
  WHEN name != 'Alaska' AND name != 'Hawaii'
    THEN ST_Transform(the_geom_webmercator,42303) 
  WHEN name = 'Alaska'
    THEN  
  ST_TransScale(
    ST_Transform(
        the_geom_webmercator
        , 3338
    )
    , -3800000
    , -900000
    , 0.7
    , 0.7
  )
  WHEN name = 'Hawaii'
    THEN 
  ST_TransScale(
    ST_Transform(
        the_geom_webmercator
        , 102007
    )
    , -1200000
    , -450000
    , 1.2
    , 1.2
  )
  END the_geom_webmercator
  , cartodb_id
FROM ne_50m_admin_1_states

Note: Builder widgets will not work on mixed SRID geometries.