CARTOframes is built on top of Pandas and GeoPandas. Therefore, it's compatible with all the data formats supported in those projects like CSV, GeoJSON, Shapefile, etc. This guide will show how to load different data files into DataFrames and how to interact with the CARTO platform to upload DataFrames into tables and download tables or SQL queries into DataFrames.
There are two main concepts we should know before continuing with the guide:
Every time we manage Geographic data, a GeoDataFrame should be used. In case a DataFrame with an encoded geometry column is used (WKB, WKT, etc) every method contains a geom_col
param to provide the name of that column and decode the geometry internally.
For more information, you can see all the examples here.
To show how to manage your data with CARTOframes, we will follow the next steps:
In [1]:
from geopandas import read_file
neighborhoods_gdf = read_file('https://data.sfgov.org/api/geospatial/pty2-tcw4?method=export&format=GeoJSON')
neighborhoods_gdf.head()
Out[1]:
In [2]:
from cartoframes.viz import Layer
Layer(neighborhoods_gdf)
Out[2]:
In [3]:
from pandas import read_csv
from geopandas import GeoDataFrame, points_from_xy
df = read_csv('http://data.sfgov.org/resource/wg3w-h783.csv')
# Clean NaN values
df = df[df['longitude'].notna()]
incidents_gdf = GeoDataFrame(df, geometry=points_from_xy(df['longitude'], df['latitude']))
incidents_gdf.head()
Out[3]:
In [4]:
from cartoframes.viz import Layer
Layer(incidents_gdf)
Out[4]:
Let's upload both GeoDataFrames to CARTO so we can see how to interact with the platform. In order to continue, you have to set your CARTO credentials. If you aren't sure about your API key, check the Authentication guide to learn how to get it.
In [5]:
from cartoframes.auth import set_default_credentials
set_default_credentials('creds.json')
In [6]:
from cartoframes import to_carto
neighborhoods_table = 'sf_neighborhoods'
incidents_table = 'sf_incidents'
to_carto(neighborhoods_gdf, neighborhoods_table, if_exists='replace')
to_carto(incidents_gdf, incidents_table, if_exists='replace')
Out[6]:
Now that we have uploaded the data, we can directly visualize the tables using:
Layer(neighborhoods_table)
Layer(incidents_table)
In [7]:
from cartoframes import read_carto
incidents_neighborhoods_gdf = read_carto('''
SELECT n.cartodb_id, n.the_geom, n.the_geom_webmercator, n.name, COUNT(*) AS incidents
FROM sf_incidents i INNER JOIN sf_neighborhoods n ON ST_Intersects(i.the_geom, n.the_geom)
GROUP BY n.cartodb_id
''')
incidents_neighborhoods_gdf.head()
Out[7]:
In [8]:
from cartoframes.viz import color_continuous_style
Layer(incidents_neighborhoods_gdf, style=color_continuous_style('incidents'))
Out[8]: