Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" hex_id value is_true float_value empty time\n",
"0 89283082c2fffff 64 True 64.1 11/1/17 11:00\n",
"1 8928308288fffff 73 True 73.1 11/1/17 11:00\n",
"2 89283082c07ffff 65 True 65.1 11/1/17 11:00\n",
"3 89283082817ffff 74 True 74.1 11/1/17 11:00\n",
"4 89283082c3bffff 66 True 66.1 11/1/17 11:00\n",
".. ... ... ... ... ... ...\n",
"83 8928309537bffff 4 True 4.1 11/1/17 11:00\n",
"84 89283082d93ffff 6 True 6.1 11/1/17 11:00\n",
"85 89283082d73ffff 1 True 1.1 11/1/17 13:00\n",
"86 8928309530bffff 1 True 1.1 11/1/17 11:00\n",
"87 8928309532bffff 1 True 1.1 11/1/17 11:00\n",
"\n",
"[88 rows x 6 columns]\n"
]
}
],
"source": [
"import pandas as pd\n",
"df = pd.read_csv('hex-data.csv')\n",
"df = df.fillna('')\n",
"print(df)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import json\n",
"with open('sf_zip_geo.json', 'r') as f:\n",
" geojson = f.read()\n",
"\n",
"json_data=json.loads(geojson)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# assign config = hex_config\n",
"%run hex_config.py"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "bb76ecb992224bf39ba7e16d77a8b454",
"version_major": 2,
"version_minor": 1
},
"text/plain": [
"<keplergl.widget.KeplerGl object at 0x12075e9c0>"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import keplergl\n",
"w1 = keplergl.KeplerGl(height=500, data={\"data_1\": df}, config=config)\n",
"# w1 = keplergl.KeplerGl(height=500)\n",
"w1"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"w1.add_data(df, 'data_1')"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'version': 'v1',\n",
" 'config': {'visState': {'filters': [],\n",
" 'layers': [{'id': 'jdys7lp',\n",
" 'type': 'hexagonId',\n",
" 'config': {'dataId': 'data_1',\n",
" 'label': 'H3 Hexagon',\n",
" 'color': [250, 116, 0],\n",
" 'highlightColor': [252, 242, 26, 255],\n",
" 'columns': {'hex_id': 'hex_id'},\n",
" 'isVisible': True,\n",
" 'visConfig': {'colorRange': {'category': 'Uber',\n",
" 'type': 'sequential',\n",
" 'colors': ['#194266',\n",
" '#355C7D',\n",
" '#63617F',\n",
" '#916681',\n",
" '#C06C84',\n",
" '#D28389',\n",
" '#E59A8F',\n",
" '#F8B195'],\n",
" 'reversed': False,\n",
" 'name': 'Sunrise'},\n",
" 'filled': True,\n",
" 'opacity': 0.8,\n",
" 'outline': False,\n",
" 'strokeColor': None,\n",
" 'strokeColorRange': {'name': 'Global Warming',\n",
" 'type': 'sequential',\n",
" 'category': 'Uber',\n",
" 'colors': ['#4C0035',\n",
" '#880030',\n",
" '#B72F15',\n",
" '#D6610A',\n",
" '#EF9100',\n",
" '#FFC300']},\n",
" 'strokeOpacity': 0.8,\n",
" 'thickness': 2,\n",
" 'coverage': 1,\n",
" 'enable3d': True,\n",
" 'sizeRange': [0, 500],\n",
" 'coverageRange': [0, 1],\n",
" 'elevationScale': 5,\n",
" 'enableElevationZoomFactor': True,\n",
" 'fixedHeight': False},\n",
" 'hidden': False,\n",
" 'textLabel': [{'field': None,\n",
" 'color': [255, 255, 255],\n",
" 'size': 50,\n",
" 'offset': [0, 0],\n",
" 'anchor': 'middle',\n",
" 'alignment': 'center',\n",
" 'outlineWidth': 0,\n",
" 'outlineColor': [255, 0, 0, 255],\n",
" 'background': False,\n",
" 'backgroundColor': [0, 0, 200, 255]}]},\n",
" 'visualChannels': {'colorField': {'name': 'value', 'type': 'integer'},\n",
" 'colorScale': 'quantize',\n",
" 'strokeColorField': None,\n",
" 'strokeColorScale': 'quantile',\n",
" 'sizeField': {'name': 'value', 'type': 'integer'},\n",
" 'sizeScale': 'linear',\n",
" 'coverageField': None,\n",
" 'coverageScale': 'linear'}}],\n",
" 'effects': [],\n",
" 'interactionConfig': {'tooltip': {'fieldsToShow': {'data_1': [{'name': 'hex_id',\n",
" 'format': None},\n",
" {'name': 'value', 'format': None}]},\n",
" 'compareMode': False,\n",
" 'compareType': 'absolute',\n",
" 'enabled': True},\n",
" 'brush': {'size': 0.5, 'enabled': False},\n",
" 'geocoder': {'enabled': False},\n",
" 'coordinate': {'enabled': False}},\n",
" 'layerBlending': 'normal',\n",
" 'overlayBlending': 'normal',\n",
" 'splitMaps': [],\n",
" 'animationConfig': {'currentTime': None, 'speed': 1},\n",
" 'editor': {'features': [], 'visible': True}},\n",
" 'mapState': {'bearing': 2.6192893401015205,\n",
" 'dragRotate': True,\n",
" 'latitude': 37.76209132041332,\n",
" 'longitude': -122.42590232651203,\n",
" 'pitch': 37.374216241015446,\n",
" 'zoom': 12.32053899007826,\n",
" 'isSplit': False,\n",
" 'isViewportSynced': True,\n",
" 'isZoomLocked': False,\n",
" 'splitMapViewports': []},\n",
" 'mapStyle': {'styleType': 'dark-matter',\n",
" 'topLayerGroups': {},\n",
" 'visibleLayerGroups': {'label': True,\n",
" 'road': True,\n",
" 'border': False,\n",
" 'building': True,\n",
" 'water': True,\n",
" 'land': True,\n",
" '3d building': False},\n",
" 'threeDBuildingColor': [15.035172933000911,\n",
" 15.035172933000911,\n",
" 15.035172933000911],\n",
" 'backgroundColor': [0, 0, 0],\n",
" 'mapStyles': {}},\n",
" 'uiState': {'mapControls': {'mapLegend': {'active': False}}}}}"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"w1.config"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"w1.add_data(json_data, 'geojson')"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Map saved to first_map.html!\n"
]
}
],
"source": [
"w1.save_to_html(file_name='first_map.html', data={\"data_1\": df}, config=config)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Apply a config with polygon color scale\n",
"\n",
"You can set layer styles, basemap, and more through the `config` dict.\n",
"The `config` structure mirrors what `w1.config` returns — you can interactively\n",
"style your map in the widget, then copy `w1.config` into your code.\n",
"\n",
"Below we load the GeoJSON zip code polygons and apply a config that:\n",
"- Uses a **geojson** layer with a quantize color scale on `OBJECTID`\n",
"- Sets the basemap to **positron** (a light free basemap, no Mapbox token needed)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "80748740e6f44091870ef47350115a31",
"version_major": 2,
"version_minor": 1
},
"text/plain": [
"<keplergl.widget.KeplerGl object at 0x130837980>"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"polygon_config = {\n",
" 'version': 'v1',\n",
" 'config': {\n",
" 'visState': {\n",
" 'layers': [{\n",
" 'type': 'geojson',\n",
" 'config': {\n",
" 'dataId': 'zip_codes',\n",
" 'label': 'SF Zip Codes',\n",
" 'isVisible': True,\n",
" 'visConfig': {\n",
" 'opacity': 0.8,\n",
" 'filled': True,\n",
" 'stroked': True,\n",
" 'thickness': 1,\n",
" 'colorRange': {\n",
" 'name': 'Sunrise 8',\n",
" 'type': 'sequential',\n",
" 'category': 'Uber',\n",
" 'colors': [\n",
" '#194266', '#355C7D', '#63617F', '#916681',\n",
" '#C06C84', '#D28389', '#E59A8F', '#F8B195'\n",
" ]\n",
" }\n",
" },\n",
" 'columns': {'geojson': '_geojson'}\n",
" },\n",
" 'visualChannels': {\n",
" 'colorField': {'name': 'OBJECTID', 'type': 'integer'},\n",
" 'colorScale': 'quantize'\n",
" }\n",
" }]\n",
" },\n",
" 'mapStyle': {\n",
" 'styleType': 'positron'\n",
" }\n",
" }\n",
"}\n",
"\n",
"w2 = keplergl.KeplerGl(\n",
" height=500,\n",
" data={'zip_codes': json_data},\n",
" config=polygon_config,\n",
" theme='light',\n",
" app_name='SF Zip Codes'\n",
")\n",
"w2"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Map saved to sf_zip_styled.html!\n"
]
}
],
"source": [
"# Export the styled polygon map\n",
"w2.save_to_html(file_name='sf_zip_styled.html')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}