bindings/pydeck-carto/examples/notebooks/carto_layer.ipynb
Render cloud from a CARTO connection using deck.gl CartoLayer.
import pydeck as pdk
import pydeck_carto as pdkc
from carto_auth import CartoAuth
carto_auth = CartoAuth.from_oauth()
pdkc.register_carto_layer()
Every CARTO account has a CARTO Data Warehouse to work with. In this example, a table with world airports from the CARTO DW is rendered.
layer = pdk.Layer(
"CartoLayer",
data="carto-demo-data.demo_tables.world_airports",
type_=pdkc.MapType.TABLE,
connection=pdkc.CartoConnection.CARTO_DW,
credentials=pdkc.get_layer_credentials(carto_auth),
get_fill_color=[238, 77, 90],
point_radius_min_pixels=2.5,
pickable=True,
)
view_state = pdk.ViewState(latitude=0, longitude=0, zoom=1)
tooltip = {"html": "<b>Name:</b> {name}", "style": {"color": "white"}}
pdk.Deck(
layer, map_style=pdk.map_styles.ROAD, initial_view_state=view_state, tooltip=tooltip
)
CARTO allows rendering cloud data from BigQuery, Snowflake, Redshift, Postgres and Databricks using custom connections.
This example performs a spatial intersection to display the airports in Spain. Note that it uses a BigQuery connection called "bigquery".
layer = pdk.Layer(
"CartoLayer",
data="""
SELECT a.geom, a.name
FROM `carto-demo-data.demo_tables.world_airports` AS a,
`carto-do-public-data.natural_earth.geography_glo_admin0countries_410` AS g
WHERE g.ADMIN = 'Spain' AND
ST_INTERSECTS(a.geom, g.geom)
""",
type_=pdkc.MapType.QUERY,
connection=pdk.types.String("bigquery"),
credentials=pdkc.get_layer_credentials(carto_auth),
get_fill_color=[238, 77, 90],
point_radius_min_pixels=2.5,
pickable=True,
)
view_state = pdk.ViewState(latitude=36, longitude=-7.44, zoom=4)
tooltip = {"html": "<b>Name:</b> {name}", "style": {"color": "white"}}
pdk.Deck(
layer, map_style=pdk.map_styles.ROAD, initial_view_state=view_state, tooltip=tooltip
)
Any data error is displayed instead of the map to provide instant feedback about the input parameters. For example, the user is not authorized, the connection or the column names do not exist, etc.