Changing Map Projections

Data/SQL Guides

Changing Map Projections

By default, CARTO stores geospatial data using the_geom column from your dataset. This column displays the latitude and longitude in a single projection, using the WGS84 cartographic method. Projections are a way of representing the three-dimensional Earth, in order to make it appear correctly in a web browser and show all the required parts of the world.

Most online maps use the Web Mercator projection, mainly because it produces simple, loadable, map tiles for efficient rendering on web browsers. A hidden column in your dataset, the_geom_webmercator, is used to store data using the Web Mercator projection. This column is automatically updated as the geometry changes.

Projections distort how the surface of the world appears, enabling it to twist or lie flat. Choosing a map projection depends on the scale of your map, the type of data that you are discovering, and the story that you are trying to convey.

In CARTO, you can change of the projection of your map with just a few lines of SQL code.

Viewing the Hidden the_geom_webmercator Column

By default, the spatial coordinates of your data are stored in the_geom column of your dataset. CARTO projects these coordinates, using the Web Mercator projection, and stores them in the hidden the_geom_webmercator column . This column is not visible in the Data View of your map, unless explicitly queried through SQL.

  1. Import the template .carto file packaged from the "Download resources" of this guide and create the map. You may notice that it is the same map that was created in the Style by Value Guide.

    Click on "Download resources" from this guide to download the zip file to your local machine. Extract the zip file to view the .carto file(s) used for this guide.

    Builder opens, displaying three map layers, the first layer contains data for world population by continent, the second layer contains ocean data, and the third layer contains land data.

  2. From the World Population map layer, click the Data View to view your dataset as a table.

    The Data View and Map View appear as icons on your map visualization when a map layer is selected. Click to switch between viewing your connected dataset as a table, or show the map view of your data.
  3. From the DATA tab of the selected layer, switch the slider from VALUES to SQL, and apply the following SQL query:

     SELECT the_geom_webmercator
     FROM world_population_by_continent
    

    the_geom_webmercator

    (Since the map styling is linked to a column that was not queried, an error appears. This is expected behavior). the_geom_webmercator column is added to your dataset with empty results; since your original map points still appear on the map visualization. This is because CARTO is drawing your map with the data in the_geom_webmercator column.

  4. Apply the following SQL query to replace the_geom_webmercator with the_geom data:

     SELECT the_geom
     FROM world_population_by_continent
    

    If you switch to the Map View and refresh the map, all the points on your map visualization disappear.

    Even though the_geom is not used for rendering the visualization, it is helpful since it is easier to build a map based on latitude and longitude coordinates. Additionally, all analyses queries are run based on the the_geom column of your dataset.

Adding a Projection to your Account

Each CARTO account comes with a set of default projections.

  1. To access the list of available projections, apply the following SQL query in the SQL view of a map layer.

     SELECT * FROM spatial_ref_sys
    

    In the Data View, your table displays the default projections that are included in your CARTO account.

    If the projection you want to use is not in the results, you can add new projections with a SQL query. For this example, add the World Robinson projection, which works well for world maps. The SRID (Spatial Reference System Identifier) code for this projection is 54030.

  2. Paste and apply the following SQL query in your SQL view.

     INSERT into spatial_ref_sys (srid, auth_name, auth_srid, proj4text, srtext) values ( 54030, 'ESRI', 54030, '+proj=robin +lon_0=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs ', 'PROJCS["World_Robinson",GEOGCS["GCS_WGS_1984",DATUM["WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]],PROJECTION["Robinson"],PARAMETER["False_Easting",0],PARAMETER["False_Northing",0],PARAMETER["Central_Meridian",0],UNIT["Meter",1],AUTHORITY["EPSG","54030"]]');
    

    This SQL query inserts the World Robinson projection into the spatial_ref_sys table, as indicated by SRID 54030. You can now use this projection to make your map!

Adding projections is a one-time process. Once a new projection is added to your account, it is available to use in any map from your account. If you have an Enterprise account, adding the projection will make it available to all of your organization users.

Applying the Robinson Projection to your World Map

Robinson projection works well for displaying the entire world as a map, so let's apply that projection. Since the_geom_webmercator stores the actual spatial data points using a different projection system, we need to transform that column.

  1. From the World Population map layer, apply the following PostGIS ST_Transform function to transform the_geom_webmercator data with the Robinson projection (54030 SRID).

     SELECT ST_Transform(the_geom_webmercator, 54030) as the_geom_webmercator,
     cartodb_id, the_geom, pop_2015, continent_name, country_code, country_name
     FROM world_population_by_continent
    
    In addition to applying ST_Transform on the_geom_webmercator, you must also select the_geom and the cartodb_id columns for the layer to be projected. You can select any other required columns from your dataset, as separated by commas.

    All the points re-appear on your map, and are projected differently using the Robinson projection.

    To summarize, the ST_Transform function works by:

    • Applying the ST_Transform function to the_geom_webmercator data.
    • CARTO identifies the Robinson projection using the SRID: 54030 projection integer.
    • ST_Transform transforms the existing WGS84 data within the_geom_webmercator into Robinson projections.
    • The map renders according to the new projection data, with the_geom_webmercator.
  2. Apply the same projection to the other map layers, to ensure the correct rendering of data.

    Apply the Robinson projection to the Land map layer:

     SELECT cartodb_id,
     the_geom,
     ST_Transform(the_geom_webmercator, 54030) as the_geom_webmercator,
     scalerank, featurecla, created_at, updated_at
     FROM ne_50m_land
    

    Apply the Robinson projection to the Ocean map layer:

     SELECT cartodb_id,
     the_geom,
     ST_Transform(the_geom_webmercator, 54030) as the_geom_webmercator,
     scalerank, featurecla, created_at, updated_at
     FROM ne_50m_ocean
    
  3. Since the default basemap are for Web Mercator projections, change your basemap to better visualize the results. For this example, select COLOR as the source.

    Robinson map projection

Projecting Non-Geometry Coordinates

If you are projecting your data using other coordinate systems that are not using lon/lat values, you will need to transform your coordinates in order to be compatible with CARTO. If you are unsure what the coordinate conversion is, you can add the CRS property to your GeoJSON file before uploading it to CARTO.

CARTO reads the CRS property and automatically converts your coordinates to EPSG:4326 (lon/lat) values. Since the projection file represents the latitude and longitude in the_geom column of your dataset, it is highly recommended to verify that your geometries appear correctly, otherwise you may be using a projection file that is not 4326 compatible.

External Resources