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.
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.
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.
From the World Population map layer, click the Data View to view your dataset as a table.
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
(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
Apply the following SQL query to replace
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.
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.
Each CARTO account comes with a set of default projections.
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.
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!
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.
From the World Population map layer, apply the following PostGIS
ST_Transform function to transform
the_geom_webmercator data with the Robinson projection (
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
the_geom_webmercator, you must also select
cartodb_idcolumns 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:
ST_Transformtransforms the existing WGS84 data within
the_geom_webmercatorinto Robinson projections.
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
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.
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.
We would love to hear from you! Was it easy to understand? Do you need more information? Let us know.