{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Geocoding and Isolines Services\n",
"\n",
"This example illustrates how to combine both the Geocoding and Isolines Data Services.\n",
"\n",
"_Note: You'll need [CARTO Account](https://carto.com/signup) credentials to reproduce this example._"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from cartoframes.auth import set_default_credentials\n",
"\n",
"set_default_credentials('creds.json')"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" address | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" Calle Serrano 15 | \n",
"
\n",
" \n",
" 1 | \n",
" Calle de San Pedro 21 | \n",
"
\n",
" \n",
" 2 | \n",
" Calle Gran Vía 46 | \n",
"
\n",
" \n",
" 3 | \n",
" Paseo de la Castellana 200 | \n",
"
\n",
" \n",
" 4 | \n",
" Calle Ntra. Sra. del Carmen 7 | \n",
"
\n",
" \n",
" 5 | \n",
" Calle de San Germán 12 | \n",
"
\n",
" \n",
" 6 | \n",
" Calle de Bravo Murillo 377 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" address\n",
"0 Calle Serrano 15\n",
"1 Calle de San Pedro 21\n",
"2 Calle Gran Vía 46\n",
"3 Paseo de la Castellana 200\n",
"4 Calle Ntra. Sra. del Carmen 7\n",
"5 Calle de San Germán 12\n",
"6 Calle de Bravo Murillo 377"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from pandas import DataFrame\n",
"\n",
"df = DataFrame([\n",
" ['Calle Serrano 15'],\n",
" ['Calle de San Pedro 21'],\n",
" ['Calle Gran Vía 46'],\n",
" ['Paseo de la Castellana 200'],\n",
" ['Calle Ntra. Sra. del Carmen 7'],\n",
" ['Calle de San Germán 12'],\n",
" ['Calle de Bravo Murillo 377'],\n",
" ],\n",
" columns=['address']\n",
")\n",
"df"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Success! Data geocoded correctly\n"
]
}
],
"source": [
"from cartoframes.data.services import Geocoding\n",
"\n",
"gc = Geocoding()\n",
"gdf, metadata = gc.geocode(df, street='address', city={'value': 'Madrid'}, country={'value': 'Spain'})"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" the_geom | \n",
" address | \n",
" gc_status_rel | \n",
" carto_geocode_hash | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" POINT (-3.68831 40.42478) | \n",
" Calle Serrano 15 | \n",
" 0.82 | \n",
" 26a9e50f26e86d071fa0ff46b9291863 | \n",
"
\n",
" \n",
" 1 | \n",
" POINT (-3.69488 40.41089) | \n",
" Calle de San Pedro 21 | \n",
" 0.95 | \n",
" 905794cd20618c2d43d7e4c8db56436c | \n",
"
\n",
" \n",
" 2 | \n",
" POINT (-3.70588 40.42049) | \n",
" Calle Gran Vía 46 | \n",
" 0.92 | \n",
" 8bcb00020040f7a01edb6085c7833d2a | \n",
"
\n",
" \n",
" 3 | \n",
" POINT (-3.68898 40.46239) | \n",
" Paseo de la Castellana 200 | \n",
" 0.95 | \n",
" 35861fd39af822c3fa5c7b562625cdcc | \n",
"
\n",
" \n",
" 4 | \n",
" POINT (-3.70221 40.45840) | \n",
" Calle Ntra. Sra. del Carmen 7 | \n",
" 0.98 | \n",
" 7abee54b2d67041fe1ad4f8f5f19a981 | \n",
"
\n",
" \n",
" 5 | \n",
" POINT (-3.69286 40.45651) | \n",
" Calle de San Germán 12 | \n",
" 0.95 | \n",
" 3db89bbedd49ce477cc3f35f4f4a3aae | \n",
"
\n",
" \n",
" 6 | \n",
" POINT (-3.69093 40.46575) | \n",
" Calle de Bravo Murillo 377 | \n",
" 0.95 | \n",
" b1e0a4698eb9a56278614aeb35106bf6 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" the_geom address gc_status_rel \\\n",
"0 POINT (-3.68831 40.42478) Calle Serrano 15 0.82 \n",
"1 POINT (-3.69488 40.41089) Calle de San Pedro 21 0.95 \n",
"2 POINT (-3.70588 40.42049) Calle Gran Vía 46 0.92 \n",
"3 POINT (-3.68898 40.46239) Paseo de la Castellana 200 0.95 \n",
"4 POINT (-3.70221 40.45840) Calle Ntra. Sra. del Carmen 7 0.98 \n",
"5 POINT (-3.69286 40.45651) Calle de San Germán 12 0.95 \n",
"6 POINT (-3.69093 40.46575) Calle de Bravo Murillo 377 0.95 \n",
"\n",
" carto_geocode_hash \n",
"0 26a9e50f26e86d071fa0ff46b9291863 \n",
"1 905794cd20618c2d43d7e4c8db56436c \n",
"2 8bcb00020040f7a01edb6085c7833d2a \n",
"3 35861fd39af822c3fa5c7b562625cdcc \n",
"4 7abee54b2d67041fe1ad4f8f5f19a981 \n",
"5 3db89bbedd49ce477cc3f35f4f4a3aae \n",
"6 b1e0a4698eb9a56278614aeb35106bf6 "
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gdf"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'total_rows': 7,\n",
" 'required_quota': 7,\n",
" 'previously_geocoded': 0,\n",
" 'previously_failed': 0,\n",
" 'records_with_geometry': 0,\n",
" 'final_records_with_geometry': 7,\n",
" 'geocoded_increment': 7,\n",
" 'successfully_geocoded': 7,\n",
" 'failed_geocodings': 0}"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"metadata"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from cartoframes.viz import Layer\n",
"\n",
"Layer(gdf)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Available Quota: 115480\n",
"Required Quota: 21\n"
]
}
],
"source": [
"from cartoframes.data.services import Isolines\n",
"\n",
"iso_service = Isolines()\n",
"isochrones = iso_service.isochrones(gdf, [100, 200, 300], mode='walk', dry_run=True)\n",
"\n",
"print('Available Quota: {0}'.format(iso_service.available_quota()))\n",
"print('Required Quota: {0}'.format(isochrones.metadata.get('required_quota')))"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Success! Isolines created correctly\n"
]
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" source_id | \n",
" data_range | \n",
" the_geom | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 0 | \n",
" 100 | \n",
" MULTIPOLYGON (((-3.68926 40.42557, -3.68797 40... | \n",
"
\n",
" \n",
" 1 | \n",
" 0 | \n",
" 200 | \n",
" MULTIPOLYGON (((-3.69012 40.42574, -3.68986 40... | \n",
"
\n",
" \n",
" 2 | \n",
" 0 | \n",
" 300 | \n",
" MULTIPOLYGON (((-3.69081 40.42643, -3.69055 40... | \n",
"
\n",
" \n",
" 3 | \n",
" 1 | \n",
" 100 | \n",
" MULTIPOLYGON (((-3.69544 40.41183, -3.69484 40... | \n",
"
\n",
" \n",
" 4 | \n",
" 1 | \n",
" 200 | \n",
" MULTIPOLYGON (((-3.69630 40.41132, -3.69621 40... | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" source_id data_range the_geom\n",
"0 0 100 MULTIPOLYGON (((-3.68926 40.42557, -3.68797 40...\n",
"1 0 200 MULTIPOLYGON (((-3.69012 40.42574, -3.68986 40...\n",
"2 0 300 MULTIPOLYGON (((-3.69081 40.42643, -3.69055 40...\n",
"3 1 100 MULTIPOLYGON (((-3.69544 40.41183, -3.69484 40...\n",
"4 1 200 MULTIPOLYGON (((-3.69630 40.41132, -3.69621 40..."
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"isochrones = iso_service.isochrones(gdf, [100, 200, 300], mode='walk')\n",
"\n",
"isochrones.data.head()"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from cartoframes.viz import Map, Layer, basic_style\n",
"\n",
"Map([\n",
" Layer(isochrones.data, basic_style(color='green', opacity='0.3')),\n",
" Layer(gdf, basic_style(size=3))\n",
"])"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}