Skip to content

Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.

5.6 KB
Raw Blame
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import geopandas"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.DataFrame(\n",
    "    {'City': ['Buenos Aires', 'Brasilia', 'Santiago', 'Bogota', 'Caracas'],\n",
    "     'Country': ['Argentina', 'Brazil', 'Chile', 'Colombia', 'Venezuela'],\n",
    "     'Latitude': [-34.58, -15.78, -33.45, 4.60, 10.48],\n",
    "     'Longitude': [-58.66, -47.91, -70.66, -74.08, -66.86]})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "point_gdf = geopandas.GeoDataFrame(df, geometry=geopandas.points_from_xy(df.Longitude, df.Latitude))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>OBJECTID</th>\n",
       "      <th>ZIP_CODE</th>\n",
       "      <th>ID</th>\n",
       "      <th>geometry</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>94107</td>\n",
       "      <td>94107</td>\n",
       "      <td>POLYGON ((-122.40116 37.78202, -122.40037 37.7...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>94105</td>\n",
       "      <td>94105</td>\n",
       "      <td>POLYGON ((-122.3925 37.79377, -122.39189 37.79...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>94129</td>\n",
       "      <td>94129</td>\n",
       "      <td>POLYGON ((-122.47099 37.78753, -122.47229 37.7...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>94121</td>\n",
       "      <td>94121</td>\n",
       "      <td>POLYGON ((-122.50446 37.78807, -122.50415 37.7...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>94118</td>\n",
       "      <td>94118</td>\n",
       "      <td>POLYGON ((-122.44889 37.77839, -122.44977 37.7...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   OBJECTID  ZIP_CODE     ID  \\\n",
       "0         1     94107  94107   \n",
       "1         2     94105  94105   \n",
       "2         3     94129  94129   \n",
       "3         4     94121  94121   \n",
       "4         5     94118  94118   \n",
       "\n",
       "                                            geometry  \n",
       "0  POLYGON ((-122.40116 37.78202, -122.40037 37.7...  \n",
       "1  POLYGON ((-122.3925 37.79377, -122.39189 37.79...  \n",
       "2  POLYGON ((-122.47099 37.78753, -122.47229 37.7...  \n",
       "3  POLYGON ((-122.50446 37.78807, -122.50415 37.7...  \n",
       "4  POLYGON ((-122.44889 37.77839, -122.44977 37.7...  "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "zipcode_gdf = geopandas.read_file('sf_zip_geo.json')\n",
    "display(zipcode_gdf.head(5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "e8da0655899f4786aedd855e9d9d5fe8",
       "version_major": 2,
       "version_minor": 1
      },
      "text/plain": [
       "<keplergl.widget.KeplerGl object at 0x1161ce2d0>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import keplergl\n",
    "w1 = keplergl.KeplerGl(height=500)\n",
    "w1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "w1.add_data(data=zipcode_gdf, name=\"zipcode\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "w1.add_data(data=point_gdf, name='cities')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Map saved to city_map.html!\n"
     ]
    }
   ],
   "source": [
    "w1.save_to_html(file_name='city_map.html', read_only=True)"
   ]
  },
  {
   "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
}