The pydeck-carto package is a wrapper of pydeck that uses the CartoLayer to enable visualizing tables and tilesets available in your cloud data warehouses as map layers within your data science workflows in Python notebooks. It provides access to the data available in the connections created in the CARTO platform, and it allows you to build visualizations with several pre-built color styling functions.
CartoLayer
CartoLayer renders cloud data from any data warehouse connection established in your CARTO platform (e.g. to Google BigQuery, Snowflake, Redshift, Postgres, and Databricks). It also provides access to data objects in the CARTO Data Warehouse associated with your CARTO account. It’s a Python wrapper over the CartoLayer in deck.gl.
Pydeck-carto is a package outside of pydeck, so calling pydeck_carto.register_carto_layer() is required to register CartoLayer in pydeck.
Styling
CARTO provides data-driven out-of-the-box styling functions for colors. Check the full list of Carto styles in deck.gl.
The different functions available are:
color_bins: Helper function for creating a color bins style. Data values of each attribute are rounded down to the nearest value in the domain and are then styled with the corresponding color.
color_categories: Helper function for creating a color category style. Data values of each attribute listed in the domain are mapped one to one with corresponding colors in the color range.
color_continuous: Helper function for creating a color continuous style. Data values of each field are interpolated linearly across values in the domain and are then styled with a blend of the corresponding color in the range.
For more information and details about function parameters, default values etc., please go to the reference.
Examples
In this example, we visualize the spatial data from two different tables available in a connection created to a database in Snowflake. The same can apply to BigQuery, Redshift, Databricks or a Postgresql-based database.
# Register CartoLayer in pydeckpdkc.register_carto_layer()hexagons_query ="""SELECT CARTO_DEV_DATA.carto.H3_BOUNDARY("H3") H3_GEOM, CROSSFIT_GYMS / TOTAL_GYMS AS dominance_ratio FROM SFDATABASE.CARTO.GYMS_CA_TOTAL_CENTROID """credentials = pdkc.get_layer_credentials(carto_auth)hexagons = pdk.Layer("CartoLayer", data = hexagons_query, type_=pdkc.MapType.QUERY, geo_column=pdk.types.String("H3_GEOM"), connection=pdk.types.String("my_carto_sf_connection"), credentials=credentials, opacity=0.2, stroked=True, get_fill_color=pdkc.styles.color_continuous("DOMINANCE_RATIO", [x/10for x inrange(10)], colors ="Tropic"), get_line_color=[0,42,42], line_width_min_pixels=2 )points_query ="""SELECT GEOM, 'crossfit' AS CATEGORYFROM SFDATABASE.CARTO.GYMS_CA_CROSSFITUNION ALLSELECT GEOM, 'competitors' AS CATEGORYFROM SFDATABASE.CARTO.GYMS_CA_COMPETITION"""points = pdk.Layer("CartoLayer", data = points_query, type_=pdkc.MapType.QUERY, geo_column=pdk.types.String("GEOM"), connection=pdk.types.String("my_carto_sf_connection"), credentials=credentials, opacity=0.8, stroked=True, pickable=True, point_radius_min_pixels=2, get_fill_color=pdkc.styles.color_categories("CATEGORY", ["competitors", "crossfit"], colors ="Tropic") )view_state = pdk.ViewState(latitude=33.64, longitude=-117.94, zoom=5)r = pdk.Deck( [hexagons, points], initial_view_state=view_state, map_style=pdk.map_styles.LIGHT,)r.to_html(iframe_height=700)
In this second example, we visualize a point-based table available from the CARTO Data Warehouse connection.
# Register CartoLayer in pydeckpdkc.register_carto_layer()# Render CartoLayer in pydeck with color_bins stylelayer = pdk.Layer("CartoLayer", data="SELECT geom, price_num FROM `shared.01_listings_la_2021_5_reviews`", type_=pdkc.MapType.QUERY, connection=pdkc.CartoConnection.CARTO_DW, credentials=pdkc.get_layer_credentials(carto_auth), point_radius_min_pixels=2.5, get_fill_color=pdkc.styles.color_bins("price_num",[0, 100, 200, 300, 400, 500], "PinkYl"), get_line_color=[0, 0, 0, 100])view_state = pdk.ViewState(latitude=34.5, longitude=-118, zoom=8)pdk.Deck(layer, map_style=pdk.map_styles.ROAD, initial_view_state=view_state)
And finally in this third example, we leverage data from a connection between CARTO and Databricks leveraging the native support for the H3 spatial index in both Databricks an CARTO platform.