{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import geopandas as gpd\n", "from lets_plot import *\n", "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The geodata is provided by © OpenStreetMap contributors and is made available here under the Open Database License (ODbL).\n" ] }, { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from lets_plot import *\n", "from lets_plot.geo_data import *\n", "\n", "# %%\n", "\n", "offices = [\n", " [\"Prague\", \"CZ\", \"Headquarters\", 100],\n", " [\"Petersburg\", \"Russia\", \"R&D Center\", 1000],\n", " [\"Moscow\", \"Russia\", \"R&D Center\", 100],\n", " [\"Novosibirsk\", \"Russia\", \"R&D Center\", 50],\n", " [\"München\", \"Germany\", \"R&D Center\", 200],\n", " [\"Amsterdam\", \"Netherlands\", \"R&D Center\", 100],\n", " [\"Boston\", \"US\", \"R&D Center\", 10],\n", " [\"Marlton\", \"US\", \"Sales\", 10],\n", " [\"Foster City\", \"US\", \"Sales\", 10],\n", "]\n", "\n", "dat = dict(\n", " city=[o[0] for o in offices],\n", " country=[o[1] for o in offices],\n", " kind=[o[2] for o in offices],\n", " size=[o[3] for o in offices],\n", ")\n", "\n", "# %%\n", "\n", "# Geocoding\n", "city_geocoder = geocode_cities(dat['city']).countries(dat['country'])\n", "\n", "# %%\n", "\n", "# The map of JetBrains major offices worldwide.\n", "p = (ggplot(dat) +\n", " # geom_livemap() +\n", " geom_point(\n", " # aes(color='kind', shape='kind', size='size'),\n", " aes(color='kind'),\n", " size=10,\n", " map_join = [['city'], ['city']],\n", " #map=city_geocoder))\n", " map=city_geocoder.get_centroids()))\n", "\n", "p.show()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['naturalearth_cities', 'naturalearth_lowres', 'nybb']" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gpd.datasets.available" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1062\n" ] }, { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
pop_estcontinentnameiso_a3gdp_md_estgeometry
100758288AsiaBhutanBTN6432.0POLYGON ((91.69666 27.77174, 92.10371 27.45261...
10129384297AsiaNepalNPL71520.0POLYGON ((88.12044 27.87654, 88.04313 27.44582...
102204924861AsiaPakistanPAK988200.0POLYGON ((77.83745 35.49401, 76.87172 34.65354...
10334124811AsiaAfghanistanAFG64080.0POLYGON ((66.51861 37.36278, 67.07578 37.35614...
1048468555AsiaTajikistanTJK25810.0POLYGON ((67.83000 37.14499, 68.39203 38.15703...
1055789122AsiaKyrgyzstanKGZ21010.0POLYGON ((70.96231 42.26615, 71.18628 42.70429...
1065351277AsiaTurkmenistanTKM94720.0POLYGON ((52.50246 41.78332, 52.94429 42.11603...
10782021564AsiaIranIRN1459000.0POLYGON ((48.56797 29.92678, 48.01457 30.45246...
10818028549AsiaSyriaSYR50280.0POLYGON ((35.71992 32.70919, 35.70080 32.71601...
1093045191AsiaArmeniaARM26300.0POLYGON ((46.50572 38.77061, 46.14362 38.74120...
1109960487EuropeSwedenSWE498100.0POLYGON ((11.02737 58.85615, 11.46827 59.43239...
1119549747EuropeBelarusBLR165400.0POLYGON ((28.17671 56.16913, 29.22951 55.91834...
11244033874EuropeUkraineUKR352600.0POLYGON ((31.78599 52.10168, 32.15944 52.06125...
11338476269EuropePolandPOL1052000.0POLYGON ((23.48413 53.91250, 23.52754 53.47012...
1148754413EuropeAustriaAUT416600.0POLYGON ((16.97967 48.12350, 16.90375 47.71487...
1159850845EuropeHungaryHUN267600.0POLYGON ((22.08561 48.42226, 22.64082 48.15024...
1163474121EuropeMoldovaMDA18540.0POLYGON ((26.61934 48.22073, 26.85782 48.36821...
11721529967EuropeRomaniaROU441000.0POLYGON ((28.23355 45.48828, 28.67978 45.30403...
1182823859EuropeLithuaniaLTU85620.0POLYGON ((26.49433 55.61511, 26.58828 55.16718...
1191944643EuropeLatviaLVA50650.0POLYGON ((27.28818 57.47453, 27.77002 57.24426...
\n", "
" ], "text/plain": [ " pop_est continent name iso_a3 gdp_md_est \\\n", "100 758288 Asia Bhutan BTN 6432.0 \n", "101 29384297 Asia Nepal NPL 71520.0 \n", "102 204924861 Asia Pakistan PAK 988200.0 \n", "103 34124811 Asia Afghanistan AFG 64080.0 \n", "104 8468555 Asia Tajikistan TJK 25810.0 \n", "105 5789122 Asia Kyrgyzstan KGZ 21010.0 \n", "106 5351277 Asia Turkmenistan TKM 94720.0 \n", "107 82021564 Asia Iran IRN 1459000.0 \n", "108 18028549 Asia Syria SYR 50280.0 \n", "109 3045191 Asia Armenia ARM 26300.0 \n", "110 9960487 Europe Sweden SWE 498100.0 \n", "111 9549747 Europe Belarus BLR 165400.0 \n", "112 44033874 Europe Ukraine UKR 352600.0 \n", "113 38476269 Europe Poland POL 1052000.0 \n", "114 8754413 Europe Austria AUT 416600.0 \n", "115 9850845 Europe Hungary HUN 267600.0 \n", "116 3474121 Europe Moldova MDA 18540.0 \n", "117 21529967 Europe Romania ROU 441000.0 \n", "118 2823859 Europe Lithuania LTU 85620.0 \n", "119 1944643 Europe Latvia LVA 50650.0 \n", "\n", " geometry \n", "100 POLYGON ((91.69666 27.77174, 92.10371 27.45261... \n", "101 POLYGON ((88.12044 27.87654, 88.04313 27.44582... \n", "102 POLYGON ((77.83745 35.49401, 76.87172 34.65354... \n", "103 POLYGON ((66.51861 37.36278, 67.07578 37.35614... \n", "104 POLYGON ((67.83000 37.14499, 68.39203 38.15703... \n", "105 POLYGON ((70.96231 42.26615, 71.18628 42.70429... \n", "106 POLYGON ((52.50246 41.78332, 52.94429 42.11603... \n", "107 POLYGON ((48.56797 29.92678, 48.01457 30.45246... \n", "108 POLYGON ((35.71992 32.70919, 35.70080 32.71601... \n", "109 POLYGON ((46.50572 38.77061, 46.14362 38.74120... \n", "110 POLYGON ((11.02737 58.85615, 11.46827 59.43239... \n", "111 POLYGON ((28.17671 56.16913, 29.22951 55.91834... \n", "112 POLYGON ((31.78599 52.10168, 32.15944 52.06125... \n", "113 POLYGON ((23.48413 53.91250, 23.52754 53.47012... \n", "114 POLYGON ((16.97967 48.12350, 16.90375 47.71487... \n", "115 POLYGON ((22.08561 48.42226, 22.64082 48.15024... \n", "116 POLYGON ((26.61934 48.22073, 26.85782 48.36821... \n", "117 POLYGON ((28.23355 45.48828, 28.67978 45.30403... \n", "118 POLYGON ((26.49433 55.61511, 26.58828 55.16718... \n", "119 POLYGON ((27.28818 57.47453, 27.77002 57.24426... " ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))\n", "print(world.size)\n", "world[100:120]" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot() + geom_polygon(aes(fill='continent'), data=world) + ggsize(800, 400)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot() + geom_path(data=world)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot() + geom_map(data=world)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot() + geom_polygon(map=world)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot() + geom_map(map=world)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot() + geom_point(map=world)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot() + geom_rect(map=world, alpha=.3)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
namegeometry
0Vatican CityPOINT (12.45339 41.90328)
1San MarinoPOINT (12.44177 43.93610)
2VaduzPOINT (9.51667 47.13372)
3LuxembourgPOINT (6.13000 49.61166)
4PalikirPOINT (158.14997 6.91664)
.........
197CairoPOINT (31.24802 30.05191)
198TokyoPOINT (139.74946 35.68696)
199ParisPOINT (2.33139 48.86864)
200SantiagoPOINT (-70.66899 -33.44807)
201SingaporePOINT (103.85387 1.29498)
\n", "

202 rows × 2 columns

\n", "
" ], "text/plain": [ " name geometry\n", "0 Vatican City POINT (12.45339 41.90328)\n", "1 San Marino POINT (12.44177 43.93610)\n", "2 Vaduz POINT (9.51667 47.13372)\n", "3 Luxembourg POINT (6.13000 49.61166)\n", "4 Palikir POINT (158.14997 6.91664)\n", ".. ... ...\n", "197 Cairo POINT (31.24802 30.05191)\n", "198 Tokyo POINT (139.74946 35.68696)\n", "199 Paris POINT (2.33139 48.86864)\n", "200 Santiago POINT (-70.66899 -33.44807)\n", "201 Singapore POINT (103.85387 1.29498)\n", "\n", "[202 rows x 2 columns]" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cities = gpd.read_file(gpd.datasets.get_path('naturalearth_cities'))\n", "cities" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot() + geom_point(data=cities)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot() + geom_point(map=cities)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import random\n", "cityNames = cities['name'].tolist()\n", "cityValue = [random.randint(10, 100) for _ in cityNames]\n", "cityData = dict(city=cityNames, val=cityValue)\n", "ggplot() + geom_point(aes(color='val'), data=cityData, map=cities, map_join=('city', 'name'), size=7)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
keygeometry
0Vatican CityPOINT (12.45339 41.90328)
1San MarinoPOINT (12.44177 43.93610)
2VaduzPOINT (9.51667 47.13372)
3LuxembourgPOINT (6.13000 49.61166)
4PalikirPOINT (158.14997 6.91664)
.........
197CairoPOINT (31.24802 30.05191)
198TokyoPOINT (139.74946 35.68696)
199ParisPOINT (2.33139 48.86864)
200SantiagoPOINT (-70.66899 -33.44807)
201SingaporePOINT (103.85387 1.29498)
\n", "

202 rows × 2 columns

\n", "
" ], "text/plain": [ " key geometry\n", "0 Vatican City POINT (12.45339 41.90328)\n", "1 San Marino POINT (12.44177 43.93610)\n", "2 Vaduz POINT (9.51667 47.13372)\n", "3 Luxembourg POINT (6.13000 49.61166)\n", "4 Palikir POINT (158.14997 6.91664)\n", ".. ... ...\n", "197 Cairo POINT (31.24802 30.05191)\n", "198 Tokyo POINT (139.74946 35.68696)\n", "199 Paris POINT (2.33139 48.86864)\n", "200 Santiago POINT (-70.66899 -33.44807)\n", "201 Singapore POINT (103.85387 1.29498)\n", "\n", "[202 rows x 2 columns]" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "citiesWithKey = cities.rename(columns = {'name':'key'}, inplace = False)\n", "citiesWithKey" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot() + geom_point(aes(color='val'), data=cityData, map=citiesWithKey, map_join=('city', 'key'), size=7)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot() + ggsize(800, 400)\\\n", "+ geom_polygon(aes(fill='continent'), data=world)\\\n", "+ geom_point(data=cities, color='cyan')" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot() + ggsize(800, 400)\\\n", "+ geom_rect(aes(fill='continent'), data=world)\\\n", "+ geom_point(data=cities, color='cyan', size=7, alpha=.5)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "25\n" ] }, { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
BoroCodeBoroNameShape_LengShape_Areageometry
05Staten Island330470.0103321.623820e+09MULTIPOLYGON (((970217.022 145643.332, 970227....
14Queens896344.0477633.045213e+09MULTIPOLYGON (((1029606.077 156073.814, 102957...
23Brooklyn741080.5231661.937479e+09MULTIPOLYGON (((1021176.479 151374.797, 102100...
31Manhattan359299.0964716.364715e+08MULTIPOLYGON (((981219.056 188655.316, 980940....
42Bronx464392.9918241.186925e+09MULTIPOLYGON (((1012821.806 229228.265, 101278...
\n", "
" ], "text/plain": [ " BoroCode BoroName Shape_Leng Shape_Area \\\n", "0 5 Staten Island 330470.010332 1.623820e+09 \n", "1 4 Queens 896344.047763 3.045213e+09 \n", "2 3 Brooklyn 741080.523166 1.937479e+09 \n", "3 1 Manhattan 359299.096471 6.364715e+08 \n", "4 2 Bronx 464392.991824 1.186925e+09 \n", "\n", " geometry \n", "0 MULTIPOLYGON (((970217.022 145643.332, 970227.... \n", "1 MULTIPOLYGON (((1029606.077 156073.814, 102957... \n", "2 MULTIPOLYGON (((1021176.479 151374.797, 102100... \n", "3 MULTIPOLYGON (((981219.056 188655.316, 980940.... \n", "4 MULTIPOLYGON (((1012821.806 229228.265, 101278... " ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "nybb = gpd.read_file(gpd.datasets.get_path('nybb'))\n", "print(nybb.size)\n", "nybb.head()" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot() + geom_polygon(aes(fill='BoroName'), data=nybb)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot() + geom_polygon(aes(fill='BoroName'), data=nybb) + coord_cartesian(xlim=[-74.05,-73.95],ylim=[40.675,40.725])" ] }, { "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.7.10" } }, "nbformat": 4, "nbformat_minor": 2 }